import buildBabyJub from "./babyjub.js"; import blake2b from "blake2b"; import createBlakeHash from "blake-hash"; import { Scalar } from "ffjavascript"; const GENPOINT_PREFIX = "PedersenGenerator"; const windowSize = 4; const nWindowsPerSegment = 50; export default async function buildPedersenHash() { const babyJub = await buildBabyJub(); return new PedersenHash(babyJub); } class PedersenHash { constructor(babyJub) { this.babyJub = babyJub; this.bases = []; } baseHash(type, S) { if (type == "blake") { return createBlakeHash("blake256").update(S).digest(); } else if (type == "blake2b") { return Buffer.from(blake2b(32).update(Buffer.from(S)).digest()); } } hash(msg, options) { options = options || {}; options.baseHash = options.baseHash || "blake"; const babyJub = this.babyJub; const bitsPerSegment = windowSize*nWindowsPerSegment; const bits = this.buffer2bits(msg); const nSegments = Math.floor((bits.length - 1)/(windowSize*nWindowsPerSegment)) +1; let accP = [babyJub.F.zero,babyJub.F.one]; for (let s=0; s> 1; res[i*8+2] = (b & 0x04) >> 2; res[i*8+3] = (b & 0x08) >> 3; res[i*8+4] = (b & 0x10) >> 4; res[i*8+5] = (b & 0x20) >> 5; res[i*8+6] = (b & 0x40) >> 6; res[i*8+7] = (b & 0x80) >> 7; } return res; } }