From 8742ec0a38640e9813dd820613bcfee8e054f12c Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Tue, 2 Jun 2020 23:13:20 +0200 Subject: [PATCH] preparephase2 with all intermediate values --- src/misc.js | 34 +++++++ src/powersoftau_preparephase2.js | 134 +++++++++++++++++++++++---- src/powersoftau_preparephase2_old.js | 48 ++++++++++ src/powersoftau_verify.js | 81 +++++++++------- 4 files changed, 247 insertions(+), 50 deletions(-) create mode 100644 src/misc.js create mode 100644 src/powersoftau_preparephase2_old.js diff --git a/src/misc.js b/src/misc.js new file mode 100644 index 0000000..24cde6c --- /dev/null +++ b/src/misc.js @@ -0,0 +1,34 @@ + +const _revTable = []; +for (let i=0; i<256; i++) { + _revTable[i] = _revSlow(i, 8); +} + +function _revSlow(idx, bits) { + let res =0; + let a = idx; + for (let i=0; i>=1; + } + return res; +} + +function bitReverse(idx, bits) { + return ( + _revTable[idx >>> 24] | + (_revTable[(idx >>> 16) & 0xFF] << 8) | + (_revTable[(idx >>> 8) & 0xFF] << 16) | + (_revTable[idx & 0xFF] << 24) + ) >>> (32-bits); +} + + +function log2( V ) +{ + return( ( ( V & 0xFFFF0000 ) !== 0 ? ( V &= 0xFFFF0000, 16 ) : 0 ) | ( ( V & 0xFF00FF00 ) !== 0 ? ( V &= 0xFF00FF00, 8 ) : 0 ) | ( ( V & 0xF0F0F0F0 ) !== 0 ? ( V &= 0xF0F0F0F0, 4 ) : 0 ) | ( ( V & 0xCCCCCCCC ) !== 0 ? ( V &= 0xCCCCCCCC, 2 ) : 0 ) | ( ( V & 0xAAAAAAAA ) !== 0 ) ); +} + +module.exports.bitReverse = bitReverse; +module.exports.log2 = log2; diff --git a/src/powersoftau_preparephase2.js b/src/powersoftau_preparephase2.js index de743ac..f07ffbb 100644 --- a/src/powersoftau_preparephase2.js +++ b/src/powersoftau_preparephase2.js @@ -1,5 +1,8 @@ const binFileUtils = require("./binfileutils"); const utils = require("./powersoftau_utils"); +const fastFile = require("fastfile"); +const {bitReverse} = require("./misc"); +const fs = require("fs"); async function preparePhase2(oldPtauFilename, newPTauFilename, verbose) { @@ -9,6 +12,8 @@ async function preparePhase2(oldPtauFilename, newPTauFilename, verbose) { const fdNew = await binFileUtils.createBinFile(newPTauFilename, "ptau", 1, 11); await utils.writePTauHeader(fdNew, curve, power); + const fdTmp = await fastFile.createOverride(newPTauFilename+ ".tmp"); + await binFileUtils.copySection(fdOld, sections, fdNew, 2); await binFileUtils.copySection(fdOld, sections, fdNew, 3); await binFileUtils.copySection(fdOld, sections, fdNew, 4); @@ -16,32 +21,129 @@ async function preparePhase2(oldPtauFilename, newPTauFilename, verbose) { await binFileUtils.copySection(fdOld, sections, fdNew, 6); await binFileUtils.copySection(fdOld, sections, fdNew, 7); - await processSection(2, 12, "G1", (1< CHUNKPOW ? CHUNKPOW : p; + const pointsPerChunk = 1< CHUNKPOW ? CHUNKPOW : p; + const pointsPerChunk = 1<=0; i--) { const curContr = contrs[i]; const prevContr = (i>0) ? contrs[i-1] : initialContribution; - await verifyContribution(curve, curContr, prevContr); + const res = await verifyContribution(curve, curContr, prevContr); + if (!res) return false; printContribution(curContr, prevContr); } console.log("-----------------------------------------------------"); @@ -262,13 +262,13 @@ async function verify(tauFilename, verbose) { console.log(" snarkjs \"powersoftau preparephase2\" to prepare this file to be used in the phase2 ceremony." ); } else { let res; - res = await verifyLagrangeEvaluations("G1", 1 << power, 2, 12, "tauG1"); + res = await verifyLagrangeEvaluations("G1", 2, 12, "tauG1"); if (!res) return false; - res = await verifyLagrangeEvaluations("G2", 1 << power, 3, 13, "tauG2"); + res = await verifyLagrangeEvaluations("G2", 3, 13, "tauG2"); if (!res) return false; - res = await verifyLagrangeEvaluations("G1", 1 << power, 4, 14, "alphaTauG1"); + res = await verifyLagrangeEvaluations("G1", 4, 14, "alphaTauG1"); if (!res) return false; - res = await verifyLagrangeEvaluations("G1", 1 << power, 5, 15, "betaTauG1"); + res = await verifyLagrangeEvaluations("G1", 5, 15, "betaTauG1"); if (!res) return false; } @@ -375,13 +375,9 @@ async function verify(tauFilename, verbose) { } - async function verifyLagrangeEvaluations(gName, nPoints, tauSection, lagrangeSection, sectionName) { + async function verifyLagrangeEvaluations(gName, tauSection, lagrangeSection, sectionName) { if (verbose) console.log(`Verifying phase2 calculated values ${sectionName}...`); - - const n8r = curve.Fr.n8; - let buff_r = new Uint8Array(nPoints * n8r); - let buffG; const G = curve[gName]; const sG = G.F.n8*2; @@ -392,33 +388,50 @@ async function verify(tauFilename, verbose) { const rng = new ChaCha(seed); - for (let i=0; i