add nim ffi and genffi build task

This commit is contained in:
Jaremy Creechley 2023-12-22 19:03:43 -07:00
parent 63188ae1a4
commit d3e667e758
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
5 changed files with 67 additions and 0 deletions

4
build.nims Normal file
View File

@ -0,0 +1,4 @@
task genffi, "update the nim ffi bindings":
exec "cargo install --force nbindgen"
exec "nbindgen --crate codex-storage-proofs --output codex_proofs_ffi.nim"

57
codex_proofs_ffi.nim Normal file
View File

@ -0,0 +1,57 @@
const EXT_ID_U256_BE* = 51
const EXT_ID_U256_LE* = 50
type StorageProofs* {.incompleteStruct.} = object
type Buffer* = object
data: ptr uint8
len: uint
type ProofCtx* = object
proof: Buffer
public_inputs: Buffer
## # Safety
#
# Use on a valid pointer to ProofCtx or panics
proc free_proof_ctx*(ctx: ptr ProofCtx) {.importc: "free_proof_ctx".}
## # Safety
#
# Use on a valid pointer to StorageProofs or panics
proc free_prover*(prover: ptr StorageProofs) {.importc: "free_prover".}
## # Safety
#
# Construct a StorageProofs object
proc init*(r1cs: ptr (ptr Buffer),
wasm: ptr (ptr Buffer),
zkey: ptr (ptr Buffer)): (ptr StorageProofs) {.importc: "init".}
## # Safety
#
# Use after constructing a StorageProofs object with init
proc prove*(prover_ptr: ptr StorageProofs,
chunks: ptr Buffer,
siblings: ptr Buffer,
hashes: ptr Buffer,
path: ptr int32,
path_len: uint,
pubkey: ptr Buffer,
root: ptr Buffer,
salt: ptr Buffer): (ptr ProofCtx) {.importc: "prove".}
## # Safety
#
# Use after constructing a StorageProofs object with init
proc prove_mpack_ext*(prover_ptr: ptr StorageProofs,
args: ptr Buffer): (ptr ProofCtx) {.importc: "prove_mpack_ext".}
## # Safety
#
# Should be called on a valid proof and public inputs previously generated by prove
proc verify*(prover_ptr: ptr StorageProofs,
proof: ptr Buffer,
public_inputs: ptr Buffer): bool {.importc: "verify".}

View File

@ -20,3 +20,5 @@ static:
{.passl: "-lcodex_storage_proofs" & " -L" & libDir.} {.passl: "-lcodex_storage_proofs" & " -L" & libDir.}
proc prove_mpack_ext()

View File

@ -7,3 +7,5 @@ srcDir = "."
# Dependencies # Dependencies
requires "nim >= 1.6.14" requires "nim >= 1.6.14"
include "build.nims"

2
config.nims Normal file
View File

@ -0,0 +1,2 @@
include "build.nims"