arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/montgomery.js
test/montgomery.jsRaw
 1const chai = require("chai");
 2const path = require("path");
 3const tester = require("circom").tester;
 4const babyJub = require("../src/babyjub.js");
 5const Fr = require("ffjavascript").bn128.Fr;
 6
 7const assert = chai.assert;
 8
 9describe("Montgomery test", function () {
10    let circuitE2M;
11    let circuitM2E;
12    let circuitMAdd;
13    let circuitMDouble;
14
15    let g = [
16        Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
17        Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
18    ];
19
20    let mg, mg2, g2, g3, mg3;
21
22    this.timeout(100000);
23    before( async() => {
24        circuitE2M = await tester(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
25        await circuitE2M.loadSymbols();
26        circuitM2E = await tester(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
27        await circuitM2E.loadSymbols();
28        circuitMAdd = await tester(path.join(__dirname, "circuits", "montgomeryadd.circom"));
29        await circuitMAdd.loadSymbols();
30        circuitMDouble = await tester(path.join(__dirname, "circuits", "montgomerydouble.circom"));
31        await circuitMDouble.loadSymbols();
32    });
33    it("Convert Edwards to Montgomery and back again", async () => {
34        let w, xout, yout;
35
36        w = await circuitE2M.calculateWitness({ in: g}, true);
37
38        xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
39        yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
40
41        mg = [xout, yout];
42
43        w = await circuitM2E.calculateWitness({ in: [xout, yout]}, true);
44
45        xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
46        yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
47
48        assert(Fr.eq(xout, g[0]));
49        assert(Fr.eq(yout, g[1]));
50    });
51    it("Should double a point", async () => {
52        let w, xout, yout;
53
54        g2 = babyJub.addPoint(g,g);
55
56        w = await circuitMDouble.calculateWitness({ in: mg}, true);
57
58        xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
59        yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
60
61        mg2 = [xout, yout];
62
63        w = await circuitM2E.calculateWitness({ in: mg2}, true);
64
65        xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
66        yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
67
68
69        assert(Fr.eq(xout, g2[0]));
70        assert(Fr.eq(yout, g2[1]));
71    });
72    it("Should add a point", async () => {
73        let w, xout, yout;
74
75        g3 = babyJub.addPoint(g,g2);
76
77        w = await circuitMAdd.calculateWitness({ in1: mg, in2: mg2}, true);
78
79        xout = w[circuitMAdd.symbols["main.out[0]"].varIdx];
80        yout = w[circuitMAdd.symbols["main.out[1]"].varIdx];
81
82        mg3 = [xout, yout];
83
84        w = await circuitM2E.calculateWitness({ in: mg3}, true);
85
86        xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
87        yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
88
89        assert(Fr.eq(xout, g3[0]));
90        assert(Fr.eq(yout, g3[1]));
91    });
92});