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.

168 lines
5.1 KiB

6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const zkSnark = require("zksnark");
  4. const compiler = require("circom");
  5. const assert = chai.assert;
  6. const bigInt = require("big-integer");
  7. const q=21888242871839275222246405745257275088548364400416034343698204186575808495617n
  8. function addPoint(a,b) {
  9. const cta = 168700n;
  10. const d = 168696n;
  11. const res = [];
  12. res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(1n + d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
  13. res[1] = bigInt((a[1]*b[1] - cta*a[0]*b[0]) * bigInt(1n - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
  14. return res;
  15. }
  16. function print(circuit, w, s) {
  17. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  18. }
  19. describe("Exponentioation test", () => {
  20. it("Should generate the Exponentiation table in k=0", async () => {
  21. const cirDef = await compiler(path.join(__dirname, "circuits", "expw4table_test.circom"));
  22. // console.log(JSON.stringify(cirDef, null, 1));
  23. // assert.equal(cirDef.nVars, 2);
  24. const circuit = new zkSnark.Circuit(cirDef);
  25. console.log("NConstrains: " + circuit.nConstraints);
  26. const w = circuit.calculateWitness({});
  27. let g = [zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  28. zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")]
  29. dbl= [zkSnark.bigInt("0"), zkSnark.bigInt("1")];
  30. for (let i=0; i<16; i++) {
  31. const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
  32. const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
  33. /*
  34. console.log(xout1.toString());
  35. console.log(yout1.toString());
  36. console.log(dbl[0]);
  37. console.log(dbl[1]);
  38. */
  39. assert(xout1.equals(dbl[0]));
  40. assert(yout1.equals(dbl[1]));
  41. dbl = addPoint([xout1, yout1],g);
  42. }
  43. });
  44. it("Should generate the Exponentiation table in k=3", async () => {
  45. const cirDef = await compiler(path.join(__dirname, "circuits", "expw4table_test3.circom"));
  46. // console.log(JSON.stringify(cirDef, null, 1));
  47. // assert.equal(cirDef.nVars, 2);
  48. const circuit = new zkSnark.Circuit(cirDef);
  49. console.log("NConstrains: " + circuit.nConstraints);
  50. const w = circuit.calculateWitness({});
  51. let g = [zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  52. zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")]
  53. for (let i=0; i<12;i++) {
  54. g = addPoint(g,g);
  55. }
  56. dbl= [zkSnark.bigInt("0"), zkSnark.bigInt("1")];
  57. for (let i=0; i<16; i++) {
  58. const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
  59. const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
  60. /*
  61. console.log(xout1.toString());
  62. console.log(yout1.toString());
  63. console.log(dbl[0]);
  64. console.log(dbl[1]);
  65. */
  66. assert(xout1.equals(dbl[0]));
  67. assert(yout1.equals(dbl[1]));
  68. dbl = addPoint([xout1, yout1],g);
  69. }
  70. });
  71. it("Should exponentiate g^31", async () => {
  72. const cirDef = await compiler(path.join(__dirname, "circuits", "exp_test.circom"));
  73. // console.log(JSON.stringify(cirDef, null, 1));
  74. // assert.equal(cirDef.nVars, 2);
  75. const circuit = new zkSnark.Circuit(cirDef);
  76. console.log("NConstrains: " + circuit.nConstraints);
  77. const w = circuit.calculateWitness({"in": 31});
  78. assert(circuit.checkWitness(w));
  79. let g = [zkSnark.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  80. zkSnark.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")]
  81. let c = [0n, 1n];
  82. for (let i=0; i<31;i++) {
  83. c = addPoint(c,g);
  84. }
  85. const xout = w[circuit.getSignalIdx(`main.out[0]`)];
  86. const yout = w[circuit.getSignalIdx(`main.out[1]`)];
  87. /*
  88. console.log(xout.toString());
  89. console.log(yout.toString());
  90. */
  91. assert(xout.equals(c[0]));
  92. assert(yout.equals(c[1]));
  93. console.log("-------")
  94. const w2 = circuit.calculateWitness({"in": (1n<<252n)+1n});
  95. const xout2 = w2[circuit.getSignalIdx(`main.out[0]`)];
  96. const yout2 = w2[circuit.getSignalIdx(`main.out[1]`)];
  97. c = [g[0], g[1]];
  98. for (let i=0; i<252;i++) {
  99. c = addPoint(c,c);
  100. }
  101. c = addPoint(c,g);
  102. /*
  103. console.log(xout2.toString());
  104. console.log(yout2.toString());
  105. console.log(c[0].toString());
  106. console.log(c[1].toString());
  107. */
  108. assert(xout2.equals(c[0]));
  109. assert(yout2.equals(c[1]));
  110. }).timeout(10000000);
  111. it("Number of constrains for 256 bits", async () => {
  112. const cirDef = await compiler(path.join(__dirname, "circuits", "exp_test_min.circom"));
  113. const circuit = new zkSnark.Circuit(cirDef);
  114. console.log("NConstrains: " + circuit.nConstraints);
  115. }).timeout(10000000);
  116. });