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.

105 lines
3.6 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( "6842263847932328569390632736104801120816056295876316310227967232893658007436"),
  11. bigInt("10520112236148895828506510766039255961372323270202387671483666293012156799229")],
  12. [ bigInt( "7512553369533424708326990019377586455744651641787163924108944444109352325495"),
  13. bigInt( "242060483180498555826438209654403949979206323274480625257315595534333598496")],
  14. [ bigInt( "480315709862415282411588615813248553518720286084247594626493599605932342246"),
  15. bigInt("15016559215090999873142530067666085992648246670781771102893391410239675444873")],
  16. [ bigInt( "8311398801616893527636419786153024398643144699386228070202625261657263599049"),
  17. bigInt("11125962584728296601438821974884453267303385157860713577195820780853779600315")],
  18. [ bigInt( "1924118814882677827825936037840538695314492559747259292440881566152665343441"),
  19. bigInt("17232376423406964731689089286495480735310130852288107159412732879983310795144")]
  20. ];
  21. describe("Double Pedersen test", function() {
  22. let circuit;
  23. this.timeout(100000);
  24. before( async() => {
  25. const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen_test.circom"));
  26. circuit = new snarkjs.Circuit(cirDef);
  27. console.log("NConstrains: " + circuit.nConstraints);
  28. });
  29. it("Should pedersen at zero", async () => {
  30. let w, xout, yout;
  31. w = circuit.calculateWitness({ in: ["0", "0"]});
  32. xout = w[circuit.getSignalIdx("main.out[0]")];
  33. yout = w[circuit.getSignalIdx("main.out[1]")];
  34. assert(xout.equals("0"));
  35. assert(yout.equals("1"));
  36. });
  37. it("Should pedersen at one first generator", async () => {
  38. let w, xout, yout;
  39. w = circuit.calculateWitness({ in: ["1", "0"]});
  40. xout = bigInt(w[circuit.getSignalIdx("main.out[0]")]);
  41. yout = bigInt(w[circuit.getSignalIdx("main.out[1]")]);
  42. assert(xout.equals(PBASE[0][0]));
  43. assert(yout.equals(PBASE[0][1]));
  44. });
  45. it("Should pedersen at one second generator", async () => {
  46. let w, xout, yout;
  47. w = circuit.calculateWitness({ in: ["0", "1"]});
  48. xout = w[circuit.getSignalIdx("main.out[0]")];
  49. yout = w[circuit.getSignalIdx("main.out[1]")];
  50. assert(xout.equals(PBASE[1][0]));
  51. assert(yout.equals(PBASE[1][1]));
  52. });
  53. it("Should pedersen at mixed generators", async () => {
  54. let w, xout, yout;
  55. w = circuit.calculateWitness({ in: ["3", "7"]});
  56. xout = w[circuit.getSignalIdx("main.out[0]")];
  57. yout = w[circuit.getSignalIdx("main.out[1]")];
  58. const r = babyJub.addPoint(
  59. babyJub.mulPointEscalar(PBASE[0], 3),
  60. babyJub.mulPointEscalar(PBASE[1], 7)
  61. );
  62. assert(xout.equals(r[0]));
  63. assert(yout.equals(r[1]));
  64. });
  65. it("Should pedersen all ones", async () => {
  66. let w, xout, yout;
  67. const allOnes = bigInt("1").shl(251).sub(bigInt("1"));
  68. w = circuit.calculateWitness({ in: [allOnes, allOnes]});
  69. xout = w[circuit.getSignalIdx("main.out[0]")];
  70. yout = w[circuit.getSignalIdx("main.out[1]")];
  71. const r2 = babyJub.addPoint(
  72. babyJub.mulPointEscalar(PBASE[0], allOnes),
  73. babyJub.mulPointEscalar(PBASE[1], allOnes)
  74. );
  75. assert(xout.equals(r2[0]));
  76. assert(yout.equals(r2[1]));
  77. });
  78. });