circomlib/test/montgomery.js
test/montgomery.jsRaw
1const chai = require("chai");
2const path = require("path");
3const snarkjs = require("snarkjs");
4const compiler = require("circom");
5const babyJub = require("../src/babyjub.js");
6
7const assert = chai.assert;
8
9const bigInt = snarkjs.bigInt;
10
11describe("Montgomery test", function () {
12 let circuitE2M;
13 let circuitM2E;
14 let circuitMAdd;
15 let circuitMDouble;
16
17 let g = [
18 snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
19 snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")];
20
21 let mg, mg2, g2, g3, mg3;
22
23 this.timeout(100000);
24 before( async() => {
25 const cirDefE2M = await compiler(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
26 circuitE2M = new snarkjs.Circuit(cirDefE2M);
27 console.log("NConstrains Edwards -> Montgomery: " + circuitE2M.nConstraints);
28
29 const cirDefM2E = await compiler(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
30 circuitM2E = new snarkjs.Circuit(cirDefM2E);
31 console.log("NConstrains Montgomery -> Edwards: " + circuitM2E.nConstraints);
32
33 const cirDefMAdd = await compiler(path.join(__dirname, "circuits", "montgomeryadd.circom"));
34 circuitMAdd = new snarkjs.Circuit(cirDefMAdd);
35 console.log("NConstrains Montgomery Add: " + circuitMAdd.nConstraints);
36
37 const cirDefMDouble = await compiler(path.join(__dirname, "circuits", "montgomerydouble.circom"));
38 circuitMDouble = new snarkjs.Circuit(cirDefMDouble);
39 console.log("NConstrains Montgomery Double: " + circuitMDouble.nConstraints);
40 });
41 it("Convert Edwards to Montgomery and back again", async () => {
42 let w, xout, yout;
43
44 w = circuitE2M.calculateWitness({ in: g});
45
46 xout = w[circuitE2M.getSignalIdx("main.out[0]")];
47 yout = w[circuitE2M.getSignalIdx("main.out[1]")];
48
49 mg = [xout, yout];
50
51 w = circuitM2E.calculateWitness({ in: [xout, yout]});
52
53 xout = w[circuitM2E.getSignalIdx("main.out[0]")];
54 yout = w[circuitM2E.getSignalIdx("main.out[1]")];
55
56 assert(xout.equals(g[0]));
57 assert(yout.equals(g[1]));
58 });
59 it("Should double a point", async () => {
60 let w, xout, yout;
61
62 g2 = babyJub.addPoint(g,g);
63
64 w = circuitMDouble.calculateWitness({ in: mg});
65
66 xout = w[circuitE2M.getSignalIdx("main.out[0]")];
67 yout = w[circuitE2M.getSignalIdx("main.out[1]")];
68
69 mg2 = [xout, yout];
70
71 w = circuitM2E.calculateWitness({ in: mg2});
72
73 xout = w[circuitM2E.getSignalIdx("main.out[0]")];
74 yout = w[circuitM2E.getSignalIdx("main.out[1]")];
75
76 assert(xout.equals(g2[0]));
77 assert(yout.equals(g2[1]));
78 });
79 it("Should add a point", async () => {
80 let w, xout, yout;
81
82 g3 = babyJub.addPoint(g,g2);
83
84 w = circuitMAdd.calculateWitness({ in1: mg, in2: mg2});
85
86 xout = w[circuitMAdd.getSignalIdx("main.out[0]")];
87 yout = w[circuitMAdd.getSignalIdx("main.out[1]")];
88
89 mg3 = [xout, yout];
90
91 w = circuitM2E.calculateWitness({ in: mg3});
92
93 xout = w[circuitM2E.getSignalIdx("main.out[0]")];
94 yout = w[circuitM2E.getSignalIdx("main.out[1]")];
95
96 assert(xout.equals(g3[0]));
97 assert(yout.equals(g3[1]));
98 });
99});