From d977da99392304a8054e412f29e6998c03729361 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 12 Feb 2024 14:38:40 -0600 Subject: [PATCH] store vkp on startup --- codex/slots/proofs/backends/circomcompat.nim | 44 +++++++++----------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/codex/slots/proofs/backends/circomcompat.nim b/codex/slots/proofs/backends/circomcompat.nim index 43e4213a..55a0cbc5 100644 --- a/codex/slots/proofs/backends/circomcompat.nim +++ b/codex/slots/proofs/backends/circomcompat.nim @@ -37,6 +37,7 @@ type wasmPath : string # path to the wasm file zKeyPath : string # path to the zkey file backendCfg : ptr CircomBn254Cfg + vkp : ptr CircomKey proc release*(self: CircomCompat) = ## Release the backend @@ -44,20 +45,6 @@ proc release*(self: CircomCompat) = self.backendCfg.unsafeAddr.releaseCfg() -proc getVerifyingKey*( - self: CircomCompat): ?!ptr CircomKey = - ## Get the verifying key - ## - - var - cfg: ptr CircomBn254Cfg = self.backendCfg - vkpPtr: ptr VerifyingKey = nil - - if cfg.getVerifyingKey(vkpPtr.addr) != ERR_OK or vkpPtr == nil: - return failure("Failed to get verifying key") - - success vkpPtr - proc prove*[H]( self: CircomCompat, input: ProofInputs[H]): ?!CircomProof = @@ -181,15 +168,17 @@ proc verify*[H]( var proofPtr = unsafeAddr proof inputs = inputs.toCircomInputs() - vkpPtr = ? self.getVerifyingKey() - let res = verifyCircuit(proofPtr, inputs.addr, vkpPtr) - if res == ERR_OK: - success true - elif res == ERR_FAILED_TO_VERIFY_PROOF: - success false - else: - failure("Failed to verify proof - err code: " & $res) + try: + let res = verifyCircuit(proofPtr, inputs.addr, self.vkp) + if res == ERR_OK: + success true + elif res == ERR_FAILED_TO_VERIFY_PROOF: + success false + else: + failure("Failed to verify proof - err code: " & $res) + finally: + inputs.releaseCircomInputs() proc init*( _: type CircomCompat, @@ -212,13 +201,20 @@ proc init*( addr cfg) != ERR_OK or cfg == nil: raiseAssert("failed to initialize circom compat config") + var + vkpPtr: ptr VerifyingKey = nil + + if cfg.getVerifyingKey(vkpPtr.addr) != ERR_OK or vkpPtr == nil: + raiseAssert("Failed to get verifying key") + CircomCompat( r1csPath : r1csPath, wasmPath : wasmPath, zKeyPath : zKeyPath, - backendCfg : cfg, slotDepth : slotDepth, datasetDepth: datasetDepth, blkDepth : blkDepth, cellElms : cellElms, - numSamples : numSamples) + numSamples : numSamples, + backendCfg : cfg, + vkp: vkpPtr)