Nim wrappers for Rln lib (Key and Params generation) (#373)

* adds the initial rln.nim wrapper

* adds test_rln_wrapper

* WIP: initial test (not working)

* changes RLNBn256 data type

* updates parameters type

* adds the wrappers and fixes the bugs

* adds tests

* replaces echo with debug
This commit is contained in:
Sanaz Taheri Boshrooyeh 2021-02-10 12:51:41 -08:00 committed by GitHub
parent 1b96ee0adb
commit 0c60e6537f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,50 @@
{.used.}
import
std/[unittest],
stew/byteutils,
chronicles,
../../waku/v2/protocol/waku_rln_relay/rln
suite "Waku rln relay":
test "rln lib: Keygen Nim Wrappers":
var
merkleDepth: csize_t = 32
# parameters.key contains the parameters related to the Poseidon hasher
# to generate this file, clone this repo https://github.com/kilic/rln
# and run the following command in the root directory of the cloned project
# cargo run --example export_test_keys
# the file is generated separately and copied here
parameters = readFile("waku/v2/protocol/waku_rln_relay/parameters.key")
pbytes = parameters.toBytes()
len : csize_t = uint(pbytes.len)
parametersBuffer = Buffer(`ptr`: unsafeAddr(pbytes[0]), len: len)
check:
# check the parameters.key is not empty
pbytes.len != 0
# ctx holds the information that is going to be used for the key generation
var
obj = RLNBn256()
objPtr = unsafeAddr(obj)
ctx = objPtr
let res = newCircuitFromParams(merkleDepth, unsafeAddr parametersBuffer, ctx)
check:
# check whether the circuit parameters are generated successfully
res == true
# keysBufferPtr will hold the generated key pairs i.e., secret and public keys
var
keysBufferPtr : Buffer
done = keyGen(ctx, keysBufferPtr)
check:
# check whether the keys are generated successfully
done == true
if done:
var generatedKeys = cast[ptr array[64, byte]](keysBufferPtr.`ptr`)[]
check:
# the public and secret keys together are 64 bytes
generatedKeys.len == 64
debug "generated keys: ", generatedKeys

View File

Binary file not shown.

View File

@ -0,0 +1,68 @@
# this module contains the Nim wrappers for the rln library https://github.com/kilic/rln/blob/3bbec368a4adc68cd5f9bfae80b17e1bbb4ef373/src/ffi.rs
import stew/byteutils, os
from strutils import rsplit
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
# librln.dylib is the binary executable of rln library (originally implemented in rust with an exposed C API)
# contains the key generation and other relevant functions
# to generate librln.dylib, clone this repo https://github.com/kilic/rln
# and run the following command in the root directory of the cloned project
# cargo build
# can find the .dylib file under the target/debug directory
# this file is already generated and copied here
const libName* = sourceDir / "librln.dylib" # TODO may need to load different libs based on OS
# Data types -----------------------------
# pub struct Buffer {
# pub ptr: *const u8,
# pub len: usize,
# }
type
Buffer* = object
`ptr`*: pointer
len*: csize_t
RLNBn256* = object
# Procedures ------------------------------
# all the following procedures are Nim wrappers for the functions defined in libName
{.push dynlib: libName.}
# pub extern "C" fn new_circuit_from_params(
# merkle_depth: usize,
# index: usize,
# parameters_buffer: *const Buffer,
# ctx: *mut *mut RLN<Bn256>,
# ) -> bool
proc newCircuitFromParams*(merkle_depth: csize_t, parameters_buffer: ptr Buffer, ctx: var ptr RLNBn256): bool{.importc: "new_circuit_from_params".}
# pub extern "C" fn key_gen(ctx: *const RLN<Bn256>, keypair_buffer: *mut Buffer) -> bool
proc keyGen*(ctx: ptr RLNBn256, keypair_buffer: var Buffer): bool {.importc: "key_gen".}
# pub extern "C" fn hash(
# ctx: *const RLN<Bn256>,
# inputs_buffer: *const Buffer,
# input_len: *const usize,
# output_buffer: *mut Buffer,
# ) -> bool
proc hash*(ctx: ptr RLNBn256, inputs_buffer:ptr Buffer, input_len: ptr csize_t, output_buffer: ptr Buffer ) {.importc: "hash".} #TODO not tested yet
# pub extern "C" fn verify(
# ctx: *const RLN<Bn256>,
# proof_buffer: *const Buffer,
# public_inputs_buffer: *const Buffer,
# result_ptr: *mut u32,
# ) -> bool
# pub extern "C" fn generate_proof(
# ctx: *const RLN<Bn256>,
# input_buffer: *const Buffer,
# output_buffer: *mut Buffer,
# ) -> bool
{.pop.}