Rework ffi (#1)

* rework ffi

* fix types

* fix types

* update ffi

* bump

* bump to master

update ffi

update tests

fix tests

update ffi

update tests
This commit is contained in:
Dmitriy Ryajov 2024-01-25 15:58:27 -06:00
parent e2abbb034e
commit 39c8891291
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
4 changed files with 83 additions and 42 deletions

View File

@ -6,6 +6,7 @@ import std/macros
const
currentDir = currentSourcePath().parentDir()
libDir* = currentDir/"vendor/circom-compat-ffi/target"/"release"
# libDir* = currentDir/"vendor/circom-compat-ffi/target"/"debug" # XXX: uncomment for debug build
libPath* = libDir/"libcircom_compat_ffi.a"
static:

View File

@ -31,39 +31,73 @@ const ERR_SERIALIZE_PROOF* = 13
const ERR_SERIALIZE_INPUTS* = 14
type CircomBn254Cfg* {.incompleteStruct.} = object
type CircomCompatCtx* {.incompleteStruct.} = object
type Buffer* = object
data*: pointer
type G1* = object
x*: array[32, byte]
y*: array[32, byte]
type G2* = object
x*: array[2, array[32, byte]]
y*: array[2, array[32, byte]]
type Proof* = object
a*: G1
b*: G2
c*: G1
type Inputs* = object
elms*: ptr array[32, byte]
len*: uint
## # Safety
#
proc init_circom_compat*(r1cs_path: pointer,
type VerifyingKey* = object
alpha1*: G1
beta2*: G2
gamma2*: G2
delta2*: G2
ic*: ptr G1
icLen*: uint
proc init_circom_config_with_checks*(r1cs_path: pointer,
wasm_path: pointer,
zkey_path: pointer,
sanity_check: bool,
cfg_ptr: ptr ptr CircomBn254Cfg): int32 {.importc: "init_circom_config_with_checks".}
proc init_circom_config*(r1cs_path: pointer,
wasm_path: pointer,
zkey_path: pointer,
cfg_ptr: ptr ptr CircomBn254Cfg): int32 {.importc: "init_circom_config".}
proc init_circom_compat*(cfg_ptr: ptr CircomBn254Cfg,
ctx_ptr: ptr ptr CircomCompatCtx): int32 {.importc: "init_circom_compat".}
proc release_circom_compat*(ctx_ptr: ptr ptr CircomCompatCtx): void {.importc: "release_circom_compat".}
proc release_buffer*(buff_ptr: ptr ptr Buffer): void {.importc: "release_buffer".}
proc release_cfg*(cfg_ptr: ptr ptr CircomBn254Cfg): void {.importc: "release_cfg".}
## # Safety
#
proc prove_circuit*(ctx_ptr: ptr CircomCompatCtx,
compress: bool,
proof_bytes_ptr: ptr ptr Buffer,
inputs_bytes_ptr: ptr ptr Buffer): int32 {.importc: "prove_circuit".}
proc release_proof*(proof_ptr: ptr ptr Proof): void {.importc: "release_proof".}
## # Safety
#
proc verify_circuit*(ctx_ptr: ptr CircomCompatCtx,
compress: bool,
proof_bytes_ptr: ptr Buffer,
inputs_bytes_ptr: ptr Buffer): int32 {.importc: "verify_circuit".}
proc release_inputs*(inputs_ptr: ptr ptr Inputs): void {.importc: "release_inputs".}
proc release_key*(key_ptr: ptr ptr VerifyingKey): void {.importc: "release_key".}
proc prove_circuit*(cfg_ptr: ptr CircomBn254Cfg,
ctx_ptr: ptr CircomCompatCtx,
proof_ptr: ptr ptr Proof): int32 {.importc: "prove_circuit".}
proc get_pub_inputs*(ctx_ptr: ptr CircomCompatCtx,
inputs_ptr: ptr ptr Inputs): int32 {.importc: "get_pub_inputs".}
proc get_verifying_key*(cfg_ptr: ptr CircomBn254Cfg,
vk_ptr: ptr ptr VerifyingKey): int32 {.importc: "get_verifying_key".}
proc verify_circuit*(proof: ptr Proof,
inputs: ptr Inputs,
pvk: ptr VerifyingKey): int32 {.importc: "verify_circuit".}
## # Safety
#
proc push_input_u256_array*(ctx_ptr: ptr CircomCompatCtx,
name_ptr: pointer,
input_ptr: pointer,

View File

@ -5,36 +5,42 @@ import ../circomcompat
suite "Test circom compat nim":
test "Should generate witness, prove and verify":
let
r1csPath = "vendor/circom-compat-ffi/fixtures/mycircuit.r1cs".cstring
wasmPath = "vendor/circom-compat-ffi/fixtures/mycircuit.wasm".cstring
r1csPath = "vendor/circom-compat-ffi/fixtures/circom2_multiplier2.r1cs".cstring
wasmPath = "vendor/circom-compat-ffi/fixtures/circom2_multiplier2.wasm".cstring
var ctx: ptr CircomCompatCtx
let res = init_circom_compat(
var cfg_ptr: ptr CircomBn254Cfg
check init_circom_config(
r1csPath,
wasmPath,
nil,
ctx.addr)
cfg_ptr.addr) == ERR_OK
check ctx.push_input_numeric_i8("a".cstring, 3) == ERR_OK
check ctx.push_input_numeric_i8("b".cstring, 11) == ERR_OK
var ctx: ptr CircomCompatCtx
check init_circom_compat(
cfg_ptr,
ctx.addr) == ERR_OK
var proofBytes: ptr Buffer
var publicBytes: ptr Buffer
check ctx.push_input_i8("a".cstring, 3) == ERR_OK
check ctx.push_input_i8("b".cstring, 11) == ERR_OK
check ctx.prove_circuit(proofBytes.addr, publicBytes.addr) == ERR_OK
var proofPtr: ptr Proof
var inputsPtr: ptr Inputs
var vkPtr: ptr VerifyingKey
check proofBytes.len > 0
check publicBytes.len > 0
check ctx.get_pub_inputs(inputsPtr.addr) == ERR_OK
check cfg_ptr.prove_circuit(ctx, proofPtr.addr) == ERR_OK
check ctx.verify_circuit(proofBytes, publicBytes) == ERR_OK
check cfg_ptr.get_verifying_key(vkPtr.addr) == ERR_OK
check verify_circuit(proofPtr, inputsPtr, vkPtr) == ERR_OK
release_proof(proofPtr.addr)
check proofPtr == nil
release_inputs(inputsPtr.addr)
check inputsPtr == nil
release_key(vkPtr.addr)
check vkPtr == nil
ctx.addr.release_circom_compat()
check ctx == nil
proofBytes.addr.release_buffer()
check proofBytes == nil
publicBytes.addr.release_buffer()
check publicBytes == nil
check res == ERR_OK

@ -1 +1 @@
Subproject commit aed402f6ee8a2dc225ec2db2eb36d1888aab7790
Subproject commit b1755a2b2e8d3978925cb5815c1564f62756b29d