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.

74 lines
1.9 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 pedersen = require("../src/pedersenHash.js");
  9. describe("Pedersen test", function() {
  10. let circuit;
  11. this.timeout(100000);
  12. before( async() => {
  13. const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen2_test.circom"));
  14. circuit = new snarkjs.Circuit(cirDef);
  15. console.log("NConstrains Pedersen2: " + circuit.nConstraints);
  16. });
  17. it("Should pedersen at zero", async () => {
  18. let w, xout, yout;
  19. w = circuit.calculateWitness({ in: 0});
  20. xout = w[circuit.getSignalIdx("main.out[0]")];
  21. yout = w[circuit.getSignalIdx("main.out[1]")];
  22. const b = Buffer.alloc(32);
  23. const h = pedersen.hash(b);
  24. const hP = babyJub.unpackPoint(h);
  25. /*
  26. console.log(`[${xout.toString()}, ${yout.toString()}]`);
  27. console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
  28. */
  29. assert(xout.equals(hP[0]));
  30. assert(yout.equals(hP[1]));
  31. });
  32. it("Should pedersen with 253 ones", async () => {
  33. let w, xout, yout;
  34. const n = bigInt.one.shl(253).sub(bigInt.one);
  35. console.log(n.toString(16));
  36. w = circuit.calculateWitness({ in: n});
  37. xout = w[circuit.getSignalIdx("main.out[0]")];
  38. yout = w[circuit.getSignalIdx("main.out[1]")];
  39. const b = Buffer.alloc(32);
  40. for (let i=0; i<31; i++) b[i] = 0xFF;
  41. b[31] = 0x1F;
  42. const h = pedersen.hash(b);
  43. const hP = babyJub.unpackPoint(h);
  44. /*
  45. console.log(`[${xout.toString()}, ${yout.toString()}]`);
  46. console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
  47. */
  48. assert(xout.equals(hP[0]));
  49. assert(yout.equals(hP[1]));
  50. });
  51. });