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-07-15 18:25:52 +00:00
{. push raises : [ Defect ] . }
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-06-08 18:56:32 +00:00
2021-04-01 00:39:27 +00:00
#------------------------------ Merkle Tree operations -----------------------------------------
2021-08-24 19:25:29 +00:00
proc update_next_member * ( ctx : RLN [ Bn256 ] ,
2021-06-08 18:56:32 +00:00
input_buffer : ptr Buffer ) : bool {. importc : " update_next_member " . }
2022-06-29 20:45:56 +00:00
## input_buffer points to the id commitment byte seq
## the return bool value indicates the success or failure of the operation
2021-02-10 20:51:41 +00:00
2021-08-24 19:25:29 +00:00
proc delete_member * ( ctx : RLN [ Bn256 ] , index : uint ) : bool {. importc : " delete_member " . }
2022-06-29 20:45:56 +00:00
## index is the position of the id commitment key to be deleted from the tree
## the deleted id commitment key is replaced with a zero leaf
## the return bool value indicates the success or failure of the operation
2021-08-24 19:25:29 +00:00
proc get_root * ( ctx : RLN [ Bn256 ] , output_buffer : ptr Buffer ) : bool {. importc : " get_root " . }
2022-06-29 20:45:56 +00:00
## get_root populates the passed pointer output_buffer with the current tree root
## the output_buffer holds the Merkle tree root of size 32 bytes
## the return bool value indicates the success or failure of the operation
##
2021-03-24 17:26:56 +00:00
#----------------------------------------------------------------------------------------------
2021-04-01 00:39:27 +00:00
#-------------------------------- zkSNARKs operations -----------------------------------------
2021-08-24 19:25:29 +00:00
proc key_gen * ( ctx : RLN [ Bn256 ] , keypair_buffer : ptr Buffer ) : bool {. importc : " key_gen " . }
2022-06-29 20:45:56 +00:00
## generates id key and id commitment key serialized inside keypair_buffer as | id_key <32 bytes>| id_commitment_key <32 bytes> |
## id commitment is the poseidon hash of the id key
## the return bool value indicates the success or failure of the operation
2021-04-01 00:39:27 +00:00
2021-08-24 19:25:29 +00:00
proc generate_proof * ( ctx : RLN [ Bn256 ] ,
2021-06-08 18:56:32 +00:00
input_buffer : ptr Buffer ,
output_buffer : ptr Buffer ) : bool {. importc : " generate_proof " . }
2021-10-26 21:42:24 +00:00
## input_buffer serialized as [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
2021-10-20 00:37:29 +00:00
## output_buffer holds the proof data and should be parsed as |proof<256>|root<32>|epoch<32>|share_x<32>|share_y<32>|nullifier<32>|
2022-06-29 20:45:56 +00:00
## integers wrapped in <> indicate value sizes in bytes
## the return bool value indicates the success or failure of the operation
##
2021-08-24 19:25:29 +00:00
proc verify * ( ctx : RLN [ Bn256 ] ,
2021-06-08 18:56:32 +00:00
proof_buffer : ptr Buffer ,
result_ptr : ptr uint32 ) : bool {. importc : " verify " . }
2021-10-26 21:42:24 +00:00
## proof_buffer [ proof<256>| root<32>| epoch<32>| share_x<32>| share_y<32>| nullifier<32> | signal_len<8> | signal<var> ]
2022-06-29 20:45:56 +00:00
## the return bool value indicates the success or failure of the call to the verify function
## the result of the verification of the zk proof is stored in the value pointed by result_ptr, where 0 indicates success and 1 is failure
2021-04-01 00:39:27 +00:00
#----------------------------------------------------------------------------------------------
#-------------------------------- Common procedures -------------------------------------------
proc new_circuit_from_params * ( merkle_depth : uint ,
parameters_buffer : ptr Buffer ,
2021-08-24 19:25:29 +00:00
ctx : ptr RLN [ Bn256 ] ) : bool {. importc : " new_circuit_from_params " . }
2022-06-29 20:45:56 +00:00
## creates an instance of rln object as defined by the rln lib https://github.com/kilic/rln/blob/7ac74183f8b69b399e3bc96c1ae8ab61c026dc43/src/public.rs#L48
## merkle_depth represent the depth of the Merkle tree
## parameters_buffer holds prover and verifier keys
## ctx holds the final created rln object
## the return bool value indicates the success or failure of the operation
2021-08-24 19:25:29 +00:00
proc hash * ( ctx : RLN [ Bn256 ] ,
2021-06-08 18:56:32 +00:00
inputs_buffer : ptr Buffer ,
2021-10-26 21:42:24 +00:00
output_buffer : ptr Buffer ) : bool {. importc : " signal_to_field " . }
2022-06-29 20:45:56 +00:00
## as explained in https://github.com/kilic/rln/blob/7ac74183f8b69b399e3bc96c1ae8ab61c026dc43/src/public.rs#L135, it hashes (sha256) the plain text supplied in inputs_buffer and then maps it to a field element
## this proc is used to map arbitrary signals to field element for the sake of proof generation
## inputs_buffer holds the hash input as a byte seq
## the hash output is generated and populated inside output_buffer
## the output_buffer contains 32 bytes hash output
2021-10-26 21:42:24 +00:00
2021-07-15 18:25:52 +00:00
{. pop . }