circom/test/basiccases.js.old
test/basiccases.js.oldRaw
1const path = require("path");
2
3const bigInt = require("big-integer");
4const c_tester = require("../index.js").c_tester;
5
6const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
7
8
9function normalize(o) {
10 if ((typeof(o) == "bigint") || o.isZero !== undefined) {
11 const res = bigInt(o);
12 return norm(res);
13 } else if (Array.isArray(o)) {
14 return o.map(normalize);
15 } else if (typeof o == "object") {
16 const res = {};
17 for (let k in o) {
18 res[k] = normalize(o[k]);
19 }
20 return res;
21 } else {
22 const res = bigInt(o);
23 return norm(res);
24 }
25
26 function norm(n) {
27 let res = n.mod(__P__);
28 if (res.isNegative()) res = __P__.add(res);
29 return res;
30 }
31}
32
33
34async function doTest(circuit, testVectors) {
35 const cir = await c_tester(path.join(__dirname, "circuits", circuit));
36
37 for (let i=0; i<testVectors.length; i++) {
38 const w = await cir.calculateWitness(normalize(testVectors[i][0]));
39 await cir.assertOut(w, normalize(testVectors[i][1]) );
40 }
41
42 await cir.release();
43}
44
45describe("basic cases", function () {
46 this.timeout(100000);
47
48 it("inout", async () => {
49 await doTest(
50 "inout.circom",
51 [
52 [{in1: 1, in2: [2,3], in3:[[4,5], [6,7], [8,9]]}, {out1: 1, out2: [2,3], out3: [[4,5], [6,7],[8,9]]}],
53 ]
54 );
55 });
56 it("add", async () => {
57 await doTest(
58 "add.circom",
59 [
60 [{in: [0,0]}, {out: 0}],
61 [{in: [0,1]}, {out: 1}],
62 [{in: [1,2]}, {out: 3}],
63 [{in: [__P__.minus(1),1]}, {out: 0}],
64 ]
65 );
66 });
67 it("add constant", async () => {
68 await doTest(
69 "addconst1.circom",
70 [
71 [{in: 0}, {out: 15}],
72 [{in: 10}, {out: 25}],
73 [{in: __P__.minus(2)}, {out: 13}],
74 ]
75 );
76 });
77 it("for unrolled", async () => {
78 await doTest(
79 "forunrolled.circom",
80 [
81 [{in: 0}, {out: [0,1,2]}],
82 [{in: 10}, {out: [10, 11, 12]}],
83 [{in: __P__.minus(2)}, {out: [__P__.minus(2), __P__.minus(1), 0]}],
84 ]
85 );
86 });
87 it("for rolled", async () => {
88 await doTest(
89 "forrolled.circom",
90 [
91 [{in: 0}, {out: 0}],
92 [{in: 10}, {out: 10}],
93 ]
94 );
95 });
96 it("while unrolled", async () => {
97 await doTest(
98 "whileunrolled.circom",
99 [
100 [{in: 0}, {out: [0,1,2]}],
101 [{in: 10}, {out: [10, 11, 12]}],
102 [{in: __P__.minus(2)}, {out: [__P__.minus(2), __P__.minus(1), 0]}],
103 ]
104 );
105 });
106 it("while rolled", async () => {
107 await doTest(
108 "whilerolled.circom",
109 [
110 [{in: 0}, {out: 0}],
111 [{in: 10}, {out: 10}],
112 ]
113 );
114 });
115 it("function1", async () => {
116 await doTest(
117 "function1.circom",
118 [
119 [{in: 0}, {out: 3}],
120 [{in: 10}, {out: 13}],
121 [{in: __P__.minus(2)}, {out: 1}],
122 ]
123 );
124 });
125 it("function2", async () => {
126 await doTest(
127 "function2.circom",
128 [
129 [{in: 0}, {out: 3}],
130 [{in: 10}, {out: 13}],
131 [{in: __P__.minus(2)}, {out: 1}],
132 ]
133 );
134 });
135 it("constants1", async () => {
136 await doTest(
137 "constants1.circom",
138 [
139 [{in: 0}, {out: 42}],
140 [{in: 10}, {out: 52}],
141 [{in: __P__.minus(2)}, {out: 40}],
142 ]
143 );
144 });
145 it("arrays", async () => {
146 await doTest(
147 "arrays.circom",
148 [
149 [{in: 0}, {out: [1, 8, 51]}],
150 [{in: 10}, {out: [11, 28, 111]}],
151 [{in: __P__.minus(2)}, {out: [__P__.minus(1), 4, 39]}],
152 ]
153 );
154 });
155 it("if unrolled", async () => {
156 await doTest(
157 "ifunrolled.circom",
158 [
159 [{in: 0}, {out: [1, 3, 6]}],
160 [{in: 10}, {out: [11, 13, 16]}],
161 [{in: __P__.minus(2)}, {out: [__P__.minus(1), 1, 4]}],
162 ]
163 );
164 });
165 it("if rolled", async () => {
166 await doTest(
167 "ifrolled.circom",
168 [
169 [{in: 0}, {out: [1, 0, 0]}],
170 [{in: 1}, {out: [0, 1, 0]}],
171 [{in: 2}, {out: [0, 0, 1]}],
172 [{in: 3}, {out: [0, 0, 0]}],
173 [{in: __P__.minus(2)}, {out: [0,0,0]}],
174 ]
175 );
176 });
177 it("inc", async () => {
178 await doTest(
179 "inc.circom",
180 [
181 [{in: 0}, {out: [5, 2]}],
182 [{in: 1}, {out: [6, 4]}],
183 [{in: 2}, {out: [7, 6]}],
184 [{in: 3}, {out: [8, 8]}],
185 [{in: __P__.minus(2)}, {out: [3,__P__.minus(2)]}],
186 ]
187 );
188 });
189 it("dec", async () => {
190 await doTest(
191 "dec.circom",
192 [
193 [{in: 0}, {out: [1, __P__.minus(2)]}],
194 [{in: 1}, {out: [2, 0]}],
195 [{in: 2}, {out: [3, 2]}],
196 [{in: 3}, {out: [4, 4]}],
197 [{in: __P__.minus(2)}, {out: [__P__.minus(1),__P__.minus(6)]}],
198 ]
199 );
200 });
201 it("ops", async () => {
202 await doTest(
203 "ops.circom",
204 [
205 [{in: [-2, 2]}, {add: 0, sub: -4, mul: -4}],
206 [{in: [-1, 1]}, {add: 0, sub: -2, mul: -1}],
207 [{in: [ 0, 0]}, {add: 0, sub: 0, mul: 0}],
208 [{in: [ 1,-1]}, {add: 0, sub: 2, mul: -1}],
209 [{in: [ 2,-2]}, {add: 0, sub: 4, mul: -4}],
210 [{in: [-2,-3]}, {add: -5, sub: 1, mul: 6}],
211 [{in: [ 2, 3]}, {add: 5, sub: -1, mul: 6}],
212 ]
213 );
214 });
215 it("ops2", async () => {
216 await doTest(
217 "ops2.circom",
218 [
219 [{in: [-2, 2]}, {div: -1, idiv: bigInt("10944121435919637611123202872628637544274182200208017171849102093287904247807"), mod: 1}],
220 [{in: [-1, 1]}, {div: -1, idiv: -1, mod: 0}],
221 [{in: [ 1,-1]}, {div: -1, idiv: 0, mod: 1}],
222 ]
223 );
224 });
225 it("ops3", async () => {
226 await doTest(
227 "ops3.circom",
228 [
229 [{in: [-2, 2]}, {neg1: 2,neg2: -2, pow: 4}],
230 [{in: [0, 1]}, {neg1: 0, neg2: -1, pow: 0}],
231 [{in: [ 1,-1]}, {neg1: -1, neg2: 1, pow: 1}],
232 ]
233 );
234 });
235 it("Comparation ops", async () => {
236 await doTest(
237 "opscmp.circom",
238 [
239 [{in: [ 8, 9]}, {lt: 1, leq: 1, eq:0, neq:1, geq: 0, gt:0}],
240 [{in: [-2,-2]}, {lt: 0, leq: 1, eq:1, neq:0, geq: 1, gt:0}],
241 [{in: [-1,-2]}, {lt: 0, leq: 0, eq:0, neq:1, geq: 1, gt:1}],
242 [{in: [ 1,-1]}, {lt: 0, leq: 0, eq:0, neq:1, geq: 1, gt:1}], // In mod, negative values are higher than positive.
243 ]
244 );
245 });
246 it("Bit ops", async () => {
247 const mask = bigInt("14474011154664524427946373126085988481658748083205070504932198000989141204991");
248 const m1m = bigInt("7414231717174750794300032619171286606889616317210963838766006185586667290624");
249 await doTest(
250 "opsbit.circom",
251 [
252 [{in: [ 5, 3]}, {and: 1, or: 7, xor:6, not1:mask.minus(5), shl: 40, shr:0}],
253 [{in: [ 0, 0]}, {and: 0, or: 0, xor:0, not1:mask, shl: 0, shr:0}],
254 [{in: [-1, 1]}, {and: 0, or: m1m.add(bigInt.one), xor:m1m.add(bigInt.one), not1:mask.minus(m1m), shl: m1m.shiftLeft(1).and(mask), shr:__P__.shiftRight(1).and(mask)}],
255 ]
256 );
257 });
258 it("Logical ops", async () => {
259 await doTest(
260 "opslog.circom",
261 [
262 [{in: [ 5, 0]}, {and: 0, or: 1, not1:0}],
263 [{in: [ 0, 1]}, {and: 0, or: 1, not1:1}],
264 [{in: [-1, 9]}, {and: 1, or: 1, not1:0}],
265 [{in: [ 0, 0]}, {and: 0, or: 0, not1:1}],
266 ]
267 );
268 });
269
270 it("Conditional Ternary operator", async () => {
271 await doTest(
272 "condternary.circom",
273 [
274 [{in: 0}, {out: 21}],
275 [{in: 1}, {out: 1}],
276 [{in: 2}, {out: 23}],
277 [{in:-1}, {out: 20}],
278 ]
279 );
280 });
281
282 it("Compute block", async () => {
283 await doTest(
284 "compute.circom",
285 [
286 [{x: 1}, {y: 7}],
287 [{x: 2}, {y: 7}],
288 [{x: 3}, {y: 11}],
289 [{x:-1}, {y: -5}],
290 ]
291 );
292 });
293 it("Component array ", async () => {
294 await doTest(
295 "componentarray.circom",
296 [
297 [{in: 1}, {out: 1}],
298 [{in: 2}, {out: 256}],
299 [{in: 3}, {out: 6561}],
300 [{in:-1}, {out: 1}],
301 ]
302 );
303 });
304 it("Component array 2d", async () => {
305 await doTest(
306 "componentarray2.circom",
307 [
308 [{in: [1,2]}, {out: [1, 256]}],
309 [{in: [0,3]}, {out: [0, 6561]}],
310 ]
311 );
312 });
313 it("Constant circuit", async () => {
314 await doTest(
315 "constantcircuit.circom",
316 [
317 // 0xbb67ae85
318 [{}, {out: [1,0,1,0, 0,0,0,1, 0,1,1,1, 0,1,0,1, 1,1,1,0, 0,1,1,0, 1,1,0,1, 1,1,0,1]}],
319 ]
320 );
321 });
322 it("Constant internal circuit", async () => {
323 await doTest(
324 "constantinternalcircuit.circom",
325 [
326 [{in: 1}, {out: 5}],
327 [{in: 0}, {out: 4}],
328 [{in: -2}, {out: 2}],
329 [{in: 10}, {out: 14}]
330 ]
331 );
332 });
333 it("include", async () => {
334 await doTest(
335 "include.circom",
336 [
337 [{in: 3}, {out: 6}],
338 [{in: 6}, {out: 15}],
339 ]
340 );
341 });
342
343});