websnark/test/bn128.js
Micah Zoltu f3a23e3a39 Removes most dependencies and switches to @noble/hashes.
* Browserify is incredibly high dependency count, so it was removed (esbuild will be added in future commit).
* NodeJS now comes with built-in support for testing, so mocha and chai are no longer required, removing most dependencies from this project.
* eslint isn't necessary for an unmaintained project, and can be used on a per-developer basis.
* package is only used to get version, which is useless and an unnecessary security risk.
* yargs brings in 16 dependencies and can be replaced with minimist which is just 1.
* normalized transitive dependencies of big-integer to all the same version.

These changes get us down to 5 dependencies total, with only one being relevant at runtime.

Also removes usage of `assert` and `Buffer`, which are NodeJS things.

Fixes a minor bug in `stringifybigint` that was incorrectly checking if something was a native `bigint`.
2024-12-02 15:33:24 +08:00

149 lines
4.6 KiB
JavaScript

const assert = require("node:assert");
const { describe, it } = require("node:test")
const refBn128 = require("snarkjs").bn128;
const refBigInt = require("snarkjs").bigInt;
const buildBn128 = require("../index.js").buildBn128;
describe("Basic tests for g1 in bn128", () => {
it("It should do a basic point doubling G1", async () => {
const bn128 = await buildBn128();
const refD = refBn128.G1.double(refBn128.g1);
const p1 = bn128.g1_allocPoint(refBn128.g1);
bn128.g1_toMontgomery(p1, p1);
bn128.g1_double(p1, p1);
bn128.g1_fromMontgomery(p1, p1);
const d = bn128.g1_getPoint(p1);
for (let i=0; i<3; i++) {
d[i] = refBigInt(d[i].toString());
}
assert(refBn128.G1.equals(d, refD));
});
it("It should add two points G1", async () => {
const bn128 = await buildBn128();
const refD = refBn128.G1.affine(refBn128.G1.mulScalar(refBn128.g1, 3));
const p1 = bn128.g1_allocPoint(refBn128.g1);
bn128.g1_toMontgomery(p1, p1);
bn128.g1_double(p1, p1);
const p2 = bn128.g1_allocPoint(refBn128.g1);
bn128.g1_toMontgomery(p2, p2);
bn128.g1_add(p1, p2, p2);
bn128.g1_affine(p2, p2);
bn128.g1_fromMontgomery(p2, p2);
const d = bn128.g1_getPoint(p2);
d[0] = refBigInt(d[0].toString());
d[1] = refBigInt(d[1].toString());
d[2] = refBigInt(d[2].toString());
assert(d[0].equals(refD[0]));
assert(d[1].equals(refD[1]));
assert(d[2].equals(1));
assert(refBn128.G1.equals(d, refD));
});
it("It should timesScalar G1", async () => {
const bn128 = await buildBn128();
const refD = refBn128.G1.mulScalar(refBn128.g1, 55);
const p1 = bn128.g1_allocPoint(refBn128.g1);
bn128.g1_toMontgomery(p1, p1);
const s = bn128.allocInt(55);
bn128.g1_timesScalar(p1, s, 32, p1);
bn128.g1_fromMontgomery(p1, p1);
const d = bn128.g1_getPoint(p1);
for (let i=0; i<3; i++) {
d[i] = refBigInt(d[i].toString());
}
assert(refBn128.G1.equals(d, refD));
});
it("G1n == 0", async () => {
const bn128 = await buildBn128();
const p1 = bn128.g1_allocPoint(refBn128.g1);
bn128.g1_toMontgomery(p1, p1);
const s = bn128.allocInt(bn128.r);
bn128.g1_timesScalar(p1, s, 32, p1);
bn128.g1_fromMontgomery(p1, p1);
assert(bn128.g1_isZero(p1));
});
it("It should do a test", { timeout: 10000000 }, async () => {
const bn128 = await buildBn128();
const t = bn128.test_AddG1(100000);
console.log(t);
})
it("It should validate the test", { timeout: 10000000 }, async () => {
const bn128 = await buildBn128();
const refD = refBn128.G1.mulScalar(refBn128.g1, 100000);
const p1 = bn128.g1_allocPoint(refBn128.g1);
bn128.g1_toMontgomery(p1, p1);
const p2 = bn128.g1_allocPoint();
bn128.testAddG1(100000, p1, p2);
bn128.g1_fromMontgomery(p2, p2);
const d = bn128.g1_getPoint(p2);
for (let i=0; i<3; i++) {
d[i] = refBigInt(d[i].toString());
}
assert(refBn128.G1.equals(d, refD));
});
it("It should do a basic point doubling in G2", async () => {
const bn128 = await buildBn128();
const refD = refBn128.G2.double(refBn128.g2);
const p1 = bn128.g2_allocPoint(refBn128.g2);
bn128.g2_toMontgomery(p1, p1);
bn128.g2_double(p1, p1);
bn128.g2_fromMontgomery(p1, p1);
const d = bn128.g2_getPoint(p1);
for (let i=0; i<3; i++) {
for (let j=0; j<2; j++) {
d[i][j] = refBigInt(d[i][j].toString());
}
}
assert(refBn128.G2.equals(d, refD));
});
it("It should add two points in G2", async () => {
const bn128 = await buildBn128();
const refD = refBn128.G2.affine(refBn128.G2.mulScalar(refBn128.g2, 3));
const p1 = bn128.g2_allocPoint(refBn128.g2);
bn128.g2_toMontgomery(p1, p1);
bn128.g2_double(p1, p1);
const p2 = bn128.g2_allocPoint(refBn128.g2);
bn128.g2_toMontgomery(p2, p2);
bn128.g2_add(p1, p2, p2);
bn128.g2_affine(p2, p2);
bn128.g2_fromMontgomery(p2, p2);
const d = bn128.g2_getPoint(p2);
for (let i=0; i<3; i++) {
for (let j=0; j<2; j++) {
d[i][j] = refBigInt(d[i][j].toString());
assert(d[i][j].equals(refD[i][j]));
}
}
assert(refBn128.G2.equals(d, refD));
});
});