circomlib/test/montgomery.js

93 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

const chai = require("chai");
const path = require("path");
2019-12-14 20:32:45 +01:00
const tester = require("circom").tester;
const babyJub = require("../src/babyjub.js");
const Fr = require("ffjavascript").bn128.Fr;
const assert = chai.assert;
describe("Montgomery test", function () {
let circuitE2M;
let circuitM2E;
let circuitMAdd;
let circuitMDouble;
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
2019-12-14 20:32:45 +01:00
];
let mg, mg2, g2, g3, mg3;
this.timeout(100000);
before( async() => {
2019-12-14 20:32:45 +01:00
circuitE2M = await tester(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
await circuitE2M.loadSymbols();
circuitM2E = await tester(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
await circuitM2E.loadSymbols();
circuitMAdd = await tester(path.join(__dirname, "circuits", "montgomeryadd.circom"));
await circuitMAdd.loadSymbols();
circuitMDouble = await tester(path.join(__dirname, "circuits", "montgomerydouble.circom"));
await circuitMDouble.loadSymbols();
});
it("Convert Edwards to Montgomery and back again", async () => {
let w, xout, yout;
2020-03-26 19:24:20 +01:00
w = await circuitE2M.calculateWitness({ in: g}, true);
2020-03-26 19:24:20 +01:00
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
mg = [xout, yout];
2020-03-26 19:24:20 +01:00
w = await circuitM2E.calculateWitness({ in: [xout, yout]}, true);
2020-03-26 19:24:20 +01:00
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
assert(Fr.eq(xout, g[0]));
assert(Fr.eq(yout, g[1]));
});
it("Should double a point", async () => {
let w, xout, yout;
g2 = babyJub.addPoint(g,g);
2020-03-26 19:24:20 +01:00
w = await circuitMDouble.calculateWitness({ in: mg}, true);
2020-03-26 19:24:20 +01:00
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
mg2 = [xout, yout];
2020-03-26 19:24:20 +01:00
w = await circuitM2E.calculateWitness({ in: mg2}, true);
2020-03-26 19:24:20 +01:00
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
assert(Fr.eq(xout, g2[0]));
assert(Fr.eq(yout, g2[1]));
});
it("Should add a point", async () => {
let w, xout, yout;
g3 = babyJub.addPoint(g,g2);
2020-03-26 19:24:20 +01:00
w = await circuitMAdd.calculateWitness({ in1: mg, in2: mg2}, true);
2020-03-26 19:24:20 +01:00
xout = w[circuitMAdd.symbols["main.out[0]"].varIdx];
yout = w[circuitMAdd.symbols["main.out[1]"].varIdx];
mg3 = [xout, yout];
2020-03-26 19:24:20 +01:00
w = await circuitM2E.calculateWitness({ in: mg3}, true);
2020-03-26 19:24:20 +01:00
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
assert(Fr.eq(xout, g3[0]));
assert(Fr.eq(yout, g3[1]));
});
});