forked from iden3/circomlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
escalarmul.js
124 lines (80 loc) · 3.52 KB
/
escalarmul.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const buildBabyjub = require("circomlibjs").buildBabyjub;
const Scalar = require("ffjavascript").Scalar;
const assert = chai.assert;
function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
describe("Exponentioation test", function () {
let babyJub;
let Fr;
this.timeout(100000);
before( async () => {
babyJub = await buildBabyjub();
Fr = babyJub.F;
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should generate the Exponentiation table in k=0", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
const w = await circuit.calculateWitness({in: 1});
await circuit.checkConstraints(w);
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
let dbl= [Fr.e("0"), Fr.e("1")];
const expectedOut = [];
for (let i=0; i<16; i++) {
expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
dbl = babyJub.addPoint(dbl,g);
}
await circuit.assertOut(w, {out: expectedOut});
});
it("Should generate the Exponentiation table in k=3", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
const w = await circuit.calculateWitness({in: 1});
await circuit.checkConstraints(w);
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
for (let i=0; i<12;i++) {
g = babyJub.addPoint(g,g);
}
let dbl= [Fr.e("0"), Fr.e("1")];
const expectedOut = [];
for (let i=0; i<16; i++) {
expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
dbl = babyJub.addPoint(dbl,g);
}
await circuit.assertOut(w, {out: expectedOut});
});
it("Should exponentiate g^31", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test.circom"));
const w = await circuit.calculateWitness({"in": 31});
await circuit.checkConstraints(w);
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
let c = [Fr.e(0), Fr.e(1)];
for (let i=0; i<31;i++) {
c = babyJub.addPoint(c,g);
}
await circuit.assertOut(w, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
const w2 = await circuit.calculateWitness({"in": Scalar.add(Scalar.shl(Scalar.e(1), 252),Scalar.e(1))});
c = [g[0], g[1]];
for (let i=0; i<252;i++) {
c = babyJub.addPoint(c,c);
}
c = babyJub.addPoint(c,g);
await circuit.assertOut(w2, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
}).timeout(10000000);
it("Number of constrains for 256 bits", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
}).timeout(10000000);
});