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