UNPKG

snarkjs

Version:

zkSNARKs implementation in JavaScript

91 lines (67 loc) 3.25 kB
/* 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 <https://www.gnu.org/licenses/>. */ import * as utils from "./powersoftau_utils.js"; import * as binFileUtils from "@iden3/binfileutils"; import { stringifyBigIntsWithField } from "./misc.js"; export default async function exportJson(pTauFilename, verbose) { const {fd, sections} = await binFileUtils.readBinFile(pTauFilename, "ptau", 1); const {curve, power} = await utils.readPTauHeader(fd, sections); const pTau = {}; pTau.q = curve.q; pTau.power = power; pTau.contributions = await utils.readContributions(fd, curve, sections); pTau.tauG1 = await exportSection(2, "G1", (2 ** power)*2 -1, "tauG1"); pTau.tauG2 = await exportSection(3, "G2", (2 ** power), "tauG2"); pTau.alphaTauG1 = await exportSection(4, "G1", (2 ** power), "alphaTauG1"); pTau.betaTauG1 = await exportSection(5, "G1", (2 ** power), "betaTauG1"); pTau.betaG2 = await exportSection(6, "G2", 1, "betaG2"); pTau.lTauG1 = await exportLagrange(12, "G1", "lTauG1"); pTau.lTauG2 = await exportLagrange(13, "G2", "lTauG2"); pTau.lAlphaTauG1 = await exportLagrange(14, "G1", "lAlphaTauG2"); pTau.lBetaTauG1 = await exportLagrange(15, "G1", "lBetaTauG2"); await fd.close(); return stringifyBigIntsWithField(curve.Fr, pTau); async function exportSection(sectionId, groupName, nPoints, sectionName) { const G = curve[groupName]; const sG = G.F.n8*2; const res = []; await binFileUtils.startReadUniqueSection(fd, sections, sectionId); for (let i=0; i< nPoints; i++) { if ((verbose)&&i&&(i%10000 == 0)) console.log(`${sectionName}: ` + i); const buff = await fd.read(sG); res.push(G.fromRprLEM(buff, 0)); } await binFileUtils.endReadSection(fd); return res; } async function exportLagrange(sectionId, groupName, sectionName) { const G = curve[groupName]; const sG = G.F.n8*2; const res = []; await binFileUtils.startReadUniqueSection(fd, sections, sectionId); for (let p=0; p<=power; p++) { if (verbose) console.log(`${sectionName}: Power: ${p}`); res[p] = []; const nPoints = (2 ** p); for (let i=0; i<nPoints; i++) { if ((verbose)&&i&&(i%10000 == 0)) console.log(`${sectionName}: ${i}/${nPoints}`); const buff = await fd.read(sG); res[p].push(G.fromRprLEM(buff, 0)); } } await binFileUtils.endReadSection(fd, true); return res; } }