This commit is contained in:
Dmitriy Ryajov 2024-02-07 10:35:44 -06:00
parent c480e4a750
commit 1cecb51087
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4

View File

@ -21,7 +21,6 @@ import ../../../stores
import ../../../merkletree import ../../../merkletree
import pkg/constantine/math/arithmetic import pkg/constantine/math/arithmetic
import pkg/constantine/math/arithmetic import pkg/constantine/math/arithmetic
import pkg/constantine/math/io/io_bigints import pkg/constantine/math/io/io_bigints
@ -29,23 +28,23 @@ export circomcompat
const const
# TODO: this defaults need to be adjusted and/or replased with cli config params # TODO: this defaults need to be adjusted and/or replased with cli config params
DefaultMaxDepth* = 32 DefaultMaxSlotDepth* = 32
DefaultMaxLog2NSlots* = 8 DefaultMaxDatasetDepth* = 8
DefaultBlockTreeDepth* = 5 DefaultBlockDepth* = 5
DefaultNCellFldElms* = 67 DefaultCellElms* = 67
DefaultNSamples* = 5 DefaultNSamples* = 5
type type
CircomCompat* = object CircomCompat* = object
r1csPath : string slotDepth : int # max depth of the slot tree
wasmPath : string datasetDepth : int # max depth of dataset tree
zKeyPath : string blkDepth : int # depth of the block merkle tree (pow2 for now)
cellElms : int # number of field elements per cell
numSamples : int # number of samples per slot
r1csPath : string # path to the r1cs file
wasmPath : string # path to the wasm file
zKeyPath : string # path to the zkey file
backendCfg : ptr CircomBn254Cfg backendCfg : ptr CircomBn254Cfg
maxDepth : int
log2NSlots : int
blkDepth : int
cellFldElms : int
nSamples : int
CircomG1* = G1 CircomG1* = G1
CircomG2* = G2 CircomG2* = G2
@ -80,6 +79,22 @@ proc prove*[H](
## Encode buffers using a backend ## Encode buffers using a backend
## ##
# NOTE: All inputs are statically sized per circuit
# and adjusted accordingly right before being passed
# 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.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"
# TODO: All parameters should match circom's static parametter # TODO: All parameters should match circom's static parametter
var var
backend: ptr CircomCompatCtx backend: ptr CircomCompatCtx
@ -121,7 +136,7 @@ proc prove*[H](
var var
slotProof = input.slotProof.mapIt( it.toBytes ).concat slotProof = input.slotProof.mapIt( it.toBytes ).concat
slotProof.setLen(self.log2NSlots) # adjust to match circom static params slotProof.setLen(self.datasetDepth) # zero pad inputs to correct size
# arrays are always flattened # arrays are always flattened
if backend.pushInputU256Array( if backend.pushInputU256Array(
@ -135,14 +150,14 @@ proc prove*[H](
merklePaths = s.merklePaths.mapIt( it.toBytes ) merklePaths = s.merklePaths.mapIt( it.toBytes )
data = s.cellData data = s.cellData
merklePaths.setLen(self.maxDepth) merklePaths.setLen(self.slotDepth) # zero pad inputs to correct size
if backend.pushInputU256Array( if backend.pushInputU256Array(
"merklePaths".cstring, "merklePaths".cstring,
merklePaths[0].addr, 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") return failure("Failed to push merkle paths")
data.setLen(self.cellFldElms * 32) # TODO: sizeof field bits/bytes data.setLen(self.cellElms * 32) # zero pad inputs to correct size
if backend.pushInputU256Array( if backend.pushInputU256Array(
"cellData".cstring, "cellData".cstring,
data[0].addr, data[0].addr,
@ -194,11 +209,11 @@ proc init*(
r1csPath : string, r1csPath : string,
wasmPath : string, wasmPath : string,
zKeyPath : string = "", zKeyPath : string = "",
maxDepth = DefaultMaxDepth, slotDepth = DefaultMaxSlotDepth,
log2NSlots = DefaultMaxLog2NSlots, datasetDepth = DefaultMaxDatasetDepth,
blkDepth = DefaultBlockTreeDepth, blkDepth = DefaultBlockDepth,
cellFldElms = DefaultNCellFldElms, cellElms = DefaultCellElms,
nSamples = DefaultNSamples): CircomCompat = numSamples = DefaultNSamples): CircomCompat =
## Create a new backend ## Create a new backend
## ##
@ -215,8 +230,8 @@ proc init*(
wasmPath : wasmPath, wasmPath : wasmPath,
zKeyPath : zKeyPath, zKeyPath : zKeyPath,
backendCfg : cfg, backendCfg : cfg,
maxDepth: maxDepth, slotDepth : slotDepth,
log2NSlots: log2NSlots, datasetDepth: datasetDepth,
blkDepth : blkDepth, blkDepth : blkDepth,
cellFldElms: cellFldElms, cellElms : cellElms,
nSamples: nSamples) numSamples : numSamples)