-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbenchmark_ec.js
108 lines (93 loc) · 3.82 KB
/
benchmark_ec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const EC = require('elliptic').ec;
// Create a new elliptic curve object
const ec = new EC('secp256k1');
const secp = require('noble-secp256k1');
const { randomBytes } = require('crypto');
const generatorPoint = ec.g;
const p_scan = "d952fe0740d9d14011fc8ead3ab7de3c739d3aa93ce9254c10b0134d80d26a30";
const P_elliptic = generatorPoint.mul(p_scan);
const P_noble = secp.Point.fromPrivateKey(p_scan);
const numRuns = 1000;
// We generate random private keys for elliptic, then use those to generate
// the same random private keys for noble-secp256k1. This way we have the same
// private keys being used for each, already in the required format to avoid
// benchmarking any overhead from converting the private key formats. We use
// a different private key in each iteration of the below for loops to avoid
// JIT optimizations that might occur if the same private key is used each time.
function getRandomPrivateKeysElliptic() {
return Array.from(Array(numRuns)).map(_ => randomBytes(32))
}
function getRandomPrivateKeysNoble(ellipticPrivateKeys) {
return ellipticPrivateKeys.map(key => BigInt(`0x${key.toString('hex')}`))
}
// Similar for the points (public keys) derived from those private keys.
function getPointsElliptic(privateKeys) {
return privateKeys.map(key => generatorPoint.mul(key))
}
function getPointsNoble(privateKeys) {
return privateKeys.map(key => secp.Point.fromPrivateKey(key))
}
// Setup the test data.
const ellipticPrivateKeys = getRandomPrivateKeysElliptic();
const ellipticPoints = getPointsElliptic(ellipticPrivateKeys);
const noblePrivateKeys = getRandomPrivateKeysNoble(ellipticPrivateKeys);
const noblePoints = getPointsNoble(noblePrivateKeys);
let startTime, endTime, deltaTime;
// -------------------------------------
// -------- Elliptic Benchmarks --------
// -------------------------------------
console.log("elliptic.js:");
// --- Private Key to Public Key ---
startTime = performance.now()
for (let k = 0; k < numRuns; k++) {
const P_scan = generatorPoint.mul(ellipticPrivateKeys[k]);
}
endTime = performance.now()
deltaTime = endTime - startTime;
console.log(`1000x privKey --> pubKey: ${deltaTime} milliseconds`);
// --- Private Key * Public Key ---
startTime = performance.now()
for (let k = 0; k < numRuns; k++) {
const P_scan = P_elliptic.mul(ellipticPrivateKeys[k]);
}
endTime = performance.now()
deltaTime = endTime - startTime;
console.log(`1000x privKey * pubKey: ${deltaTime} milliseconds`);
// --- Public Key + Public Key ---
startTime = performance.now()
for (let k = 0; k < numRuns; k++) {
const P_scan = P_elliptic.add(ellipticPoints[k]);
}
endTime = performance.now()
deltaTime = endTime - startTime;
console.log(`1000x pubKey + pubKey: ${deltaTime} milliseconds`);
console.log("-----------------------------------------");
// --------------------------------------------
// -------- noble-secp256k1 Benchmarks --------
// --------------------------------------------
console.log("noble-secp256k1.js:");
// --- Private Key to Public Key ---
startTime = performance.now()
for (let k = 0; k < numRuns; k++) {
const P_scan = secp.getPublicKey(noblePrivateKeys[k]);
}
endTime = performance.now()
deltaTime = endTime - startTime;
console.log(`1000x privKey --> pubKey: ${deltaTime} milliseconds`);
// --- Private Key * Public Key ---
startTime = performance.now()
for (let k = 0; k < numRuns; k++) {
const P_scan = secp.getSharedSecret(noblePrivateKeys[k], P_noble);
// const P_scan = P_noble.multiply(noblePrivateKeys[k]);
}
endTime = performance.now()
deltaTime = endTime - startTime;
console.log(`1000x privKey * pubKey: ${deltaTime} milliseconds`);
// --- Public Key + Public Key ---
startTime = performance.now()
for (let k = 0; k < numRuns; k++) {
const P_scan = P_noble.add(noblePoints[k]);
}
endTime = performance.now()
deltaTime = endTime - startTime;
console.log(`1000x pubKey + pubKey: ${deltaTime} milliseconds`);