circom/test/basiccases.js
test/basiccases.jsRaw
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 it("inout", async () => {
48 await doTest(
49 "inout.circom",
50 [
51 [{in1: 1, in2: [2,3], in3:[[4,5], [6,7], [8,9]]}, {out1: 1, out2: [2,3], out3: [[4,5], [6,7],[8,9]]}],
52 ]
53 );
54 });
55 it("add", async () => {
56 await doTest(
57 "add.circom",
58 [
59 [{in: [0,0]}, {out: 0}],
60 [{in: [0,1]}, {out: 1}],
61 [{in: [1,2]}, {out: 3}],
62 [{in: [__P__.minus(1),1]}, {out: 0}],
63 ]
64 );
65 });
66 it("add constant", async () => {
67 await doTest(
68 "addconst1.circom",
69 [
70 [{in: 0}, {out: 15}],
71 [{in: 10}, {out: 25}],
72 [{in: __P__.minus(2)}, {out: 13}],
73 ]
74 );
75 });
76 it("for unrolled", async () => {
77 await doTest(
78 "forunrolled.circom",
79 [
80 [{in: 0}, {out: [0,1,2]}],
81 [{in: 10}, {out: [10, 11, 12]}],
82 [{in: __P__.minus(2)}, {out: [__P__.minus(2), __P__.minus(1), 0]}],
83 ]
84 );
85 });
86 it("for rolled", async () => {
87 await doTest(
88 "forrolled.circom",
89 [
90 [{in: 0}, {out: 0}],
91 [{in: 10}, {out: 10}],
92 ]
93 );
94 });
95 it("while unrolled", async () => {
96 await doTest(
97 "whileunrolled.circom",
98 [
99 [{in: 0}, {out: [0,1,2]}],
100 [{in: 10}, {out: [10, 11, 12]}],
101 [{in: __P__.minus(2)}, {out: [__P__.minus(2), __P__.minus(1), 0]}],
102 ]
103 );
104 });
105 it("while rolled", async () => {
106 await doTest(
107 "whilerolled.circom",
108 [
109 [{in: 0}, {out: 0}],
110 [{in: 10}, {out: 10}],
111 ]
112 );
113 });
114 it("function1", async () => {
115 await doTest(
116 "function1.circom",
117 [
118 [{in: 0}, {out: 3}],
119 [{in: 10}, {out: 13}],
120 [{in: __P__.minus(2)}, {out: 1}],
121 ]
122 );
123 });
124 it("function2", async () => {
125 await doTest(
126 "function2.circom",
127 [
128 [{in: 0}, {out: 3}],
129 [{in: 10}, {out: 13}],
130 [{in: __P__.minus(2)}, {out: 1}],
131 ]
132 );
133 });
134 it("constants1", async () => {
135 await doTest(
136 "constants1.circom",
137 [
138 [{in: 0}, {out: 42}],
139 [{in: 10}, {out: 52}],
140 [{in: __P__.minus(2)}, {out: 40}],
141 ]
142 );
143 });
144 it("arrays", async () => {
145 await doTest(
146 "arrays.circom",
147 [
148 [{in: 0}, {out: [1, 8, 51]}],
149 [{in: 10}, {out: [11, 28, 111]}],
150 [{in: __P__.minus(2)}, {out: [__P__.minus(1), 4, 39]}],
151 ]
152 );
153 });
154 it("if unrolled", async () => {
155 await doTest(
156 "ifunrolled.circom",
157 [
158 [{in: 0}, {out: [1, 3, 6]}],
159 [{in: 10}, {out: [11, 13, 16]}],
160 [{in: __P__.minus(2)}, {out: [__P__.minus(1), 1, 4]}],
161 ]
162 );
163 });
164 it("if rolled", async () => {
165 await doTest(
166 "ifrolled.circom",
167 [
168 [{in: 0}, {out: [1, 0, 0]}],
169 [{in: 1}, {out: [0, 1, 0]}],
170 [{in: 2}, {out: [0, 0, 1]}],
171 [{in: 3}, {out: [0, 0, 0]}],
172 [{in: __P__.minus(2)}, {out: [0,0,0]}],
173 ]
174 );
175 });
176 it("inc", async () => {
177 await doTest(
178 "inc.circom",
179 [
180 [{in: 0}, {out: [5, 2]}],
181 [{in: 1}, {out: [6, 4]}],
182 [{in: 2}, {out: [7, 6]}],
183 [{in: 3}, {out: [8, 8]}],
184 [{in: __P__.minus(2)}, {out: [3,__P__.minus(2)]}],
185 ]
186 );
187 });
188 it("dec", async () => {
189 await doTest(
190 "dec.circom",
191 [
192 [{in: 0}, {out: [1, __P__.minus(2)]}],
193 [{in: 1}, {out: [2, 0]}],
194 [{in: 2}, {out: [3, 2]}],
195 [{in: 3}, {out: [4, 4]}],
196 [{in: __P__.minus(2)}, {out: [__P__.minus(1),__P__.minus(6)]}],
197 ]
198 );
199 });
200 it("ops", async () => {
201 await doTest(
202 "ops.circom",
203 [
204 [{in: [-2, 2]}, {add: 0, sub: -4, mul: -4}],
205 [{in: [-1, 1]}, {add: 0, sub: -2, mul: -1}],
206 [{in: [ 0, 0]}, {add: 0, sub: 0, mul: 0}],
207 [{in: [ 1,-1]}, {add: 0, sub: 2, mul: -1}],
208 [{in: [ 2,-2]}, {add: 0, sub: 4, mul: -4}],
209 [{in: [-2,-3]}, {add: -5, sub: 1, mul: 6}],
210 [{in: [ 2, 3]}, {add: 5, sub: -1, mul: 6}],
211 ]
212 );
213 });
214 it("ops2", async () => {
215 await doTest(
216 "ops2.circom",
217 [
218 [{in: [-2, 2]}, {div: -1, idiv: bigInt("10944121435919637611123202872628637544274182200208017171849102093287904247807"), mod: 1}],
219 [{in: [-1, 1]}, {div: -1, idiv: -1, mod: 0}],
220 [{in: [ 1,-1]}, {div: -1, idiv: 0, mod: 1}],
221 ]
222 );
223 });
224 it("ops3", async () => {
225 await doTest(
226 "ops3.circom",
227 [
228 [{in: [-2, 2]}, {neg1: 2,neg2: -2, pow: 4}],
229 [{in: [0, 1]}, {neg1: 0, neg2: -1, pow: 0}],
230 [{in: [ 1,-1]}, {neg1: -1, neg2: 1, pow: 1}],
231 ]
232 );
233 });
234 it("Comparation ops", async () => {
235 await doTest(
236 "opscmp.circom",
237 [
238 [{in: [ 8, 9]}, {lt: 1, leq: 1, eq:0, neq:1, geq: 0, gt:0}],
239 [{in: [-2,-2]}, {lt: 0, leq: 1, eq:1, neq:0, geq: 1, gt:0}],
240 [{in: [-1,-2]}, {lt: 0, leq: 0, eq:0, neq:1, geq: 1, gt:1}],
241 [{in: [ 1,-1]}, {lt: 1, leq: 1, eq:0, neq:1, geq: 0, gt:0}], // In mod, negative values are higher than positive.
242 ]
243 );
244 });
245 it("Bit ops", async () => {
246 const mask = bigInt("14474011154664524427946373126085988481658748083205070504932198000989141204991");
247 const m1m = bigInt("7414231717174750794300032619171286606889616317210963838766006185586667290624");
248 await doTest(
249 "opsbit.circom",
250 [
251 [{in: [ 5, 3]}, {and: 1, or: 7, xor:6, not1:mask.minus(5), shl: 40, shr:0}],
252 [{in: [ 0, 0]}, {and: 0, or: 0, xor:0, not1:mask, shl: 0, shr:0}],
253 [{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)}],
254 ]
255 );
256 });
257 it("Logical ops", async () => {
258 await doTest(
259 "opslog.circom",
260 [
261 [{in: [ 5, 0]}, {and: 0, or: 1, not1:0}],
262 [{in: [ 0, 1]}, {and: 0, or: 1, not1:1}],
263 [{in: [-1, 9]}, {and: 1, or: 1, not1:0}],
264 [{in: [ 0, 0]}, {and: 0, or: 0, not1:1}],
265 ]
266 );
267 });
268 it("Conditional Ternary operator", async () => {
269 await doTest(
270 "condternary.circom",
271 [
272 [{in: 0}, {out: 21}],
273 [{in: 1}, {out: 1}],
274 [{in: 2}, {out: 23}],
275 [{in:-1}, {out: 20}],
276 ]
277 );
278 });
279 it("Compute block", async () => {
280 await doTest(
281 "compute.circom",
282 [
283 [{x: 1}, {y: 7}],
284 [{x: 2}, {y: 7}],
285 [{x: 3}, {y: 11}],
286 [{x:-1}, {y: -5}],
287 ]
288 );
289 });
290 it("Component array ", async () => {
291 await doTest(
292 "componentarray.circom",
293 [
294 [{in: 1}, {out: 1}],
295 [{in: 2}, {out: 256}],
296 [{in: 3}, {out: 6561}],
297 [{in:-1}, {out: 1}],
298 ]
299 );
300 });
301});