mirror of
https://github.com/status-im/nim-dagger.git
synced 2025-02-20 10:28:12 +00:00
adding verification
This commit is contained in:
parent
4fdddafccd
commit
50d3ab84ea
@ -65,8 +65,11 @@ proc parseJsons(
|
|||||||
if ctx.pushInputU256Array(key.cstring, num.addr, 1) != ERR_OK:
|
if ctx.pushInputU256Array(key.cstring, num.addr, 1) != ERR_OK:
|
||||||
raise newException(ValueError, "Failed to push BigInt from dec string")
|
raise newException(ValueError, "Failed to push BigInt from dec string")
|
||||||
elif value.kind == JInt:
|
elif value.kind == JInt:
|
||||||
|
# var num = value.getInt().int32
|
||||||
|
# if ctx.pushInputI32(key.cstring, num) != ERR_OK:
|
||||||
|
# raise newException(ValueError, "Failed to push JInt")
|
||||||
var num = value.getInt().uint64
|
var num = value.getInt().uint64
|
||||||
echo "NUM: ", num
|
echo "NUM: ", num, " orig: ", value.getInt()
|
||||||
if ctx.pushInputU64(key.cstring, num) != ERR_OK:
|
if ctx.pushInputU64(key.cstring, num) != ERR_OK:
|
||||||
raise newException(ValueError, "Failed to push JInt")
|
raise newException(ValueError, "Failed to push JInt")
|
||||||
elif value.kind == JArray:
|
elif value.kind == JArray:
|
||||||
@ -76,53 +79,31 @@ proc parseJsons(
|
|||||||
echo "unhandled val: " & $value
|
echo "unhandled val: " & $value
|
||||||
raise newException(ValueError, "Failed to push Json of " & $value.kind)
|
raise newException(ValueError, "Failed to push Json of " & $value.kind)
|
||||||
|
|
||||||
proc prove*(self: CircomCircuit, input: JsonNode) =
|
proc initCircomCtx*(
|
||||||
## Encode buffers using a ctx
|
self: CircomCircuit, input: JsonNode
|
||||||
##
|
): ptr CircomCompatCtx =
|
||||||
|
|
||||||
# TODO: All parameters should match circom's static parametter
|
# 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 CircomCircuit ctx")
|
raiseAssert("failed to initialize CircomCircuit ctx")
|
||||||
|
|
||||||
for key, value in input:
|
for key, value in input:
|
||||||
echo "KEY: ", key, " VAL: ", value.kind
|
echo "KEY: ", key, " VAL: ", value.kind
|
||||||
ctx.parseJsons(key, value)
|
ctx.parseJsons(key, value)
|
||||||
|
|
||||||
|
return ctx
|
||||||
|
|
||||||
# if ctx.pushInputU32("slotIndex".cstring, input.slotIndex.uint32) != ERR_OK:
|
proc prove*(
|
||||||
# return failure("Failed to push slotIndex")
|
self: CircomCircuit, input: JsonNode
|
||||||
|
): CircomProof =
|
||||||
|
## Encode buffers using a ctx
|
||||||
|
##
|
||||||
|
|
||||||
# var slotProof = input.slotProof.mapIt(it.toBytes).concat
|
var ctx = initCircomCtx(self, input)
|
||||||
|
defer:
|
||||||
# slotProof.setLen(self.datasetDepth) # zero pad inputs to correct size
|
if ctx != nil:
|
||||||
|
ctx.addr.releaseCircomCompat()
|
||||||
# arrays are always flattened
|
|
||||||
# if ctx.pushInputU256Array(
|
|
||||||
# "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:
|
|
||||||
# var
|
|
||||||
# merklePaths = s.merklePaths.mapIt(it.toBytes)
|
|
||||||
# data = s.cellData
|
|
||||||
|
|
||||||
# merklePaths.setLen(self.slotDepth) # zero pad inputs to correct size
|
|
||||||
# if ctx.pushInputU256Array(
|
|
||||||
# "merklePaths".cstring,
|
|
||||||
# merklePaths[0].addr,
|
|
||||||
# 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:
|
|
||||||
# return failure("Failed to push cell data")
|
|
||||||
|
|
||||||
var proofPtr: ptr Proof = nil
|
var proofPtr: ptr Proof = nil
|
||||||
|
|
||||||
@ -139,9 +120,41 @@ proc prove*(self: CircomCircuit, input: JsonNode) =
|
|||||||
|
|
||||||
# echo "Proof:"
|
# echo "Proof:"
|
||||||
# echo proof
|
# echo proof
|
||||||
echo "\nProof:json:"
|
echo "\nProof:json: "
|
||||||
let g16proof: Groth16Proof = proof.toGroth16Proof()
|
let g16proof: Groth16Proof = proof.toGroth16Proof()
|
||||||
echo pretty(%*(g16proof))
|
echo pretty(%*(g16proof))
|
||||||
|
return proof
|
||||||
|
|
||||||
|
proc verify*(
|
||||||
|
self: CircomCircuit,
|
||||||
|
jsonInput: JsonNode,
|
||||||
|
proof: CircomProof,
|
||||||
|
): bool =
|
||||||
|
## Verify a proof using a ctx
|
||||||
|
|
||||||
|
var ctx = initCircomCtx(self, jsonInput)
|
||||||
|
defer:
|
||||||
|
if ctx != nil:
|
||||||
|
ctx.addr.releaseCircomCompat()
|
||||||
|
|
||||||
|
var inputs: ptr Inputs
|
||||||
|
|
||||||
|
doAssert ctx.get_pub_inputs(inputs.addr) == ERR_OK
|
||||||
|
|
||||||
|
try:
|
||||||
|
let res = verifyCircuit(proof.unsafeAddr, inputs, self.vkp)
|
||||||
|
|
||||||
|
if res == ERR_OK:
|
||||||
|
result = true
|
||||||
|
elif res == ERR_FAILED_TO_VERIFY_PROOF:
|
||||||
|
result = false
|
||||||
|
else:
|
||||||
|
raise newException(ValueError, "Failed to verify proof - err code: " & $res)
|
||||||
|
|
||||||
|
echo "proof verification result: ", result
|
||||||
|
finally:
|
||||||
|
release_inputs(inputs.addr)
|
||||||
|
|
||||||
|
|
||||||
proc printHelp() =
|
proc printHelp() =
|
||||||
echo "usage:"
|
echo "usage:"
|
||||||
@ -259,7 +272,8 @@ proc run*() =
|
|||||||
inputData = self.inputsPath.readFile()
|
inputData = self.inputsPath.readFile()
|
||||||
inputs: JsonNode = !JsonNode.parse(inputData)
|
inputs: JsonNode = !JsonNode.parse(inputData)
|
||||||
|
|
||||||
prove(self, inputs)
|
let proof = prove(self, inputs)
|
||||||
|
let verified = verify(self, inputs, proof)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
run()
|
run()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user