import { Scalar, getCurveFromName } from "ffjavascript"; import ethers from "ethers"; const SEED = "mimcsponge"; const NROUNDS = 220; export default async function buildMimcSponge() { const bn128 = await getCurveFromName("bn128"); return new MimcSponge(bn128.Fr); } class MimcSponge { constructor (F) { this.F = F; this.cts = this.getConstants(SEED, NROUNDS); } 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 = cn.mod(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