-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add noble to benchmark #7
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f447d7f
chore: add noble to benchmark
twoeths 7bb15c2
fix: use runsFactor=1000 to benchmark more precisely
twoeths 4184b81
Merge branch 'main' into tuyen/add_noble_to_benchmark
wemeetagain db6db1d
chore: update @noble/ciphers
wemeetagain 9a1283f
chore: update ci node version
wemeetagain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,60 @@ | ||
import {itBench, setBenchOpts} from "@dapplion/benchmark"; | ||
import crypto from "crypto"; | ||
import {ChaCha20Poly1305 as ChaCha20Poly1305Stablelib} from "@stablelib/chacha20poly1305"; | ||
import {chacha20poly1305 as noble} from '@noble/ciphers/chacha'; | ||
import {ChaCha20Poly1305} from "../../src/chacha20poly1305"; | ||
import {KEY_LENGTH, NONCE_LENGTH, TAG_LENGTH} from "../../common/const"; | ||
import {KEY_LENGTH, NONCE_LENGTH} from "../../common/const"; | ||
import {newInstance} from "../../src/wasm"; | ||
import {formatBytes} from "./util"; | ||
|
||
describe("chacha20poly1305", function () { | ||
this.timeout(0); | ||
setBenchOpts({ | ||
minMs: 30_000, | ||
}); | ||
const ctx = newInstance(); | ||
const asImpl = new ChaCha20Poly1305(ctx); | ||
const chainsafe = new ChaCha20Poly1305(ctx); | ||
|
||
const key = new Uint8Array(crypto.randomBytes(KEY_LENGTH)); | ||
const jsImpl = new ChaCha20Poly1305Stablelib(key); | ||
const stablelib = new ChaCha20Poly1305Stablelib(key); | ||
const nonce = new Uint8Array(crypto.randomBytes(NONCE_LENGTH)); | ||
// const jsImpl = new ChaCha20Poly1305Stablelib(key); | ||
const ad = new Uint8Array(crypto.randomBytes(32)); | ||
const runsFactor = 1000; | ||
|
||
for (const dataLength of [512, 4096, 16384, 65_536, 524_288]) { | ||
const plainText = new Uint8Array(crypto.randomBytes(dataLength)); | ||
const sealed = jsImpl.seal(nonce, plainText, ad); | ||
|
||
itBench({ | ||
id: `as seal with data length ${dataLength}`, | ||
fn: () => { | ||
asImpl.seal(key, nonce, plainText, ad); | ||
}, | ||
}); | ||
|
||
itBench({ | ||
id: `js seal with data length ${dataLength}`, | ||
fn: () => { | ||
jsImpl.seal(nonce, plainText, ad); | ||
}, | ||
}); | ||
for (const dataLength of [32, 64, 1024, 4096, 8192, 65536]) { | ||
const plainText = new Uint8Array(crypto.randomBytes(dataLength)); | ||
const sealed = stablelib.seal(nonce, plainText, ad); | ||
const testCases: {impl: string; sealFn: () => void; openFn: () => void}[] = [ | ||
{impl: "chainsafe", sealFn: () => chainsafe.seal(key, nonce, plainText, ad), openFn: () => chainsafe.open(key, nonce, sealed, ad)}, | ||
{impl: "stablelib", sealFn: () => stablelib.seal(nonce, plainText, ad), openFn: () => stablelib.open(nonce, sealed, ad)}, | ||
{impl: "noble", sealFn: () => noble(key, nonce, ad).encrypt(plainText), openFn: () => noble(key, nonce, ad).decrypt(sealed)} | ||
] | ||
|
||
itBench({ | ||
id: `as open with data length ${dataLength}`, | ||
beforeEach: () => new Uint8Array(sealed), | ||
fn: (clonedSealed) => { | ||
// overwriteSealed as true to avoid memory allocation | ||
asImpl.open(key, nonce, clonedSealed, ad, clonedSealed.subarray(0, clonedSealed.length - TAG_LENGTH)); | ||
}, | ||
}); | ||
// seal | ||
for (const {impl, sealFn} of testCases) { | ||
itBench({ | ||
id: `${impl} seal ${formatBytes(dataLength)}`, | ||
fn: () => { | ||
for (let i = 0; i < runsFactor; i++) { | ||
sealFn(); | ||
} | ||
}, | ||
runsFactor, | ||
}); | ||
} | ||
|
||
itBench({ | ||
id: `js open with data length ${dataLength}`, | ||
beforeEach: () => new Uint8Array(sealed), | ||
fn: (clonedSealed) => { | ||
jsImpl.open(nonce, clonedSealed, ad, clonedSealed.subarray(0, clonedSealed.length - TAG_LENGTH)); | ||
}, | ||
}); | ||
// open | ||
for (const {impl, openFn} of testCases) { | ||
itBench({ | ||
id: `${impl} open ${formatBytes(dataLength)}`, | ||
fn: () => { | ||
for (let i = 0; i < runsFactor; i++) { | ||
openFn(); | ||
} | ||
}, | ||
runsFactor, | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function formatBytes(bytes: number): string { | ||
if (bytes < 1024) { | ||
return bytes + " bytes"; | ||
} else if (bytes < 1024 * 1024) { | ||
return (bytes / 1024).toFixed(2) + " KB"; | ||
} else { | ||
return (bytes / (1024 * 1024)).toFixed(2) + " MB"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -531,6 +531,11 @@ | |
"@jridgewell/resolve-uri" "^3.1.0" | ||
"@jridgewell/sourcemap-codec" "^1.4.14" | ||
|
||
"@noble/ciphers@^0.5.1": | ||
version "0.5.1" | ||
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-0.5.1.tgz#292f388b69c9ed80d49dca1a5cbfd4ff06852111" | ||
integrity sha512-aNE06lbe36ifvMbbWvmmF/8jx6EQPu2HVg70V95T+iGjOuYwPpAccwAQc2HlXO2D0aiQ3zavbMga4jjWnrpiPA== | ||
|
||
"@nodelib/[email protected]": | ||
version "2.1.5" | ||
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format in reverse to it's easier to read,