2021-06-08 18:56:32 +00:00
|
|
|
|
2021-03-23 08:04:51 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-01-07 20:34:24 +00:00
|
|
|
import
|
2022-09-27 04:40:04 +00:00
|
|
|
std/options, sequtils, times, deques,
|
2022-06-28 23:59:38 +00:00
|
|
|
testutils/unittests, chronos, chronicles, stint,
|
2021-02-22 17:40:02 +00:00
|
|
|
stew/byteutils, stew/shims/net as stewNet,
|
|
|
|
libp2p/crypto/crypto,
|
2022-08-05 10:48:01 +00:00
|
|
|
json,
|
2022-09-06 22:18:56 +00:00
|
|
|
../../waku/v2/protocol/waku_message,
|
2022-09-30 12:43:42 +00:00
|
|
|
../../waku/v2/protocol/waku_rln_relay/[rln,
|
|
|
|
waku_rln_relay_utils,
|
|
|
|
waku_rln_relay_types,
|
|
|
|
waku_rln_relay_constants,
|
|
|
|
waku_rln_relay_metrics],
|
2021-02-22 17:40:02 +00:00
|
|
|
../../waku/v2/node/wakunode2,
|
2022-06-28 23:59:38 +00:00
|
|
|
../test_helpers
|
2021-02-17 21:54:49 +00:00
|
|
|
|
2022-09-30 12:43:42 +00:00
|
|
|
const RlnRelayPubsubTopic = "waku/2/rlnrelay/proto"
|
|
|
|
const RlnRelayContentTopic = "waku/2/rlnrelay/proto"
|
2021-09-24 17:32:45 +00:00
|
|
|
|
2021-02-17 21:54:49 +00:00
|
|
|
procSuite "Waku rln relay":
|
2021-09-17 17:31:25 +00:00
|
|
|
asyncTest "mount waku-rln-relay in the off-chain mode":
|
|
|
|
let
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
node = WakuNode.new(nodeKey, ValidIpAddress.init("0.0.0.0"),
|
|
|
|
Port(60000))
|
|
|
|
await node.start()
|
|
|
|
|
|
|
|
# preparing inputs to mount rln-relay
|
|
|
|
|
|
|
|
# create a group of 100 membership keys
|
|
|
|
let
|
|
|
|
(groupKeys, root) = createMembershipList(100)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
groupKeys.len == 100
|
|
|
|
let
|
2021-09-17 17:31:25 +00:00
|
|
|
# convert the keys to MembershipKeyPair structs
|
|
|
|
groupKeyPairs = groupKeys.toMembershipKeyPairs()
|
|
|
|
# extract the id commitments
|
|
|
|
groupIDCommitments = groupKeyPairs.mapIt(it.idCommitment)
|
|
|
|
debug "groupKeyPairs", groupKeyPairs
|
|
|
|
debug "groupIDCommitments", groupIDCommitments
|
2022-05-10 21:09:18 +00:00
|
|
|
|
|
|
|
# index indicates the position of a membership key pair in the static list of group keys i.e., groupKeyPairs
|
2021-09-17 17:31:25 +00:00
|
|
|
# the corresponding key pair will be used to mount rlnRelay on the current node
|
2022-05-10 21:09:18 +00:00
|
|
|
# index also represents the index of the leaf in the Merkle tree that contains node's commitment key
|
2021-09-24 17:32:45 +00:00
|
|
|
let index = MembershipIndex(5)
|
2021-09-17 17:31:25 +00:00
|
|
|
|
|
|
|
# -------- mount rln-relay in the off-chain mode
|
2022-09-30 12:43:42 +00:00
|
|
|
await node.mountRelay(@[RlnRelayPubsubTopic])
|
2022-06-17 22:00:19 +00:00
|
|
|
node.mountRlnRelayStatic(group = groupIDCommitments,
|
|
|
|
memKeyPair = groupKeyPairs[index],
|
|
|
|
memIndex = index,
|
2022-09-30 12:43:42 +00:00
|
|
|
pubsubTopic = RlnRelayPubsubTopic,
|
|
|
|
contentTopic = RlnRelayContentTopic)
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-09-17 17:31:25 +00:00
|
|
|
# get the root of Merkle tree which is constructed inside the mountRlnRelay proc
|
|
|
|
let calculatedRoot = node.wakuRlnRelay.rlnInstance.getMerkleRoot().value().toHex
|
|
|
|
debug "calculated root by mountRlnRelay", calculatedRoot
|
|
|
|
|
|
|
|
# this part checks whether the Merkle tree is constructed correctly inside the mountRlnRelay proc
|
2022-05-10 21:09:18 +00:00
|
|
|
# this check is done by comparing the tree root resulted from mountRlnRelay i.e., calculatedRoot
|
2021-09-17 17:31:25 +00:00
|
|
|
# against the root which is the expected root
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
calculatedRoot == root
|
2021-09-17 17:31:25 +00:00
|
|
|
|
|
|
|
await node.stop()
|
2021-03-24 17:26:56 +00:00
|
|
|
|
2021-02-17 21:54:49 +00:00
|
|
|
suite "Waku rln relay":
|
|
|
|
|
2022-08-05 20:58:19 +00:00
|
|
|
when defined(rln) or (not defined(rln) and not defined(rlnzerokit)):
|
|
|
|
test "key_gen 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`: addr(pbytes[0]), len: len)
|
|
|
|
check:
|
|
|
|
# check the parameters.key is not empty
|
|
|
|
pbytes.len != 0
|
2021-02-17 21:54:49 +00:00
|
|
|
|
2022-08-05 20:58:19 +00:00
|
|
|
var
|
|
|
|
rlnInstance: RLN[Bn256]
|
|
|
|
let res = new_circuit_from_params(merkleDepth, addr parametersBuffer,
|
|
|
|
addr rlnInstance)
|
2021-02-17 21:54:49 +00:00
|
|
|
check:
|
2022-08-05 20:58:19 +00:00
|
|
|
# check whether the circuit parameters are generated successfully
|
|
|
|
res == true
|
|
|
|
|
|
|
|
# keysBufferPtr will hold the generated key pairs i.e., secret and public keys
|
|
|
|
var
|
|
|
|
keysBuffer: Buffer
|
|
|
|
keysBufferPtr = addr(keysBuffer)
|
|
|
|
done = key_gen(rlnInstance, 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
|
|
|
|
|
|
|
|
when defined(rlnzerokit):
|
|
|
|
test "key_gen Nim Wrappers":
|
|
|
|
var
|
|
|
|
merkleDepth: csize_t = 20
|
|
|
|
|
|
|
|
var rlnInstance = createRLNInstance()
|
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
|
|
|
|
# keysBufferPtr will hold the generated key pairs i.e., secret and public keys
|
|
|
|
var
|
|
|
|
keysBuffer: Buffer
|
|
|
|
keysBufferPtr = addr(keysBuffer)
|
|
|
|
done = key_gen(rlnInstance.value(), 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
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-02-18 22:59:10 +00:00
|
|
|
test "membership Key Gen":
|
2021-03-24 17:26:56 +00:00
|
|
|
# create an RLN instance
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-24 19:25:29 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
2021-03-24 17:26:56 +00:00
|
|
|
|
2021-08-24 19:25:29 +00:00
|
|
|
var key = membershipKeyGen(rlnInstance.value)
|
2022-05-10 21:09:18 +00:00
|
|
|
var empty: array[32, byte]
|
2021-02-18 22:59:10 +00:00
|
|
|
check:
|
|
|
|
key.isSome
|
2021-08-26 23:14:51 +00:00
|
|
|
key.get().idKey.len == 32
|
|
|
|
key.get().idCommitment.len == 32
|
|
|
|
key.get().idKey != empty
|
|
|
|
key.get().idCommitment != empty
|
2022-05-10 21:09:18 +00:00
|
|
|
|
|
|
|
debug "the generated membership key pair: ", key
|
2021-06-08 18:56:32 +00:00
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
test "get_root Nim binding":
|
|
|
|
# create an RLN instance which also includes an empty Merkle tree
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-24 19:25:29 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
2021-03-24 17:26:56 +00:00
|
|
|
|
|
|
|
# read the Merkle Tree root
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
|
|
|
root1 {.noinit.}: Buffer = Buffer()
|
2021-03-24 17:26:56 +00:00
|
|
|
rootPtr1 = addr(root1)
|
2021-08-24 19:25:29 +00:00
|
|
|
get_root_successful1 = get_root(rlnInstance.value, rootPtr1)
|
2021-10-26 21:42:24 +00:00
|
|
|
check:
|
|
|
|
get_root_successful1
|
|
|
|
root1.len == 32
|
2021-03-24 17:26:56 +00:00
|
|
|
|
|
|
|
# read the Merkle Tree root
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
|
|
|
root2 {.noinit.}: Buffer = Buffer()
|
2021-03-24 17:26:56 +00:00
|
|
|
rootPtr2 = addr(root2)
|
2021-08-24 19:25:29 +00:00
|
|
|
get_root_successful2 = get_root(rlnInstance.value, rootPtr2)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
2021-10-26 21:42:24 +00:00
|
|
|
get_root_successful2
|
|
|
|
root2.len == 32
|
2021-03-24 17:26:56 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
var rootValue1 = cast[ptr array[32, byte]] (root1.`ptr`)
|
2021-03-24 17:26:56 +00:00
|
|
|
let rootHex1 = rootValue1[].toHex
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
var rootValue2 = cast[ptr array[32, byte]] (root2.`ptr`)
|
2021-03-24 17:26:56 +00:00
|
|
|
let rootHex2 = rootValue2[].toHex
|
|
|
|
|
2021-08-26 23:14:51 +00:00
|
|
|
# the two roots must be identical
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
rootHex1 == rootHex2
|
2021-08-26 23:14:51 +00:00
|
|
|
test "getMerkleRoot utils":
|
|
|
|
# create an RLN instance which also includes an empty Merkle tree
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-26 23:14:51 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
|
|
|
|
# read the Merkle Tree root
|
|
|
|
var root1 = getMerkleRoot(rlnInstance.value())
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
root1.isOk
|
2021-08-26 23:14:51 +00:00
|
|
|
let rootHex1 = root1.value().toHex
|
|
|
|
|
|
|
|
# read the Merkle Tree root
|
|
|
|
var root2 = getMerkleRoot(rlnInstance.value())
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
root2.isOk
|
2021-08-26 23:14:51 +00:00
|
|
|
let rootHex2 = root2.value().toHex
|
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
# the two roots must be identical
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
rootHex1 == rootHex2
|
2021-03-24 17:26:56 +00:00
|
|
|
|
|
|
|
test "update_next_member Nim Wrapper":
|
|
|
|
# create an RLN instance which also includes an empty Merkle tree
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-24 19:25:29 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
2021-03-24 17:26:56 +00:00
|
|
|
|
|
|
|
# generate a key pair
|
2021-08-24 19:25:29 +00:00
|
|
|
var keypair = membershipKeyGen(rlnInstance.value)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
keypair.isSome()
|
2021-10-26 21:42:24 +00:00
|
|
|
var pkBuffer = toBuffer(keypair.get().idCommitment)
|
2021-03-24 17:26:56 +00:00
|
|
|
let pkBufferPtr = addr pkBuffer
|
|
|
|
|
|
|
|
# add the member to the tree
|
2021-08-24 19:25:29 +00:00
|
|
|
var member_is_added = update_next_member(rlnInstance.value, pkBufferPtr)
|
2021-03-24 17:26:56 +00:00
|
|
|
check:
|
|
|
|
member_is_added == true
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
test "delete_member Nim wrapper":
|
|
|
|
# create an RLN instance which also includes an empty Merkle tree
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-24 19:25:29 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
2021-03-24 17:26:56 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
# delete the first member
|
2021-09-24 17:32:45 +00:00
|
|
|
var deleted_member_index = MembershipIndex(0)
|
2021-08-24 19:25:29 +00:00
|
|
|
let deletion_success = delete_member(rlnInstance.value, deleted_member_index)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
deletion_success
|
2021-06-08 18:56:32 +00:00
|
|
|
|
2021-08-26 23:14:51 +00:00
|
|
|
test "insertMember rln utils":
|
|
|
|
# create an RLN instance which also includes an empty Merkle tree
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-26 23:14:51 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
var rln = rlnInstance.value
|
|
|
|
# generate a key pair
|
|
|
|
var keypair = rln.membershipKeyGen()
|
|
|
|
check:
|
2022-05-10 21:09:18 +00:00
|
|
|
keypair.isSome()
|
|
|
|
check:
|
|
|
|
rln.insertMember(keypair.get().idCommitment)
|
|
|
|
|
2021-08-26 23:14:51 +00:00
|
|
|
test "removeMember rln utils":
|
|
|
|
# create an RLN instance which also includes an empty Merkle tree
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-26 23:14:51 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
var rln = rlnInstance.value
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
2021-09-24 17:32:45 +00:00
|
|
|
rln.removeMember(MembershipIndex(0))
|
2021-08-26 23:14:51 +00:00
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
test "Merkle tree consistency check between deletion and insertion":
|
|
|
|
# create an RLN instance
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-24 19:25:29 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
2021-03-24 17:26:56 +00:00
|
|
|
|
|
|
|
# read the Merkle Tree root
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
|
|
|
root1 {.noinit.}: Buffer = Buffer()
|
2021-03-24 17:26:56 +00:00
|
|
|
rootPtr1 = addr(root1)
|
2021-08-24 19:25:29 +00:00
|
|
|
get_root_successful1 = get_root(rlnInstance.value, rootPtr1)
|
2021-10-26 21:42:24 +00:00
|
|
|
check:
|
|
|
|
get_root_successful1
|
|
|
|
root1.len == 32
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-03-24 17:26:56 +00:00
|
|
|
# generate a key pair
|
2021-08-24 19:25:29 +00:00
|
|
|
var keypair = membershipKeyGen(rlnInstance.value)
|
2021-10-26 21:42:24 +00:00
|
|
|
check: keypair.isSome()
|
|
|
|
var pkBuffer = toBuffer(keypair.get().idCommitment)
|
2021-03-24 17:26:56 +00:00
|
|
|
let pkBufferPtr = addr pkBuffer
|
|
|
|
|
|
|
|
# add the member to the tree
|
2021-08-24 19:25:29 +00:00
|
|
|
var member_is_added = update_next_member(rlnInstance.value, pkBufferPtr)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
member_is_added
|
2021-03-24 17:26:56 +00:00
|
|
|
|
|
|
|
# read the Merkle Tree root after insertion
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
|
|
|
root2 {.noinit.}: Buffer = Buffer()
|
2021-03-24 17:26:56 +00:00
|
|
|
rootPtr2 = addr(root2)
|
2021-08-24 19:25:29 +00:00
|
|
|
get_root_successful2 = get_root(rlnInstance.value, rootPtr2)
|
2021-10-26 21:42:24 +00:00
|
|
|
check:
|
|
|
|
get_root_successful2
|
|
|
|
root2.len == 32
|
2021-03-24 17:26:56 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
# delete the first member
|
2021-09-24 17:32:45 +00:00
|
|
|
var deleted_member_index = MembershipIndex(0)
|
2021-08-24 19:25:29 +00:00
|
|
|
let deletion_success = delete_member(rlnInstance.value, deleted_member_index)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
deletion_success
|
2021-03-24 17:26:56 +00:00
|
|
|
|
|
|
|
# read the Merkle Tree root after the deletion
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
|
|
|
root3 {.noinit.}: Buffer = Buffer()
|
2021-03-24 17:26:56 +00:00
|
|
|
rootPtr3 = addr(root3)
|
2021-08-24 19:25:29 +00:00
|
|
|
get_root_successful3 = get_root(rlnInstance.value, rootPtr3)
|
2021-10-26 21:42:24 +00:00
|
|
|
check:
|
|
|
|
get_root_successful3
|
|
|
|
root3.len == 32
|
2021-03-24 17:26:56 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
var rootValue1 = cast[ptr array[32, byte]] (root1.`ptr`)
|
2021-03-24 17:26:56 +00:00
|
|
|
let rootHex1 = rootValue1[].toHex
|
|
|
|
debug "The initial root", rootHex1
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
var rootValue2 = cast[ptr array[32, byte]] (root2.`ptr`)
|
2021-03-24 17:26:56 +00:00
|
|
|
let rootHex2 = rootValue2[].toHex
|
|
|
|
debug "The root after insertion", rootHex2
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
var rootValue3 = cast[ptr array[32, byte]] (root3.`ptr`)
|
2021-03-24 17:26:56 +00:00
|
|
|
let rootHex3 = rootValue3[].toHex
|
|
|
|
debug "The root after deletion", rootHex3
|
|
|
|
|
|
|
|
# the root must change after the insertion
|
2021-10-26 21:42:24 +00:00
|
|
|
check: not(rootHex1 == rootHex2)
|
2021-03-24 17:26:56 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
## The initial root of the tree (empty tree) must be identical to
|
2021-03-24 17:26:56 +00:00
|
|
|
## the root of the tree after one insertion followed by a deletion
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
rootHex1 == rootHex3
|
2021-08-26 23:14:51 +00:00
|
|
|
test "Merkle tree consistency check between deletion and insertion using rln utils":
|
|
|
|
# create an RLN instance
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-26 23:14:51 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
var rln = rlnInstance.value()
|
|
|
|
|
|
|
|
# read the Merkle Tree root
|
|
|
|
var root1 = rln.getMerkleRoot()
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
root1.isOk
|
2021-08-26 23:14:51 +00:00
|
|
|
let rootHex1 = root1.value().toHex()
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-08-26 23:14:51 +00:00
|
|
|
# generate a key pair
|
|
|
|
var keypair = rln.membershipKeyGen()
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
keypair.isSome()
|
|
|
|
let member_inserted = rln.insertMember(keypair.get().idCommitment)
|
|
|
|
check:
|
|
|
|
member_inserted
|
2021-08-26 23:14:51 +00:00
|
|
|
|
|
|
|
# read the Merkle Tree root after insertion
|
|
|
|
var root2 = rln.getMerkleRoot()
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
root2.isOk
|
2021-08-26 23:14:51 +00:00
|
|
|
let rootHex2 = root2.value().toHex()
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
|
|
|
|
# delete the first member
|
2021-09-24 17:32:45 +00:00
|
|
|
var deleted_member_index = MembershipIndex(0)
|
2021-08-26 23:14:51 +00:00
|
|
|
let deletion_success = rln.removeMember(deleted_member_index)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
deletion_success
|
2021-08-26 23:14:51 +00:00
|
|
|
|
|
|
|
# read the Merkle Tree root after the deletion
|
|
|
|
var root3 = rln.getMerkleRoot()
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
root3.isOk
|
2021-08-26 23:14:51 +00:00
|
|
|
let rootHex3 = root3.value().toHex()
|
|
|
|
|
|
|
|
|
|
|
|
debug "The initial root", rootHex1
|
|
|
|
debug "The root after insertion", rootHex2
|
|
|
|
debug "The root after deletion", rootHex3
|
|
|
|
|
|
|
|
# the root must change after the insertion
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
not(rootHex1 == rootHex2)
|
2021-08-26 23:14:51 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
## The initial root of the tree (empty tree) must be identical to
|
2021-08-26 23:14:51 +00:00
|
|
|
## the root of the tree after one insertion followed by a deletion
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
rootHex1 == rootHex3
|
2021-08-26 23:14:51 +00:00
|
|
|
|
2021-04-01 00:39:27 +00:00
|
|
|
test "hash Nim Wrappers":
|
|
|
|
# create an RLN instance
|
2021-09-17 17:31:25 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
2021-08-24 19:25:29 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-04-01 00:39:27 +00:00
|
|
|
# prepare the input
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
2021-10-26 21:42:24 +00:00
|
|
|
msg = "Hello".toBytes()
|
|
|
|
hashInput = appendLength(msg)
|
2022-05-10 21:09:18 +00:00
|
|
|
hashInputBuffer = toBuffer(hashInput)
|
2021-04-01 00:39:27 +00:00
|
|
|
|
|
|
|
# prepare other inputs to the hash function
|
2021-10-26 21:42:24 +00:00
|
|
|
var outputBuffer: Buffer
|
2022-05-10 21:09:18 +00:00
|
|
|
|
|
|
|
let hashSuccess = hash(rlnInstance.value, addr hashInputBuffer,
|
|
|
|
addr outputBuffer)
|
|
|
|
check:
|
|
|
|
hashSuccess
|
|
|
|
let outputArr = cast[ptr array[32, byte]](outputBuffer.`ptr`)[]
|
2022-08-05 20:58:19 +00:00
|
|
|
|
|
|
|
when defined(rln) or (not defined(rln) and not defined(rlnzerokit)):
|
|
|
|
check:
|
|
|
|
"efb8ac39dc22eaf377fe85b405b99ba78dbc2f3f32494add4501741df946bd1d" ==
|
|
|
|
outputArr.toHex()
|
|
|
|
when defined(rlnzerokit):
|
|
|
|
check:
|
|
|
|
"4c6ea217404bd5f10e243bac29dc4f1ec36bf4a41caba7b4c8075c54abb3321e" ==
|
2022-05-10 21:09:18 +00:00
|
|
|
outputArr.toHex()
|
2021-04-01 00:39:27 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
|
|
|
hashOutput = cast[ptr array[32, byte]] (outputBuffer.`ptr`)[]
|
2021-04-01 00:39:27 +00:00
|
|
|
hashOutputHex = hashOutput.toHex()
|
|
|
|
|
|
|
|
debug "hash output", hashOutputHex
|
|
|
|
|
2021-10-20 00:37:29 +00:00
|
|
|
test "hash utils":
|
|
|
|
# create an RLN instance
|
|
|
|
var rlnInstance = createRLNInstance()
|
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
let rln = rlnInstance.value
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-10-20 00:37:29 +00:00
|
|
|
# prepare the input
|
2021-10-26 21:42:24 +00:00
|
|
|
let msg = "Hello".toBytes()
|
2021-04-01 00:39:27 +00:00
|
|
|
|
2021-10-26 21:42:24 +00:00
|
|
|
let hash = rln.hash(msg)
|
2022-08-05 20:58:19 +00:00
|
|
|
|
|
|
|
when defined(rln) or (not defined(rln) and not defined(rlnzerokit)):
|
|
|
|
check:
|
|
|
|
"efb8ac39dc22eaf377fe85b405b99ba78dbc2f3f32494add4501741df946bd1d" ==
|
|
|
|
hash.toHex()
|
|
|
|
when defined(rlnzerokit):
|
|
|
|
check:
|
|
|
|
"4c6ea217404bd5f10e243bac29dc4f1ec36bf4a41caba7b4c8075c54abb3321e" ==
|
2022-05-10 21:09:18 +00:00
|
|
|
hash.toHex()
|
|
|
|
|
2021-09-17 17:31:25 +00:00
|
|
|
test "create a list of membership keys and construct a Merkle tree based on the list":
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
2021-09-17 17:31:25 +00:00
|
|
|
groupSize = 100
|
2022-05-10 21:09:18 +00:00
|
|
|
(list, root) = createMembershipList(groupSize)
|
2021-09-17 17:31:25 +00:00
|
|
|
|
|
|
|
debug "created membership key list", list
|
|
|
|
debug "the Merkle tree root", root
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-09-17 17:31:25 +00:00
|
|
|
check:
|
2022-05-10 21:09:18 +00:00
|
|
|
list.len == groupSize # check the number of keys
|
2022-09-30 12:43:42 +00:00
|
|
|
root.len == HashHexSize # check the size of the calculated tree root
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-09-17 17:31:25 +00:00
|
|
|
test "check correctness of toMembershipKeyPairs and calcMerkleRoot":
|
2022-09-30 12:43:42 +00:00
|
|
|
let groupKeys = StaticGroupKeys
|
2021-09-17 17:31:25 +00:00
|
|
|
|
|
|
|
# create a set of MembershipKeyPair objects from groupKeys
|
|
|
|
let groupKeyPairs = groupKeys.toMembershipKeyPairs()
|
|
|
|
# extract the id commitments
|
|
|
|
let groupIDCommitments = groupKeyPairs.mapIt(it.idCommitment)
|
|
|
|
# calculate the Merkle tree root out of the extracted id commitments
|
|
|
|
let root = calcMerkleRoot(groupIDCommitments)
|
|
|
|
|
|
|
|
debug "groupKeyPairs", groupKeyPairs
|
|
|
|
debug "groupIDCommitments", groupIDCommitments
|
|
|
|
debug "root", root
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
2021-09-17 17:31:25 +00:00
|
|
|
# check that the correct number of key pairs is created
|
|
|
|
groupKeyPairs.len == StaticGroupSize
|
|
|
|
# compare the calculated root against the correct root
|
2022-09-30 12:43:42 +00:00
|
|
|
root == StaticGroupMerkleRoot
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2022-08-05 20:58:19 +00:00
|
|
|
when defined(rln) or (not defined(rln) and not defined(rlnzerokit)):
|
|
|
|
test "RateLimitProof Protobuf encode/init test":
|
|
|
|
var
|
|
|
|
proof: ZKSNARK
|
|
|
|
merkleRoot: MerkleNode
|
|
|
|
epoch: Epoch
|
|
|
|
shareX: MerkleNode
|
|
|
|
shareY: MerkleNode
|
|
|
|
nullifier: Nullifier
|
|
|
|
# populate fields with dummy values
|
|
|
|
for x in proof.mitems: x = 1
|
|
|
|
for x in merkleRoot.mitems: x = 2
|
|
|
|
for x in epoch.mitems: x = 3
|
|
|
|
for x in shareX.mitems: x = 4
|
|
|
|
for x in shareY.mitems: x = 5
|
|
|
|
for x in nullifier.mitems: x = 6
|
|
|
|
|
|
|
|
let
|
|
|
|
rateLimitProof = RateLimitProof(proof: proof,
|
|
|
|
merkleRoot: merkleRoot,
|
|
|
|
epoch: epoch,
|
|
|
|
shareX: shareX,
|
|
|
|
shareY: shareY,
|
|
|
|
nullifier: nullifier)
|
|
|
|
protobuf = rateLimitProof.encode()
|
|
|
|
decodednsp = RateLimitProof.init(protobuf.buffer)
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2022-08-05 20:58:19 +00:00
|
|
|
check:
|
|
|
|
decodednsp.isErr == false
|
|
|
|
decodednsp.value == rateLimitProof
|
|
|
|
|
|
|
|
when defined(rlnzerokit):
|
|
|
|
test "RateLimitProof Protobuf encode/init test":
|
|
|
|
var
|
|
|
|
proof: ZKSNARK
|
|
|
|
merkleRoot: MerkleNode
|
|
|
|
epoch: Epoch
|
|
|
|
shareX: MerkleNode
|
|
|
|
shareY: MerkleNode
|
|
|
|
nullifier: Nullifier
|
|
|
|
rlnIdentifier: RlnIdentifier
|
|
|
|
|
|
|
|
# populate fields with dummy values
|
|
|
|
for x in proof.mitems: x = 1
|
|
|
|
for x in merkleRoot.mitems: x = 2
|
|
|
|
for x in epoch.mitems: x = 3
|
|
|
|
for x in shareX.mitems: x = 4
|
|
|
|
for x in shareY.mitems: x = 5
|
|
|
|
for x in nullifier.mitems: x = 6
|
|
|
|
for x in rlnIdentifier.mitems: x = 7
|
|
|
|
|
|
|
|
let
|
|
|
|
rateLimitProof = RateLimitProof(proof: proof,
|
|
|
|
merkleRoot: merkleRoot,
|
|
|
|
epoch: epoch,
|
|
|
|
shareX: shareX,
|
|
|
|
shareY: shareY,
|
|
|
|
nullifier: nullifier,
|
|
|
|
rlnIdentifier: rlnIdentifier)
|
|
|
|
protobuf = rateLimitProof.encode()
|
|
|
|
decodednsp = RateLimitProof.init(protobuf.buffer)
|
2021-10-20 00:37:29 +00:00
|
|
|
|
2022-08-05 20:58:19 +00:00
|
|
|
check:
|
|
|
|
decodednsp.isErr == false
|
|
|
|
decodednsp.value == rateLimitProof
|
2021-10-20 00:37:29 +00:00
|
|
|
|
|
|
|
test "test proofVerify and proofGen for a valid proof":
|
|
|
|
var rlnInstance = createRLNInstance()
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
rlnInstance.isOk
|
2021-10-20 00:37:29 +00:00
|
|
|
var rln = rlnInstance.value
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
2021-10-20 00:37:29 +00:00
|
|
|
# create a membership key pair
|
|
|
|
memKeys = membershipKeyGen(rln).get()
|
|
|
|
# peer's index in the Merkle Tree
|
|
|
|
index = 5
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
# Create a Merkle tree with random members
|
2021-10-20 00:37:29 +00:00
|
|
|
for i in 0..10:
|
|
|
|
var member_is_added: bool = false
|
|
|
|
if (i == index):
|
|
|
|
# insert the current peer's pk
|
|
|
|
member_is_added = rln.insertMember(memKeys.idCommitment)
|
|
|
|
else:
|
|
|
|
# create a new key pair
|
|
|
|
let memberKeys = rln.membershipKeyGen()
|
|
|
|
member_is_added = rln.insertMember(memberKeys.get().idCommitment)
|
|
|
|
# check the member is added
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
member_is_added
|
2021-10-20 00:37:29 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
# prepare the message
|
2021-10-26 21:42:24 +00:00
|
|
|
let messageBytes = "Hello".toBytes()
|
2021-10-20 00:37:29 +00:00
|
|
|
|
|
|
|
# prepare the epoch
|
2022-05-10 21:09:18 +00:00
|
|
|
var epoch: Epoch
|
|
|
|
debug "epoch", epochHex = epoch.toHex()
|
2021-10-20 00:37:29 +00:00
|
|
|
|
|
|
|
# generate proof
|
2022-05-10 21:09:18 +00:00
|
|
|
let proofRes = rln.proofGen(data = messageBytes,
|
2021-10-26 21:42:24 +00:00
|
|
|
memKeys = memKeys,
|
|
|
|
memIndex = MembershipIndex(index),
|
|
|
|
epoch = epoch)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
proofRes.isOk()
|
2021-10-20 00:37:29 +00:00
|
|
|
let proof = proofRes.value
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-10-20 00:37:29 +00:00
|
|
|
# verify the proof
|
|
|
|
let verified = rln.proofVerify(data = messageBytes,
|
2021-10-26 21:42:24 +00:00
|
|
|
proof = proof)
|
2022-09-20 13:08:05 +00:00
|
|
|
|
|
|
|
# Ensure the proof verification did not error out
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
2022-09-20 13:08:05 +00:00
|
|
|
verified.isOk()
|
|
|
|
verified.value() == true
|
2021-10-20 00:37:29 +00:00
|
|
|
|
|
|
|
test "test proofVerify and proofGen for an invalid proof":
|
|
|
|
var rlnInstance = createRLNInstance()
|
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
var rln = rlnInstance.value
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
2021-10-20 00:37:29 +00:00
|
|
|
# create a membership key pair
|
|
|
|
memKeys = membershipKeyGen(rln).get()
|
|
|
|
# peer's index in the Merkle Tree
|
|
|
|
index = 5
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
# Create a Merkle tree with random members
|
2021-10-20 00:37:29 +00:00
|
|
|
for i in 0..10:
|
|
|
|
var member_is_added: bool = false
|
|
|
|
if (i == index):
|
|
|
|
# insert the current peer's pk
|
|
|
|
member_is_added = rln.insertMember(memKeys.idCommitment)
|
|
|
|
else:
|
|
|
|
# create a new key pair
|
|
|
|
let memberKeys = rln.membershipKeyGen()
|
|
|
|
member_is_added = rln.insertMember(memberKeys.get().idCommitment)
|
|
|
|
# check the member is added
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
member_is_added
|
2021-10-20 00:37:29 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
# prepare the message
|
2021-10-26 21:42:24 +00:00
|
|
|
let messageBytes = "Hello".toBytes()
|
2021-10-20 00:37:29 +00:00
|
|
|
|
|
|
|
# prepare the epoch
|
2022-05-10 21:09:18 +00:00
|
|
|
var epoch: Epoch
|
|
|
|
debug "epoch in bytes", epochHex = epoch.toHex()
|
2021-10-20 00:37:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
let badIndex = 4
|
|
|
|
# generate proof
|
2022-05-10 21:09:18 +00:00
|
|
|
let proofRes = rln.proofGen(data = messageBytes,
|
2021-10-26 21:42:24 +00:00
|
|
|
memKeys = memKeys,
|
|
|
|
memIndex = MembershipIndex(badIndex),
|
|
|
|
epoch = epoch)
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
proofRes.isOk()
|
2021-10-20 00:37:29 +00:00
|
|
|
let proof = proofRes.value
|
|
|
|
|
2022-10-07 19:24:54 +00:00
|
|
|
# verify the proof (should not be verified) against the internal RLN tree root
|
2021-10-20 00:37:29 +00:00
|
|
|
let verified = rln.proofVerify(data = messageBytes,
|
2022-10-07 19:24:54 +00:00
|
|
|
proof = proof,
|
|
|
|
validRoots = @[rln.getMerkleRoot().value()])
|
2022-09-20 13:08:05 +00:00
|
|
|
|
|
|
|
require:
|
|
|
|
verified.isOk()
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
2022-09-20 13:08:05 +00:00
|
|
|
verified.value() == false
|
|
|
|
|
2022-09-27 04:40:04 +00:00
|
|
|
test "validate roots which are part of the acceptable window":
|
2022-09-20 13:08:05 +00:00
|
|
|
# Setup:
|
2022-09-27 04:40:04 +00:00
|
|
|
# This step consists of creating the rln instance and waku-rln-relay,
|
2022-09-20 13:08:05 +00:00
|
|
|
# Inserting members, and creating a valid proof with the merkle root
|
2022-09-27 04:40:04 +00:00
|
|
|
# create an RLN instance
|
2022-09-20 13:08:05 +00:00
|
|
|
var rlnInstance = createRLNInstance()
|
|
|
|
require:
|
2022-09-27 04:40:04 +00:00
|
|
|
rlnInstance.isOk()
|
2022-09-20 13:08:05 +00:00
|
|
|
var rln = rlnInstance.value
|
|
|
|
|
2022-09-27 04:40:04 +00:00
|
|
|
let rlnRelay = WakuRLNRelay(rlnInstance:rln)
|
|
|
|
|
2022-09-20 13:08:05 +00:00
|
|
|
let
|
|
|
|
# create a membership key pair
|
2022-09-27 04:40:04 +00:00
|
|
|
memKeys = membershipKeyGen(rlnRelay.rlnInstance).get()
|
|
|
|
# peer's index in the Merkle Tree.
|
2022-09-20 13:08:05 +00:00
|
|
|
index = 5
|
|
|
|
|
2022-09-27 04:40:04 +00:00
|
|
|
let membershipCount = AcceptableRootWindowSize + 5
|
|
|
|
|
2022-09-20 13:08:05 +00:00
|
|
|
# Create a Merkle tree with random members
|
2022-09-27 04:40:04 +00:00
|
|
|
for i in 0..membershipCount:
|
|
|
|
var memberIsAdded: RlnRelayResult[void]
|
2022-09-20 13:08:05 +00:00
|
|
|
if (i == index):
|
|
|
|
# insert the current peer's pk
|
2022-09-27 04:40:04 +00:00
|
|
|
memberIsAdded = rlnRelay.insertMember(memKeys.idCommitment)
|
2022-09-20 13:08:05 +00:00
|
|
|
else:
|
|
|
|
# create a new key pair
|
2022-09-27 04:40:04 +00:00
|
|
|
let memberKeys = rlnRelay.rlnInstance.membershipKeyGen()
|
|
|
|
memberIsAdded = rlnRelay.insertMember(memberKeys.get().idCommitment)
|
|
|
|
# require that the member is added
|
|
|
|
require:
|
|
|
|
memberIsAdded.isOk()
|
2022-09-20 13:08:05 +00:00
|
|
|
|
|
|
|
# Given:
|
|
|
|
# This step includes constructing a valid message with the latest merkle root
|
|
|
|
# prepare the message
|
|
|
|
let messageBytes = "Hello".toBytes()
|
|
|
|
|
|
|
|
# prepare the epoch
|
|
|
|
var epoch: Epoch
|
|
|
|
debug "epoch in bytes", epochHex = epoch.toHex()
|
|
|
|
|
|
|
|
# generate proof
|
2022-09-27 04:40:04 +00:00
|
|
|
let validProofRes = rlnRelay.rlnInstance.proofGen(data = messageBytes,
|
2022-09-20 13:08:05 +00:00
|
|
|
memKeys = memKeys,
|
|
|
|
memIndex = MembershipIndex(index),
|
|
|
|
epoch = epoch)
|
|
|
|
require:
|
|
|
|
validProofRes.isOk()
|
|
|
|
let validProof = validProofRes.value
|
|
|
|
|
|
|
|
# validate the root (should be true)
|
2022-09-27 04:40:04 +00:00
|
|
|
let verified = rlnRelay.validateRoot(validProof.merkleRoot)
|
2022-09-20 13:08:05 +00:00
|
|
|
|
|
|
|
require:
|
2022-09-27 04:40:04 +00:00
|
|
|
verified == true
|
2022-09-20 13:08:05 +00:00
|
|
|
|
|
|
|
# When:
|
2022-09-27 04:40:04 +00:00
|
|
|
# This test depends on the local merkle tree root being part of a
|
|
|
|
# acceptable set of roots, which is denoted by AcceptableRootWindowSize
|
|
|
|
# The following action is equivalent to a member being removed upon listening to the events emitted by the contract
|
2022-09-20 13:08:05 +00:00
|
|
|
|
2022-09-27 04:40:04 +00:00
|
|
|
# Progress the local tree by removing members
|
|
|
|
for i in 0..AcceptableRootWindowSize - 2:
|
|
|
|
discard rlnRelay.removeMember(MembershipIndex(i))
|
|
|
|
# Ensure the local tree root has changed
|
|
|
|
let currentMerkleRoot = rlnRelay.rlnInstance.getMerkleRoot()
|
2022-09-20 13:08:05 +00:00
|
|
|
|
2022-09-27 04:40:04 +00:00
|
|
|
require:
|
|
|
|
currentMerkleRoot.isOk()
|
|
|
|
currentMerkleRoot.value() != validProof.merkleRoot
|
2022-09-20 13:08:05 +00:00
|
|
|
|
|
|
|
# Then:
|
2022-09-27 04:40:04 +00:00
|
|
|
# we try to verify a root against this window,
|
|
|
|
# which should return true
|
|
|
|
let olderRootVerified = rlnRelay.validateRoot(validProof.merkleRoot)
|
2022-09-20 13:08:05 +00:00
|
|
|
|
2022-09-27 04:40:04 +00:00
|
|
|
check:
|
|
|
|
olderRootVerified == true
|
|
|
|
|
|
|
|
test "invalidate roots which are not part of the acceptable window":
|
|
|
|
# Setup:
|
|
|
|
# This step consists of creating the rln instance and waku-rln-relay,
|
|
|
|
# Inserting members, and creating a valid proof with the merkle root
|
|
|
|
|
|
|
|
require:
|
|
|
|
AcceptableRootWindowSize < 10
|
|
|
|
|
|
|
|
# create an RLN instance
|
|
|
|
var rlnInstance = createRLNInstance()
|
2022-09-20 13:08:05 +00:00
|
|
|
require:
|
2022-09-27 04:40:04 +00:00
|
|
|
rlnInstance.isOk()
|
|
|
|
var rln = rlnInstance.value
|
|
|
|
|
|
|
|
let rlnRelay = WakuRLNRelay(rlnInstance:rln)
|
|
|
|
|
|
|
|
let
|
|
|
|
# create a membership key pair
|
|
|
|
memKeys = membershipKeyGen(rlnRelay.rlnInstance).get()
|
|
|
|
# peer's index in the Merkle Tree.
|
|
|
|
index = 6
|
|
|
|
|
|
|
|
let membershipCount = AcceptableRootWindowSize + 5
|
|
|
|
|
|
|
|
# Create a Merkle tree with random members
|
|
|
|
for i in 0..membershipCount:
|
|
|
|
var memberIsAdded: RlnRelayResult[void]
|
|
|
|
if (i == index):
|
|
|
|
# insert the current peer's pk
|
|
|
|
memberIsAdded = rlnRelay.insertMember(memKeys.idCommitment)
|
|
|
|
else:
|
|
|
|
# create a new key pair
|
|
|
|
let memberKeys = rlnRelay.rlnInstance.membershipKeyGen()
|
|
|
|
memberIsAdded = rlnRelay.insertMember(memberKeys.get().idCommitment)
|
|
|
|
# require that the member is added
|
|
|
|
require:
|
|
|
|
memberIsAdded.isOk()
|
|
|
|
|
|
|
|
# Given:
|
|
|
|
# This step includes constructing a valid message with the latest merkle root
|
|
|
|
# prepare the message
|
|
|
|
let messageBytes = "Hello".toBytes()
|
|
|
|
|
|
|
|
# prepare the epoch
|
|
|
|
var epoch: Epoch
|
|
|
|
debug "epoch in bytes", epochHex = epoch.toHex()
|
|
|
|
|
|
|
|
# generate proof
|
|
|
|
let validProofRes = rlnRelay.rlnInstance.proofGen(data = messageBytes,
|
|
|
|
memKeys = memKeys,
|
|
|
|
memIndex = MembershipIndex(index),
|
|
|
|
epoch = epoch)
|
|
|
|
require:
|
|
|
|
validProofRes.isOk()
|
|
|
|
let validProof = validProofRes.value
|
|
|
|
|
|
|
|
# validate the root (should be true)
|
|
|
|
let verified = rlnRelay.validateRoot(validProof.merkleRoot)
|
|
|
|
|
|
|
|
require:
|
|
|
|
verified == true
|
|
|
|
|
|
|
|
# When:
|
|
|
|
# This test depends on the local merkle tree root being part of a
|
|
|
|
# acceptable set of roots, which is denoted by AcceptableRootWindowSize
|
|
|
|
# The following action is equivalent to a member being removed upon listening to the events emitted by the contract
|
|
|
|
|
|
|
|
# Progress the local tree by removing members
|
|
|
|
for i in 0..AcceptableRootWindowSize:
|
|
|
|
discard rlnRelay.removeMember(MembershipIndex(i))
|
|
|
|
# Ensure the local tree root has changed
|
|
|
|
let currentMerkleRoot = rlnRelay.rlnInstance.getMerkleRoot()
|
|
|
|
require:
|
|
|
|
currentMerkleRoot.isOk()
|
|
|
|
currentMerkleRoot.value() != validProof.merkleRoot
|
|
|
|
|
|
|
|
# Then:
|
|
|
|
# we try to verify a proof against this window,
|
|
|
|
# which should return false
|
|
|
|
let olderRootVerified = rlnRelay.validateRoot(validProof.merkleRoot)
|
2022-09-20 13:08:05 +00:00
|
|
|
|
|
|
|
check:
|
2022-09-27 04:40:04 +00:00
|
|
|
olderRootVerified == false
|
2022-09-20 13:08:05 +00:00
|
|
|
|
2021-11-23 22:48:40 +00:00
|
|
|
test "toEpoch and fromEpoch consistency check":
|
|
|
|
# check edge cases
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
|
|
|
epoch = uint64.high # rln epoch
|
|
|
|
epochBytes = epoch.toEpoch()
|
|
|
|
decodedEpoch = epochBytes.fromEpoch()
|
|
|
|
check:
|
|
|
|
epoch == decodedEpoch
|
|
|
|
debug "encoded and decode time", epoch = epoch, epochBytes = epochBytes,
|
|
|
|
decodedEpoch = decodedEpoch
|
|
|
|
|
2021-11-23 22:48:40 +00:00
|
|
|
test "Epoch comparison":
|
|
|
|
# check edge cases
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
2021-11-23 22:48:40 +00:00
|
|
|
time1 = uint64.high
|
|
|
|
time2 = uint64.high - 1
|
|
|
|
epoch1 = time1.toEpoch()
|
|
|
|
epoch2 = time2.toEpoch()
|
2022-05-10 21:09:18 +00:00
|
|
|
check:
|
|
|
|
diff(epoch1, epoch2) == int64(1)
|
|
|
|
diff(epoch2, epoch1) == int64(-1)
|
|
|
|
|
2021-11-23 22:48:40 +00:00
|
|
|
test "updateLog and hasDuplicate tests":
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
2021-11-23 22:48:40 +00:00
|
|
|
wakurlnrelay = WakuRLNRelay()
|
|
|
|
epoch = getCurrentEpoch()
|
|
|
|
|
|
|
|
# cretae some dummy nullifiers and secret shares
|
|
|
|
var nullifier1: Nullifier
|
|
|
|
for index, x in nullifier1.mpairs: nullifier1[index] = 1
|
|
|
|
var shareX1: MerkleNode
|
|
|
|
for index, x in shareX1.mpairs: shareX1[index] = 1
|
|
|
|
let shareY1 = shareX1
|
|
|
|
|
|
|
|
var nullifier2: Nullifier
|
|
|
|
for index, x in nullifier2.mpairs: nullifier2[index] = 2
|
|
|
|
var shareX2: MerkleNode
|
|
|
|
for index, x in shareX2.mpairs: shareX2[index] = 2
|
|
|
|
let shareY2 = shareX2
|
|
|
|
|
|
|
|
let nullifier3 = nullifier1
|
|
|
|
var shareX3: MerkleNode
|
|
|
|
for index, x in shareX3.mpairs: shareX3[index] = 3
|
|
|
|
let shareY3 = shareX3
|
|
|
|
|
2022-08-05 20:58:19 +00:00
|
|
|
## TODO: when zerokit rln is integrated, RateLimitProof should be initialized passing a rlnIdentifier too (now implicitely set to 0)
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
|
|
|
wm1 = WakuMessage(proof: RateLimitProof(epoch: epoch,
|
|
|
|
nullifier: nullifier1, shareX: shareX1, shareY: shareY1))
|
|
|
|
wm2 = WakuMessage(proof: RateLimitProof(epoch: epoch,
|
|
|
|
nullifier: nullifier2, shareX: shareX2, shareY: shareY2))
|
|
|
|
wm3 = WakuMessage(proof: RateLimitProof(epoch: epoch,
|
|
|
|
nullifier: nullifier3, shareX: shareX3, shareY: shareY3))
|
2021-11-23 22:48:40 +00:00
|
|
|
|
|
|
|
# check whether hasDuplicate correctly finds records with the same nullifiers but different secret shares
|
|
|
|
# no duplicate for wm1 should be found, since the log is empty
|
|
|
|
let result1 = wakurlnrelay.hasDuplicate(wm1)
|
|
|
|
check:
|
|
|
|
result1.isOk
|
|
|
|
# no duplicate is found
|
|
|
|
result1.value == false
|
|
|
|
# add it to the log
|
|
|
|
discard wakurlnrelay.updateLog(wm1)
|
|
|
|
|
|
|
|
# # no duplicate for wm2 should be found, its nullifier differs from wm1
|
|
|
|
let result2 = wakurlnrelay.hasDuplicate(wm2)
|
|
|
|
check:
|
|
|
|
result2.isOk
|
|
|
|
# no duplicate is found
|
|
|
|
result2.value == false
|
|
|
|
# add it to the log
|
|
|
|
discard wakurlnrelay.updateLog(wm2)
|
|
|
|
|
|
|
|
# wm3 has the same nullifier as wm1 but different secret shares, it should be detected as duplicate
|
|
|
|
let result3 = wakurlnrelay.hasDuplicate(wm3)
|
|
|
|
check:
|
2022-05-10 21:09:18 +00:00
|
|
|
result3.isOk
|
2021-11-23 22:48:40 +00:00
|
|
|
# it is a duplicate
|
|
|
|
result3.value == true
|
|
|
|
|
|
|
|
test "validateMessage test":
|
|
|
|
# setup a wakurlnrelay peer with a static group----------
|
|
|
|
|
|
|
|
# create a group of 100 membership keys
|
|
|
|
let
|
|
|
|
(groupKeys, root) = createMembershipList(100)
|
|
|
|
# convert the keys to MembershipKeyPair structs
|
|
|
|
groupKeyPairs = groupKeys.toMembershipKeyPairs()
|
|
|
|
# extract the id commitments
|
|
|
|
groupIDCommitments = groupKeyPairs.mapIt(it.idCommitment)
|
|
|
|
debug "groupKeyPairs", groupKeyPairs
|
|
|
|
debug "groupIDCommitments", groupIDCommitments
|
2022-05-10 21:09:18 +00:00
|
|
|
|
|
|
|
# index indicates the position of a membership key pair in the static list of group keys i.e., groupKeyPairs
|
2021-11-23 22:48:40 +00:00
|
|
|
# the corresponding key pair will be used to mount rlnRelay on the current node
|
2022-05-10 21:09:18 +00:00
|
|
|
# index also represents the index of the leaf in the Merkle tree that contains node's commitment key
|
2021-11-23 22:48:40 +00:00
|
|
|
let index = MembershipIndex(5)
|
|
|
|
|
|
|
|
# create an RLN instance
|
|
|
|
var rlnInstance = createRLNInstance()
|
|
|
|
doAssert(rlnInstance.isOk)
|
|
|
|
var rln = rlnInstance.value
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
let
|
|
|
|
wakuRlnRelay = WakuRLNRelay(membershipIndex: index,
|
|
|
|
membershipKeyPair: groupKeyPairs[index], rlnInstance: rln)
|
2021-11-23 22:48:40 +00:00
|
|
|
|
2022-09-27 04:40:04 +00:00
|
|
|
# add members
|
|
|
|
let commitmentAddRes = wakuRlnRelay.addAll(groupIDCommitments)
|
|
|
|
require:
|
|
|
|
commitmentAddRes.isOk()
|
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
# get the current epoch time
|
2021-11-23 22:48:40 +00:00
|
|
|
let time = epochTime()
|
|
|
|
|
|
|
|
# create some messages from the same peer and append rln proof to them, except wm4
|
2022-05-10 21:09:18 +00:00
|
|
|
var
|
2021-11-23 22:48:40 +00:00
|
|
|
wm1 = WakuMessage(payload: "Valid message".toBytes())
|
|
|
|
proofAdded1 = wakuRlnRelay.appendRLNProof(wm1, time)
|
|
|
|
# another message in the same epoch as wm1, it will break the messaging rate limit
|
|
|
|
wm2 = WakuMessage(payload: "Spam".toBytes())
|
|
|
|
proofAdded2 = wakuRlnRelay.appendRLNProof(wm2, time)
|
2022-05-10 21:09:18 +00:00
|
|
|
# wm3 points to the next epoch
|
2021-11-23 22:48:40 +00:00
|
|
|
wm3 = WakuMessage(payload: "Valid message".toBytes())
|
2022-09-30 12:43:42 +00:00
|
|
|
proofAdded3 = wakuRlnRelay.appendRLNProof(wm3, time+EpochUnitSeconds)
|
2022-05-10 21:09:18 +00:00
|
|
|
wm4 = WakuMessage(payload: "Invalid message".toBytes())
|
|
|
|
|
2021-11-23 22:48:40 +00:00
|
|
|
# checks proofs are added
|
|
|
|
check:
|
|
|
|
proofAdded1
|
|
|
|
proofAdded2
|
|
|
|
proofAdded3
|
|
|
|
|
|
|
|
# validate messages
|
|
|
|
# validateMessage proc checks the validity of the message fields and adds it to the log (if valid)
|
|
|
|
let
|
2022-01-28 21:57:17 +00:00
|
|
|
msgValidate1 = wakuRlnRelay.validateMessage(wm1, some(time))
|
2021-11-23 22:48:40 +00:00
|
|
|
# wm2 is published within the same Epoch as wm1 and should be found as spam
|
2022-01-28 21:57:17 +00:00
|
|
|
msgValidate2 = wakuRlnRelay.validateMessage(wm2, some(time))
|
2022-05-10 21:09:18 +00:00
|
|
|
# a valid message should be validated successfully
|
2022-01-28 21:57:17 +00:00
|
|
|
msgValidate3 = wakuRlnRelay.validateMessage(wm3, some(time))
|
2021-11-23 22:48:40 +00:00
|
|
|
# wm4 has no rln proof and should not be validated
|
2022-01-28 21:57:17 +00:00
|
|
|
msgValidate4 = wakuRlnRelay.validateMessage(wm4, some(time))
|
2021-11-23 22:48:40 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
|
2021-11-23 22:48:40 +00:00
|
|
|
check:
|
|
|
|
msgValidate1 == MessageValidationResult.Valid
|
2022-05-10 21:09:18 +00:00
|
|
|
msgValidate2 == MessageValidationResult.Spam
|
2021-11-23 22:48:40 +00:00
|
|
|
msgValidate3 == MessageValidationResult.Valid
|
|
|
|
msgValidate4 == MessageValidationResult.Invalid
|
2022-08-05 20:58:19 +00:00
|
|
|
|
2022-05-30 19:14:07 +00:00
|
|
|
test "toIDCommitment and toUInt256":
|
|
|
|
# create an instance of rln
|
|
|
|
var rlnInstance = createRLNInstance()
|
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
|
|
|
|
# create a key pair
|
|
|
|
var keypair = rlnInstance.value.membershipKeyGen()
|
|
|
|
check:
|
|
|
|
keypair.isSome()
|
|
|
|
|
|
|
|
# convert the idCommitment to UInt256
|
|
|
|
let idCUInt = keypair.get().idCommitment.toUInt256()
|
|
|
|
# convert the UInt256 back to ICommitment
|
|
|
|
let idCommitment = toIDCommitment(idCUInt)
|
2021-11-23 22:48:40 +00:00
|
|
|
|
2022-05-30 19:14:07 +00:00
|
|
|
# check that the conversion has not distorted the original value
|
|
|
|
check:
|
|
|
|
keypair.get().idCommitment == idCommitment
|
2022-08-05 10:48:01 +00:00
|
|
|
|
|
|
|
test "Read Persistent RLN credentials":
|
|
|
|
# create an RLN instance
|
|
|
|
var rlnInstance = createRLNInstance()
|
|
|
|
check:
|
|
|
|
rlnInstance.isOk == true
|
|
|
|
|
|
|
|
var key = membershipKeyGen(rlnInstance.value)
|
|
|
|
var empty: array[32, byte]
|
|
|
|
check:
|
|
|
|
key.isSome
|
|
|
|
key.get().idKey.len == 32
|
|
|
|
key.get().idCommitment.len == 32
|
|
|
|
key.get().idKey != empty
|
|
|
|
key.get().idCommitment != empty
|
|
|
|
|
|
|
|
debug "the generated membership key pair: ", key
|
|
|
|
|
|
|
|
let
|
|
|
|
k = key.get()
|
|
|
|
index = MembershipIndex(1)
|
|
|
|
|
|
|
|
var rlnMembershipCredentials = RlnMembershipCredentials(membershipKeyPair: k, rlnIndex: index)
|
|
|
|
|
|
|
|
let path = "testPath.txt"
|
|
|
|
|
|
|
|
# Write RLN credentials
|
|
|
|
writeFile(path, pretty(%rlnMembershipCredentials))
|
|
|
|
|
|
|
|
var credentials = readPersistentRlnCredentials(path)
|
|
|
|
|
|
|
|
check:
|
|
|
|
credentials.membershipKeyPair == k
|
|
|
|
credentials.rlnIndex == index
|
2022-09-30 12:43:42 +00:00
|
|
|
|
|
|
|
test "histogram static bucket generation":
|
|
|
|
let buckets = generateBucketsForHistogram(10)
|
2022-08-05 10:48:01 +00:00
|
|
|
|
2022-09-30 12:43:42 +00:00
|
|
|
check:
|
|
|
|
buckets.len == 5
|
|
|
|
buckets == [2.0, 4.0, 6.0, 8.0, 10.0]
|