circomlib/test/montgomery.js
test/montgomery.jsRaw
1const chai = require("chai");
2const path = require("path");
3const bigInt = require("big-integer");
4const tester = require("circom").tester;
5const babyJub = require("../src/babyjub.js");
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 bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
17 bigInt("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});
37
38 xout = w[circuitE2M.symbols["main.out[0]"].idxWit];
39 yout = w[circuitE2M.symbols["main.out[1]"].idxWit];
40
41 mg = [xout, yout];
42
43 w = await circuitM2E.calculateWitness({ in: [xout, yout]});
44
45 xout = w[circuitM2E.symbols["main.out[0]"].idxWit];
46 yout = w[circuitM2E.symbols["main.out[1]"].idxWit];
47
48 assert(xout.equals(g[0]));
49 assert(yout.equals(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});
57
58 xout = w[circuitE2M.symbols["main.out[0]"].idxWit];
59 yout = w[circuitE2M.symbols["main.out[1]"].idxWit];
60
61 mg2 = [xout, yout];
62
63 w = await circuitM2E.calculateWitness({ in: mg2});
64
65 xout = w[circuitM2E.symbols["main.out[0]"].idxWit];
66 yout = w[circuitM2E.symbols["main.out[1]"].idxWit];
67
68 assert(xout.equals(g2[0]));
69 assert(yout.equals(g2[1]));
70 });
71 it("Should add a point", async () => {
72 let w, xout, yout;
73
74 g3 = babyJub.addPoint(g,g2);
75
76 w = await circuitMAdd.calculateWitness({ in1: mg, in2: mg2});
77
78 xout = w[circuitMAdd.symbols["main.out[0]"].idxWit];
79 yout = w[circuitMAdd.symbols["main.out[1]"].idxWit];
80
81 mg3 = [xout, yout];
82
83 w = await circuitM2E.calculateWitness({ in: mg3});
84
85 xout = w[circuitM2E.symbols["main.out[0]"].idxWit];
86 yout = w[circuitM2E.symbols["main.out[1]"].idxWit];
87
88 assert(xout.equals(g3[0]));
89 assert(yout.equals(g3[1]));
90 });
91});