rework circom compat

This commit is contained in:
Dmitriy Ryajov 2024-01-29 14:57:40 -06:00
parent 5f0d214f32
commit 79088c2383
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4

View File

@ -21,6 +21,9 @@ import ../../../merkletree
import pkg/constantine/math/arithmetic import pkg/constantine/math/arithmetic
import pkg/constantine/math/arithmetic
import pkg/constantine/math/io/io_bigints
export circomcompat export circomcompat
type type
@ -30,10 +33,12 @@ type
zKeyPath : string zKeyPath : string
backendCfg : ptr CircomBn254Cfg backendCfg : ptr CircomBn254Cfg
CircomProof* = object CircomG1* = G1
proof*: Proof CircomG2* = G2
backend: ptr CircomCompatCtx
cfg: ptr CircomBn254Cfg CircomProof* = Proof
CircomInputs* = Inputs
CircomKey* = VerifyingKey
proc release*(self: CircomCompat) = proc release*(self: CircomCompat) =
## Release the backend ## Release the backend
@ -41,12 +46,19 @@ proc release*(self: CircomCompat) =
self.backendCfg.unsafeAddr.releaseCfg() self.backendCfg.unsafeAddr.releaseCfg()
proc release*(proof: CircomProof) = proc getVerifyingKey*(
## Release the backend context self: CircomCompat): ?!ptr CircomKey =
## Get the verifying key
## ##
proof.backend.unsafeAddr.release_circom_compat() var
doAssert(proof.backend == nil) 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*( proc prove*(
self: CircomCompat, self: CircomCompat,
@ -130,31 +142,27 @@ proc prove*(
proofPtr[] proofPtr[]
finally: finally:
if proofPtr != nil: if proofPtr != nil:
release_proof(proofPtr.addr) proofPtr.addr.releaseProof()
success CircomProof( if backend != nil:
proof: proof, backend.addr.releaseCircomCompat()
cfg: self.backendCfg,
backend: backend)
proc verify*(self: CircomCompat, proof: CircomProof): ?!bool = success proof
proc verify*(
self: CircomCompat,
proof: CircomProof,
inputs: CircomInputs,
vkp: CircomKey): ?!bool =
## Verify a proof using a backend ## Verify a proof using a backend
## ##
var var
inputsPtr: ptr Inputs = nil proofPtr : ptr Proof = unsafeAddr proof
vkPtr: ptr VerifyingKey = nil inputsPtr: ptr Inputs = unsafeAddr inputs
vpkPtr: ptr CircomKey = unsafeAddr vkp
if (let res = proof.cfg.getVerifyingKey(vkPtr.addr); res != ERR_OK) or let res = verifyCircuit(proofPtr, inputsPtr, vpkPtr)
vkPtr == nil:
return failure("Failed to get verifying key - err code: " & $res)
if (let res = proof.backend.getPubInputs(inputsPtr.addr); res != ERR_OK) or
inputsPtr == nil:
return failure("Failed to get public inputs - err code: " & $res)
try:
let res = verifyCircuit(proof.proof.unsafeAddr, inputsPtr, vkPtr)
if res == ERR_OK: if res == ERR_OK:
success true success true
elif res == ERR_FAILED_TO_VERIFY_PROOF: elif res == ERR_FAILED_TO_VERIFY_PROOF:
@ -162,13 +170,6 @@ proc verify*(self: CircomCompat, proof: CircomProof): ?!bool =
else: else:
failure("Failed to verify proof - err code: " & $res) failure("Failed to verify proof - err code: " & $res)
finally:
if inputsPtr != nil:
releaseInputs(inputsPtr.addr)
if vkPtr != nil:
releaseKey(vkPtr.addr)
proc init*( proc init*(
_: type CircomCompat, _: type CircomCompat,
r1csPath: string, r1csPath: string,