store vkp on startup

This commit is contained in:
Dmitriy Ryajov 2024-02-12 14:38:40 -06:00 committed by Eric
parent 20618507c9
commit d977da9939
No known key found for this signature in database
1 changed files with 20 additions and 24 deletions

View File

@ -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)