circomlib/test/escalarmulfix.js
test/escalarmulfix.jsRaw
1const chai = require("chai");
2const path = require("path");
3const snarkjs = require("snarkjs");
4const compiler = require("circom");
5const babyjub = require("../src/babyjub");
6
7const assert = chai.assert;
8
9const bigInt = snarkjs.bigInt;
10
11
12function print(circuit, w, s) {
13 console.log(s + ": " + w[circuit.getSignalIdx(s)]);
14}
15
16describe("Escalarmul test", function () {
17 let circuit;
18
19 this.timeout(100000);
20
21 before( async() => {
22 const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
23 circuit = new snarkjs.Circuit(cirDef);
24 console.log("NConstrains Escalarmul fix: " + circuit.nConstraints);
25 });
26
27 it("Should generate Same escalar mul", async () => {
28
29 const w = circuit.calculateWitness({"e": 0});
30
31 assert(circuit.checkWitness(w));
32
33 const xout = w[circuit.getSignalIdx("main.out[0]")];
34 const yout = w[circuit.getSignalIdx("main.out[1]")];
35
36 assert(xout.equals(0));
37 assert(yout.equals(1));
38 });
39
40 it("Should generate Same escalar mul", async () => {
41
42 const w = circuit.calculateWitness({"e": 1});
43
44 assert(circuit.checkWitness(w));
45
46 const xout = w[circuit.getSignalIdx("main.out[0]")];
47 const yout = w[circuit.getSignalIdx("main.out[1]")];
48
49 assert(xout.equals(babyjub.Base8[0]));
50 assert(yout.equals(babyjub.Base8[1]));
51 });
52
53 it("Should generate scalar mul of a specific constant", async () => {
54
55 const s = bigInt("2351960337287830298912035165133676222414898052661454064215017316447594616519");
56 const base8 = [
57 bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
58 bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
59 ];
60
61 const w = circuit.calculateWitness({"e": s});
62
63 assert(circuit.checkWitness(w));
64
65 const xout = w[circuit.getSignalIdx("main.out[0]")];
66 const yout = w[circuit.getSignalIdx("main.out[1]")];
67
68 const expectedRes = babyjub.mulPointEscalar(base8, s);
69
70 assert(xout.equals(expectedRes[0]));
71 assert(yout.equals(expectedRes[1]));
72 });
73
74 it("Should generate scalar mul of the firsts 50 elements", async () => {
75
76 const base8 = [
77 bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
78 bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
79 ];
80
81 for (let i=0; i<50; i++) {
82 const s = bigInt(i);
83
84 const w = circuit.calculateWitness({"e": s});
85
86 assert(circuit.checkWitness(w));
87
88 const xout = w[circuit.getSignalIdx("main.out[0]")];
89 const yout = w[circuit.getSignalIdx("main.out[1]")];
90
91 const expectedRes = babyjub.mulPointEscalar(base8, s);
92
93 assert(xout.equals(expectedRes[0]));
94 assert(yout.equals(expectedRes[1]));
95 }
96 });
97
98 it("If multiply by order should return 0", async () => {
99
100 const w = circuit.calculateWitness({"e": babyjub.subOrder });
101
102 assert(circuit.checkWitness(w));
103
104 const xout = w[circuit.getSignalIdx("main.out[0]")];
105 const yout = w[circuit.getSignalIdx("main.out[1]")];
106
107 assert(xout.equals(bigInt.zero));
108 assert(yout.equals(bigInt.one));
109 });
110
111});
112