Skip to content

Commit

Permalink
improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xavi-pinsach committed Aug 12, 2023
1 parent a288d71 commit dc709c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
49 changes: 18 additions & 31 deletions src/kzg_basic_prover.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const readPTauHeader = require("./ptau_utils");
module.exports = async function kzg_basic_prover(evalsBufferArray, pTauFilename, options) {
const logger = options.logger;

if (logger) {
logger.info("> KZG BASIC PROVER STARTED");
logger.info("");
}
if (logger) logger.info("> KZG BASIC PROVER STARTED");

const { fd: fdPTau, sections: pTauSections } = await readBinFile(pTauFilename, "ptau", 1, 1 << 22, 1 << 24);
const { curve, power: nBitsPTau } = await readPTauHeader(fdPTau, pTauSections);
Expand All @@ -32,7 +29,6 @@ module.exports = async function kzg_basic_prover(evalsBufferArray, pTauFilename,
throw new Error("Polynomial length must be power of two.");
}


// Ensure the powers of Tau file is sufficiently large
if (nBitsPTau < nBits) {
throw new Error("Powers of Tau has not enough values for this polynomial");
Expand All @@ -54,8 +50,6 @@ module.exports = async function kzg_basic_prover(evalsBufferArray, pTauFilename,
let proof = {};
let challenges = {};

// STEP 0. Get the settings and prepare the setup
// Ensure all polynomials have the same length
const pols = [];
for (let i = 0; i < evalsBufferArray.length; i++) {
// Convert the evaluations to Montgomery form
Expand All @@ -65,43 +59,39 @@ module.exports = async function kzg_basic_prover(evalsBufferArray, pTauFilename,
pols[i] = await Polynomial.fromEvaluations(evals, curve, logger);
}

// STEP 1. Generate the polynomial commitments of all polynomials
// STEP 1. Generate the polynomial commitments for all polynomials
logger.info("> STEP 1. Compute polynomial commitments");
proof.commitments = [];
for(let i=0; i<pols.length; i++) {
pols[i].coef = await curve.Fr.batchToMontgomery(pols[i].coef.slice(0, pols[i].coef.byteLength));
proof.commitments[i] = await pols[i].multiExponentiation(PTau, `pol${i}`);
logger.info(`··· [p${i}(X)]_1 = `, curve.G1.toString(proof.commitments[i]));
logger.info(`··· [p${i}(X)]_1 =`, curve.G1.toString(proof.commitments[i]));
}

// STEP 2. Get challenge xi from transcript
logger.info("> STEP 2. Get challenge xi");
// STEP 2. Compute opening evaluations
logger.info("> STEP 2. Compute opening evaluations");
// STEP 2.1 Compute challenge xi
const transcript = new Keccak256Transcript(curve);
for(let i=0; i<pols.length; i++) {
transcript.addPolCommitment(proof.commitments[i]);
}
for(commitment of proof.commitments) transcript.addPolCommitment(commitment);
challenges.xi = transcript.getChallenge();
logger.info("··· xi = ", curve.Fr.toString(challenges.xi));
logger.info("··· xi =", curve.Fr.toString(challenges.xi));

// STEP 3. Calculate the evaluations p(xi) = y for all polynomials
logger.info("> STEP 3. Calculate the opening p(xi) = y");
// STEP 2.2 Compute evaluations
proof.evaluations = [];
for(let i=0; i<pols.length; i++) {
proof.evaluations[i] = pols[i].evaluate(challenges.xi);
logger.info(`··· y${i} = `, curve.Fr.toString(proof.evaluations[i]));
logger.info(`··· y${i} =`, curve.Fr.toString(proof.evaluations[i]));
}

// STEP 4. Get challenge alpha from transcript
logger.info("> STEP 4. Get challenge alpha");
// STEP 3. Calculate the polynomial q(X)
logger.info("> STEP 3. Calculate the polynomial q(X)");
// STEP 3.1 Compute challenge alpha
transcript.reset();
for(let i=0; i<pols.length; i++) {
transcript.addEvaluation(proof.evaluations[i]);
}
for(evaluation of proof.evaluations) transcript.addEvaluation(evaluation);
challenges.alpha = transcript.getChallenge();
logger.info("··· alpha = ", curve.Fr.toString(challenges.alpha));
logger.info("··· alpha =", curve.Fr.toString(challenges.alpha));

// STEP 5. Calculate the polynomial q(X)
logger.info("> STEP 5. Calculate the polynomial q(X)");
// STEP 3.1 Calculate the polynomial q(X)
let polQ = new Polynomial(new Uint8Array(curve.Fr.n8 * polLen), curve, logger);

let currentAlpha = curve.Fr.one;
Expand All @@ -115,12 +105,9 @@ module.exports = async function kzg_basic_prover(evalsBufferArray, pTauFilename,
}

proof.commitQ = await polQ.multiExponentiation(PTau, "Q");
logger.info("··· [q(X)]_1 = ", curve.G1.toString(proof.commitQ));
logger.info("··· [q(X)]_1 =", curve.G1.toString(proof.commitQ));

if (logger) {
logger.info("");
logger.info("> KZG BASIC PROVER FINISHED");
}
if (logger) logger.info("> KZG BASIC PROVER FINISHED");

await fdPTau.close();

Expand Down
14 changes: 4 additions & 10 deletions src/kzg_basic_verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const readPTauHeader = require("./ptau_utils");
module.exports = async function kzg_basic_verifier(proof, pTauFilename, options) {
const logger = options.logger;

if (logger) {
logger.info("> KZG BASIC VERIFIER STARTED");
logger.info("");
}
if (logger) logger.info("> KZG BASIC VERIFIER STARTED");

const { fd: fdPTau, sections: pTauSections } = await readBinFile(pTauFilename, "ptau", 1, 1 << 22, 1 << 24);
const { curve } = await readPTauHeader(fdPTau, pTauSections);
Expand All @@ -31,7 +28,7 @@ module.exports = async function kzg_basic_verifier(proof, pTauFilename, options)
transcript.addPolCommitment(proof.commitments[i]);
}
challenges.xi = transcript.getChallenge();
logger.info("··· xi = ", curve.Fr.toString(challenges.xi));
logger.info("··· xi =", curve.Fr.toString(challenges.xi));

// STEP 2. Calculate challenge alpha from transcript
logger.info("> STEP 2. Compute challenge alpha");
Expand All @@ -40,7 +37,7 @@ module.exports = async function kzg_basic_verifier(proof, pTauFilename, options)
transcript.addEvaluation(proof.evaluations[i]);
}
challenges.alpha = transcript.getChallenge();
logger.info("··· alpha = ", curve.Fr.toString(challenges.alpha));
logger.info("··· alpha =", curve.Fr.toString(challenges.alpha));

// STEP 3. Compute [F]_1
let currentAlpha = curve.Fr.one;
Expand Down Expand Up @@ -82,10 +79,7 @@ module.exports = async function kzg_basic_verifier(proof, pTauFilename, options)
}
}

if (logger) {
logger.info("");
logger.info("> KZG BASIC VERIFIER FINISHED");
}
if (logger) logger.info("");

await fdPTau.close();

Expand Down

0 comments on commit dc709c4

Please sign in to comment.