2024-02-14 07:54:05 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2024-02-14 07:54:05 +00:00
|
|
|
|
2024-08-02 14:43:22 +00:00
|
|
|
import stew/results, stint
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import
|
2024-06-20 09:35:21 +00:00
|
|
|
./rln/waku_rln_relay_utils,
|
2024-08-02 14:43:22 +00:00
|
|
|
waku/[
|
|
|
|
waku_keystore/protocol_types,
|
|
|
|
waku_rln_relay,
|
|
|
|
waku_rln_relay/rln,
|
|
|
|
waku_rln_relay/protocol_types,
|
|
|
|
],
|
|
|
|
../waku_keystore/utils,
|
|
|
|
testutils/unittests
|
2024-02-14 07:54:05 +00:00
|
|
|
|
|
|
|
from std/times import epochTime
|
|
|
|
|
|
|
|
func defaultRateCommitment*(): RateCommitment =
|
|
|
|
let idCredential = defaultIdentityCredential()
|
2024-03-15 23:08:47 +00:00
|
|
|
return RateCommitment(idCommitment: idCredential.idCommitment, userMessageLimit: 100)
|
2024-02-14 07:54:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let expectedLeaf =
|
|
|
|
"09beac7784abfadc9958b3176b352389d0b969ccc7f8bccf3e968ed632e26eca"
|
2024-02-14 07:54:05 +00:00
|
|
|
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
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2024-02-14 07:54:05 +00:00
|
|
|
assert success, "failed to insert member"
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let proofRes = rln.proofGen(
|
|
|
|
data = @[],
|
|
|
|
membership = credential,
|
|
|
|
userMessageLimit = rateCommitment.userMessageLimit,
|
|
|
|
messageId = 0,
|
|
|
|
index = 0,
|
|
|
|
epoch = uint64(epochTime() / 1.float64).toEpoch(),
|
|
|
|
)
|
|
|
|
|
2024-02-14 07:54:05 +00:00
|
|
|
assert proofRes.isOk, $proofRes.error
|
|
|
|
|
|
|
|
let proof = proofRes.value
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let proofVerifyRes =
|
|
|
|
rln.proofVerify(data = @[], proof = proof, validRoots = @[merkleRoot])
|
2024-02-14 07:54:05 +00:00
|
|
|
|
|
|
|
assert proofVerifyRes.isOk, $proofVerifyRes.error
|
|
|
|
assert proofVerifyRes.value, "proof verification failed"
|