This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
benchmark.js
99 lines (76 loc) · 3.34 KB
/
benchmark.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
const path = require('path');
const { Runtime, getNewVM } = require('../huff/src/runtime.js');
const bn128Reference = require('./js_snippets/bn128_reference');
const pathToTestData = path.posix.resolve(__dirname, './huff_modules');
const main = new Runtime('main_loop.huff', pathToTestData);
const vm = getNewVM();
async function runMainLoop(numPoints, numIterations) {
const iterations = [...new Array(numIterations)].map(() => {
const points = [...new Array(numPoints)].map(() => bn128Reference.randomPoint());
const scalars = [...new Array(numPoints)].map(() => bn128Reference.randomScalar());
const calldata = [...new Array(numPoints)].reduce((acc, x, i) => {
return [
...acc,
{ index: i * 2 * 32, value: points[i].x },
{ index: (i * 2 + 1) * 32, value: points[i].y },
{ index: numPoints * 64 + i * 32, value: scalars[i] },
];
}, []);
return main(vm, 'MAIN__WEIERSTRUDEL', [], [], calldata, 1);
});
const results = await Promise.all(iterations);
const cumulativeGas = results.reduce((acc, { gas }) => {
return Number(acc) + Number(gas);
}, 0);
return Math.round(cumulativeGas / numIterations);
}
async function benchmark(i) {
let gas;
console.log(`Running scalar multi-exponentiation benchmarks over ${i} iterations`);
console.log('1 point...');
gas = await runMainLoop(1, i);
console.log(`average gas cost for 1 point: ${gas} gas`);
console.log('2 points...');
gas = await runMainLoop(2, i);
console.log(`average gas cost for 2 points: ${gas} gas`);
console.log('3 points...');
gas = await runMainLoop(3, i);
console.log(`average gas cost for 3 points: ${gas} gas`);
console.log('4 points...');
gas = await runMainLoop(4, i);
console.log(`average gas cost for 4 points: ${gas} gas`);
console.log('5 points...');
gas = await runMainLoop(5, i);
console.log(`average gas cost for 5 points: ${gas} gas`);
console.log('6 points...');
gas = await runMainLoop(6, i);
console.log(`average gas cost for 6 points: ${gas} gas`);
console.log('7 points...');
gas = await runMainLoop(7, i);
console.log(`average gas cost for 7 points: ${gas} gas`);
console.log('8 points...');
gas = await runMainLoop(8, i);
console.log(`average gas cost for 8 points: ${gas} gas`);
console.log('9 points...');
gas = await runMainLoop(9, i);
console.log(`average gas cost for 9 points: ${gas} gas`);
console.log('10 points...');
gas = await runMainLoop(10, i);
console.log(`average gas cost for 10 points: ${gas} gas`);
console.log('11 points...');
gas = await runMainLoop(11, i);
console.log(`average gas cost for 11 points: ${gas} gas`);
console.log('12 points...');
gas = await runMainLoop(12, i);
console.log(`average gas cost for 12 points: ${gas} gas`);
console.log('13 points...');
gas = await runMainLoop(13, i);
console.log(`average gas cost for 13 points: ${gas} gas`);
console.log('14 points...');
gas = await runMainLoop(14, i);
console.log(`average gas cost for 14 points: ${gas} gas`);
console.log('15 points...');
gas = await runMainLoop(15, i);
console.log(`average gas cost for 15 points: ${gas} gas`);
}
benchmark(10).then(() => console.log('...fin'));