/* Copyright 2018 0KIMS association. This file is part of snarkJS. snarkJS is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. snarkJS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with snarkJS. If not, see . */ import * as binFileUtils from "@iden3/binfileutils"; import * as zkeyUtils from "./zkey_utils.js"; import { getCurveFromQ as getCurve } from "./curves.js"; import * as misc from "./misc.js"; import { blake2b } from "@noble/hashes/blake2b"; import * as utils from "./zkey_utils.js"; import { hashToG2 as hashToG2 } from "./keypair.js"; import { applyKeyToSection } from "./mpc_applykey.js"; export default async function beacon(zkeyNameOld, zkeyNameNew, name, beaconHashStr, numIterationsExp, logger) { const beaconHash = misc.hex2ByteArray(beaconHashStr); if ( (beaconHash.byteLength == 0) || (beaconHash.byteLength*2 !=beaconHashStr.length)) { if (logger) logger.error("Invalid Beacon Hash. (It must be a valid hexadecimal sequence)"); return false; } if (beaconHash.length>=256) { if (logger) logger.error("Maximum length of beacon hash is 255 bytes"); return false; } numIterationsExp = parseInt(numIterationsExp); if ((numIterationsExp<10)||(numIterationsExp>63)) { if (logger) logger.error("Invalid numIterationsExp. (Must be between 10 and 63)"); return false; } const {fd: fdOld, sections: sections} = await binFileUtils.readBinFile(zkeyNameOld, "zkey", 2); const zkey = await zkeyUtils.readHeader(fdOld, sections); if (zkey.protocol != "groth16") { throw new Error("zkey file is not groth16"); } const curve = await getCurve(zkey.q); const mpcParams = await zkeyUtils.readMPCParams(fdOld, curve, sections); const fdNew = await binFileUtils.createBinFile(zkeyNameNew, "zkey", 1, 10); const rng = await misc.rngFromBeaconParams(beaconHash, numIterationsExp); const transcriptHasher = blake2b.create({ dkLen: 64 });; transcriptHasher.update(mpcParams.csHash); for (let i=0; i