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.

100 lines
3.5 KiB

  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 babyJub = require("../src/babyjub.js");
  8. const PBASE =
  9. [
  10. [bigInt("10457101036533406547632367118273992217979173478358440826365724437999023779287"),bigInt("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
  11. [bigInt("2671756056509184035029146175565761955751135805354291559563293617232983272177"),bigInt("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
  12. [bigInt("5802099305472655231388284418920769829666717045250560929368476121199858275951"),bigInt("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
  13. [bigInt("7107336197374528537877327281242680114152313102022415488494307685842428166594"),bigInt("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
  14. [bigInt("20265828622013100949498132415626198973119240347465898028410217039057588424236"),bigInt("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
  15. ];
  16. describe("Double Pedersen test", function() {
  17. let circuit;
  18. this.timeout(100000);
  19. before( async() => {
  20. const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen_test.circom"));
  21. circuit = new snarkjs.Circuit(cirDef);
  22. console.log("NConstrains: " + circuit.nConstraints);
  23. });
  24. it("Should pedersen at zero", async () => {
  25. let w, xout, yout;
  26. w = circuit.calculateWitness({ in: ["0", "0"]});
  27. xout = w[circuit.getSignalIdx("main.out[0]")];
  28. yout = w[circuit.getSignalIdx("main.out[1]")];
  29. assert(xout.equals("0"));
  30. assert(yout.equals("1"));
  31. });
  32. it("Should pedersen at one first generator", async () => {
  33. let w, xout, yout;
  34. w = circuit.calculateWitness({ in: ["1", "0"]});
  35. xout = bigInt(w[circuit.getSignalIdx("main.out[0]")]);
  36. yout = bigInt(w[circuit.getSignalIdx("main.out[1]")]);
  37. assert(xout.equals(PBASE[0][0]));
  38. assert(yout.equals(PBASE[0][1]));
  39. });
  40. it("Should pedersen at one second generator", async () => {
  41. let w, xout, yout;
  42. w = circuit.calculateWitness({ in: ["0", "1"]});
  43. xout = w[circuit.getSignalIdx("main.out[0]")];
  44. yout = w[circuit.getSignalIdx("main.out[1]")];
  45. assert(xout.equals(PBASE[1][0]));
  46. assert(yout.equals(PBASE[1][1]));
  47. });
  48. it("Should pedersen at mixed generators", async () => {
  49. let w, xout, yout;
  50. w = circuit.calculateWitness({ in: ["3", "7"]});
  51. xout = w[circuit.getSignalIdx("main.out[0]")];
  52. yout = w[circuit.getSignalIdx("main.out[1]")];
  53. const r = babyJub.addPoint(
  54. babyJub.mulPointEscalar(PBASE[0], 3),
  55. babyJub.mulPointEscalar(PBASE[1], 7)
  56. );
  57. assert(xout.equals(r[0]));
  58. assert(yout.equals(r[1]));
  59. });
  60. it("Should pedersen all ones", async () => {
  61. let w, xout, yout;
  62. const allOnes = bigInt("1").shl(250).sub(bigInt("1"));
  63. w = circuit.calculateWitness({ in: [allOnes, allOnes]});
  64. xout = w[circuit.getSignalIdx("main.out[0]")];
  65. yout = w[circuit.getSignalIdx("main.out[1]")];
  66. const r2 = babyJub.addPoint(
  67. babyJub.mulPointEscalar(PBASE[0], allOnes),
  68. babyJub.mulPointEscalar(PBASE[1], allOnes)
  69. );
  70. assert(xout.equals(r2[0]));
  71. assert(yout.equals(r2[1]));
  72. });
  73. });