From e7412f3d986f6a89382f444b6fa26e770226643f Mon Sep 17 00:00:00 2001 From: Tornado Contrib Date: Tue, 30 Apr 2024 16:55:51 +0000 Subject: [PATCH] Conditionally generate groth16 --- dist/index.js | 19 ++++++++++++------- dist/index.mjs | 20 ++++++++++++-------- dist/index.umd.js | 20 +++++++++++++------- dist/websnark.d.ts | 1 + src/websnark.ts | 10 ++++++---- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/dist/index.js b/dist/index.js index 67c8946..a28c076 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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; diff --git a/dist/index.mjs b/dist/index.mjs index 956c70a..9c2e2db 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -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 }; diff --git a/dist/index.umd.js b/dist/index.umd.js index 5563d32..c069312 100644 --- a/dist/index.umd.js +++ b/dist/index.umd.js @@ -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), diff --git a/dist/websnark.d.ts b/dist/websnark.d.ts index 461d57a..3d06a56 100644 --- a/dist/websnark.d.ts +++ b/dist/websnark.d.ts @@ -24,4 +24,5 @@ export type snarkProofs = { proof: BytesLike; args: snarkArgs; }; +export declare function initGroth16(): Promise; export declare function calculateSnarkProof(input: snarkInputs, circuit: object, provingKey: ArrayBuffer): Promise; diff --git a/src/websnark.ts b/src/websnark.ts index 99cb49a..f40e281 100644 --- a/src/websnark.ts +++ b/src/websnark.ts @@ -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 { - 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');