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.

249 lines
7.7 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. it("inout", async () => {
  38. await doTest(
  39. "inout.circom",
  40. [
  41. [{in1: 1, in2: [2,3], in3:[[4,5], [6,7], [8,9]]}, {out1: 1, out2: [2,3], out3: [[4,5], [6,7],[8,9]]}],
  42. ]
  43. );
  44. });
  45. it("add", async () => {
  46. await doTest(
  47. "add.circom",
  48. [
  49. [{in: [0,0]}, {out: 0}],
  50. [{in: [0,1]}, {out: 1}],
  51. [{in: [1,2]}, {out: 3}],
  52. [{in: [__P__.minus(1),1]}, {out: 0}],
  53. ]
  54. );
  55. });
  56. it("add constant", async () => {
  57. await doTest(
  58. "addconst1.circom",
  59. [
  60. [{in: 0}, {out: 15}],
  61. [{in: 10}, {out: 25}],
  62. [{in: __P__.minus(2)}, {out: 13}],
  63. ]
  64. );
  65. });
  66. it("for unrolled", async () => {
  67. await doTest(
  68. "forunrolled.circom",
  69. [
  70. [{in: 0}, {out: [0,1,2]}],
  71. [{in: 10}, {out: [10, 11, 12]}],
  72. [{in: __P__.minus(2)}, {out: [__P__.minus(2), __P__.minus(1), 0]}],
  73. ]
  74. );
  75. });
  76. it("for rolled", async () => {
  77. await doTest(
  78. "forrolled.circom",
  79. [
  80. [{in: 0}, {out: 0}],
  81. [{in: 10}, {out: 10}],
  82. ]
  83. );
  84. });
  85. it("function1", async () => {
  86. await doTest(
  87. "function1.circom",
  88. [
  89. [{in: 0}, {out: 3}],
  90. [{in: 10}, {out: 13}],
  91. [{in: __P__.minus(2)}, {out: 1}],
  92. ]
  93. );
  94. });
  95. it("function2", async () => {
  96. await doTest(
  97. "function2.circom",
  98. [
  99. [{in: 0}, {out: 3}],
  100. [{in: 10}, {out: 13}],
  101. [{in: __P__.minus(2)}, {out: 1}],
  102. ]
  103. );
  104. });
  105. it("constants1", async () => {
  106. await doTest(
  107. "constants1.circom",
  108. [
  109. [{in: 0}, {out: 42}],
  110. [{in: 10}, {out: 52}],
  111. [{in: __P__.minus(2)}, {out: 40}],
  112. ]
  113. );
  114. });
  115. it("arrays", async () => {
  116. await doTest(
  117. "arrays.circom",
  118. [
  119. [{in: 0}, {out: [1, 8, 51]}],
  120. [{in: 10}, {out: [11, 28, 111]}],
  121. [{in: __P__.minus(2)}, {out: [__P__.minus(1), 4, 39]}],
  122. ]
  123. );
  124. });
  125. it("if unrolled", async () => {
  126. await doTest(
  127. "ifunrolled.circom",
  128. [
  129. [{in: 0}, {out: [1, 3, 6]}],
  130. [{in: 10}, {out: [11, 13, 16]}],
  131. [{in: __P__.minus(2)}, {out: [__P__.minus(1), 1, 4]}],
  132. ]
  133. );
  134. });
  135. it("if rolled", async () => {
  136. await doTest(
  137. "ifrolled.circom",
  138. [
  139. [{in: 0}, {out: [1, 0, 0]}],
  140. [{in: 1}, {out: [0, 1, 0]}],
  141. [{in: 2}, {out: [0, 0, 1]}],
  142. [{in: 3}, {out: [0, 0, 0]}],
  143. [{in: __P__.minus(2)}, {out: [0,0,0]}],
  144. ]
  145. );
  146. });
  147. it("inc", async () => {
  148. await doTest(
  149. "inc.circom",
  150. [
  151. [{in: 0}, {out: [5, 2]}],
  152. [{in: 1}, {out: [6, 4]}],
  153. [{in: 2}, {out: [7, 6]}],
  154. [{in: 3}, {out: [8, 8]}],
  155. [{in: __P__.minus(2)}, {out: [3,__P__.minus(2)]}],
  156. ]
  157. );
  158. });
  159. it("dec", async () => {
  160. await doTest(
  161. "dec.circom",
  162. [
  163. [{in: 0}, {out: [1, __P__.minus(2)]}],
  164. [{in: 1}, {out: [2, 0]}],
  165. [{in: 2}, {out: [3, 2]}],
  166. [{in: 3}, {out: [4, 4]}],
  167. [{in: __P__.minus(2)}, {out: [__P__.minus(1),__P__.minus(6)]}],
  168. ]
  169. );
  170. });
  171. it("ops", async () => {
  172. await doTest(
  173. "ops.circom",
  174. [
  175. [{in: [-2, 2]}, {add: 0, sub: -4, mul: -4}],
  176. [{in: [-1, 1]}, {add: 0, sub: -2, mul: -1}],
  177. [{in: [ 0, 0]}, {add: 0, sub: 0, mul: 0}],
  178. [{in: [ 1,-1]}, {add: 0, sub: 2, mul: -1}],
  179. [{in: [ 2,-2]}, {add: 0, sub: 4, mul: -4}],
  180. [{in: [-2,-3]}, {add: -5, sub: 1, mul: 6}],
  181. [{in: [ 2, 3]}, {add: 5, sub: -1, mul: 6}],
  182. ]
  183. );
  184. });
  185. it("ops2", async () => {
  186. await doTest(
  187. "ops2.circom",
  188. [
  189. [{in: [-2, 2]}, {div: -1, idiv: bigInt("10944121435919637611123202872628637544274182200208017171849102093287904247807"), mod: 1}],
  190. [{in: [-1, 1]}, {div: -1, idiv: -1, mod: 0}],
  191. [{in: [ 1,-1]}, {div: -1, idiv: 0, mod: 1}],
  192. ]
  193. );
  194. });
  195. it("ops3", async () => {
  196. await doTest(
  197. "ops3.circom",
  198. [
  199. [{in: [-2, 2]}, {neg1: 2,neg2: -2, pow: 4}],
  200. [{in: [0, 1]}, {neg1: 0, neg2: -1, pow: 0}],
  201. [{in: [ 1,-1]}, {neg1: -1, neg2: 1, pow: 1}],
  202. ]
  203. );
  204. });
  205. it("Comparation ops", async () => {
  206. await doTest(
  207. "opscmp.circom",
  208. [
  209. [{in: [ 8, 9]}, {lt: 1, leq: 1, eq:0, neq:1, geq: 0, gt:0}],
  210. [{in: [-2,-2]}, {lt: 0, leq: 1, eq:1, neq:0, geq: 1, gt:0}],
  211. [{in: [-1,-2]}, {lt: 0, leq: 0, eq:0, neq:1, geq: 1, gt:1}],
  212. [{in: [ 1,-1]}, {lt: 1, leq: 1, eq:0, neq:1, geq: 0, gt:0}], // In mod, negative values are higher than positive.
  213. ]
  214. );
  215. });
  216. it("Bit ops", async () => {
  217. const mask = bigInt("14474011154664524427946373126085988481658748083205070504932198000989141204991");
  218. const m1m = bigInt("7414231717174750794300032619171286606889616317210963838766006185586667290624");
  219. await doTest(
  220. "opsbit.circom",
  221. [
  222. [{in: [ 5, 3]}, {and: 1, or: 7, xor:6, not1:mask.minus(5), shl: 40, shr:0}],
  223. [{in: [ 0, 0]}, {and: 0, or: 0, xor:0, not1:mask, shl: 0, shr:0}],
  224. [{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)}],
  225. ]
  226. );
  227. });
  228. it("Logical ops", async () => {
  229. await doTest(
  230. "opslog.circom",
  231. [
  232. [{in: [ 5, 0]}, {and: 0, or: 1, not1:0}],
  233. [{in: [ 0, 1]}, {and: 0, or: 1, not1:1}],
  234. [{in: [-1, 9]}, {and: 1, or: 1, not1:0}],
  235. [{in: [ 0, 0]}, {and: 0, or: 0, not1:1}],
  236. ]
  237. );
  238. });
  239. });