import circom helpers

This commit is contained in:
Jaremy Creechley 2024-05-10 22:47:39 +03:00
parent e39b5ef694
commit 018be61ebc
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
1 changed files with 80 additions and 80 deletions

View File

@ -13,7 +13,7 @@ import pkg/constantine/math/[arithmetic, io/io_bigints, io/io_fields]
import ./utils
import ./create_circuits
type CircuitFiles* = object
type CircomCircuit* = object
r1cs*: string
wasm*: string
zkey*: string
@ -33,9 +33,7 @@ proc release*(self: CircomCompat) =
if not isNil(self.vkp):
self.vkp.unsafeAddr.release_key()
proc prove*[H](
self: CircomCompat,
input: ProofInputs[H]): ?!CircomProof =
proc prove*[H](self: CircomCompat, input: ProofInputs[H]): ?!CircomProof =
## Encode buffers using a ctx
##
@ -44,28 +42,27 @@ proc prove*[H](
# to the circom ffi - `setLen` is used to adjust the
# sequence length to the correct size which also 0 pads
# to the correct length
doAssert input.samples.len == self.numSamples,
"Number of samples does not match"
doAssert input.samples.len == self.numSamples, "Number of samples does not match"
doAssert input.slotProof.len <= self.datasetDepth,
"Number of slot proofs does not match"
doAssert input.samples.allIt(
block:
(it.merklePaths.len <= self.slotDepth + self.blkDepth and
it.cellData.len <= self.cellElms * 32)), "Merkle paths length does not match"
(
it.merklePaths.len <= self.slotDepth + self.blkDepth and
it.cellData.len <= self.cellElms * 32
)
), "Merkle paths length does not match"
# TODO: All parameters should match circom's static parametter
var
ctx: ptr CircomCompatCtx
var ctx: ptr CircomCompatCtx
defer:
if ctx != nil:
ctx.addr.releaseCircomCompat()
if initCircomCompat(
self.backendCfg,
addr ctx) != ERR_OK or ctx == nil:
if initCircomCompat(self.backendCfg, addr ctx) != ERR_OK or ctx == nil:
raiseAssert("failed to initialize CircomCompat ctx")
var
@ -73,40 +70,37 @@ proc prove*[H](
dataSetRoot = input.datasetRoot.toBytes
slotRoot = input.slotRoot.toBytes
if ctx.pushInputU256Array(
"entropy".cstring, entropy[0].addr, entropy.len.uint32) != ERR_OK:
if ctx.pushInputU256Array("entropy".cstring, entropy[0].addr, entropy.len.uint32) !=
ERR_OK:
return failure("Failed to push entropy")
if ctx.pushInputU256Array(
"dataSetRoot".cstring, dataSetRoot[0].addr, dataSetRoot.len.uint32) != ERR_OK:
"dataSetRoot".cstring, dataSetRoot[0].addr, dataSetRoot.len.uint32
) != ERR_OK:
return failure("Failed to push data set root")
if ctx.pushInputU256Array(
"slotRoot".cstring, slotRoot[0].addr, slotRoot.len.uint32) != ERR_OK:
if ctx.pushInputU256Array("slotRoot".cstring, slotRoot[0].addr, slotRoot.len.uint32) !=
ERR_OK:
return failure("Failed to push data set root")
if ctx.pushInputU32(
"nCellsPerSlot".cstring, input.nCellsPerSlot.uint32) != ERR_OK:
if ctx.pushInputU32("nCellsPerSlot".cstring, input.nCellsPerSlot.uint32) != ERR_OK:
return failure("Failed to push nCellsPerSlot")
if ctx.pushInputU32(
"nSlotsPerDataSet".cstring, input.nSlotsPerDataSet.uint32) != ERR_OK:
if ctx.pushInputU32("nSlotsPerDataSet".cstring, input.nSlotsPerDataSet.uint32) !=
ERR_OK:
return failure("Failed to push nSlotsPerDataSet")
if ctx.pushInputU32(
"slotIndex".cstring, input.slotIndex.uint32) != ERR_OK:
if ctx.pushInputU32("slotIndex".cstring, input.slotIndex.uint32) != ERR_OK:
return failure("Failed to push slotIndex")
var
slotProof = input.slotProof.mapIt( it.toBytes ).concat
var slotProof = input.slotProof.mapIt(it.toBytes).concat
slotProof.setLen(self.datasetDepth) # zero pad inputs to correct size
# arrays are always flattened
if ctx.pushInputU256Array(
"slotProof".cstring,
slotProof[0].addr,
uint (slotProof[0].len * slotProof.len)) != ERR_OK:
"slotProof".cstring, slotProof[0].addr, uint (slotProof[0].len * slotProof.len)
) != ERR_OK:
return failure("Failed to push slot proof")
for s in input.samples:
@ -118,24 +112,19 @@ proc prove*[H](
if ctx.pushInputU256Array(
"merklePaths".cstring,
merklePaths[0].addr,
uint (merklePaths[0].len * merklePaths.len)) != ERR_OK:
uint (merklePaths[0].len * merklePaths.len),
) != ERR_OK:
return failure("Failed to push merkle paths")
data.setLen(self.cellElms * 32) # zero pad inputs to correct size
if ctx.pushInputU256Array(
"cellData".cstring,
data[0].addr,
data.len.uint) != ERR_OK:
if ctx.pushInputU256Array("cellData".cstring, data[0].addr, data.len.uint) != ERR_OK:
return failure("Failed to push cell data")
var
proofPtr: ptr Proof = nil
var proofPtr: ptr Proof = nil
let proof =
try:
if (
let res = self.backendCfg.proveCircuit(ctx, proofPtr.addr);
res != ERR_OK) or
if (let res = self.backendCfg.proveCircuit(ctx, proofPtr.addr); res != ERR_OK) or
proofPtr == nil:
return failure("Failed to prove - err code: " & $res)
@ -146,10 +135,20 @@ proc prove*[H](
success proof
proc verify*[H](
self: CircomCompat,
proof: CircomProof,
inputs: ProofInputs[H]): ?!bool =
proc toCircomInputs*(inputs: ProofInputs[Poseidon2Hash]): Inputs =
var
slotIndex = inputs.slotIndex.toF.toBytes.toArray32
datasetRoot = inputs.datasetRoot.toBytes.toArray32
entropy = inputs.entropy.toBytes.toArray32
elms = [entropy, datasetRoot, slotIndex]
let inputsPtr = allocShared0(32 * elms.len)
copyMem(inputsPtr, addr elms[0], elms.len * 32)
CircomInputs(elms: cast[ptr array[32, byte]](inputsPtr), len: elms.len.uint)
proc verify*(self: CircomCompat, proof: CircomProof, inputs: ProofInputs[H]): ?!bool =
## Verify a proof using a ctx
##
@ -177,25 +176,25 @@ proc init*(
datasetDepth = DefaultMaxDatasetDepth,
blkDepth = DefaultBlockDepth,
cellElms = DefaultCellElms,
numSamples = DefaultSamplesNum): CircomCompat =
numSamples = DefaultSamplesNum,
): CircomCompat =
## Create a new ctx
##
var cfg: ptr CircomBn254Cfg
var zkey = if zkeyPath.len > 0: zkeyPath.cstring else: nil
if initCircomConfig(
r1csPath.cstring,
wasmPath.cstring,
zkey, cfg.addr) != ERR_OK or cfg == nil:
if cfg != nil: cfg.addr.releaseCfg()
if initCircomConfig(r1csPath.cstring, wasmPath.cstring, zkey, cfg.addr) != ERR_OK or
cfg == nil:
if cfg != nil:
cfg.addr.releaseCfg()
raiseAssert("failed to initialize circom compat config")
var
vkpPtr: ptr VerifyingKey = nil
var vkpPtr: ptr VerifyingKey = nil
if cfg.getVerifyingKey(vkpPtr.addr) != ERR_OK or vkpPtr == nil:
if vkpPtr != nil: vkpPtr.addr.releaseKey()
if vkpPtr != nil:
vkpPtr.addr.releaseKey()
raiseAssert("Failed to get verifying key")
CircomCompat(
@ -208,10 +207,11 @@ proc init*(
cellElms: cellElms,
numSamples: numSamples,
backendCfg: cfg,
vkp : vkpPtr)
vkp: vkpPtr,
)
proc runArkCircom(
args: CircuitArgs, files: CircuitFiles, proofInputs: ProofInputs[Poseidon2Hash]
args: CircuitArgs, files: CircomCircuit, proofInputs: ProofInputs[Poseidon2Hash]
) =
echo "Loading sample proof..."
var circom = CircomCompat.init(
@ -250,7 +250,7 @@ proc printHelp() =
quit(1)
proc parseCliOptions(args: var CircuitArgs, files: var CircuitFiles) =
proc parseCliOptions(args: var CircuitArgs, files: var CircomCircuit) =
var argCtr: int = 0
template expectPath(val: string): string =
if val == "":
@ -299,7 +299,7 @@ proc run*() =
var
args = CircuitArgs()
files = CircuitFiles()
files = CircomCircuit()
parseCliOptions(args, files)