From b7ce32cf4b65b773e403b9deeb1b3cb5a4379ddc Mon Sep 17 00:00:00 2001 From: Sanaz Taheri Boshrooyeh <35961250+staheri14@users.noreply.github.com> Date: Thu, 18 Feb 2021 14:59:10 -0800 Subject: [PATCH] Rln relay key generation (#383) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * deletes the newline * adds rln to gitignore Co-authored-by: Oskar Thorén --- .gitignore | 2 + tests/.DS_Store | Bin 0 -> 6148 bytes tests/v2/test_waku_rln_relay.nim | 16 ++++- .../waku_rln_relay/waku_rln_relay_utils.nim | 63 ++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 tests/.DS_Store create mode 100644 waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim diff --git a/.gitignore b/.gitignore index f51063449..79c513c2f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ # Ignore simulation generated metrics files /metrics/prometheus /metrics/waku-sim-all-nodes-grafana-dashboard.json + +rln diff --git a/tests/.DS_Store b/tests/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..12199e22e15615ace40004a7d7ccadfb39531530 GIT binary patch literal 6148 zcmeHK&2AGh5FV$2Izfm%R0>C5kT^tDk^)s#A+16=^oF#E9@=WVOCqw;b{%CmjUWW= zW0XhWO?U$y2R{F#wB1DFgb?*aB_6k0!^e^(HDtv8}(AGQFxt@w;R=3%#vZ zGAp7a8%}g^G8n?;?}50@J~HgEvC2HJ|V8v_-G`e9;#3-Q7-GJllD( zSTuO+N%!U9+4;v$^UsSfmwFe7;Wwz|k;Nr^MMdX-5lpgFW*^XA`YwGOItCmAcZ&gg z2Ji=WD{YspW56-+2Mnn5K|mGy7E6Qr=s=;L0KhuTT2NPS2^AwO`W8!r=z%a33N)e2 zJ~5aHhh11d-(qRdgcGxm4`yd(_JzXq>=<8YcVfOlS2_k912-AiFilV0|F74-|8EAl zmSey%@Lw^&>IcC=A1T?pwUV5=YX$TSszmvf2CqU;(N{5i)m8i$ss(nTDnQ?2X%H5O P`y-%faD`*wuQKopcI}es literal 0 HcmV?d00001 diff --git a/tests/v2/test_waku_rln_relay.nim b/tests/v2/test_waku_rln_relay.nim index 907f26f56..4b3b46b12 100644 --- a/tests/v2/test_waku_rln_relay.nim +++ b/tests/v2/test_waku_rln_relay.nim @@ -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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim b/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim new file mode 100644 index 000000000..f7714d18b --- /dev/null +++ b/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim @@ -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) \ No newline at end of file