circom/test/fieldasm.js
test/fieldasm.jsRaw
1const tester = require("../c/buildasm/buildzqfieldtester.js");
2
3const ZqField = require("fflib").ZqField;
4
5const bigInt = require("big-integer");
6
7const bn128q = new bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208583");
8const bn128r = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
9const secp256k1q = new bigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
10const secp256k1r = new bigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
11const mnt6753q = new bigInt("41898490967918953402344214791240637128170709919953949071783502921025352812571106773058893763790338921418070971888458477323173057491593855069696241854796396165721416325350064441470418137846398469611935719059908164220784476160001");
12const mnt6753r = new bigInt("41898490967918953402344214791240637128170709919953949071783502921025352812571106773058893763790338921418070971888253786114353726529584385201591605722013126468931404347949840543007986327743462853720628051692141265303114721689601");
13
14describe("field asm test", function () {
15 this.timeout(1000000000);
16 it("bn128r add", async () => {
17 const tv = buildTestVector2(bn128r, "add");
18 await tester(bn128r, tv);
19 });
20
21 it("secp256k1q add", async () => {
22 const tv = buildTestVector2(secp256k1q, "add");
23 await tester(secp256k1q, tv);
24 });
25 it("mnt6753q add", async () => {
26 const tv = buildTestVector2(mnt6753q, "add");
27 await tester(mnt6753q, tv);
28 });
29 it("bn128r sub", async () => {
30 const tv = buildTestVector2(bn128r, "sub");
31 await tester(bn128r, tv);
32 });
33 it("secp256k1q sub", async () => {
34 const tv = buildTestVector2(secp256k1q, "sub");
35 await tester(secp256k1q, tv);
36 });
37 it("mnt6753q sub", async () => {
38 const tv = buildTestVector2(mnt6753q, "sub");
39 await tester(mnt6753q, tv);
40 });
41
42 it("bn128r neg", async () => {
43 const tv = buildTestVector1(bn128r, "neg");
44 await tester(bn128r, tv);
45 });
46 it("secp256k1q neg", async () => {
47 const tv = buildTestVector1(secp256k1q, "neg");
48 await tester(secp256k1q, tv);
49 });
50 it("mnt6753q neg", async () => {
51 const tv = buildTestVector1(mnt6753q, "neg");
52 await tester(mnt6753q, tv);
53 });
54 it("bn128r mul", async () => {
55 const tv = buildTestVector2(bn128r, "mul");
56 await tester(bn128r, tv);
57 });
58 it("secp256k1q mul", async () => {
59 const tv = buildTestVector2(secp256k1q, "mul");
60 await tester(secp256k1q, tv);
61 });
62 it("mnt6753q mul", async () => {
63 const tv = buildTestVector2(mnt6753q, "mul");
64 await tester(mnt6753q, tv);
65 });
66 it("bn128r binary and", async () => {
67 const tv = buildTestVector2(bn128r, "band");
68 await tester(bn128r, tv);
69 });
70 it("secp256k1q binary and", async () => {
71 const tv = buildTestVector2(secp256k1q, "band");
72 await tester(secp256k1q, tv);
73 });
74 it("mnt6753q binary and", async () => {
75 const tv = buildTestVector2(mnt6753q, "band");
76 await tester(mnt6753q, tv);
77 });
78 it("bn128r binary or", async () => {
79 const tv = buildTestVector2(bn128r, "bor");
80 await tester(bn128r, tv);
81 });
82 it("secp256k1q binary or", async () => {
83 const tv = buildTestVector2(secp256k1q, "bor");
84 await tester(secp256k1q, tv);
85 });
86 it("mnt6753q binary or", async () => {
87 const tv = buildTestVector2(mnt6753q, "bor");
88 await tester(mnt6753q, tv);
89 });
90 it("bn128r binary xor", async () => {
91 const tv = buildTestVector2(bn128r, "bxor");
92 await tester(bn128r, tv);
93 });
94 it("secp256k1q binary xor", async () => {
95 const tv = buildTestVector2(secp256k1q, "bxor");
96 await tester(secp256k1q, tv);
97 });
98 it("mnt6753q binary xor", async () => {
99 const tv = buildTestVector2(mnt6753q, "bxor");
100 await tester(mnt6753q, tv);
101 });
102 it("bn128r binary not", async () => {
103 const tv = buildTestVector1(bn128r, "bnot");
104 await tester(bn128r, tv);
105 });
106 it("secp256k1q binary not", async () => {
107 const tv = buildTestVector1(secp256k1q, "bnot");
108 await tester(secp256k1q, tv);
109 });
110 it("mnt6753q binary not", async () => {
111 const tv = buildTestVector1(mnt6753q, "bnot");
112 await tester(mnt6753q, tv);
113 });
114 it("bn128r eq", async () => {
115 const tv = buildTestVector2(bn128r, "eq");
116 await tester(bn128r, tv);
117 });
118 it("secp256k1q eq", async () => {
119 const tv = buildTestVector2(secp256k1q, "eq");
120 await tester(secp256k1q, tv);
121 });
122 it("mnt6753q eq", async () => {
123 const tv = buildTestVector2(mnt6753q, "eq");
124 await tester(mnt6753q, tv);
125 });
126 it("bn128r neq", async () => {
127 const tv = buildTestVector2(bn128r, "neq");
128 await tester(bn128r, tv);
129 });
130 it("secp256k1q neq", async () => {
131 const tv = buildTestVector2(secp256k1q, "neq");
132 await tester(secp256k1q, tv);
133 });
134 it("mnt6753q neq", async () => {
135 const tv = buildTestVector2(mnt6753q, "neq");
136 await tester(mnt6753q, tv);
137 });
138 it("bn128r lt", async () => {
139 const tv = buildTestVector2(bn128r, "lt");
140 await tester(bn128r, tv);
141 });
142 it("secp256k1q lt", async () => {
143 const tv = buildTestVector2(secp256k1q, "lt");
144 await tester(secp256k1q, tv);
145 });
146 it("mnt6753q lt", async () => {
147 const tv = buildTestVector2(mnt6753q, "lt");
148 await tester(mnt6753q, tv);
149 });
150 it("bn128r gt", async () => {
151 const tv = buildTestVector2(bn128r, "gt");
152 await tester(bn128r, tv);
153 });
154 it("secp256k1q gt", async () => {
155 const tv = buildTestVector2(secp256k1q, "gt");
156 await tester(secp256k1q, tv);
157 });
158 it("mnt6753q gt", async () => {
159 const tv = buildTestVector2(mnt6753q, "gt");
160 await tester(mnt6753q, tv);
161 });
162 it("bn128r leq", async () => {
163 const tv = buildTestVector2(bn128r, "leq");
164 await tester(bn128r, tv);
165 });
166 it("secp256k1q leq", async () => {
167 const tv = buildTestVector2(secp256k1q, "leq");
168 await tester(secp256k1q, tv);
169 });
170 it("mnt6753q leq", async () => {
171 const tv = buildTestVector2(mnt6753q, "leq");
172 await tester(mnt6753q, tv);
173 });
174 it("bn128r geq", async () => {
175 const tv = buildTestVector2(bn128r, "geq");
176 await tester(bn128r, tv);
177 });
178 it("secp256k1q geq", async () => {
179 const tv = buildTestVector2(secp256k1q, "geq");
180 await tester(secp256k1q, tv);
181 });
182 it("mnt6753q geq", async () => {
183 const tv = buildTestVector2(mnt6753q, "geq");
184 await tester(mnt6753q, tv);
185 });
186 it("bn128r logical and", async () => {
187 const tv = buildTestVector2(bn128r, "land");
188 await tester(bn128r, tv);
189 });
190 it("secp256k1q logical and", async () => {
191 const tv = buildTestVector2(secp256k1q, "land");
192 await tester(secp256k1q, tv);
193 });
194 it("mnt6753q logical and", async () => {
195 const tv = buildTestVector2(mnt6753q, "land");
196 await tester(mnt6753q, tv);
197 });
198 it("bn128r logical or", async () => {
199 const tv = buildTestVector2(bn128r, "lor");
200 await tester(bn128r, tv);
201 });
202 it("secp256k1q logical or", async () => {
203 const tv = buildTestVector2(secp256k1q, "lor");
204 await tester(secp256k1q, tv);
205 });
206 it("mnt6753q logical or", async () => {
207 const tv = buildTestVector2(mnt6753q, "lor");
208 await tester(mnt6753q, tv);
209 });
210 it("bn128r logical not", async () => {
211 const tv = buildTestVector1(bn128r, "lnot");
212 await tester(bn128r, tv);
213 });
214 it("secp256k1q logical not", async () => {
215 const tv = buildTestVector1(secp256k1q, "lnot");
216 await tester(secp256k1q, tv);
217 });
218 it("mnt6753q logical not", async () => {
219 const tv = buildTestVector1(mnt6753q, "lnot");
220 await tester(mnt6753q, tv);
221 });
222 it("bn128r idiv", async () => {
223 const tv = buildTestVector2(bn128r, "idiv");
224 await tester(bn128r, tv);
225 });
226 it("secp256k1q idiv", async () => {
227 const tv = buildTestVector2(secp256k1q, "idiv");
228 await tester(secp256k1q, tv);
229 });
230 it("mnt6753q idiv", async () => {
231 const tv = buildTestVector2(mnt6753q, "idiv");
232 await tester(mnt6753q, tv);
233 });
234 it("bn128r inv", async () => {
235 const tv = buildTestVector1(bn128r, "inv");
236 await tester(bn128r, tv);
237 });
238 it("secp256k1q inv", async () => {
239 const tv = buildTestVector1(secp256k1q, "inv");
240 await tester(secp256k1q, tv);
241 });
242 it("mnt6753q inv", async () => {
243 const tv = buildTestVector1(mnt6753q, "inv");
244 await tester(mnt6753q, tv);
245 });
246 it("bn128r div", async () => {
247 const tv = buildTestVector2(bn128r, "div");
248 await tester(bn128r, tv);
249 });
250 it("secp256k1q div", async () => {
251 const tv = buildTestVector2(secp256k1q, "div");
252 await tester(secp256k1q, tv);
253 });
254 it("mnt6753q div", async () => {
255 const tv = buildTestVector2(mnt6753q, "div");
256 await tester(mnt6753q, tv);
257 });
258 it("bn128r square", async () => {
259 const tv = buildTestVector1(bn128r, "square");
260 await tester(bn128r, tv);
261 });
262 it("secp256k1q square", async () => {
263 const tv = buildTestVector1(secp256k1q, "square");
264 await tester(secp256k1q, tv);
265 });
266 it("mnt6753q square", async () => {
267 const tv = buildTestVector1(mnt6753q, "square");
268 await tester(mnt6753q, tv);
269 });
270
271});
272
273function buildTestVector2(p, op) {
274 const F = new ZqField(p);
275 const tv = [];
276 const nums = getCriticalNumbers(p, 2);
277
278 const excludeZero = ["div", "mod", "idiv"].indexOf(op) >= 0;
279
280 for (let i=0; i<nums.length; i++) {
281 for (let j=0; j<nums.length; j++) {
282 if ((excludeZero)&&(nums[j][0].isZero())) continue;
283 tv.push([
284 [nums[i][1], nums[j][1], op],
285 F[op](nums[i][0], nums[j][0])
286 ]);
287 }
288 }
289
290 return tv;
291}
292
293function buildTestVector1(p, op) {
294 const F = new ZqField(p);
295 const tv = [];
296 const nums = getCriticalNumbers(p, 2);
297
298 const excludeZero = ["inv"].indexOf(op) >= 0;
299
300 for (let i=0; i<nums.length; i++) {
301 if ((excludeZero)&&(nums[i][0].isZero())) continue;
302 tv.push([
303 [nums[i][1], op],
304 F[op](nums[i][0])
305 ]);
306 }
307
308 return tv;
309}
310
311function getCriticalNumbers(p, lim) {
312 const numbers = [];
313
314 addFrontier(0);
315 addFrontier(bigInt.one.shiftLeft(31));
316 addFrontier(p.minus(bigInt.one.shiftLeft(31)));
317 addFrontier(bigInt.one.shiftLeft(32));
318 addFrontier(p.minus(bigInt.one.shiftLeft(32)));
319 addFrontier(bigInt.one.shiftLeft(63));
320 addFrontier(p.minus(bigInt.one.shiftLeft(63)));
321 addFrontier(bigInt.one.shiftLeft(64));
322 addFrontier(p.minus(bigInt.one.shiftLeft(64)));
323 addFrontier(bigInt.one.shiftLeft(p.bitLength()-1));
324 addFrontier(p.shiftRight(1));
325
326 function addFrontier(f) {
327 for (let i=-lim; i<=lim; i++) {
328 let n = bigInt(f).add(bigInt(i));
329 n = n.mod(p);
330 if (n.isNegative()) n = p.add(n);
331 addNumber(n);
332 }
333 }
334
335 return numbers;
336
337 function addNumber(n) {
338 if (n.lt(bigInt("80000000", 16)) ) {
339 addShortPositive(n);
340 addShortMontgomeryPositive(n);
341 }
342 if (n.geq(p.minus(bigInt("80000000", 16))) ) {
343 addShortNegative(n);
344 addShortMontgomeryNegative(n);
345 }
346 addLongNormal(n);
347 addLongMontgomery(n);
348
349 function addShortPositive(a) {
350 numbers.push([a, "0x"+a.toString(16)]);
351 }
352
353 function addShortMontgomeryPositive(a) {
354 let S = "0x" + bigInt("40", 16).shiftLeft(56).add(a).toString(16);
355 S = S + "," + getLongString(toMontgomery(a));
356 numbers.push([a, S]);
357 }
358
359 function addShortNegative(a) {
360 const b = bigInt("80000000", 16 ).add(a.minus( p.minus(bigInt("80000000", 16 ))));
361 numbers.push([a, "0x"+b.toString(16)]);
362 }
363
364 function addShortMontgomeryNegative(a) {
365 const b = bigInt("80000000", 16 ).add(a.minus( p.minus(bigInt("80000000", 16 ))));
366 let S = "0x" + bigInt("40", 16).shiftLeft(56).add(b).toString(16);
367 S = S + "," + getLongString(toMontgomery(a));
368 numbers.push([a, S]);
369 }
370
371 function addLongNormal(a) {
372 let S = "0x" + bigInt("80", 16).shiftLeft(56).toString(16);
373 S = S + "," + getLongString(a);
374 numbers.push([a, S]);
375 }
376
377
378 function addLongMontgomery(a) {
379
380 let S = "0x" + bigInt("C0", 16).shiftLeft(56).toString(16);
381 S = S + "," + getLongString(toMontgomery(a));
382 numbers.push([a, S]);
383 }
384
385 function getLongString(a) {
386 if (a.isZero()) {
387 return "0x0";
388 }
389 let r = a;
390 let S = "";
391 while (!r.isZero()) {
392 if (S!= "") S = S+",";
393 S += "0x" + r.and(bigInt("FFFFFFFFFFFFFFFF", 16)).toString(16);
394 r = r.shiftRight(64);
395 }
396 return S;
397 }
398
399 function toMontgomery(a) {
400 const n64 = Math.floor((p.bitLength() - 1) / 64)+1;
401 const R = bigInt.one.shiftLeft(n64*64);
402 return a.times(R).mod(p);
403 }
404
405 }
406}
407