thread local dupes

This commit is contained in:
Jaremy Creechley 2024-06-03 18:57:14 +01:00
parent 45e760a7a3
commit 1e6133a804
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
3 changed files with 29 additions and 13 deletions

View File

@ -28,15 +28,20 @@ type
proof: CircomProof proof: CircomProof
inputs: ProofInputs[H] inputs: ProofInputs[H]
proc proveTask[H](args: ptr ProverArgs[H], results: SignalQueuePtr[?!CircomProof]) = var
let circom = args[].circom localCircom {.threadvar.}: Option[CircomCompat]
let data = args[].data
let proof = circom.prove(data) proc proveTask[H](args: ptr ProverArgs[H], results: SignalQueuePtr[?!CircomProof]) =
if localCircom.isNone:
localCircom = some args.circom.duplicate()
var data = args[].data
let proof = localCircom.get().prove(data)
echo "PROVE TASK: proof: ", proof echo "PROVE TASK: proof: ", proof
let verified = circom.verify(proof.get(), data) let verified = localCircom.get().verify(proof.get(), data)
echo "PROVE TASK: verify: ", verified echo "PROVE TASK: verify: ", verified
if (let sent = results.send(proof); sent.isErr()): if (let sent = results.send(proof); sent.isErr()):
@ -50,7 +55,7 @@ proc prove*[H](
without queue =? newSignalQueue[?!CircomProof](maxItems = 1), qerr: without queue =? newSignalQueue[?!CircomProof](maxItems = 1), qerr:
return failure(qerr) return failure(qerr)
var args = (ref ProverArgs[H])(circom: self.circom.duplicate(), data: input) var args = (ref ProverArgs[H])(circom: self.circom, data: input)
GC_ref(args) GC_ref(args)
proc spawnTask() = proc spawnTask() =
@ -71,10 +76,13 @@ proc prove*[H](
success(proof) success(proof)
proc verifyTask[H](args: ptr VerifierArgs[H], results: SignalQueuePtr[?!bool]) = proc verifyTask[H](args: ptr VerifierArgs[H], results: SignalQueuePtr[?!bool]) =
let circom = args.circom
let proof = args.proof if localCircom.isNone:
let inputs = args.inputs localCircom = some args.circom.duplicate()
let verified = circom.verify(proof, inputs)
var proof = args[].proof
var inputs = args[].inputs
let verified = localCircom.get().verify(proof, inputs)
if (let sent = results.send(verified); sent.isErr()): if (let sent = results.send(verified); sent.isErr()):
error "Error sending verification results", msg = sent.error().msg error "Error sending verification results", msg = sent.error().msg
@ -87,7 +95,7 @@ proc verify*[H](
without queue =? newSignalQueue[?!bool](maxItems = 1), qerr: without queue =? newSignalQueue[?!bool](maxItems = 1), qerr:
return failure(qerr) return failure(qerr)
var args = (ref VerifierArgs[H])(circom: self.circom.duplicate(), proof: proof, inputs: inputs) var args = (ref VerifierArgs[H])(circom: self.circom, proof: proof, inputs: inputs)
GC_ref(args) GC_ref(args)
proc spawnTask() = proc spawnTask() =

View File

@ -245,4 +245,12 @@ proc duplicate*(
if cfg != nil: if cfg != nil:
cfg.addr.releaseCfg() cfg.addr.releaseCfg()
raiseAssert("failed to initialize circom compat config") raiseAssert("failed to initialize circom compat config")
CircomCompat(params: self.params, backendCfg: cfg, vkp: self.vkp)
var
vkpPtr: ptr VerifyingKey = nil
if cfg.getVerifyingKey(vkpPtr.addr) != ERR_OK or vkpPtr == nil:
if vkpPtr != nil: vkpPtr.addr.releaseKey()
raiseAssert("Failed to get verifying key")
CircomCompat(params: self.params, backendCfg: cfg, vkp: vkpPtr)

View File

@ -101,7 +101,7 @@ suite "Test Prover":
r1cs = "tests/circuits/fixtures/proof_main.r1cs" r1cs = "tests/circuits/fixtures/proof_main.r1cs"
wasm = "tests/circuits/fixtures/proof_main.wasm" wasm = "tests/circuits/fixtures/proof_main.wasm"
taskpool = Taskpool.new(num_threads = 6) taskpool = Taskpool.new(num_threads = 5)
params = CircomCompatParams.init(r1cs, wasm) params = CircomCompatParams.init(r1cs, wasm)
circomBackend = AsyncCircomCompat.init(params, taskpool) circomBackend = AsyncCircomCompat.init(params, taskpool)
prover = Prover.new(store, circomBackend, samples) prover = Prover.new(store, circomBackend, samples)