snarkjs/test/algebra.js
test/algebra.jsRaw
1const chai = require("chai");
2
3const bigInt = require("../src/bigint.js");
4const BN128 = require("../src/BN128.js");
5const F1Field = require("../src/zqfield.js");
6
7const assert = chai.assert;
8
9
10describe("F1 testing", () => {
11 it("Should compute euclidean", () => {
12 const F = new F1Field(bigInt(7));
13 const res = F.inverse(bigInt(4));
14
15 assert(F.equals(res, bigInt(2)));
16 });
17
18 it("Should multiply and divide in F1", () => {
19 const bn128 = new BN128();
20 const a = bigInt("1");
21 const b = bn128.F1.affine(bigInt("-3"));
22 const c = bn128.F1.mul(a,b);
23 const d = bn128.F1.div(c,b);
24
25 assert(bn128.F1.equals(a, d));
26 });
27});
28
29
30describe("Curve G1 Test", () => {
31 it("r*one == 0", () => {
32 const bn128 = new BN128();
33
34 const res = bn128.G1.mulEscalar(bn128.G1.g, bn128.r);
35
36 assert(bn128.G1.equals(res, bn128.G1.zero), "G1 does not have range r");
37 });
38
39 it("Should add match in various in G1", () => {
40
41 const bn128 = new BN128();
42
43 const r1 = bigInt(33);
44 const r2 = bigInt(44);
45
46 const gr1 = bn128.G1.mulEscalar(bn128.G1.g, r1);
47 const gr2 = bn128.G1.mulEscalar(bn128.G1.g, r2);
48
49 const grsum1 = bn128.G1.add(gr1, gr2);
50
51 const grsum2 = bn128.G1.mulEscalar(bn128.G1.g, r1.add(r2));
52
53 assert(bn128.G1.equals(grsum1, grsum2));
54 });
55});
56
57describe("Curve G2 Test", () => {
58 it ("r*one == 0", () => {
59 const bn128 = new BN128();
60
61 const res = bn128.G2.mulEscalar(bn128.G2.g, bn128.r);
62
63 assert(bn128.G2.equals(res, bn128.G2.zero), "G2 does not have range r");
64 });
65
66 it("Should add match in various in G2", () => {
67 const bn128 = new BN128();
68
69 const r1 = bigInt(33);
70 const r2 = bigInt(44);
71
72 const gr1 = bn128.G2.mulEscalar(bn128.G2.g, r1);
73 const gr2 = bn128.G2.mulEscalar(bn128.G2.g, r2);
74
75 const grsum1 = bn128.G2.add(gr1, gr2);
76
77 const grsum2 = bn128.G2.mulEscalar(bn128.G2.g, r1.add(r2));
78
79 /*
80 console.log(G2.toString(grsum1));
81 console.log(G2.toString(grsum2));
82 */
83
84 assert(bn128.G2.equals(grsum1, grsum2));
85 });
86});
87
88describe("F6 testing", () => {
89 it("Should multiply and divide in F6", () => {
90 const bn128 = new BN128();
91 const a =
92 [
93 [bigInt("1"), bigInt("2")],
94 [bigInt("3"), bigInt("4")],
95 [bigInt("5"), bigInt("6")]
96 ];
97 const b =
98 [
99 [bigInt("12"), bigInt("11")],
100 [bigInt("10"), bigInt("9")],
101 [bigInt("8"), bigInt("7")]
102 ];
103 const c = bn128.F6.mul(a,b);
104 const d = bn128.F6.div(c,b);
105
106 assert(bn128.F6.equals(a, d));
107 });
108});
109
110describe("F12 testing", () => {
111 it("Should multiply and divide in F12", () => {
112 const bn128 = new BN128();
113 const a =
114 [
115 [
116 [bigInt("1"), bigInt("2")],
117 [bigInt("3"), bigInt("4")],
118 [bigInt("5"), bigInt("6")]
119 ],
120 [
121 [bigInt("7"), bigInt("8")],
122 [bigInt("9"), bigInt("10")],
123 [bigInt("11"), bigInt("12")]
124 ]
125 ];
126 const b =
127 [
128 [
129 [bigInt("12"), bigInt("11")],
130 [bigInt("10"), bigInt("9")],
131 [bigInt("8"), bigInt("7")]
132 ],
133 [
134 [bigInt("6"), bigInt("5")],
135 [bigInt("4"), bigInt("3")],
136 [bigInt("2"), bigInt("1")]
137 ]
138 ];
139 const c = bn128.F12.mul(a,b);
140 const d = bn128.F12.div(c,b);
141
142 assert(bn128.F12.equals(a, d));
143 });
144});
145
146describe("Pairing", () => {
147 it("Should match pairing", () => {
148 const bn128 = new BN128();
149
150
151 const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
152 const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
153
154 const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
155 const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
156
157
158 const pre1a = bn128.precomputeG1(g1a);
159 const pre2a = bn128.precomputeG2(g2a);
160 const pre1b = bn128.precomputeG1(g1b);
161 const pre2b = bn128.precomputeG2(g2b);
162
163 const r1 = bn128.millerLoop(pre1a, pre2a);
164 const r2 = bn128.millerLoop(pre1b, pre2b);
165
166 const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
167
168 const res = bn128.finalExponentiation(rbe);
169
170 assert(bn128.F12.equals(res, bn128.F12.one));
171 }).timeout(10000);
172
173 it("Should match pairing 2", () => {
174 const bn128 = new BN128();
175
176
177 const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
178 const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
179
180 const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
181 const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
182
183
184 const pre1a = bn128.precomputeG1(g1a);
185 const pre2a = bn128.precomputeG2(g2a);
186 const pre1b = bn128.precomputeG1(g1b);
187 const pre2b = bn128.precomputeG2(g2b);
188
189 const r1 = bn128.millerLoop(pre1a, pre2a);
190 const r2 = bn128.millerLoop(pre1b, pre2b);
191
192 const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
193
194 const res = bn128.finalExponentiation(rbe);
195
196 assert(bn128.F12.equals(res, bn128.F12.one));
197 }).timeout(10000);
198});