2018-11-27 16:03:57 +01:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
2019-12-14 20:32:45 +01:00
|
|
|
const tester = require("circom").tester;
|
2018-11-27 16:03:57 +01:00
|
|
|
const babyjub = require("../src/babyjub");
|
2020-04-18 22:33:59 +02:00
|
|
|
const Fr = require("ffjavascript").bn128.Fr;
|
2018-11-27 16:03:57 +01:00
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
function print(circuit, w, s) {
|
|
|
|
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Escalarmul test", function () {
|
|
|
|
let circuit;
|
|
|
|
|
|
|
|
this.timeout(100000);
|
|
|
|
|
|
|
|
before( async() => {
|
2019-12-14 20:32:45 +01:00
|
|
|
circuit = await tester(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
|
2018-11-27 16:03:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should generate Same escalar mul", async () => {
|
|
|
|
|
2019-12-14 20:32:45 +01:00
|
|
|
const w = await circuit.calculateWitness({"e": 0});
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2019-12-16 21:35:52 +01:00
|
|
|
await circuit.checkConstraints(w);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
await circuit.assertOut(w, {out: [0,1]}, true);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should generate Same escalar mul", async () => {
|
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
const w = await circuit.calculateWitness({"e": 1}, true);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2019-12-16 21:35:52 +01:00
|
|
|
await circuit.checkConstraints(w);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2019-12-14 20:32:45 +01:00
|
|
|
await circuit.assertOut(w, {out: babyjub.Base8});
|
2018-11-27 16:03:57 +01:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2019-07-30 19:35:19 +02:00
|
|
|
it("Should generate scalar mul of a specific constant", async () => {
|
|
|
|
|
2020-04-18 22:33:59 +02:00
|
|
|
const s = Fr.e("2351960337287830298912035165133676222414898052661454064215017316447594616519");
|
2019-07-30 19:35:19 +02:00
|
|
|
const base8 = [
|
2020-04-18 22:33:59 +02:00
|
|
|
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
|
|
|
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
2019-07-30 19:35:19 +02:00
|
|
|
];
|
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
const w = await circuit.calculateWitness({"e": s}, true);
|
2019-07-30 19:35:19 +02:00
|
|
|
|
2019-12-16 21:35:52 +01:00
|
|
|
await circuit.checkConstraints(w);
|
2019-07-30 19:35:19 +02:00
|
|
|
|
|
|
|
const expectedRes = babyjub.mulPointEscalar(base8, s);
|
|
|
|
|
2019-12-14 20:32:45 +01:00
|
|
|
await circuit.assertOut(w, {out: expectedRes});
|
|
|
|
|
2019-07-30 19:35:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should generate scalar mul of the firsts 50 elements", async () => {
|
|
|
|
|
|
|
|
const base8 = [
|
2020-04-18 22:33:59 +02:00
|
|
|
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
|
|
|
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
2019-07-30 19:35:19 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
for (let i=0; i<50; i++) {
|
2020-04-18 22:33:59 +02:00
|
|
|
const s = Fr.e(i);
|
2019-07-30 19:35:19 +02:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
const w = await circuit.calculateWitness({"e": s}, true);
|
2019-07-30 19:35:19 +02:00
|
|
|
|
2019-12-16 21:35:52 +01:00
|
|
|
await circuit.checkConstraints(w);
|
2019-07-30 19:35:19 +02:00
|
|
|
|
|
|
|
const expectedRes = babyjub.mulPointEscalar(base8, s);
|
|
|
|
|
2019-12-14 20:32:45 +01:00
|
|
|
await circuit.assertOut(w, {out: expectedRes});
|
2019-07-30 19:35:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-27 16:03:57 +01:00
|
|
|
it("If multiply by order should return 0", async () => {
|
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
const w = await circuit.calculateWitness({"e": babyjub.subOrder }, true);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2019-12-16 21:35:52 +01:00
|
|
|
await circuit.checkConstraints(w);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2019-12-14 20:32:45 +01:00
|
|
|
await circuit.assertOut(w, {out: [0,1]});
|
2018-11-27 16:03:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|