Skip to content

Commit

Permalink
feat: Improve proving speed by caching zkey curve (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalinbhardwaj authored Jun 12, 2022
1 parent 51723ad commit 227d151
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/groth16_prove.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default async function groth16Prove(zkeyFileName, witnessFileName, logger
throw new Error(`Invalid witness length. Circuit: ${zkey.nVars}, witness: ${wtns.nWitness}`);
}

const curve = await getCurve(zkey.q);
const curve = zkey.curve;
const Fr = curve.Fr;
const G1 = curve.G1;
const G2 = curve.G2;
Expand Down
2 changes: 1 addition & 1 deletion src/plonk_prove.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async function plonk16Prove(zkeyFileName, witnessFileName, logger
throw new Error(`Invalid witness length. Circuit: ${zkey.nVars}, witness: ${wtns.nWitness}, ${zkey.nAdditions}`);
}

const curve = await getCurve(zkey.q);
const curve = zkey.curve;
const Fr = curve.Fr;
const G1 = curve.G1;
const n8r = curve.Fr.n8;
Expand Down
38 changes: 17 additions & 21 deletions src/zkey_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,17 @@ async function readHeaderGroth16(fd, sections, toObject) {
const n8r = await fd.readULE32();
zkey.n8r = n8r;
zkey.r = await binFileUtils.readBigInt(fd, n8r);

let curve = await getCurve(zkey.q);

zkey.curve = await getCurve(zkey.q);
zkey.nVars = await fd.readULE32();
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
zkey.power = log2(zkey.domainSize);
zkey.vk_alpha_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_2 = await readG2(fd, curve, toObject);
zkey.vk_gamma_2 = await readG2(fd, curve, toObject);
zkey.vk_delta_1 = await readG1(fd, curve, toObject);
zkey.vk_delta_2 = await readG2(fd, curve, toObject);
zkey.vk_alpha_1 = await readG1(fd, zkey.curve, toObject);
zkey.vk_beta_1 = await readG1(fd, zkey.curve, toObject);
zkey.vk_beta_2 = await readG2(fd, zkey.curve, toObject);
zkey.vk_gamma_2 = await readG2(fd, zkey.curve, toObject);
zkey.vk_delta_1 = await readG1(fd, zkey.curve, toObject);
zkey.vk_delta_2 = await readG2(fd, zkey.curve, toObject);
await binFileUtils.endReadSection(fd);

return zkey;
Expand All @@ -274,9 +272,7 @@ async function readHeaderPlonk(fd, sections, protocol, toObject) {
const n8r = await fd.readULE32();
zkey.n8r = n8r;
zkey.r = await binFileUtils.readBigInt(fd, n8r);

let curve = await getCurve(zkey.q);

zkey.curve = await getCurve(zkey.q);
zkey.nVars = await fd.readULE32();
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
Expand All @@ -286,15 +282,15 @@ async function readHeaderPlonk(fd, sections, protocol, toObject) {
zkey.k1 = await fd.read(n8r);
zkey.k2 = await fd.read(n8r);

zkey.Qm = await readG1(fd, curve, toObject);
zkey.Ql = await readG1(fd, curve, toObject);
zkey.Qr = await readG1(fd, curve, toObject);
zkey.Qo = await readG1(fd, curve, toObject);
zkey.Qc = await readG1(fd, curve, toObject);
zkey.S1 = await readG1(fd, curve, toObject);
zkey.S2 = await readG1(fd, curve, toObject);
zkey.S3 = await readG1(fd, curve, toObject);
zkey.X_2 = await readG2(fd, curve, toObject);
zkey.Qm = await readG1(fd, zkey.curve, toObject);
zkey.Ql = await readG1(fd, zkey.curve, toObject);
zkey.Qr = await readG1(fd, zkey.curve, toObject);
zkey.Qo = await readG1(fd, zkey.curve, toObject);
zkey.Qc = await readG1(fd, zkey.curve, toObject);
zkey.S1 = await readG1(fd, zkey.curve, toObject);
zkey.S2 = await readG1(fd, zkey.curve, toObject);
zkey.S3 = await readG1(fd, zkey.curve, toObject);
zkey.X_2 = await readG2(fd, zkey.curve, toObject);

await binFileUtils.endReadSection(fd);

Expand Down

0 comments on commit 227d151

Please sign in to comment.