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.

121 lines
3.4 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const wasm_tester = require("circom_tester").wasm;
  4. const buildBabyjub = require("circomlibjs").buildBabyjub;
  5. const Scalar = require("ffjavascript").Scalar;
  6. const assert = chai.assert;
  7. function print(circuit, w, s) {
  8. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  9. }
  10. describe("Exponentioation test", function () {
  11. let babyJub;
  12. let Fr;
  13. this.timeout(100000);
  14. before( async () => {
  15. babyJub = await buildBabyjub();
  16. Fr = babyJub.F;
  17. });
  18. it("Should generate the Exponentiation table in k=0", async () => {
  19. const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
  20. const w = await circuit.calculateWitness({in: 1});
  21. await circuit.checkConstraints(w);
  22. let g = [
  23. Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  24. Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
  25. ];
  26. let dbl= [Fr.e("0"), Fr.e("1")];
  27. const expectedOut = [];
  28. for (let i=0; i<16; i++) {
  29. expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
  30. dbl = babyJub.addPoint(dbl,g);
  31. }
  32. await circuit.assertOut(w, {out: expectedOut});
  33. });
  34. it("Should generate the Exponentiation table in k=3", async () => {
  35. const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
  36. const w = await circuit.calculateWitness({in: 1});
  37. await circuit.checkConstraints(w);
  38. let g = [
  39. Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  40. Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
  41. ];
  42. for (let i=0; i<12;i++) {
  43. g = babyJub.addPoint(g,g);
  44. }
  45. let dbl= [Fr.e("0"), Fr.e("1")];
  46. const expectedOut = [];
  47. for (let i=0; i<16; i++) {
  48. expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
  49. dbl = babyJub.addPoint(dbl,g);
  50. }
  51. await circuit.assertOut(w, {out: expectedOut});
  52. });
  53. it("Should exponentiate g^31", async () => {
  54. const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test.circom"));
  55. const w = await circuit.calculateWitness({"in": 31});
  56. await circuit.checkConstraints(w);
  57. let g = [
  58. Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  59. Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
  60. ];
  61. let c = [Fr.e(0), Fr.e(1)];
  62. for (let i=0; i<31;i++) {
  63. c = babyJub.addPoint(c,g);
  64. }
  65. await circuit.assertOut(w, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
  66. const w2 = await circuit.calculateWitness({"in": Scalar.add(Scalar.shl(Scalar.e(1), 252),Scalar.e(1))});
  67. c = [g[0], g[1]];
  68. for (let i=0; i<252;i++) {
  69. c = babyJub.addPoint(c,c);
  70. }
  71. c = babyJub.addPoint(c,g);
  72. await circuit.assertOut(w2, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
  73. }).timeout(10000000);
  74. it("Number of constrains for 256 bits", async () => {
  75. const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
  76. }).timeout(10000000);
  77. });