arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/src/mimc7.js
src/mimc7.jsRaw
 1const bn128 = require("snarkjs").bn128;
 2const bigInt = require("snarkjs").bigInt;
 3const Web3 = require("web3");
 4const F = bn128.Fr;
 5
 6const SEED = "iden3_mimc";
 7const nRounds = 91;
 8
 9
10exports.getConstants = (seed, nRounds) => {
11    const cts = new Array(nRounds);
12    let c = Web3.utils.keccak256(SEED);
13    for (let i=1; i<nRounds; i++) {
14        c = Web3.utils.keccak256(c);
15
16        const n1 = Web3.utils.toBN(c).mod(Web3.utils.toBN(F.q.toString()));
17        const c2 = Web3.utils.padLeft(Web3.utils.toHex(n1), 64);
18        cts[i] = bigInt(Web3.utils.toBN(c2).toString());
19    }
20    cts[0] = bigInt(0);
21    return cts;
22};
23
24const cts = exports.getConstants(SEED, 91);
25
26exports.hash =  (_x_in, _k) =>{
27    const x_in = bigInt(_x_in);
28    const k = bigInt(_k);
29    let r;
30    for (let i=0; i<nRounds; i++) {
31        const c = cts[i];
32        const t = (i==0) ? F.add(x_in, k) : F.add(F.add(r, k), c);
33        r = F.exp(t, 7);
34    }
35    return F.affine(F.add(r, k));
36};