-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated speed, added benchmarks for native crypto and tweetnacl
- Loading branch information
Showing
23 changed files
with
457 additions
and
88 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const bench = require("nanobench"); | ||
const crypto = require("crypto"); | ||
const dcrypto = require("../../lib/index.cjs"); | ||
const nacl = require("tweetnacl"); | ||
|
||
const times = 10000; | ||
|
||
const data = []; | ||
for (let i = 0; i < times; i++) { | ||
data.push(nacl.randomBytes(256)); | ||
} | ||
|
||
let signature; | ||
let _verification; | ||
|
||
const keyPair = nacl.sign.keyPair(); | ||
|
||
bench(`Ed25519 sign/verify native crypto ${times} times`, (b) => { | ||
b.start(); | ||
|
||
const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519"); | ||
|
||
for (let i = 0; i < times; i++) { | ||
signature = crypto.sign(null, data[i], privateKey); | ||
_verification = crypto.verify(null, data[i], publicKey, signature); | ||
} | ||
|
||
b.end(); | ||
}); | ||
|
||
bench(`Ed25519 @deliberative/crypto ${times} times`, async (b) => { | ||
b.start(); | ||
|
||
const wasmSignMemory = dcrypto.loadWasmMemory.sign(256); | ||
const wasmSignModule = await dcrypto.loadWasmModule({ | ||
wasmMemory: wasmSignMemory, | ||
}); | ||
|
||
const wasmVerifyMemory = dcrypto.loadWasmMemory.verify(256); | ||
const wasmVerifyModule = await dcrypto.loadWasmModule({ | ||
wasmMemory: wasmVerifyMemory, | ||
}); | ||
|
||
for (let i = 0; i < times; i++) { | ||
signature = await dcrypto.sign(data[i], keyPair.secretKey, wasmSignModule); | ||
_verification = await dcrypto.verify( | ||
data[i], | ||
signature, | ||
keyPair.publicKey, | ||
wasmVerifyModule, | ||
); | ||
} | ||
|
||
b.end(); | ||
}); | ||
|
||
bench(`Ed25519 tweetnacl ${times} times`, (b) => { | ||
b.start(); | ||
|
||
for (let i = 0; i < times; i++) { | ||
signature = nacl.sign.detached(data[i], keyPair.secretKey); | ||
_verification = nacl.sign.detached.verify( | ||
data[i], | ||
signature, | ||
keyPair.publicKey, | ||
); | ||
} | ||
|
||
b.end(); | ||
}); |
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,46 @@ | ||
const bench = require("nanobench"); | ||
const crypto = require("crypto"); | ||
const dcrypto = require("../../lib/index.cjs"); | ||
const nacl = require("tweetnacl"); | ||
|
||
const times = 10000; | ||
|
||
const data = []; | ||
for (let i = 0; i < times; i++) { | ||
data.push(nacl.randomBytes(256)); | ||
} | ||
|
||
let _hash; | ||
|
||
bench(`sha512 native crypto ${times} times`, (b) => { | ||
b.start(); | ||
|
||
for (let i = 0; i < times; i++) { | ||
_hash = crypto.createHash("sha512").update(data[i]).digest(); | ||
} | ||
|
||
b.end(); | ||
}); | ||
|
||
bench(`sha512 @deliberative/crypto ${times} times`, async (b) => { | ||
b.start(); | ||
|
||
const wasmMemory = dcrypto.loadWasmMemory.sha512(256); | ||
const wasmModule = await dcrypto.loadWasmModule({ wasmMemory }); | ||
|
||
for (let i = 0; i < times; i++) { | ||
_hash = await dcrypto.sha512(data[i], wasmModule); | ||
} | ||
|
||
b.end(); | ||
}); | ||
|
||
bench(`sha512 tweetnacl ${times} times`, (b) => { | ||
b.start(); | ||
|
||
for (let i = 0; i < times; i++) { | ||
_hash = nacl.hash(data[i]); | ||
} | ||
|
||
b.end(); | ||
}); |
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,141 @@ | ||
const bench = require("nanobench"); | ||
const crypto = require("crypto"); | ||
const dcrypto = require("../../lib/index.cjs"); | ||
const nacl = require("tweetnacl"); | ||
|
||
const times = 10000; | ||
|
||
const data = []; | ||
for (let i = 0; i < times; i++) { | ||
data.push(nacl.randomBytes(256)); | ||
} | ||
|
||
bench(`X25519 sign/verify native crypto ${times} times`, (b) => { | ||
b.start(); | ||
|
||
const aliceKeyPair = crypto.generateKeyPairSync("x25519"); | ||
const alicePubExport = aliceKeyPair.publicKey.export({ | ||
type: "spki", | ||
format: "pem", | ||
}); | ||
|
||
const bobKeyPair = crypto.generateKeyPairSync("x25519"); | ||
const bobPubExport = bobKeyPair.publicKey.export({ | ||
type: "spki", | ||
format: "pem", | ||
}); | ||
|
||
const bobKeyAgree = crypto.diffieHellman({ | ||
publicKey: crypto.createPublicKey(alicePubExport), | ||
privateKey: bobKeyPair.privateKey, | ||
}); | ||
|
||
const aliceKeyAgree = crypto.diffieHellman({ | ||
publicKey: crypto.createPublicKey(bobPubExport), | ||
privateKey: aliceKeyPair.privateKey, | ||
}); | ||
|
||
const nonce = new Uint8Array(12).fill(2); | ||
|
||
for (let i = 0; i < times; i++) { | ||
const cipher = crypto.createCipheriv( | ||
"chacha20-poly1305", | ||
bobKeyAgree, | ||
nonce, | ||
{ | ||
authTagLength: 16, | ||
}, | ||
); | ||
const ciphertext = cipher.update(data[i]); | ||
cipher.final(); | ||
const tag = cipher.getAuthTag(); | ||
|
||
// Now transmit { ciphertext, nonce, tag }. | ||
|
||
const decipher = crypto.createDecipheriv( | ||
"chacha20-poly1305", | ||
aliceKeyAgree, | ||
nonce, | ||
{ | ||
authTagLength: 16, | ||
}, | ||
); | ||
decipher.setAuthTag(tag); | ||
decipher.update(ciphertext, "binary"); | ||
|
||
try { | ||
decipher.final(); | ||
} catch (err) { | ||
throw new Error("Authentication failed!", { cause: err }); | ||
} | ||
} | ||
|
||
b.end(); | ||
}); | ||
|
||
bench(`X25519 @deliberative/crypto ${times} times`, async (b) => { | ||
b.start(); | ||
const keyPair = await dcrypto.keyPair(); | ||
|
||
const wasmEncryptMemory = dcrypto.loadWasmMemory.encryptForwardSecret( | ||
256, | ||
64, | ||
); | ||
const wasmEncryptModule = await dcrypto.loadWasmModule({ | ||
wasmMemory: wasmEncryptMemory, | ||
}); | ||
|
||
const wasmDecryptMemory = dcrypto.loadWasmMemory.decryptForwardSecret( | ||
5000, | ||
64, | ||
); | ||
const wasmDecryptModule = await dcrypto.loadWasmModule({ | ||
wasmMemory: wasmDecryptMemory, | ||
}); | ||
|
||
const additional = new Uint8Array(64).fill(2); | ||
for (let i = 0; i < times; i++) { | ||
const encryptedWithModule = await dcrypto.encryptForwardSecrecy( | ||
data[i], | ||
keyPair.publicKey, | ||
additional, | ||
wasmEncryptModule, | ||
); | ||
|
||
const _decryptedWithModule = await dcrypto.decryptForwardSecrecy( | ||
encryptedWithModule, | ||
keyPair.secretKey, | ||
additional, | ||
wasmDecryptModule, | ||
); | ||
} | ||
|
||
b.end(); | ||
}); | ||
|
||
bench(`X25519 tweetnacl ${times} times`, (b) => { | ||
b.start(); | ||
|
||
const aliceKeyPair = nacl.box.keyPair(); | ||
const bobKeyPair = nacl.box.keyPair(); | ||
|
||
const nonce = new Uint8Array(nacl.box.nonceLength).fill(2); | ||
|
||
for (let i = 0; i < times; i++) { | ||
const box = nacl.box( | ||
data[i], | ||
nonce, | ||
aliceKeyPair.publicKey, | ||
bobKeyPair.secretKey, | ||
); | ||
|
||
const open = nacl.box.open( | ||
box, | ||
nonce, | ||
bobKeyPair.publicKey, | ||
aliceKeyPair.secretKey, | ||
); | ||
} | ||
|
||
b.end(); | ||
}); |
Oops, something went wrong.