Files
Hashpass-lib/benchmarks/sekp256k1.ts
2025-08-16 16:11:33 +10:00

58 lines
1.6 KiB
TypeScript

import { Bytes, User } from '../src/index.js';
const alice = await User.fromSecret('alice');
const bob = await User.fromSecret('bob');
const alicePublicKey = await alice.getPublicKey();
const bobPublicKey = await bob.getPublicKey();
const keyStart = performance.now();
const aliceSharedSecret = await alice.getSharedSecret(bobPublicKey);
const bobSharedSecret = await bob.getSharedSecret(alicePublicKey);
const keyEnd = performance.now();
console.log(`Key generation time: ${keyEnd - keyStart}ms`);
const message = 'Hello, world!';
const count = 100_000;
const start = performance.now();
for (let i = 0; i < count; i++) {
// Encrypt key with shared secret
const encryptedKey = await aliceSharedSecret.encrypt(Bytes.fromUtf8(message));
// Decrypt key with shared secret
const decryptedKey = await bobSharedSecret.decrypt(encryptedKey);
if (decryptedKey.toUtf8() !== message) {
throw new Error('Decrypted message does not match original message');
}
}
const end = performance.now();
const totalTimeMs = end - start;
const operationsPerSecond = (count / totalTimeMs) * 1000;
console.log(
`Aes encrypt/decrypt per second: ${operationsPerSecond} (Total time: ${totalTimeMs}ms)`,
);
const startEncrypt = performance.now();
for (let i = 0; i < count; i++) {
// Encrypt key with shared secret
const encryptedKey = await aliceSharedSecret.encrypt(Bytes.fromUtf8(message));
}
const endEncrypt = performance.now();
const totalTimeMsEncrypt = endEncrypt - startEncrypt;
const operationsPerSecondEncrypt = (count / totalTimeMsEncrypt) * 1000;
console.log(
`Aes encrypt per second: ${operationsPerSecondEncrypt} (Total time: ${totalTimeMsEncrypt}ms)`,
);