circomlib/test/escalarmul.js

116 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-10-21 20:51:38 +03:00
const chai = require("chai");
const path = require("path");
2019-12-14 22:32:45 +03:00
const tester = require("circom").tester;
const babyJub = require("../src/babyjub.js");
const Fr = require("ffjavascript").bn128.Fr;
2018-10-21 20:51:38 +03:00
const assert = chai.assert;
function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
2019-12-14 22:32:45 +03:00
describe("Exponentioation test", function () {
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
this.timeout(100000);
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
it("Should generate the Exponentiation table in k=0", async () => {
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({in: 1});
2018-10-21 20:51:38 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
2019-12-14 22:32:45 +03:00
];
let dbl= [Fr.e("0"), Fr.e("1")];
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
const expectedOut = [];
2018-10-21 20:51:38 +03:00
for (let i=0; i<16; i++) {
2019-12-14 22:32:45 +03:00
expectedOut.push(dbl);
dbl = babyJub.addPoint(dbl,g);
2018-10-21 20:51:38 +03:00
}
2019-12-14 22:32:45 +03:00
await circuit.assertOut(w, {out: expectedOut});
2018-10-21 20:51:38 +03:00
});
it("Should generate the Exponentiation table in k=3", async () => {
2019-12-14 22:32:45 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({in: 1});
2018-10-21 20:51:38 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
2019-12-14 22:32:45 +03:00
];
2018-10-21 20:51:38 +03:00
for (let i=0; i<12;i++) {
2019-12-14 22:32:45 +03:00
g = babyJub.addPoint(g,g);
2018-10-21 20:51:38 +03:00
}
let dbl= [Fr.e("0"), Fr.e("1")];
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
const expectedOut = [];
2019-12-14 22:32:45 +03:00
for (let i=0; i<16; i++) {
expectedOut.push(dbl);
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
dbl = babyJub.addPoint(dbl,g);
2018-10-21 20:51:38 +03:00
}
2019-12-14 22:32:45 +03:00
await circuit.assertOut(w, {out: expectedOut});
2018-10-21 20:51:38 +03:00
});
it("Should exponentiate g^31", async () => {
2019-12-14 22:32:45 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test.circom"));
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
const w = await circuit.calculateWitness({"in": 31});
2018-10-21 20:51:38 +03:00
2019-12-16 23:35:52 +03:00
await circuit.checkConstraints(w);
2018-10-21 20:51:38 +03:00
2019-12-14 22:32:45 +03:00
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
2019-12-14 22:32:45 +03:00
];
2018-10-21 20:51:38 +03:00
let c = [Fr.e(0), Fr.e(1)];
2018-10-21 20:51:38 +03:00
for (let i=0; i<31;i++) {
2019-12-14 22:32:45 +03:00
c = babyJub.addPoint(c,g);
2018-10-21 20:51:38 +03:00
}
2019-12-14 22:32:45 +03:00
await circuit.assertOut(w, {out: c});
2018-10-21 20:51:38 +03:00
const w2 = await circuit.calculateWitness({"in": Fr.add(Fr.shl(Fr.e(1), Fr.e(252)),Fr.one)});
2018-10-21 20:51:38 +03:00
c = [g[0], g[1]];
for (let i=0; i<252;i++) {
2019-12-14 22:32:45 +03:00
c = babyJub.addPoint(c,c);
2018-10-21 20:51:38 +03:00
}
2019-12-14 22:32:45 +03:00
c = babyJub.addPoint(c,g);
2019-12-14 22:32:45 +03:00
await circuit.assertOut(w2, {out: c});
2018-10-21 20:51:38 +03:00
}).timeout(10000000);
it("Number of constrains for 256 bits", async () => {
2019-12-14 22:32:45 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
2018-10-21 20:51:38 +03:00
}).timeout(10000000);
});