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.

41 lines
2.4 KiB

  1. const chai = require("chai");
  2. const assert = chai.assert;
  3. const bigInt = require("snarkjs").bigInt;
  4. const poseidon = require("../src/poseidon.js");
  5. describe('poseidon primitives', () => {
  6. it('poseidon two bigInt', () => {
  7. const poseidonHash = poseidon.createHash();
  8. const h1 = poseidonHash([bigInt(1), bigInt(2)]);
  9. assert.equal(h1.toString(), '12242166908188651009877250812424843524687801523336557272219921456462821518061');
  10. const h2 = poseidonHash([bigInt(3), bigInt(4)]);
  11. assert.equal(h2.toString(), '17185195740979599334254027721507328033796809509313949281114643312710535000993');
  12. });
  13. it('poseidon bigInt array (multiHash)', () => {
  14. const msg = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
  15. const msgBuff = Buffer.from(msg, 'utf-8');
  16. const n = 31;
  17. const msgArray = [];
  18. const fullParts = Math.floor(msgBuff.length / n);
  19. for (let i = 0; i < fullParts; i++) {
  20. const v = bigInt.leBuff2int(msgBuff.slice(n * i, n * (i + 1)));
  21. msgArray.push(v);
  22. }
  23. if (msgBuff.length % n !== 0) {
  24. const v = bigInt.leBuff2int(msgBuff.slice(fullParts * n));
  25. msgArray.push(v);
  26. }
  27. let h = poseidon.multiHash(msgArray);
  28. assert.equal(h.toString(), '11821124228916291136371255062457365369197326845706357273715164664419275913793');
  29. });
  30. it('poseidon hash buffer', () => {
  31. const msg = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
  32. const msgBuff = Buffer.from(msg, 'utf-8');
  33. let h = poseidon.hashBuffer(msgBuff);
  34. assert.equal(h.toString(), '11821124228916291136371255062457365369197326845706357273715164664419275913793');
  35. });
  36. });