Skip to content

Commit

Permalink
fix script stalling by removing threads at the end of curve calls
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Oct 26, 2023
1 parent 488da5f commit 2895dfe
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions circuits/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import { zKey, groth16 } from 'snarkjs'

import { stringifyBigInts } from 'maci-crypto'

/*
* https://github.com/iden3/snarkjs/issues/152
* Need to cleanup the threads to avoid stalling
*/
const cleanThreads = async () => {
if (!globalThis) {
return Promise.resolve(true)
}

const curves = ['curve_bn128', 'curve_bls12381']
const promises = Promise.all(curves.map(curve => {
return globalThis[curve]?.terminate? globalThis[curve]?.terminate() : null
}).filter(Boolean))

return promises
}

const genProof = (
inputs: string[],
rapidsnarkExePath: string,
Expand Down Expand Up @@ -68,11 +85,13 @@ const genProof = (

const verifyProof = async (publicInputs: any, proof: any, vk: any) => {
const isValid = await groth16.verify(vk, publicInputs, proof)
await cleanThreads()
return isValid
}

const extractVk = async (zkeyPath: string) => {
const vk = await zKey.exportVerificationKey(zkeyPath)
await cleanThreads()
return vk
}

Expand Down

0 comments on commit 2895dfe

Please sign in to comment.