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.

91 lines
2.9 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const bigInt = require("big-integer");
  4. const tester = require("circom").tester;
  5. const babyJub = require("../src/babyjub.js");
  6. const assert = chai.assert;
  7. describe("Montgomery test", function () {
  8. let circuitE2M;
  9. let circuitM2E;
  10. let circuitMAdd;
  11. let circuitMDouble;
  12. let g = [
  13. bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  14. bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
  15. ];
  16. let mg, mg2, g2, g3, mg3;
  17. this.timeout(100000);
  18. before( async() => {
  19. circuitE2M = await tester(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
  20. await circuitE2M.loadSymbols();
  21. circuitM2E = await tester(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
  22. await circuitM2E.loadSymbols();
  23. circuitMAdd = await tester(path.join(__dirname, "circuits", "montgomeryadd.circom"));
  24. await circuitMAdd.loadSymbols();
  25. circuitMDouble = await tester(path.join(__dirname, "circuits", "montgomerydouble.circom"));
  26. await circuitMDouble.loadSymbols();
  27. });
  28. it("Convert Edwards to Montgomery and back again", async () => {
  29. let w, xout, yout;
  30. w = await circuitE2M.calculateWitness({ in: g});
  31. xout = w[circuitE2M.symbols["main.out[0]"].idxWit];
  32. yout = w[circuitE2M.symbols["main.out[1]"].idxWit];
  33. mg = [xout, yout];
  34. w = await circuitM2E.calculateWitness({ in: [xout, yout]});
  35. xout = w[circuitM2E.symbols["main.out[0]"].idxWit];
  36. yout = w[circuitM2E.symbols["main.out[1]"].idxWit];
  37. assert(xout.equals(g[0]));
  38. assert(yout.equals(g[1]));
  39. });
  40. it("Should double a point", async () => {
  41. let w, xout, yout;
  42. g2 = babyJub.addPoint(g,g);
  43. w = await circuitMDouble.calculateWitness({ in: mg});
  44. xout = w[circuitE2M.symbols["main.out[0]"].idxWit];
  45. yout = w[circuitE2M.symbols["main.out[1]"].idxWit];
  46. mg2 = [xout, yout];
  47. w = await circuitM2E.calculateWitness({ in: mg2});
  48. xout = w[circuitM2E.symbols["main.out[0]"].idxWit];
  49. yout = w[circuitM2E.symbols["main.out[1]"].idxWit];
  50. assert(xout.equals(g2[0]));
  51. assert(yout.equals(g2[1]));
  52. });
  53. it("Should add a point", async () => {
  54. let w, xout, yout;
  55. g3 = babyJub.addPoint(g,g2);
  56. w = await circuitMAdd.calculateWitness({ in1: mg, in2: mg2});
  57. xout = w[circuitMAdd.symbols["main.out[0]"].idxWit];
  58. yout = w[circuitMAdd.symbols["main.out[1]"].idxWit];
  59. mg3 = [xout, yout];
  60. w = await circuitM2E.calculateWitness({ in: mg3});
  61. xout = w[circuitM2E.symbols["main.out[0]"].idxWit];
  62. yout = w[circuitM2E.symbols["main.out[1]"].idxWit];
  63. assert(xout.equals(g3[0]));
  64. assert(yout.equals(g3[1]));
  65. });
  66. });