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.

50 lines
1.8 KiB

  1. const chai = require("chai");
  2. const snarkjs = require("snarkjs");
  3. const mimcjs = require("../src/mimc7.js");
  4. const expect = chai.expect;
  5. const bigInt = snarkjs.bigInt;
  6. function bytesToHex(buff) {
  7. return `0x${buff.toString("hex")}`;
  8. }
  9. function bigIntToBuffer(number) {
  10. const buff = Buffer.alloc(32);
  11. let pos = buff.length - 1;
  12. while (!number.isZero()) {
  13. buff[pos] = number.and(255);
  14. number = number.shiftRight(8);
  15. pos -= 1;
  16. }
  17. return buff;
  18. }
  19. describe("[mimc hash] Javascript test", function () {
  20. this.timeout(100000);
  21. before( async () => {
  22. });
  23. it("Mimc7 hash with 1 entry", () => {
  24. const entries = [bigInt(12)];
  25. const hash = mimcjs.multiHash(entries);
  26. const hashHex = bytesToHex(bigIntToBuffer(hash));
  27. expect(hashHex).to.be.equal("0x04252a243a23848a29fa5783336905f6394585fb38d0d89f16d36084c53cb73c");
  28. });
  29. it("Mimc7 hash with 2 entries", () => {
  30. const entries = [bigInt(12), bigInt(45), bigInt(78), bigInt(41)];
  31. const hi = mimcjs.multiHash(entries.slice(2));
  32. const hiHex = bytesToHex(bigIntToBuffer(hi));
  33. expect(hiHex).to.be.equal("0x1fd4bc970a697084ec1f83ecf81936d4a047e27c654752ddbc89f9ed1728e0ab");
  34. const hv = mimcjs.multiHash(entries.slice(0, 2));
  35. const hvHex = bytesToHex(bigIntToBuffer(hv));
  36. expect(hvHex).to.be.equal("0x263924eb9ae730cea9ce31bb9ada695ec3525536b4c058813552b074db36ba9a");
  37. });
  38. it("Mimc7 hash with 4 entries", () => {
  39. const entries = [bigInt(12), bigInt(45), bigInt(78), bigInt(41)];
  40. const hash = mimcjs.multiHash(entries);
  41. const hashHex = bytesToHex(bigIntToBuffer(hash));
  42. expect(hashHex).to.be.equal("0x10e02cc6c8fc40cda121602903df911f6398d65f84ff1f27c680d0b7d85b7418");
  43. });
  44. });