Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rln-relay-v2): add tests for serde #2421

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions tests/waku_rln_relay/rln_v2/test_rln_relay_v2_serde.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{.used.}

when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import
../rln/waku_rln_relay_utils,
../../../waku/waku_keystore/protocol_types,
../../../waku/waku_rln_relay,
../../../waku/waku_rln_relay/rln

import
testutils/unittests
import
stew/results,
stint
from std/times import epochTime

func fromStrToBytesLe(v: string): seq[byte] =
try:
return @(hexToUint[256](v).toBytesLE())
except ValueError:
# this should never happen
return @[]

func defaultIdentityCredential*(): IdentityCredential =
# zero out the values we don't need
return IdentityCredential(
idTrapdoor: default(IdentityTrapdoor),
idNullifier: default(IdentityNullifier),
idSecretHash: fromStrToBytesLe("7984f7c054ad7793d9f31a1e9f29eaa8d05966511e546bced89961eb8874ab9"),
idCommitment: fromStrToBytesLe("51c31de3bff7e52dc7b2eb34fc96813bacf38bde92d27fe326ce5d8296322a7")
)

func defaultRateCommitment*(): RateCommitment =
let idCredential = defaultIdentityCredential()
return RateCommitment(
idCommitment: idCredential.idCommitment,
userMessageLimit: 100
)

suite "RLN Relay v2: serde":
test "toLeaf: converts a rateCommitment to a valid leaf":
# this test vector is from zerokit
let rateCommitment = defaultRateCommitment()

let leafRes = toLeaf(rateCommitment)
assert leafRes.isOk, $leafRes.error

let expectedLeaf = "09beac7784abfadc9958b3176b352389d0b969ccc7f8bccf3e968ed632e26eca"
check expectedLeaf == leafRes.value.inHex()


test "proofGen: generates a valid zk proof":
# this test vector is from zerokit
let rlnInstance = createRLNInstanceWrapper()
assert rlnInstance.isOk, $rlnInstance.error
let rln = rlnInstance.value

let credential = defaultIdentityCredential()
let rateCommitment = defaultRateCommitment()
let success = rln.insertMember(@(rateCommitment.toLeaf().value))
let merkleRootRes = rln.getMerkleRoot()
assert merkleRootRes.isOk, $merkleRootRes.error
let merkleRoot = merkleRootRes.value

assert success, "failed to insert member"

let proofRes = rln.proofGen(data = @[],
membership = credential,
userMessageLimit = rateCommitment.userMessageLimit,
messageId = 0,
index = 0,
epoch = calcEpoch(epochTime()))

assert proofRes.isOk, $proofRes.error

let proof = proofRes.value

let proofVerifyRes = rln.proofVerify(data = @[],
proof = proof,
validRoots = @[merkleRoot])

assert proofVerifyRes.isOk, $proofVerifyRes.error
assert proofVerifyRes.value, "proof verification failed"



4 changes: 2 additions & 2 deletions waku/waku_rln_relay/conversion_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ when defined(rln_v2):
## the serialization is done as instructed in https://github.com/kilic/rln/blob/7ac74183f8b69b399e3bc96c1ae8ab61c026dc43/src/public.rs#L146
## [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
let memIndexBytes = toBytes(uint64(memIndex), Endianness.littleEndian)
let userMessageLimitBytes = toBytes(uint64(userMessageLimit), Endianness.littleEndian)
let messageIdBytes = toBytes(uint64(messageId), Endianness.littleEndian)
let userMessageLimitBytes = cast[array[32, byte]](toBytes(uint64(userMessageLimit), Endianness.littleEndian))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using cast is a good way to introduce undefined behavior - it should be avoided in most cases - array[...](toBytes...) will work iff this is a compatible conversion, otherwise you need to construct the array in some other way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 4095279

let messageIdBytes = cast[array[32, byte]](toBytes(uint64(messageId), Endianness.littleEndian))
let lenPrefMsg = encodeLengthPrefix(msg)
let output = concat(@idSecretHash, @memIndexBytes, @userMessageLimitBytes, @messageIdBytes, @externalNullifier, lenPrefMsg)
return output
Expand Down
4 changes: 2 additions & 2 deletions waku/waku_rln_relay/rln/wrappers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ proc poseidon*(data: seq[seq[byte]]): RlnRelayResult[array[32, byte]] =
when defined(rln_v2):
func toLeaf*(rateCommitment: RateCommitment): RlnRelayResult[MerkleNode] {.inline.} =
let idCommitment = rateCommitment.idCommitment
let userMessageLimit = rateCommitment.userMessageLimit
let leafRes = poseidon(@[@idCommitment, cast[seq[byte]](userMessageLimit)])
let userMessageLimit = cast[array[32, byte]](rateCommitment.userMessageLimit)
let leafRes = poseidon(@[@idCommitment, @userMessageLimit])
return leafRes

func toLeaves*(rateCommitments: seq[RateCommitment]): RlnRelayResult[seq[MerkleNode]] {.inline.} =
Expand Down
Loading