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.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const assert = chai.assert;
  6. const bigInt = snarkjs.bigInt;
  7. const q=bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  8. function addPoint(a,b) {
  9. const cta = bigInt("168700");
  10. const d = bigInt("168696");
  11. const res = [];
  12. res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(bigInt.one + 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(bigInt.one - 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", "escalarmulw4table_test.circom"));
  22. // console.log(JSON.stringify(cirDef, null, 1));
  23. // assert.equal(cirDef.nVars, 2);
  24. const circuit = new snarkjs.Circuit(cirDef);
  25. console.log("NConstrains: " + circuit.nConstraints);
  26. const w = circuit.calculateWitness({in: 1});
  27. let g = [bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  28. bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
  29. dbl= [bigInt("0"), snarkjs.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", "escalarmulw4table_test3.circom"));
  46. // console.log(JSON.stringify(cirDef, null, 1));
  47. // assert.equal(cirDef.nVars, 2);
  48. const circuit = new snarkjs.Circuit(cirDef);
  49. console.log("NConstrains: " + circuit.nConstraints);
  50. const w = circuit.calculateWitness({in: 1});
  51. let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  52. snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
  53. for (let i=0; i<12;i++) {
  54. g = addPoint(g,g);
  55. }
  56. dbl= [snarkjs.bigInt("0"), snarkjs.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", "escalarmul_test.circom"));
  73. // console.log(JSON.stringify(cirDef, null, 1));
  74. // assert.equal(cirDef.nVars, 2);
  75. const circuit = new snarkjs.Circuit(cirDef);
  76. console.log("NConstrains: " + circuit.nConstraints);
  77. const w = circuit.calculateWitness({"in": 31});
  78. assert(circuit.checkWitness(w));
  79. let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  80. snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
  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", "escalarmul_test_min.circom"));
  113. const circuit = new snarkjs.Circuit(cirDef);
  114. console.log("NConstrains: " + circuit.nConstraints);
  115. }).timeout(10000000);
  116. });