2021-02-10 20:51:41 +00:00
|
|
|
# this module contains the Nim wrappers for the rln library https://github.com/kilic/rln/blob/3bbec368a4adc68cd5f9bfae80b17e1bbb4ef373/src/ffi.rs
|
|
|
|
|
2021-06-08 18:56:32 +00:00
|
|
|
import
|
|
|
|
os,
|
|
|
|
waku_rln_relay_types
|
2021-03-02 20:57:48 +00:00
|
|
|
|
|
|
|
const libPath = "vendor/rln/target/debug/"
|
2021-02-12 20:12:10 +00:00
|
|
|
when defined(Windows):
|
|
|
|
const libName* = libPath / "rln.dll"
|
|
|
|
elif defined(Linux):
|
|
|
|
const libName* = libPath / "librln.so"
|
|
|
|
elif defined(MacOsX):
|
|
|
|
const libName* = libPath / "librln.dylib"
|
2021-02-10 20:51:41 +00:00
|
|
|
|
2021-06-08 18:56:32 +00:00
|
|
|
# all the following procedures are Nim wrappers for the functions defined in libName
|
2021-04-01 00:39:27 +00:00
|
|
|
{.push dynlib: libName, raises: [Defect].}
|
2021-02-10 20:51:41 +00:00
|
|
|
|
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
## Buffer struct is taken from
|
|
|
|
# https://github.com/celo-org/celo-threshold-bls-rs/blob/master/crates/threshold-bls-ffi/src/ffi.rs
|
|
|
|
type Buffer* = object
|
|
|
|
`ptr`*: ptr uint8
|
|
|
|
len*: uint
|
2021-02-10 20:51:41 +00:00
|
|
|
|
2021-04-01 00:39:27 +00:00
|
|
|
type Auth* = object
|
|
|
|
secret_buffer*: ptr Buffer
|
|
|
|
index*: uint
|
2021-06-08 18:56:32 +00:00
|
|
|
|
2021-04-01 00:39:27 +00:00
|
|
|
#------------------------------ Merkle Tree operations -----------------------------------------
|
2021-02-10 20:51:41 +00:00
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
proc update_next_member*(ctx: ptr RLN[Bn256],
|
2021-06-08 18:56:32 +00:00
|
|
|
input_buffer: ptr Buffer): bool {.importc: "update_next_member".}
|
2021-02-10 20:51:41 +00:00
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
proc delete_member*(ctx: ptr RLN[Bn256], index: uint): bool {.importc: "delete_member".}
|
2021-02-10 20:51:41 +00:00
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
proc get_root*(ctx: ptr RLN[Bn256], output_buffer: ptr Buffer): bool {.importc: "get_root".}
|
|
|
|
#----------------------------------------------------------------------------------------------
|
2021-04-01 00:39:27 +00:00
|
|
|
#-------------------------------- zkSNARKs operations -----------------------------------------
|
|
|
|
|
|
|
|
proc key_gen*(ctx: ptr RLN[Bn256], keypair_buffer: ptr Buffer): bool {.importc: "key_gen".}
|
|
|
|
|
|
|
|
proc generate_proof*(ctx: ptr RLN[Bn256],
|
2021-06-08 18:56:32 +00:00
|
|
|
input_buffer: ptr Buffer,
|
|
|
|
auth: ptr Auth,
|
|
|
|
output_buffer: ptr Buffer): bool {.importc: "generate_proof".}
|
2021-04-01 00:39:27 +00:00
|
|
|
|
|
|
|
proc verify*(ctx: ptr RLN[Bn256],
|
2021-06-08 18:56:32 +00:00
|
|
|
proof_buffer: ptr Buffer,
|
|
|
|
result_ptr: ptr uint32): bool {.importc: "verify".}
|
2021-04-01 00:39:27 +00:00
|
|
|
#----------------------------------------------------------------------------------------------
|
|
|
|
#-------------------------------- Common procedures -------------------------------------------
|
|
|
|
|
|
|
|
proc new_circuit_from_params*(merkle_depth: uint,
|
|
|
|
parameters_buffer: ptr Buffer,
|
|
|
|
ctx: ptr (ptr RLN[Bn256])): bool {.importc: "new_circuit_from_params".}
|
2021-02-10 20:51:41 +00:00
|
|
|
|
2021-04-01 00:39:27 +00:00
|
|
|
proc hash*(ctx: ptr RLN[Bn256],
|
2021-06-08 18:56:32 +00:00
|
|
|
inputs_buffer: ptr Buffer,
|
|
|
|
input_len: uint,
|
|
|
|
output_buffer: ptr Buffer): bool {.importc: "hash".}
|
2021-04-01 00:39:27 +00:00
|
|
|
{.pop.}
|