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.js");
|
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;
|
|
|
|
|
|
|
|
describe("Montgomery test", function () {
|
|
|
|
let circuitE2M;
|
|
|
|
let circuitM2E;
|
|
|
|
let circuitMAdd;
|
|
|
|
let circuitMDouble;
|
|
|
|
|
|
|
|
let g = [
|
2020-04-18 22:33:59 +02:00
|
|
|
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
|
|
|
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
2019-12-14 20:32:45 +01:00
|
|
|
];
|
2018-11-27 16:03:57 +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();
|
2018-11-27 16:03:57 +01:00
|
|
|
});
|
|
|
|
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);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
|
|
|
|
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
|
2018-11-27 16:03:57 +01:00
|
|
|
|
|
|
|
mg = [xout, yout];
|
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
w = await circuitM2E.calculateWitness({ in: [xout, yout]}, true);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
|
|
|
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-04-18 22:33:59 +02:00
|
|
|
assert(Fr.eq(xout, g[0]));
|
|
|
|
assert(Fr.eq(yout, g[1]));
|
2018-11-27 16:03:57 +01:00
|
|
|
});
|
|
|
|
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);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
|
|
|
|
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
|
2018-11-27 16:03:57 +01:00
|
|
|
|
|
|
|
mg2 = [xout, yout];
|
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
w = await circuitM2E.calculateWitness({ in: mg2}, true);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
|
|
|
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-04-18 22:33:59 +02:00
|
|
|
|
|
|
|
assert(Fr.eq(xout, g2[0]));
|
|
|
|
assert(Fr.eq(yout, g2[1]));
|
2018-11-27 16:03:57 +01:00
|
|
|
});
|
|
|
|
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);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
xout = w[circuitMAdd.symbols["main.out[0]"].varIdx];
|
|
|
|
yout = w[circuitMAdd.symbols["main.out[1]"].varIdx];
|
2018-11-27 16:03:57 +01:00
|
|
|
|
|
|
|
mg3 = [xout, yout];
|
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
w = await circuitM2E.calculateWitness({ in: mg3}, true);
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-03-26 19:24:20 +01:00
|
|
|
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
|
|
|
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
2018-11-27 16:03:57 +01:00
|
|
|
|
2020-04-18 22:33:59 +02:00
|
|
|
assert(Fr.eq(xout, g3[0]));
|
|
|
|
assert(Fr.eq(yout, g3[1]));
|
2018-11-27 16:03:57 +01:00
|
|
|
});
|
|
|
|
});
|