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.

112 lines
3.3 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const babyjub = require("../src/babyjub");
  6. const assert = chai.assert;
  7. const bigInt = snarkjs.bigInt;
  8. function print(circuit, w, s) {
  9. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  10. }
  11. describe("Escalarmul test", function () {
  12. let circuit;
  13. this.timeout(100000);
  14. before( async() => {
  15. const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
  16. circuit = new snarkjs.Circuit(cirDef);
  17. console.log("NConstrains Escalarmul fix: " + circuit.nConstraints);
  18. });
  19. it("Should generate Same escalar mul", async () => {
  20. const w = circuit.calculateWitness({"e": 0});
  21. assert(circuit.checkWitness(w));
  22. const xout = w[circuit.getSignalIdx("main.out[0]")];
  23. const yout = w[circuit.getSignalIdx("main.out[1]")];
  24. assert(xout.equals(0));
  25. assert(yout.equals(1));
  26. });
  27. it("Should generate Same escalar mul", async () => {
  28. const w = circuit.calculateWitness({"e": 1});
  29. assert(circuit.checkWitness(w));
  30. const xout = w[circuit.getSignalIdx("main.out[0]")];
  31. const yout = w[circuit.getSignalIdx("main.out[1]")];
  32. assert(xout.equals(babyjub.Base8[0]));
  33. assert(yout.equals(babyjub.Base8[1]));
  34. });
  35. it("Should generate scalar mul of a specific constant", async () => {
  36. const s = bigInt("2351960337287830298912035165133676222414898052661454064215017316447594616519");
  37. const base8 = [
  38. bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  39. bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
  40. ];
  41. const w = circuit.calculateWitness({"e": s});
  42. assert(circuit.checkWitness(w));
  43. const xout = w[circuit.getSignalIdx("main.out[0]")];
  44. const yout = w[circuit.getSignalIdx("main.out[1]")];
  45. const expectedRes = babyjub.mulPointEscalar(base8, s);
  46. assert(xout.equals(expectedRes[0]));
  47. assert(yout.equals(expectedRes[1]));
  48. });
  49. it("Should generate scalar mul of the firsts 50 elements", async () => {
  50. const base8 = [
  51. bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  52. bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
  53. ];
  54. for (let i=0; i<50; i++) {
  55. const s = bigInt(i);
  56. const w = circuit.calculateWitness({"e": s});
  57. assert(circuit.checkWitness(w));
  58. const xout = w[circuit.getSignalIdx("main.out[0]")];
  59. const yout = w[circuit.getSignalIdx("main.out[1]")];
  60. const expectedRes = babyjub.mulPointEscalar(base8, s);
  61. assert(xout.equals(expectedRes[0]));
  62. assert(yout.equals(expectedRes[1]));
  63. }
  64. });
  65. it("If multiply by order should return 0", async () => {
  66. const w = circuit.calculateWitness({"e": babyjub.subOrder });
  67. assert(circuit.checkWitness(w));
  68. const xout = w[circuit.getSignalIdx("main.out[0]")];
  69. const yout = w[circuit.getSignalIdx("main.out[1]")];
  70. assert(xout.equals(bigInt.zero));
  71. assert(yout.equals(bigInt.one));
  72. });
  73. });