Skip to content

Commit

Permalink
Allow passing prover options. First one is singleThread param for cur…
Browse files Browse the repository at this point in the history
…ve threadManager.
  • Loading branch information
OBrezhniev committed Oct 14, 2024
1 parent bd45295 commit 448e2d7
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 160 deletions.
67 changes: 36 additions & 31 deletions build/browser.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1089,37 +1089,41 @@ const bn128r$1 = Scalar.e("21888242871839275222246405745257275088548364400416034
const bls12381q = Scalar.e("1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab", 16);
const bn128q = Scalar.e("21888242871839275222246405745257275088696311157297823662689037894645226208583");

async function getCurveFromR(r) {
async function getCurveFromR(r, options) {
let curve;
// check that options param is defined and that options.singleThread is defined
let singleThread = options && options.singleThread;
if (Scalar.eq(r, bn128r$1)) {
curve = await buildBn128();
curve = await buildBn128(singleThread);
} else if (Scalar.eq(r, bls12381r$1)) {
curve = await buildBls12381();
curve = await buildBls12381(singleThread);
} else {
throw new Error(`Curve not supported: ${Scalar.toString(r)}`);
}
return curve;
}

async function getCurveFromQ(q) {
async function getCurveFromQ(q, options) {
let curve;
let singleThread = options && options.singleThread;
if (Scalar.eq(q, bn128q)) {
curve = await buildBn128();
curve = await buildBn128(singleThread);
} else if (Scalar.eq(q, bls12381q)) {
curve = await buildBls12381();
curve = await buildBls12381(singleThread);
} else {
throw new Error(`Curve not supported: ${Scalar.toString(q)}`);
}
return curve;
}

async function getCurveFromName(name) {
async function getCurveFromName(name, options) {
let curve;
let singleThread = options && options.singleThread;
const normName = normalizeName(name);
if (["BN128", "BN254", "ALTBN128"].indexOf(normName) >= 0) {
curve = await buildBn128();
curve = await buildBn128(singleThread);
} else if (["BLS12381"].indexOf(normName) >= 0) {
curve = await buildBls12381();
curve = await buildBls12381(singleThread);
} else {
throw new Error(`Curve not supported: ${name}`);
}
Expand Down Expand Up @@ -2489,19 +2493,19 @@ async function readG2(fd, curve, toObject) {
}


async function readHeader$1(fd, sections, toObject) {
async function readHeader$1(fd, sections, toObject, options) {
// Read Header
/////////////////////
await startReadUniqueSection(fd, sections, 1);
const protocolId = await fd.readULE32();
await endReadSection(fd);

if (protocolId === GROTH16_PROTOCOL_ID) {
return await readHeaderGroth16(fd, sections, toObject);
return await readHeaderGroth16(fd, sections, toObject, options);
} else if (protocolId === PLONK_PROTOCOL_ID) {
return await readHeaderPlonk(fd, sections, toObject);
return await readHeaderPlonk(fd, sections, toObject, options);
} else if (protocolId === FFLONK_PROTOCOL_ID) {
return await readHeaderFFlonk(fd, sections, toObject);
return await readHeaderFFlonk(fd, sections, toObject, options);
} else {
throw new Error("Protocol not supported: ");
}
Expand All @@ -2510,7 +2514,7 @@ async function readHeader$1(fd, sections, toObject) {



async function readHeaderGroth16(fd, sections, toObject) {
async function readHeaderGroth16(fd, sections, toObject, options) {
const zkey = {};

zkey.protocol = "groth16";
Expand All @@ -2525,7 +2529,7 @@ async function readHeaderGroth16(fd, sections, toObject) {
const n8r = await fd.readULE32();
zkey.n8r = n8r;
zkey.r = await readBigInt(fd, n8r);
zkey.curve = await getCurveFromQ(zkey.q);
zkey.curve = await getCurveFromQ(zkey.q, options);
zkey.nVars = await fd.readULE32();
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
Expand All @@ -2542,7 +2546,7 @@ async function readHeaderGroth16(fd, sections, toObject) {

}

async function readHeaderPlonk(fd, sections, toObject) {
async function readHeaderPlonk(fd, sections, toObject, options) {
const zkey = {};

zkey.protocol = "plonk";
Expand All @@ -2557,7 +2561,7 @@ async function readHeaderPlonk(fd, sections, toObject) {
const n8r = await fd.readULE32();
zkey.n8r = n8r;
zkey.r = await readBigInt(fd, n8r);
zkey.curve = await getCurveFromQ(zkey.q);
zkey.curve = await getCurveFromQ(zkey.q, options);
zkey.nVars = await fd.readULE32();
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
Expand All @@ -2582,7 +2586,7 @@ async function readHeaderPlonk(fd, sections, toObject) {
return zkey;
}

async function readHeaderFFlonk(fd, sections, toObject) {
async function readHeaderFFlonk(fd, sections, toObject, options) {
const zkey = {};

zkey.protocol = "fflonk";
Expand All @@ -2592,7 +2596,7 @@ async function readHeaderFFlonk(fd, sections, toObject) {
const n8q = await fd.readULE32();
zkey.n8q = n8q;
zkey.q = await readBigInt(fd, n8q);
zkey.curve = await getCurveFromQ(zkey.q);
zkey.curve = await getCurveFromQ(zkey.q, options);

const n8r = await fd.readULE32();
zkey.n8r = n8r;
Expand Down Expand Up @@ -2955,14 +2959,14 @@ async function read(fileName) {
*/
const {stringifyBigInts: stringifyBigInts$4} = utils;

async function groth16Prove(zkeyFileName, witnessFileName, logger) {
async function groth16Prove(zkeyFileName, witnessFileName, logger, options) {
const {fd: fdWtns, sections: sectionsWtns} = await readBinFile(witnessFileName, "wtns", 2);

const wtns = await readHeader(fdWtns, sectionsWtns);

const {fd: fdZKey, sections: sectionsZKey} = await readBinFile(zkeyFileName, "zkey", 2);

const zkey = await readHeader$1(fdZKey, sectionsZKey);
const zkey = await readHeader$1(fdZKey, sectionsZKey, undefined, options);

if (zkey.protocol != "groth16") {
throw new Error("zkey file is not groth16");
Expand Down Expand Up @@ -3981,14 +3985,14 @@ async function wtnsCalculate(_input, wasmFileName, wtnsFileName, options) {
*/
const {unstringifyBigInts: unstringifyBigInts$a} = utils;

async function groth16FullProve(_input, wasmFile, zkeyFileName, logger, wtnsCalcOptions) {
async function groth16FullProve(_input, wasmFile, zkeyFileName, logger, wtnsCalcOptions, proverOptions) {
const input = unstringifyBigInts$a(_input);

const wtns= {
type: "mem"
};
await wtnsCalculate(input, wasmFile, wtns, wtnsCalcOptions);
return await groth16Prove(zkeyFileName, wtns, logger);
return await groth16Prove(zkeyFileName, wtns, logger, proverOptions);
}

/*
Expand Down Expand Up @@ -11940,7 +11944,7 @@ class Evaluations {
*/
const {stringifyBigInts: stringifyBigInts$1} = utils;

async function plonk16Prove(zkeyFileName, witnessFileName, logger) {
async function plonk16Prove(zkeyFileName, witnessFileName, logger, options) {
const {fd: fdWtns, sections: sectionsWtns} = await readBinFile(witnessFileName, "wtns", 2);

// Read witness file
Expand All @@ -11951,7 +11955,7 @@ async function plonk16Prove(zkeyFileName, witnessFileName, logger) {
if (logger) logger.debug("> Reading zkey file");
const {fd: fdZKey, sections: zkeySections} = await readBinFile(zkeyFileName, "zkey", 2);

const zkey = await readHeader$1(fdZKey, zkeySections);
const zkey = await readHeader$1(fdZKey, zkeySections, undefined, options);
if (zkey.protocol != "plonk") {
throw new Error("zkey file is not plonk");
}
Expand Down Expand Up @@ -12804,14 +12808,14 @@ async function plonk16Prove(zkeyFileName, witnessFileName, logger) {
*/
const {unstringifyBigInts: unstringifyBigInts$5} = utils;

async function plonkFullProve(_input, wasmFile, zkeyFileName, logger, wtnsCalcOptions) {
async function plonkFullProve(_input, wasmFile, zkeyFileName, logger, wtnsCalcOptions, proverOptions) {
const input = unstringifyBigInts$5(_input);

const wtns= {
type: "mem"
};
await wtnsCalculate(input, wasmFile, wtns, wtnsCalcOptions);
return await plonk16Prove(zkeyFileName, wtns, logger);
return await plonk16Prove(zkeyFileName, wtns, logger, proverOptions);
}

/*
Expand Down Expand Up @@ -14141,7 +14145,7 @@ async function fflonkSetup(r1csFilename, ptauFilename, zkeyFilename, logger) {
const { stringifyBigInts } = utils;


async function fflonkProve(zkeyFileName, witnessFileName, logger) {
async function fflonkProve(zkeyFileName, witnessFileName, logger, options) {
if (logger) logger.info("FFLONK PROVER STARTED");

// Read witness file
Expand All @@ -14158,7 +14162,8 @@ async function fflonkProve(zkeyFileName, witnessFileName, logger) {
fd: fdZKey,
sections: zkeySections
} = await readBinFile(zkeyFileName, "zkey", 2);
const zkey = await readHeader$1(fdZKey, zkeySections);

const zkey = await readHeader$1(fdZKey, zkeySections, undefined, options);

if (zkey.protocolId !== FFLONK_PROTOCOL_ID) {
throw new Error("zkey file is not fflonk");
Expand Down Expand Up @@ -15395,7 +15400,7 @@ async function fflonkProve(zkeyFileName, witnessFileName, logger) {
*/
const {unstringifyBigInts: unstringifyBigInts$2} = utils;

async function fflonkFullProve(_input, wasmFilename, zkeyFilename, logger, wtnsCalcOptions) {
async function fflonkFullProve(_input, wasmFilename, zkeyFilename, logger, wtnsCalcOptions, proverOptions) {
const input = unstringifyBigInts$2(_input);

const wtns= {type: "mem"};
Expand All @@ -15404,7 +15409,7 @@ async function fflonkFullProve(_input, wasmFilename, zkeyFilename, logger, wtnsC
await wtnsCalculate(input, wasmFilename, wtns, wtnsCalcOptions);

// Compute the proof
return await fflonkProve(zkeyFilename, wtns, logger);
return await fflonkProve(zkeyFilename, wtns, logger, proverOptions);
}

/*
Expand Down
Loading

0 comments on commit 448e2d7

Please sign in to comment.