Conditionally generate groth16

This commit is contained in:
Tornado Contrib 2024-04-30 16:55:51 +00:00
parent 8cc20ded52
commit e7412f3d98
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
5 changed files with 44 additions and 26 deletions

19
dist/index.js vendored

@ -7848,14 +7848,18 @@ var __async = (__this, __arguments, generator) => {
});
};
let groth16;
const groth16Promise = (() => __async(void 0, null, function* () {
if (!groth16) {
groth16 = yield websnarkGroth({ wasmInitialMemory: 2e3 });
}
}))();
function initGroth16() {
return __async(this, null, function* () {
if (!groth16) {
groth16 = yield websnarkGroth({ wasmInitialMemory: 2e3 });
}
});
}
function calculateSnarkProof(input, circuit, provingKey) {
return __async(this, null, function* () {
yield groth16Promise;
if (!groth16) {
yield initGroth16();
}
const snarkInput = {
root: input.root,
nullifierHash: BigInt(input.nullifierHex).toString(),
@ -7870,7 +7874,7 @@ function calculateSnarkProof(input, circuit, provingKey) {
};
console.log("Start generating SNARK proof", snarkInput);
console.time("SNARK proof time");
const proofData = yield websnarkUtils__namespace.genWitnessAndProve(groth16, snarkInput, circuit, provingKey);
const proofData = yield websnarkUtils__namespace.genWitnessAndProve(yield groth16, snarkInput, circuit, provingKey);
const proof = websnarkUtils__namespace.toSolidityInput(proofData).proof;
console.timeEnd("SNARK proof time");
const args = [
@ -7988,6 +7992,7 @@ exports.getTokenBalances = getTokenBalances;
exports.getWeightRandom = getWeightRandom;
exports.getWithdrawals = getWithdrawals;
exports.hexToBytes = hexToBytes;
exports.initGroth16 = initGroth16;
exports.isNode = isNode;
exports.isRelayerUpdated = isRelayerUpdated;
exports.jobsSchema = jobsSchema;

20
dist/index.mjs vendored

@ -7827,14 +7827,18 @@ var __async = (__this, __arguments, generator) => {
});
};
let groth16;
const groth16Promise = (() => __async(void 0, null, function* () {
if (!groth16) {
groth16 = yield websnarkGroth({ wasmInitialMemory: 2e3 });
}
}))();
function initGroth16() {
return __async(this, null, function* () {
if (!groth16) {
groth16 = yield websnarkGroth({ wasmInitialMemory: 2e3 });
}
});
}
function calculateSnarkProof(input, circuit, provingKey) {
return __async(this, null, function* () {
yield groth16Promise;
if (!groth16) {
yield initGroth16();
}
const snarkInput = {
root: input.root,
nullifierHash: BigInt(input.nullifierHex).toString(),
@ -7849,7 +7853,7 @@ function calculateSnarkProof(input, circuit, provingKey) {
};
console.log("Start generating SNARK proof", snarkInput);
console.time("SNARK proof time");
const proofData = yield websnarkUtils.genWitnessAndProve(groth16, snarkInput, circuit, provingKey);
const proofData = yield websnarkUtils.genWitnessAndProve(yield groth16, snarkInput, circuit, provingKey);
const proof = websnarkUtils.toSolidityInput(proofData).proof;
console.timeEnd("SNARK proof time");
const args = [
@ -7864,4 +7868,4 @@ function calculateSnarkProof(input, circuit, provingKey) {
});
}
export { BaseDepositsService, BaseEchoService, BaseEncryptedNotesService, BaseEventsService, BaseGovernanceService, BaseRegistryService, BatchBlockService, BatchEventsService, BatchTransactionService, DEPOSIT, Deposit, ENS__factory, ERC20__factory, GET_DEPOSITS, GET_ECHO_EVENTS, GET_ENCRYPTED_NOTES, GET_GOVERNANCE_APY, GET_GOVERNANCE_EVENTS, GET_NOTE_ACCOUNTS, GET_REGISTERED, GET_STATISTIC, GET_WITHDRAWALS, GasPriceOracle__factory, Invoice, MIN_STAKE_BALANCE, MerkleTreeService, Mimc, Multicall__factory, NetId, NodeDepositsService, NodeEchoService, NodeEncryptedNotesService, NodeGovernanceService, NodeRegistryService, NoteAccount, OffchainOracle__factory, OvmGasPriceOracle__factory, Pedersen, RelayerClient, ReverseRecords__factory, TokenPriceOracle, TornadoBrowserProvider, TornadoFeeOracle, TornadoRpcSigner, TornadoVoidSigner, TornadoWallet, TreeCache, WITHDRAWAL, _META, addNetwork, ajv, base64ToBytes, bigIntReplacer, bnToBytes, buffPedersenHash, bufferToBytes, bytesToBN, bytesToBase64, bytesToHex, calculateScore, calculateSnarkProof, chunk, concatBytes, convertETHToTokenAmount, createDeposit, crypto, customConfig, defaultConfig, defaultUserAgent, download, enabledChains, existsAsync, index as factories, fetch, fetchData, fetchGetUrlFunc, getAllDeposits, getAllEncryptedNotes, getAllGovernanceEvents, getAllGraphEchoEvents, getAllRegisters, getAllWithdrawals, getConfig, getDeposits, getEncryptedNotes, getGasOraclePlugin, getGovernanceEvents, getGraphEchoEvents, getHttpAgent, getInstanceByAddress, getMeta, getNetworkConfig, getNoteAccounts, getProvider, getProviderWithNetId, getRegisters, getStatistic, getStatusSchema, getSubdomains, getSupportedInstances, getTokenBalances, getWeightRandom, getWithdrawals, hexToBytes, isNode, isRelayerUpdated, jobsSchema, leBuff2Int, leInt2Buff, loadCachedEvents, loadSavedEvents, mimc, multicall, packEncryptedMessage, parseAddress, parseKey, parseMnemonic, parseNumber, parseRecoveryKey, parseRelayer, parseSemanticVersion, parseUrl, pedersen, pickWeightedRandomRelayer, populateTransaction, queryGraph, rBigInt, saveUserFile, sleep, substring, toFixedHex, toFixedLength, unpackEncryptedMessage, unzipAsync, validateUrl, zipAsync };
export { BaseDepositsService, BaseEchoService, BaseEncryptedNotesService, BaseEventsService, BaseGovernanceService, BaseRegistryService, BatchBlockService, BatchEventsService, BatchTransactionService, DEPOSIT, Deposit, ENS__factory, ERC20__factory, GET_DEPOSITS, GET_ECHO_EVENTS, GET_ENCRYPTED_NOTES, GET_GOVERNANCE_APY, GET_GOVERNANCE_EVENTS, GET_NOTE_ACCOUNTS, GET_REGISTERED, GET_STATISTIC, GET_WITHDRAWALS, GasPriceOracle__factory, Invoice, MIN_STAKE_BALANCE, MerkleTreeService, Mimc, Multicall__factory, NetId, NodeDepositsService, NodeEchoService, NodeEncryptedNotesService, NodeGovernanceService, NodeRegistryService, NoteAccount, OffchainOracle__factory, OvmGasPriceOracle__factory, Pedersen, RelayerClient, ReverseRecords__factory, TokenPriceOracle, TornadoBrowserProvider, TornadoFeeOracle, TornadoRpcSigner, TornadoVoidSigner, TornadoWallet, TreeCache, WITHDRAWAL, _META, addNetwork, ajv, base64ToBytes, bigIntReplacer, bnToBytes, buffPedersenHash, bufferToBytes, bytesToBN, bytesToBase64, bytesToHex, calculateScore, calculateSnarkProof, chunk, concatBytes, convertETHToTokenAmount, createDeposit, crypto, customConfig, defaultConfig, defaultUserAgent, download, enabledChains, existsAsync, index as factories, fetch, fetchData, fetchGetUrlFunc, getAllDeposits, getAllEncryptedNotes, getAllGovernanceEvents, getAllGraphEchoEvents, getAllRegisters, getAllWithdrawals, getConfig, getDeposits, getEncryptedNotes, getGasOraclePlugin, getGovernanceEvents, getGraphEchoEvents, getHttpAgent, getInstanceByAddress, getMeta, getNetworkConfig, getNoteAccounts, getProvider, getProviderWithNetId, getRegisters, getStatistic, getStatusSchema, getSubdomains, getSupportedInstances, getTokenBalances, getWeightRandom, getWithdrawals, hexToBytes, initGroth16, isNode, isRelayerUpdated, jobsSchema, leBuff2Int, leInt2Buff, loadCachedEvents, loadSavedEvents, mimc, multicall, packEncryptedMessage, parseAddress, parseKey, parseMnemonic, parseNumber, parseRecoveryKey, parseRelayer, parseSemanticVersion, parseUrl, pedersen, pickWeightedRandomRelayer, populateTransaction, queryGraph, rBigInt, saveUserFile, sleep, substring, toFixedHex, toFixedLength, unpackEncryptedMessage, unzipAsync, validateUrl, zipAsync };

20
dist/index.umd.js vendored

@ -80085,6 +80085,7 @@ function substring(str, length = 10) {
"use strict";
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ O: () => (/* binding */ initGroth16),
/* harmony export */ i: () => (/* binding */ calculateSnarkProof)
/* harmony export */ });
/* harmony import */ var _tornado_websnark_src_utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(84276);
@ -80118,14 +80119,18 @@ var __async = (__this, __arguments, generator) => {
let groth16;
const groth16Promise = (() => __async(void 0, null, function* () {
if (!groth16) {
groth16 = yield _tornado_websnark_src_groth16__WEBPACK_IMPORTED_MODULE_1___default()({ wasmInitialMemory: 2e3 });
}
}))();
function initGroth16() {
return __async(this, null, function* () {
if (!groth16) {
groth16 = yield _tornado_websnark_src_groth16__WEBPACK_IMPORTED_MODULE_1___default()({ wasmInitialMemory: 2e3 });
}
});
}
function calculateSnarkProof(input, circuit, provingKey) {
return __async(this, null, function* () {
yield groth16Promise;
if (!groth16) {
yield initGroth16();
}
const snarkInput = {
root: input.root,
nullifierHash: BigInt(input.nullifierHex).toString(),
@ -80140,7 +80145,7 @@ function calculateSnarkProof(input, circuit, provingKey) {
};
console.log("Start generating SNARK proof", snarkInput);
console.time("SNARK proof time");
const proofData = yield _tornado_websnark_src_utils__WEBPACK_IMPORTED_MODULE_0__.genWitnessAndProve(groth16, snarkInput, circuit, provingKey);
const proofData = yield _tornado_websnark_src_utils__WEBPACK_IMPORTED_MODULE_0__.genWitnessAndProve(yield groth16, snarkInput, circuit, provingKey);
const proof = _tornado_websnark_src_utils__WEBPACK_IMPORTED_MODULE_0__.toSolidityInput(proofData).proof;
console.timeEnd("SNARK proof time");
const args = [
@ -201414,6 +201419,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ getWeightRandom: () => (/* reexport safe */ _relayerClient__WEBPACK_IMPORTED_MODULE_17__.c$),
/* harmony export */ getWithdrawals: () => (/* reexport safe */ _graphql__WEBPACK_IMPORTED_MODULE_1__.getWithdrawals),
/* harmony export */ hexToBytes: () => (/* reexport safe */ _utils__WEBPACK_IMPORTED_MODULE_20__.aT),
/* harmony export */ initGroth16: () => (/* reexport safe */ _websnark__WEBPACK_IMPORTED_MODULE_21__.O),
/* harmony export */ isNode: () => (/* reexport safe */ _utils__WEBPACK_IMPORTED_MODULE_20__.Ll),
/* harmony export */ isRelayerUpdated: () => (/* reexport safe */ _relayerClient__WEBPACK_IMPORTED_MODULE_17__.mU),
/* harmony export */ jobsSchema: () => (/* reexport safe */ _schemas__WEBPACK_IMPORTED_MODULE_2__.Us),

1
dist/websnark.d.ts vendored

@ -24,4 +24,5 @@ export type snarkProofs = {
proof: BytesLike;
args: snarkArgs;
};
export declare function initGroth16(): Promise<void>;
export declare function calculateSnarkProof(input: snarkInputs, circuit: object, provingKey: ArrayBuffer): Promise<snarkProofs>;

@ -39,18 +39,20 @@ export type snarkProofs = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let groth16: any;
const groth16Promise = (async () => {
export async function initGroth16() {
if (!groth16) {
groth16 = await websnarkGroth({ wasmInitialMemory: 2000 });
}
})();
}
export async function calculateSnarkProof(
input: snarkInputs,
circuit: object,
provingKey: ArrayBuffer,
): Promise<snarkProofs> {
await groth16Promise;
if (!groth16) {
await initGroth16();
}
const snarkInput = {
root: input.root,
@ -68,7 +70,7 @@ export async function calculateSnarkProof(
console.log('Start generating SNARK proof', snarkInput);
console.time('SNARK proof time');
const proofData = await websnarkUtils.genWitnessAndProve(groth16, snarkInput, circuit, provingKey);
const proofData = await websnarkUtils.genWitnessAndProve(await groth16, snarkInput, circuit, provingKey);
const proof = websnarkUtils.toSolidityInput(proofData).proof as BytesLike;
console.timeEnd('SNARK proof time');