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.

92 lines
2.9 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const tester = require("circom").tester;
  4. const babyJub = require("../src/babyjub.js");
  5. const Fr = require("ffjavascript").bn128.Fr;
  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. Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  14. Fr.e("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}, true);
  31. xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
  32. yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
  33. mg = [xout, yout];
  34. w = await circuitM2E.calculateWitness({ in: [xout, yout]}, true);
  35. xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
  36. yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
  37. assert(Fr.eq(xout, g[0]));
  38. assert(Fr.eq(yout, 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}, true);
  44. xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
  45. yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
  46. mg2 = [xout, yout];
  47. w = await circuitM2E.calculateWitness({ in: mg2}, true);
  48. xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
  49. yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
  50. assert(Fr.eq(xout, g2[0]));
  51. assert(Fr.eq(yout, 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}, true);
  57. xout = w[circuitMAdd.symbols["main.out[0]"].varIdx];
  58. yout = w[circuitMAdd.symbols["main.out[1]"].varIdx];
  59. mg3 = [xout, yout];
  60. w = await circuitM2E.calculateWitness({ in: mg3}, true);
  61. xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
  62. yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
  63. assert(Fr.eq(xout, g3[0]));
  64. assert(Fr.eq(yout, g3[1]));
  65. });
  66. });