circomlibjs/src/mimc7.js

80 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-10-30 13:57:29 +02:00
import {getCurveFromName, Scalar} from "ffjavascript";
2021-10-06 11:28:14 +02:00
2021-10-30 13:57:29 +02:00
import ethers from "ethers";
2021-10-06 11:28:14 +02:00
const SEED = "mimc";
const NROUNDS = 91;
2021-10-30 13:57:29 +02:00
export default async function buildMimc7() {
const bn128 = await getCurveFromName("bn128");
return new Mimc7(bn128.Fr);
}
2021-10-06 11:28:14 +02:00
2021-10-30 13:57:29 +02:00
class Mimc7 {
constructor (F) {
this.F = F;
this.cts = this.getConstants(SEED, 91);
2021-10-06 11:28:14 +02:00
}
2021-10-30 13:57:29 +02:00
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<nRounds; i++) {
c = ethers.utils.keccak256(c);
2021-10-06 11:28:14 +02:00
2021-10-30 13:57:29 +02:00
cts[i] = F.e(c);
}
cts[0] = F.e(0);
return cts;
2021-10-06 11:28:14 +02:00
}
2021-10-30 13:57:29 +02:00
hash (_x_in, _k) {
const F = this.F;
const x_in = F.e(_x_in);
const k = F.e(_k);
let r;
for (let i=0; i<NROUNDS; i++) {
const c = this.cts[i];
const t = (i==0) ? F.add(x_in, k) : F.add(F.add(r, k), c);
const t2 = F.square(t);
const t4 = F.square(t2);
r = F.mul(F.mul(t4, t2), t);
}
return F.add(r, k);
2021-10-06 11:28:14 +02:00
}
2021-10-30 13:57:29 +02:00
multiHash(arr, key) {
const F = this.F;
let r;
if (typeof(key) === "undefined") {
r = F.zero;
} else {
r = key;
}
for (let i=0; i<arr.length; i++) {
r = F.add(
F.add(
r,
arr[i]
),
this.hash(F.e(arr[i]), r)
);
}
return r;
2021-10-06 11:28:14 +02:00
}
2021-10-30 13:57:29 +02:00
}