diff --git a/codex/slots/proofs/backends/asynccircoms.nim b/codex/slots/proofs/backends/asynccircoms.nim index b3046e34..5a008b5d 100644 --- a/codex/slots/proofs/backends/asynccircoms.nim +++ b/codex/slots/proofs/backends/asynccircoms.nim @@ -114,3 +114,11 @@ proc init*( ## let circom = CircomCompat.init(params) AsyncCircomCompat(circom: circom, tp: tp) + +proc duplicate*( + self: AsyncCircomCompat, tp: Taskpool +): AsyncCircomCompat = + ## Create a new async circom + ## + let circom = self.circom.duplicate() + AsyncCircomCompat(circom: circom, tp: tp) diff --git a/codex/slots/proofs/backends/circomcompat.nim b/codex/slots/proofs/backends/circomcompat.nim index 6cd925dd..2fee7b35 100644 --- a/codex/slots/proofs/backends/circomcompat.nim +++ b/codex/slots/proofs/backends/circomcompat.nim @@ -235,3 +235,13 @@ proc init*( raiseAssert("Failed to get verifying key") CircomCompat(params: params, backendCfg: cfg, vkp: vkpPtr) + +proc duplicate*( + self: CircomCompat, +): CircomCompat = + ## Create a new ctx + var cfg: ptr CircomBn254Cfg + if duplicateCircomConfig(self.backendCfg, cfg.addr) != ERR_OK or cfg == nil: + if cfg != nil: cfg.addr.releaseCfg() + raiseAssert("failed to initialize circom compat config") + CircomCompat(params: self.params, backendCfg: cfg, vkp: self.vkp) diff --git a/tests/codex/slots/testprover.nim b/tests/codex/slots/testprover.nim index 662e899f..0065a246 100644 --- a/tests/codex/slots/testprover.nim +++ b/tests/codex/slots/testprover.nim @@ -77,27 +77,46 @@ suite "Test Prover": check: (await prover.verify(proof, inputs)).tryGet == true - test "Should sample and prove many slot": + test "Should sample and prove a slot with another circom": let r1cs = "tests/circuits/fixtures/proof_main.r1cs" wasm = "tests/circuits/fixtures/proof_main.wasm" - taskpool = Taskpool.new(num_threads = 6) + taskpool = Taskpool.new(num_threads = 2) params = CircomCompatParams.init(r1cs, wasm) circomBackend = AsyncCircomCompat.init(params, taskpool) prover = Prover.new(store, circomBackend, samples) + challenge = 1234567.toF.toBytes.toArray32 + (inputs, proof) = (await prover.prove(1, verifiable, challenge)).tryGet - var proofs = newSeq[Future[?!(AnyProofInputs, AnyProof)]]() - for i in 1..50: - echo "PROVE: ", i - let - challenge = (1234567).toF.toBytes.toArray32 + let + taskpool2 = Taskpool.new(num_threads = 2) + circomVerifyBackend = circomBackend.duplicate(taskpool2) + proverVerify = Prover.new(store, circomVerifyBackend, samples) + check: + (await proverVerify.verify(proof, inputs)).tryGet == true - proofs.add(prover.prove(1, verifiable, challenge)) + # test "Should sample and prove many slot": + # let + # r1cs = "tests/circuits/fixtures/proof_main.r1cs" + # wasm = "tests/circuits/fixtures/proof_main.wasm" - await allFutures(proofs) + # taskpool = Taskpool.new(num_threads = 6) + # params = CircomCompatParams.init(r1cs, wasm) + # circomBackend = AsyncCircomCompat.init(params, taskpool) + # prover = Prover.new(store, circomBackend, samples) - for pf in proofs: - let (inputs, proof) = (await pf).tryGet - check: - (await prover.verify(proof, inputs)).tryGet == true + # var proofs = newSeq[Future[?!(AnyProofInputs, AnyProof)]]() + # for i in 1..50: + # echo "PROVE: ", i + # let + # challenge = (1234567).toF.toBytes.toArray32 + + # proofs.add(prover.prove(1, verifiable, challenge)) + + # await allFutures(proofs) + + # for pf in proofs: + # let (inputs, proof) = (await pf).tryGet + # check: + # (await prover.verify(proof, inputs)).tryGet == true diff --git a/vendor/nim-circom-compat b/vendor/nim-circom-compat index cc2e742b..7c1105bb 160000 --- a/vendor/nim-circom-compat +++ b/vendor/nim-circom-compat @@ -1 +1 @@ -Subproject commit cc2e742b7ac7a0fdf273b6bebdb9b5b42e3dc722 +Subproject commit 7c1105bb77f0ce7b1b3cc0cb3e78db89265ba05f