import {getCurveFromName, Scalar} from "ffjavascript"; import ethers from "ethers"; const SEED = "mimc"; const NROUNDS = 91; export default async function buildMimc7() { const bn128 = await getCurveFromName("bn128"); return new Mimc7(bn128.Fr); } class Mimc7 { constructor (F) { this.F = F; this.cts = this.getConstants(SEED, 91); } getIV(seed) { const F = this.F; if (typeof seed === "undefined") seed = SEED; const c = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(seed+"_iv")); const cn = Scalar.e(c); const iv = Scalar.mod(cn, F.p); return iv; }; getConstants(seed, nRounds) { const F = this.F; if (typeof seed === "undefined") seed = SEED; if (typeof nRounds === "undefined") nRounds = NROUNDS; const cts = new Array(nRounds); let c = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(SEED)); for (let i=1; i