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.

101 lines
3.2 KiB

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