You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
10 KiB

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