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("function1", async () => {
96 await doTest(
97 "function1.circom",
98 [
99 [{in: 0}, {out: 3}],
100 [{in: 10}, {out: 13}],
101 [{in: __P__.minus(2)}, {out: 1}],
102 ]
103 );
104 });
105 it("function2", async () => {
106 await doTest(
107 "function2.circom",
108 [
109 [{in: 0}, {out: 3}],
110 [{in: 10}, {out: 13}],
111 [{in: __P__.minus(2)}, {out: 1}],
112 ]
113 );
114 });
115 it("constants1", async () => {
116 await doTest(
117 "constants1.circom",
118 [
119 [{in: 0}, {out: 42}],
120 [{in: 10}, {out: 52}],
121 [{in: __P__.minus(2)}, {out: 40}],
122 ]
123 );
124 });
125 it("arrays", async () => {
126 await doTest(
127 "arrays.circom",
128 [
129 [{in: 0}, {out: [1, 8, 51]}],
130 [{in: 10}, {out: [11, 28, 111]}],
131 [{in: __P__.minus(2)}, {out: [__P__.minus(1), 4, 39]}],
132 ]
133 );
134 });
135 it("if unrolled", async () => {
136 await doTest(
137 "ifunrolled.circom",
138 [
139 [{in: 0}, {out: [1, 3, 6]}],
140 [{in: 10}, {out: [11, 13, 16]}],
141 [{in: __P__.minus(2)}, {out: [__P__.minus(1), 1, 4]}],
142 ]
143 );
144 });
145 it("if rolled", async () => {
146 await doTest(
147 "ifrolled.circom",
148 [
149 [{in: 0}, {out: [1, 0, 0]}],
150 [{in: 1}, {out: [0, 1, 0]}],
151 [{in: 2}, {out: [0, 0, 1]}],
152 [{in: 3}, {out: [0, 0, 0]}],
153 [{in: __P__.minus(2)}, {out: [0,0,0]}],
154 ]
155 );
156 });
157 it("inc", async () => {
158 await doTest(
159 "inc.circom",
160 [
161 [{in: 0}, {out: [5, 2]}],
162 [{in: 1}, {out: [6, 4]}],
163 [{in: 2}, {out: [7, 6]}],
164 [{in: 3}, {out: [8, 8]}],
165 [{in: __P__.minus(2)}, {out: [3,__P__.minus(2)]}],
166 ]
167 );
168 });
169 it("dec", async () => {
170 await doTest(
171 "dec.circom",
172 [
173 [{in: 0}, {out: [1, __P__.minus(2)]}],
174 [{in: 1}, {out: [2, 0]}],
175 [{in: 2}, {out: [3, 2]}],
176 [{in: 3}, {out: [4, 4]}],
177 [{in: __P__.minus(2)}, {out: [__P__.minus(1),__P__.minus(6)]}],
178 ]
179 );
180 });
181 it("ops", async () => {
182 await doTest(
183 "ops.circom",
184 [
185 [{in: [-2, 2]}, {add: 0, sub: -4, mul: -4}],
186 [{in: [-1, 1]}, {add: 0, sub: -2, mul: -1}],
187 [{in: [ 0, 0]}, {add: 0, sub: 0, mul: 0}],
188 [{in: [ 1,-1]}, {add: 0, sub: 2, mul: -1}],
189 [{in: [ 2,-2]}, {add: 0, sub: 4, mul: -4}],
190 [{in: [-2,-3]}, {add: -5, sub: 1, mul: 6}],
191 [{in: [ 2, 3]}, {add: 5, sub: -1, mul: 6}],
192 ]
193 );
194 });
195 it("ops2", async () => {
196 await doTest(
197 "ops2.circom",
198 [
199 [{in: [-2, 2]}, {div: -1, idiv: bigInt("10944121435919637611123202872628637544274182200208017171849102093287904247807"), mod: 1}],
200 [{in: [-1, 1]}, {div: -1, idiv: -1, mod: 0}],
201 [{in: [ 1,-1]}, {div: -1, idiv: 0, mod: 1}],
202 ]
203 );
204 });
205});