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.

58 lines
1.7 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const mimcjs = require("../src/mimcsponge.js");
  6. const assert = chai.assert;
  7. describe("MiMC Sponge Circuit test", function () {
  8. let circuit;
  9. this.timeout(100000);
  10. it("Should check permutation", async () => {
  11. const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
  12. circuit = new snarkjs.Circuit(cirDef);
  13. console.log("MiMC Feistel constraints: " + circuit.nConstraints);
  14. const w = circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
  15. const xLout = w[circuit.getSignalIdx("main.xL_out")];
  16. const xRout = w[circuit.getSignalIdx("main.xR_out")];
  17. const out2 = mimcjs.hash(1,2,3);
  18. assert.equal(xLout.toString(), out2.xL.toString());
  19. assert.equal(xRout.toString(), out2.xR.toString());
  20. assert(circuit.checkWitness(w));
  21. });
  22. it("Should check hash", async () => {
  23. const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_hash_test.circom"));
  24. circuit = new snarkjs.Circuit(cirDef);
  25. console.log("MiMC Sponge constraints: " + circuit.nConstraints);
  26. const w = circuit.calculateWitness({ins: [1, 2], k: 0});
  27. const o1 = w[circuit.getSignalIdx("main.outs[0]")];
  28. const o2 = w[circuit.getSignalIdx("main.outs[1]")];
  29. const o3 = w[circuit.getSignalIdx("main.outs[2]")];
  30. const out2 = mimcjs.multiHash([1,2], 0, 3);
  31. assert.equal(o1.toString(), out2[0].toString());
  32. assert.equal(o2.toString(), out2[1].toString());
  33. assert.equal(o3.toString(), out2[2].toString());
  34. assert(circuit.checkWitness(w));
  35. });
  36. });