Skip to content

Instantly share code, notes, and snippets.

@paulmillr
Last active July 26, 2023 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulmillr/cccaf4e57aa54f5d6946922a3c36fb4a to your computer and use it in GitHub Desktop.
Save paulmillr/cccaf4e57aa54f5d6946922a3c36fb4a to your computer and use it in GitHub Desktop.
Timing differences between different keys in noble and elliptic
import { secp256k1 } from '@noble/curves/secp256k1';
import elliptic from 'elliptic';
import { mark } from 'micro-bmark';
const EC = elliptic.ec;
(async () => {
var ec = new EC('secp256k1');
const a = '0000000000000000000000000000000000000000000000000000000000000003';
const b = '3000000000000000000000000000000000000000000000000000000000000000';
const c = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
const rand = 'badcafeebadcafeebadcafeebadcafeebadcafeebadcafeebadcafeebadcafee';
const rand_P = secp256k1.ProjectivePoint.fromPrivateKey(rand);
console.log('noble')
await mark('G * a', 7000, () => secp256k1.getPublicKey(a));
await mark('G * b', 7000, () => secp256k1.getPublicKey(b));
await mark('G * c', 7000, () => secp256k1.getPublicKey(c));
const a_ = BigInt('0x' + a), b_ = BigInt('0x' + b), c_ = BigInt('0x' + c);
await mark('P * a', 1000, () => rand_P.multiply(a_));
await mark('P * b', 1000, () => rand_P.multiply(b_));
await mark('P * c', 1000, () => rand_P.multiply(c_));
console.log('noble unsafe');
await mark('P * a', 1000, () => rand_P.multiplyUnsafe(a_));
await mark('P * b', 1000, () => rand_P.multiplyUnsafe(b_));
await mark('P * c', 1000, () => rand_P.multiplyUnsafe(c_));
console.log('elliptic');
const as = ec.keyFromPrivate(a);
const bs = ec.keyFromPrivate(b);
const cs = ec.keyFromPrivate(c);
const p = ec.keyFromPrivate(rand);
const P = p.getPublic();
await mark('e G * a', 4000, () => ec.keyFromPrivate(a).getPublic());
await mark('e G * b', 4000, () => ec.keyFromPrivate(b).getPublic());
await mark('e G * c', 4000, () => ec.keyFromPrivate(c).getPublic());
await mark('e P * a', 1000, () => as.derive(P));
await mark('e P * b', 1000, () => bs.derive(P));
await mark('e P * c', 1000, () => cs.derive(P));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment