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.

172 lines
5.3 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
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. assert(circuit.checkWitness(w));
  28. let g = [bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  29. bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
  30. dbl= [bigInt("0"), snarkjs.bigInt("1")];
  31. for (let i=0; i<16; i++) {
  32. const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
  33. const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
  34. // console.log(xout1.toString());
  35. // console.log(yout1.toString());
  36. // console.log(dbl[0]);
  37. // console.log(dbl[1]);
  38. assert(xout1.equals(dbl[0]));
  39. assert(yout1.equals(dbl[1]));
  40. dbl = addPoint([xout1, yout1],g);
  41. }
  42. });
  43. it("Should generate the Exponentiation table in k=3", async () => {
  44. const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
  45. // console.log(JSON.stringify(cirDef, null, 1));
  46. // assert.equal(cirDef.nVars, 2);
  47. const circuit = new snarkjs.Circuit(cirDef);
  48. console.log("NConstrains: " + circuit.nConstraints);
  49. const w = circuit.calculateWitness({in: 1});
  50. assert(circuit.checkWitness(w));
  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. // console.log(xout1.toString());
  61. // console.log(yout1.toString());
  62. // console.log(dbl[0]);
  63. // console.log(dbl[1]);
  64. assert(xout1.equals(dbl[0]));
  65. assert(yout1.equals(dbl[1]));
  66. dbl = addPoint([xout1, yout1],g);
  67. }
  68. });
  69. it("Should exponentiate g^31", async () => {
  70. const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test.circom"), {reduceConstraints: true});
  71. // console.log(JSON.stringify(cirDef, null, 1));
  72. // assert.equal(cirDef.nVars, 2);
  73. const circuit = new snarkjs.Circuit(cirDef);
  74. console.log("NConstrains: " + circuit.nConstraints);
  75. const w = circuit.calculateWitness({"in": 31});
  76. assert(circuit.checkWitness(w));
  77. let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  78. snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
  79. let c = [0n, 1n];
  80. for (let i=0; i<31;i++) {
  81. c = addPoint(c,g);
  82. }
  83. const xout = w[circuit.getSignalIdx(`main.out[0]`)];
  84. const yout = w[circuit.getSignalIdx(`main.out[1]`)];
  85. /*
  86. console.log(xout.toString());
  87. console.log(yout.toString());
  88. */
  89. assert(xout.equals(c[0]));
  90. assert(yout.equals(c[1]));
  91. console.log("-------")
  92. const w2 = circuit.calculateWitness({"in": (1n<<252n)+1n});
  93. const xout2 = w2[circuit.getSignalIdx(`main.out[0]`)];
  94. const yout2 = w2[circuit.getSignalIdx(`main.out[1]`)];
  95. c = [g[0], g[1]];
  96. for (let i=0; i<252;i++) {
  97. c = addPoint(c,c);
  98. }
  99. c = addPoint(c,g);
  100. // console.log(xout2.toString());
  101. // console.log(yout2.toString());
  102. // console.log(c[0].toString());
  103. // console.log(c[1].toString());
  104. assert(xout2.equals(c[0]));
  105. assert(yout2.equals(c[1]));
  106. }).timeout(10000000);
  107. it("Number of constrains for 256 bits", async () => {
  108. const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
  109. const circuit = new snarkjs.Circuit(cirDef);
  110. console.log("NConstrains: " + circuit.nConstraints);
  111. }).timeout(10000000);
  112. });