Rln relay key generation (#383)

* cleans up imported modules

* adds uploadContract proc and consolidates rln-relay test files

* deletes test_rln_relay_wrappers

* deletes wrappers tests

* adds waku_rln_relay_utils

* adds the unit test for the membership key generation

* adds the key generation procedure

* adds unit test of key gen proc

* removes extra spaces

* Update tests/v2/test_waku_rln_relay.nim

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>

* deletes the newline

* adds rln to gitignore

Co-authored-by: Oskar Thorén <ot@oskarthoren.com>
This commit is contained in:
Sanaz Taheri Boshrooyeh 2021-02-18 14:59:10 -08:00 committed by GitHub
parent 211e891262
commit b7ce32cf4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 2 deletions

2
.gitignore vendored
View File

@ -28,3 +28,5 @@
# Ignore simulation generated metrics files
/metrics/prometheus
/metrics/waku-sim-all-nodes-grafana-dashboard.json
rln

BIN
tests/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -2,7 +2,7 @@ import
chronos, chronicles, options, stint, unittest,
web3,
stew/byteutils,
../../waku/v2/protocol/waku_rln_relay/rln,
../../waku/v2/protocol/waku_rln_relay/[rln, waku_rln_relay_utils],
../test_helpers,
test_utils
@ -206,4 +206,16 @@ suite "Waku rln relay":
check:
# the public and secret keys together are 64 bytes
generatedKeys.len == 64
debug "generated keys: ", generatedKeys
debug "generated keys: ", generatedKeys
test "membership Key Gen":
var key = membershipKeyGen()
var empty : array[32,byte]
check:
key.isSome
key.get().secretKey.len == 32
key.get().publicKey.len == 32
key.get().secretKey != empty
key.get().publicKey != empty
debug "the generated membership key pair: ", key

View File

@ -0,0 +1,63 @@
import
chronicles, options, chronos, stint,
stew/byteutils,
rln
type MembershipKeyPair* = object
secretKey*: array[32, byte]
publicKey*: array[32, byte]
proc membershipKeyGen*(): Option[MembershipKeyPair] =
# generates a MembershipKeyPair that can be used for the registration into the rln membership contract
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 the parameters.key is not empty
if(pbytes.len == 0):
debug "error in parameters.key"
return none(MembershipKeyPair)
# 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 whether the circuit parameters are generated successfully
if(res == false):
debug "error in parameters generation"
return none(MembershipKeyPair)
# keysBufferPtr will hold the generated key pairs i.e., secret and public keys
var
keysBufferPtr : Buffer
done = keyGen(ctx, keysBufferPtr)
# check whether the keys are generated successfully
if(done == false):
debug "error in key generation"
return none(MembershipKeyPair)
var generatedKeys = cast[ptr array[64, byte]](keysBufferPtr.`ptr`)[]
# the public and secret keys together are 64 bytes
if (generatedKeys.len != 64):
debug "the generated keys are invalid"
return none(MembershipKeyPair)
var
secret = cast[array[32, byte]](generatedKeys[0..31])
public = cast[array[32, byte]](generatedKeys[31..^1])
keypair = MembershipKeyPair(secretKey: secret, publicKey: public)
return some(keypair)