Browse Source

add MiMC7 & Poseidon test vectors, add Poseidon multiHash & hashBuffer, add MiMC7 hashBuffer

feature/pr-19
arnaucube 4 years ago
parent
commit
e9b5da742a
4 changed files with 132 additions and 0 deletions
  1. +18
    -0
      src/mimc7.js
  2. +41
    -0
      src/poseidon.js
  3. +32
    -0
      test/mimc7.js
  4. +41
    -0
      test/poseidon.js

+ 18
- 0
src/mimc7.js

@ -62,3 +62,21 @@ exports.multiHash = (arr, key) => {
}
return F.affine(r);
};
// hashBuffer performs the MiMC7 hash over a buffer array, splitting the bytes into 31 bytes bigints,
// and making chunks of two bigints to perform the MiMC7 hash
exports.hashBuffer = (msgBuff) => {
const n = 31;
const msgArray = [];
const fullParts = Math.floor(msgBuff.length / n);
for (let i = 0; i < fullParts; i++) {
const v = bigInt.leBuff2int(msgBuff.slice(n * i, n * (i + 1)));
msgArray.push(v);
}
if (msgBuff.length % n !== 0) {
const v = bigInt.leBuff2int(msgBuff.slice(fullParts * n));
msgArray.push(v);
}
return exports.multiHash(msgArray);
};

+ 41
- 0
src/poseidon.js

@ -113,4 +113,45 @@ exports.createHash = (t, nRoundsF, nRoundsP, seed) => {
};
};
// hash exposes the generic Poseidon hash function
// with nRoundsF:8, nRoundsP: 57, t: 6
exports.hash = (arr) => {
const poseidonHash = exports.createHash(6, 8, 57);
return poseidonHash(arr);
}
// multiHash splits the bigint array into chunks of five elements
// and performs the Poseidon hash over the five elements chunks
exports.multiHash = (arr) => {
let r = bigInt(0);
for (let i=0; i<arr.length; i = i+5) {
let fiveElems = [];
for (let j=0; j<5; j++) {
if (i+j < arr.length) {
fiveElems.push(arr[i+j]);
} else {
fiveElems.push(bigInt(0));
}
}
const ph = exports.hash(fiveElems);
r = F.add(r, ph);
}
return F.affine(r);
};
// hashBuffer performs the Poseidon hash over a buffer array, splitting the bytes into 31 bytes bigints,
// and making chunks of five bigints to perform the Poseidon hash
exports.hashBuffer = (msgBuff) => {
const n = 31;
const msgArray = [];
const fullParts = Math.floor(msgBuff.length / n);
for (let i = 0; i < fullParts; i++) {
const v = bigInt.leBuff2int(msgBuff.slice(n * i, n * (i + 1)));
msgArray.push(v);
}
if (msgBuff.length % n !== 0) {
const v = bigInt.leBuff2int(msgBuff.slice(fullParts * n));
msgArray.push(v);
}
return exports.multiHash(msgArray);
};

+ 32
- 0
test/mimc7.js

@ -0,0 +1,32 @@
const chai = require("chai");
const assert = chai.assert;
const bigInt = require("snarkjs").bigInt;
const mimc7 = require("../src/mimc7.js");
describe('mimc7 primitives', () => {
it('hash two bigInt', () => {
const h = mimc7.hash(bigInt(12), bigInt(45));
assert.equal(h.toString(), '19746142529723647765530752502670948774458299263315590587358840390982005703908');
});
it('hash bigInt array (multiHash)', () => {
const h1 = mimc7.multiHash([bigInt(12)]);
assert.equal(h1.toString(), '16051049095595290701999129793867590386356047218708919933694064829788708231421');
const h2 = mimc7.multiHash([bigInt(78), bigInt(41)]);
assert.equal(h2.toString(), '2938611815373543102852102540059918590261345652613741345181300284995514063984');
const h4 = mimc7.multiHash([bigInt(12), bigInt(45)]);
assert.equal(h4.toString(), '9949998637984578981906561631883120271399801229641312099559043216173958006905');
const h5 = mimc7.multiHash([bigInt(12), bigInt(45), bigInt(78), bigInt(41)]);
assert.equal(h5.toString(), '18226366069841799622585958305961373004333097209608110160936134895615261821931');
});
it('mimc7 hash buffer', () => {
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.';
const msgBuff = Buffer.from(msg, 'utf-8');
let h = mimc7.hashBuffer(msgBuff);
assert.equal(h.toString(), '16855787120419064316734350414336285711017110414939748784029922801367685456065');
});
});

+ 41
- 0
test/poseidon.js

@ -0,0 +1,41 @@
const chai = require("chai");
const assert = chai.assert;
const bigInt = require("snarkjs").bigInt;
const poseidon = require("../src/poseidon.js");
describe('poseidon primitives', () => {
it('poseidon two bigInt', () => {
const poseidonHash = poseidon.createHash();
const h1 = poseidonHash([bigInt(1), bigInt(2)]);
assert.equal(h1.toString(), '12242166908188651009877250812424843524687801523336557272219921456462821518061');
const h2 = poseidonHash([bigInt(3), bigInt(4)]);
assert.equal(h2.toString(), '17185195740979599334254027721507328033796809509313949281114643312710535000993');
});
it('poseidon bigInt array (multiHash)', () => {
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.';
const msgBuff = Buffer.from(msg, 'utf-8');
const n = 31;
const msgArray = [];
const fullParts = Math.floor(msgBuff.length / n);
for (let i = 0; i < fullParts; i++) {
const v = bigInt.leBuff2int(msgBuff.slice(n * i, n * (i + 1)));
msgArray.push(v);
}
if (msgBuff.length % n !== 0) {
const v = bigInt.leBuff2int(msgBuff.slice(fullParts * n));
msgArray.push(v);
}
let h = poseidon.multiHash(msgArray);
assert.equal(h.toString(), '11821124228916291136371255062457365369197326845706357273715164664419275913793');
});
it('poseidon hash buffer', () => {
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.';
const msgBuff = Buffer.from(msg, 'utf-8');
let h = poseidon.hashBuffer(msgBuff);
assert.equal(h.toString(), '11821124228916291136371255062457365369197326845706357273715164664419275913793');
});
});

Loading…
Cancel
Save