mirror of https://github.com/waku-org/nwaku.git
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:
parent
211e891262
commit
b7ce32cf4b
|
@ -28,3 +28,5 @@
|
|||
# Ignore simulation generated metrics files
|
||||
/metrics/prometheus
|
||||
/metrics/waku-sim-all-nodes-grafana-dashboard.json
|
||||
|
||||
rln
|
||||
|
|
Binary file not shown.
|
@ -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
|
||||
|
||||
|
@ -207,3 +207,15 @@ suite "Waku rln relay":
|
|||
# the public and secret keys together are 64 bytes
|
||||
generatedKeys.len == 64
|
||||
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
|
|
@ -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)
|
Loading…
Reference in New Issue