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("0x237c92644dbddb86d8a259e0e923aaab65a93f1ec5758b8799988894ac0958fd");
  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("0x067f3202335ea256ae6e6aadcd2d5f7f4b06a00b2d1e0de903980d5ab552dc70");
  34. const hv = mimcjs.multiHash(entries.slice(0, 2));
  35. const hvHex = bytesToHex(bigIntToBuffer(hv));
  36. expect(hvHex).to.be.equal("0x15ff7fe9793346a17c3150804bcb36d161c8662b110c50f55ccb7113948d8879");
  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("0x284bc1f34f335933a23a433b6ff3ee179d682cd5e5e2fcdd2d964afa85104beb");
  43. });
  44. });