Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks. #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env node

// Measuring elliptic curve operations for ffjavascript
import { BigBuffer, buildBn128, buildBls12381, F1Field } from "ffjavascript";
import { Bench } from "tinybench";

const curves = [
buildBls12381(false).then((curve) => {
curve.name = "bls12_381";
return curve;
}),
buildBn128(false),
];

function bench_add_ff(F) {
const x = F.random();
const y = F.random();
return () => {
F.add(x, y);
};
}

function bench_invert(F) {
const x = F.random();
return () => {
F.invert(x);
};
}

function bench_add_ec(G, Fr) {
const x = G.timesScalar(G.g, Fr.random());
const y = G.timesScalar(G.g, Fr.random());
return () => {
G.add(x, y);
};
}

function bench_mul_ec(G, Fr) {
const x = G.F.random();
const y = G.timesScalar(G.g, Fr.random());
return () => {
G.timesScalar(y, x);
};
}

function bench_pairing(curve) {
const x = curve.Fr.random();
const y = curve.Fr.random();
const g1 = curve.G1.timesScalar(curve.G1.g, x);
const g2 = curve.G2.timesScalar(curve.G2.g, y);
return () => {
const pre1 = curve.prepareG1(g1);
const pre2 = curve.prepareG2(g2);
const r1 = curve.millerLoop(pre1, pre2);
const r2 = curve.finalExponentiation(r1);
};
}

function bench_group(bench, name, fn, range) {
for (var i = range[0]; i < range[1]; i++) {
bench.add(name + "/" + i, fn(i));
}
}

function bench_msm(G, Fr) {
return async (_n) => {
// size is log-scale
let n = Math.pow(2, _n);
const scalars = new BigBuffer(n * Fr.n8);
const bases = new BigBuffer(n * G.F.n8 * 2);
for (let i = 0; i < n; i++) {
const num = Fr.e(i + 1);
scalars.set(Fr.fromMontgomery(num), i * Fr.n8);
bases.set(G.toAffine(G.timesFr(G.g, num)), i * G.F.n8 * 2);
}
await G.multiExpAffine(bases, scalars, false, "");
};
}

async function run() {
const bench = new Bench();

for (const curve of curves) {
const c = await curve;

bench
.add(c.name + "/add_ff", bench_add_ff(c.Fr))
.add(c.name + "/invert", bench_invert(c.Fr))
.add(c.name + "/mul_G1", bench_mul_ec(c.G1, c.Fr))
.add(c.name + "/mul_G2", bench_mul_ec(c.G2, c.Fr))
.add(c.name + "/pairing", bench_pairing(c))
.add(c.name + "/add_G1", bench_add_ec(c.G1, c.Fr))
.add(c.name + "/add_G2", bench_add_ec(c.G2, c.Fr));

bench_group(bench, c.name + "/msm_G1", bench_msm(c.G1, c.Fr), [10, 16]);
bench_group(bench, c.name + "/msm_G2", bench_msm(c.G2, c.Fr), [10, 16]);
}

await bench.run();
process.stdout.write(JSON.stringify(bench.table()));
return bench.results;
}

run().then(() => {
process.exit(0);
});
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"scripts": {
"test": "mocha",
"build": "rollup -c rollup.cjs.config.js"
"build": "rollup -c rollup.cjs.config.js",
"bench": "node ./bench.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,6 +42,7 @@
"chai": "^4.2.0",
"eslint": "^8.0.1",
"mocha": "^10.0.0",
"rollup": "^2.38.5"
"rollup": "^2.38.5",
"tinybench": "^2.5.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. This looks great! Can you avoid the extra dependency? Perhaps using https://nodejs.org/api/perf_hooks.html 🤔

Copy link
Author

@mmaker mmaker Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing the pull request. I understand your concerns about adding another (dev)dependency. I chose it for its ease of use and detailed output. Over time, I've developed parsers tailored for its format in zka.lc, which allows for a deeper analysis of results.

While Node.js' perf hooks are useful, tinybench provides certain advantages in terms of comprehensiveness and user-friendliness. Transitioning now would require a significant overhaul of both benchmarks and the associated toolchain.

I'm open to discussing a potential compromise that might benefit us both.

}
}