mirror of https://github.com/waku-org/nwaku.git
Enables passing RLN object across different function calls (#705)
* tests rln instance as pointer * test insertion * tests deletion * tests proof generation and verification * updates rln instance type and the rln api * deletes old API * removes temporary tests * deletes unused codes * Delete settings.json * reverts the changes in tests v2 * removes an old comment
This commit is contained in:
parent
6ebe26ad05
commit
c222d83bcc
|
@ -180,13 +180,12 @@ procSuite "Waku rln relay":
|
||||||
await web3.close()
|
await web3.close()
|
||||||
|
|
||||||
# create an RLN instance
|
# create an RLN instance
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
check:
|
||||||
ctxPtr = addr(ctx)
|
rlnInstance.isOk == true
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
|
||||||
|
|
||||||
# generate the membership keys
|
# generate the membership keys
|
||||||
let membershipKeyPair = membershipKeyGen(ctxPtr)
|
let membershipKeyPair = membershipKeyGen(rlnInstance.value)
|
||||||
|
|
||||||
check:
|
check:
|
||||||
membershipKeyPair.isSome
|
membershipKeyPair.isSome
|
||||||
|
@ -242,13 +241,9 @@ suite "Waku rln relay":
|
||||||
# check the parameters.key is not empty
|
# check the parameters.key is not empty
|
||||||
pbytes.len != 0
|
pbytes.len != 0
|
||||||
|
|
||||||
# ctx holds the information that is going to be used for the key generation
|
|
||||||
var
|
var
|
||||||
obj = RLN[Bn256]()
|
rlnInstance: RLN[Bn256]
|
||||||
objPtr = addr(obj)
|
let res = new_circuit_from_params(merkleDepth, addr parametersBuffer, addr rlnInstance)
|
||||||
objptrptr = addr(objPtr)
|
|
||||||
ctx = objptrptr
|
|
||||||
let res = new_circuit_from_params(merkleDepth, addr parametersBuffer, ctx)
|
|
||||||
check:
|
check:
|
||||||
# check whether the circuit parameters are generated successfully
|
# check whether the circuit parameters are generated successfully
|
||||||
res == true
|
res == true
|
||||||
|
@ -257,7 +252,7 @@ suite "Waku rln relay":
|
||||||
var
|
var
|
||||||
keysBuffer : Buffer
|
keysBuffer : Buffer
|
||||||
keysBufferPtr = addr(keysBuffer)
|
keysBufferPtr = addr(keysBuffer)
|
||||||
done = key_gen(ctx[], keysBufferPtr)
|
done = key_gen(rlnInstance, keysBufferPtr)
|
||||||
check:
|
check:
|
||||||
# check whether the keys are generated successfully
|
# check whether the keys are generated successfully
|
||||||
done == true
|
done == true
|
||||||
|
@ -271,12 +266,11 @@ suite "Waku rln relay":
|
||||||
|
|
||||||
test "membership Key Gen":
|
test "membership Key Gen":
|
||||||
# create an RLN instance
|
# create an RLN instance
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
check:
|
||||||
ctxPtr = addr(ctx)
|
rlnInstance.isOk == true
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
|
||||||
|
|
||||||
var key = membershipKeyGen(ctxPtr)
|
var key = membershipKeyGen(rlnInstance.value)
|
||||||
var empty : array[32,byte]
|
var empty : array[32,byte]
|
||||||
check:
|
check:
|
||||||
key.isSome
|
key.isSome
|
||||||
|
@ -289,16 +283,15 @@ suite "Waku rln relay":
|
||||||
|
|
||||||
test "get_root Nim binding":
|
test "get_root Nim binding":
|
||||||
# create an RLN instance which also includes an empty Merkle tree
|
# create an RLN instance which also includes an empty Merkle tree
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
check:
|
||||||
ctxPtr = addr(ctx)
|
rlnInstance.isOk == true
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
|
||||||
|
|
||||||
# read the Merkle Tree root
|
# read the Merkle Tree root
|
||||||
var
|
var
|
||||||
root1 {.noinit.} : Buffer = Buffer()
|
root1 {.noinit.} : Buffer = Buffer()
|
||||||
rootPtr1 = addr(root1)
|
rootPtr1 = addr(root1)
|
||||||
get_root_successful1 = get_root(ctxPtr, rootPtr1)
|
get_root_successful1 = get_root(rlnInstance.value, rootPtr1)
|
||||||
doAssert(get_root_successful1)
|
doAssert(get_root_successful1)
|
||||||
doAssert(root1.len == 32)
|
doAssert(root1.len == 32)
|
||||||
|
|
||||||
|
@ -306,7 +299,7 @@ suite "Waku rln relay":
|
||||||
var
|
var
|
||||||
root2 {.noinit.} : Buffer = Buffer()
|
root2 {.noinit.} : Buffer = Buffer()
|
||||||
rootPtr2 = addr(root2)
|
rootPtr2 = addr(root2)
|
||||||
get_root_successful2 = get_root(ctxPtr, rootPtr2)
|
get_root_successful2 = get_root(rlnInstance.value, rootPtr2)
|
||||||
doAssert(get_root_successful2)
|
doAssert(get_root_successful2)
|
||||||
doAssert(root2.len == 32)
|
doAssert(root2.len == 32)
|
||||||
|
|
||||||
|
@ -321,77 +314,74 @@ suite "Waku rln relay":
|
||||||
|
|
||||||
test "update_next_member Nim Wrapper":
|
test "update_next_member Nim Wrapper":
|
||||||
# create an RLN instance which also includes an empty Merkle tree
|
# create an RLN instance which also includes an empty Merkle tree
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
check:
|
||||||
ctxPtr = addr(ctx)
|
rlnInstance.isOk == true
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
|
||||||
|
|
||||||
# generate a key pair
|
# generate a key pair
|
||||||
var keypair = membershipKeyGen(ctxPtr)
|
var keypair = membershipKeyGen(rlnInstance.value)
|
||||||
doAssert(keypair.isSome())
|
doAssert(keypair.isSome())
|
||||||
var pkBuffer = Buffer(`ptr`: addr(keypair.get().publicKey[0]), len: 32)
|
var pkBuffer = Buffer(`ptr`: addr(keypair.get().publicKey[0]), len: 32)
|
||||||
let pkBufferPtr = addr pkBuffer
|
let pkBufferPtr = addr pkBuffer
|
||||||
|
|
||||||
# add the member to the tree
|
# add the member to the tree
|
||||||
var member_is_added = update_next_member(ctxPtr, pkBufferPtr)
|
var member_is_added = update_next_member(rlnInstance.value, pkBufferPtr)
|
||||||
check:
|
check:
|
||||||
member_is_added == true
|
member_is_added == true
|
||||||
|
|
||||||
test "delete_member Nim wrapper":
|
test "delete_member Nim wrapper":
|
||||||
# create an RLN instance which also includes an empty Merkle tree
|
# create an RLN instance which also includes an empty Merkle tree
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
check:
|
||||||
ctxPtr = addr(ctx)
|
rlnInstance.isOk == true
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
|
||||||
|
|
||||||
# delete the first member
|
# delete the first member
|
||||||
var deleted_member_index = uint(0)
|
var deleted_member_index = uint(0)
|
||||||
let deletion_success = delete_member(ctxPtr, deleted_member_index)
|
let deletion_success = delete_member(rlnInstance.value, deleted_member_index)
|
||||||
doAssert(deletion_success)
|
doAssert(deletion_success)
|
||||||
|
|
||||||
test "Merkle tree consistency check between deletion and insertion":
|
test "Merkle tree consistency check between deletion and insertion":
|
||||||
# create an RLN instance
|
# create an RLN instance
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
check:
|
||||||
ctxPtr = addr(ctx)
|
rlnInstance.isOk == true
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
|
||||||
|
|
||||||
# read the Merkle Tree root
|
# read the Merkle Tree root
|
||||||
var
|
var
|
||||||
root1 {.noinit.} : Buffer = Buffer()
|
root1 {.noinit.} : Buffer = Buffer()
|
||||||
rootPtr1 = addr(root1)
|
rootPtr1 = addr(root1)
|
||||||
get_root_successful1 = get_root(ctxPtr, rootPtr1)
|
get_root_successful1 = get_root(rlnInstance.value, rootPtr1)
|
||||||
doAssert(get_root_successful1)
|
doAssert(get_root_successful1)
|
||||||
doAssert(root1.len == 32)
|
doAssert(root1.len == 32)
|
||||||
|
|
||||||
# generate a key pair
|
# generate a key pair
|
||||||
var keypair = membershipKeyGen(ctxPtr)
|
var keypair = membershipKeyGen(rlnInstance.value)
|
||||||
doAssert(keypair.isSome())
|
doAssert(keypair.isSome())
|
||||||
var pkBuffer = Buffer(`ptr`: addr(keypair.get().publicKey[0]), len: 32)
|
var pkBuffer = Buffer(`ptr`: addr(keypair.get().publicKey[0]), len: 32)
|
||||||
let pkBufferPtr = addr pkBuffer
|
let pkBufferPtr = addr pkBuffer
|
||||||
|
|
||||||
# add the member to the tree
|
# add the member to the tree
|
||||||
var member_is_added = update_next_member(ctxPtr, pkBufferPtr)
|
var member_is_added = update_next_member(rlnInstance.value, pkBufferPtr)
|
||||||
doAssert(member_is_added)
|
doAssert(member_is_added)
|
||||||
|
|
||||||
# read the Merkle Tree root after insertion
|
# read the Merkle Tree root after insertion
|
||||||
var
|
var
|
||||||
root2 {.noinit.} : Buffer = Buffer()
|
root2 {.noinit.} : Buffer = Buffer()
|
||||||
rootPtr2 = addr(root2)
|
rootPtr2 = addr(root2)
|
||||||
get_root_successful2 = get_root(ctxPtr, rootPtr2)
|
get_root_successful2 = get_root(rlnInstance.value, rootPtr2)
|
||||||
doAssert(get_root_successful2)
|
doAssert(get_root_successful2)
|
||||||
doAssert(root2.len == 32)
|
doAssert(root2.len == 32)
|
||||||
|
|
||||||
# delete the first member
|
# delete the first member
|
||||||
var deleted_member_index = uint(0)
|
var deleted_member_index = uint(0)
|
||||||
let deletion_success = delete_member(ctxPtr, deleted_member_index)
|
let deletion_success = delete_member(rlnInstance.value, deleted_member_index)
|
||||||
doAssert(deletion_success)
|
doAssert(deletion_success)
|
||||||
|
|
||||||
# read the Merkle Tree root after the deletion
|
# read the Merkle Tree root after the deletion
|
||||||
var
|
var
|
||||||
root3 {.noinit.} : Buffer = Buffer()
|
root3 {.noinit.} : Buffer = Buffer()
|
||||||
rootPtr3 = addr(root3)
|
rootPtr3 = addr(root3)
|
||||||
get_root_successful3 = get_root(ctxPtr, rootPtr3)
|
get_root_successful3 = get_root(rlnInstance.value, rootPtr3)
|
||||||
doAssert(get_root_successful3)
|
doAssert(get_root_successful3)
|
||||||
doAssert(root3.len == 32)
|
doAssert(root3.len == 32)
|
||||||
|
|
||||||
|
@ -415,11 +405,10 @@ suite "Waku rln relay":
|
||||||
doAssert(rootHex1 == rootHex3)
|
doAssert(rootHex1 == rootHex3)
|
||||||
test "hash Nim Wrappers":
|
test "hash Nim Wrappers":
|
||||||
# create an RLN instance
|
# create an RLN instance
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
check:
|
||||||
ctxPtr = addr(ctx)
|
rlnInstance.isOk == true
|
||||||
doAssert(createRLNInstance(30, ctxPtr))
|
|
||||||
|
|
||||||
# prepare the input
|
# prepare the input
|
||||||
var
|
var
|
||||||
hashInput : array[32, byte]
|
hashInput : array[32, byte]
|
||||||
|
@ -435,7 +424,7 @@ suite "Waku rln relay":
|
||||||
outputBuffer: Buffer
|
outputBuffer: Buffer
|
||||||
numOfInputs = 1.uint # the number of hash inputs that can be 1 or 2
|
numOfInputs = 1.uint # the number of hash inputs that can be 1 or 2
|
||||||
|
|
||||||
let hashSuccess = hash(ctxPtr, addr hashInputBuffer, numOfInputs, addr outputBuffer)
|
let hashSuccess = hash(rlnInstance.value, addr hashInputBuffer, numOfInputs, addr outputBuffer)
|
||||||
doAssert(hashSuccess)
|
doAssert(hashSuccess)
|
||||||
let outputArr = cast[ptr array[32,byte]](outputBuffer.`ptr`)[]
|
let outputArr = cast[ptr array[32,byte]](outputBuffer.`ptr`)[]
|
||||||
doAssert("53a6338cdbf02f0563cec1898e354d0d272c8f98b606c538945c6f41ef101828" == outputArr.toHex())
|
doAssert("53a6338cdbf02f0563cec1898e354d0d272c8f98b606c538945c6f41ef101828" == outputArr.toHex())
|
||||||
|
@ -448,15 +437,15 @@ suite "Waku rln relay":
|
||||||
|
|
||||||
test "generate_proof and verify Nim Wrappers":
|
test "generate_proof and verify Nim Wrappers":
|
||||||
# create an RLN instance
|
# create an RLN instance
|
||||||
var
|
|
||||||
ctx = RLN[Bn256]()
|
|
||||||
ctxPtr = addr(ctx)
|
|
||||||
|
|
||||||
# check if the rln instance is created successfully
|
# check if the rln instance is created successfully
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
var rlnInstance = createRLNInstance(32)
|
||||||
|
check:
|
||||||
|
rlnInstance.isOk == true
|
||||||
|
|
||||||
|
|
||||||
# create the membership key
|
# create the membership key
|
||||||
var auth = membershipKeyGen(ctxPtr)
|
var auth = membershipKeyGen(rlnInstance.value)
|
||||||
var skBuffer = Buffer(`ptr`: addr(auth.get().secretKey[0]), len: 32)
|
var skBuffer = Buffer(`ptr`: addr(auth.get().secretKey[0]), len: 32)
|
||||||
|
|
||||||
# peer's index in the Merkle Tree
|
# peer's index in the Merkle Tree
|
||||||
|
@ -471,11 +460,11 @@ suite "Waku rln relay":
|
||||||
if (i == index):
|
if (i == index):
|
||||||
# insert the current peer's pk
|
# insert the current peer's pk
|
||||||
var pkBuffer = Buffer(`ptr`: addr(auth.get().publicKey[0]), len: 32)
|
var pkBuffer = Buffer(`ptr`: addr(auth.get().publicKey[0]), len: 32)
|
||||||
member_is_added = update_next_member(ctxPtr, addr pkBuffer)
|
member_is_added = update_next_member(rlnInstance.value, addr pkBuffer)
|
||||||
else:
|
else:
|
||||||
var memberKeys = membershipKeyGen(ctxPtr)
|
var memberKeys = membershipKeyGen(rlnInstance.value)
|
||||||
var pkBuffer = Buffer(`ptr`: addr(memberKeys.get().publicKey[0]), len: 32)
|
var pkBuffer = Buffer(`ptr`: addr(memberKeys.get().publicKey[0]), len: 32)
|
||||||
member_is_added = update_next_member(ctxPtr, addr pkBuffer)
|
member_is_added = update_next_member(rlnInstance.value, addr pkBuffer)
|
||||||
# check the member is added
|
# check the member is added
|
||||||
doAssert(member_is_added)
|
doAssert(member_is_added)
|
||||||
|
|
||||||
|
@ -505,7 +494,7 @@ suite "Waku rln relay":
|
||||||
|
|
||||||
# generate the proof
|
# generate the proof
|
||||||
var proof: Buffer
|
var proof: Buffer
|
||||||
let proofIsSuccessful = generate_proof(ctxPtr, addr inputBuffer, addr authObj, addr proof)
|
let proofIsSuccessful = generate_proof(rlnInstance.value, addr inputBuffer, addr authObj, addr proof)
|
||||||
# check whether the generate_proof call is done successfully
|
# check whether the generate_proof call is done successfully
|
||||||
doAssert(proofIsSuccessful)
|
doAssert(proofIsSuccessful)
|
||||||
var proofValue = cast[ptr array[416,byte]] (proof.`ptr`)
|
var proofValue = cast[ptr array[416,byte]] (proof.`ptr`)
|
||||||
|
@ -537,7 +526,7 @@ suite "Waku rln relay":
|
||||||
debug "nullifier", nullifier
|
debug "nullifier", nullifier
|
||||||
|
|
||||||
var f = 0.uint32
|
var f = 0.uint32
|
||||||
let verifyIsSuccessful = verify(ctxPtr, addr proof, addr f)
|
let verifyIsSuccessful = verify(rlnInstance.value, addr proof, addr f)
|
||||||
doAssert(verifyIsSuccessful)
|
doAssert(verifyIsSuccessful)
|
||||||
# f = 0 means the proof is verified
|
# f = 0 means the proof is verified
|
||||||
doAssert(f == 0)
|
doAssert(f == 0)
|
||||||
|
@ -547,13 +536,13 @@ suite "Waku rln relay":
|
||||||
var badIndex = 8
|
var badIndex = 8
|
||||||
var badAuthObj: Auth = Auth(secret_buffer: addr skBuffer, index: uint(badIndex))
|
var badAuthObj: Auth = Auth(secret_buffer: addr skBuffer, index: uint(badIndex))
|
||||||
var badProof: Buffer
|
var badProof: Buffer
|
||||||
let badProofIsSuccessful = generate_proof(ctxPtr, addr inputBuffer, addr badAuthObj, addr badProof)
|
let badProofIsSuccessful = generate_proof(rlnInstance.value, addr inputBuffer, addr badAuthObj, addr badProof)
|
||||||
# check whether the generate_proof call is done successfully
|
# check whether the generate_proof call is done successfully
|
||||||
doAssert(badProofIsSuccessful)
|
doAssert(badProofIsSuccessful)
|
||||||
|
|
||||||
var badF = 0.uint32
|
var badF = 0.uint32
|
||||||
let badVerifyIsSuccessful = verify(ctxPtr, addr badProof, addr badF)
|
let badVerifyIsSuccessful = verify(rlnInstance.value, addr badProof, addr badF)
|
||||||
doAssert(badVerifyIsSuccessful)
|
doAssert(badVerifyIsSuccessful)
|
||||||
# badF=1 means the proof is not verified
|
# badF=1 means the proof is not verified
|
||||||
# verification of the bad proof should fail
|
# verification of the bad proof should fail
|
||||||
doAssert(badF == 1)
|
doAssert(badF == 1)
|
|
@ -419,13 +419,11 @@ when defined(rln):
|
||||||
doAssert(membershipContractAddress.isSome())
|
doAssert(membershipContractAddress.isSome())
|
||||||
|
|
||||||
# create an RLN instance
|
# create an RLN instance
|
||||||
var
|
var rlnInstance = createRLNInstance(32)
|
||||||
ctx = RLN[Bn256]()
|
doAssert(rlnInstance.isOk)
|
||||||
ctxPtr = addr(ctx)
|
|
||||||
doAssert(createRLNInstance(32, ctxPtr))
|
|
||||||
|
|
||||||
# generate the membership keys
|
# generate the membership keys
|
||||||
let membershipKeyPair = membershipKeyGen(ctxPtr)
|
let membershipKeyPair = membershipKeyGen(rlnInstance.value)
|
||||||
# check whether keys are generated
|
# check whether keys are generated
|
||||||
doAssert(membershipKeyPair.isSome())
|
doAssert(membershipKeyPair.isSome())
|
||||||
debug "the membership key for the rln relay is generated"
|
debug "the membership key for the rln relay is generated"
|
||||||
|
|
|
@ -30,23 +30,24 @@ type Auth* = object
|
||||||
|
|
||||||
#------------------------------ Merkle Tree operations -----------------------------------------
|
#------------------------------ Merkle Tree operations -----------------------------------------
|
||||||
|
|
||||||
proc update_next_member*(ctx: ptr RLN[Bn256],
|
proc update_next_member*(ctx: RLN[Bn256],
|
||||||
input_buffer: ptr Buffer): bool {.importc: "update_next_member".}
|
input_buffer: ptr Buffer): bool {.importc: "update_next_member".}
|
||||||
|
|
||||||
proc delete_member*(ctx: ptr RLN[Bn256], index: uint): bool {.importc: "delete_member".}
|
proc delete_member*(ctx: RLN[Bn256], index: uint): bool {.importc: "delete_member".}
|
||||||
|
|
||||||
|
proc get_root*(ctx: RLN[Bn256], output_buffer: ptr Buffer): bool {.importc: "get_root".}
|
||||||
|
|
||||||
proc get_root*(ctx: ptr RLN[Bn256], output_buffer: ptr Buffer): bool {.importc: "get_root".}
|
|
||||||
#----------------------------------------------------------------------------------------------
|
#----------------------------------------------------------------------------------------------
|
||||||
#-------------------------------- zkSNARKs operations -----------------------------------------
|
#-------------------------------- zkSNARKs operations -----------------------------------------
|
||||||
|
|
||||||
proc key_gen*(ctx: ptr RLN[Bn256], keypair_buffer: ptr Buffer): bool {.importc: "key_gen".}
|
proc key_gen*(ctx: RLN[Bn256], keypair_buffer: ptr Buffer): bool {.importc: "key_gen".}
|
||||||
|
|
||||||
proc generate_proof*(ctx: ptr RLN[Bn256],
|
proc generate_proof*(ctx: RLN[Bn256],
|
||||||
input_buffer: ptr Buffer,
|
input_buffer: ptr Buffer,
|
||||||
auth: ptr Auth,
|
auth: ptr Auth,
|
||||||
output_buffer: ptr Buffer): bool {.importc: "generate_proof".}
|
output_buffer: ptr Buffer): bool {.importc: "generate_proof".}
|
||||||
|
|
||||||
proc verify*(ctx: ptr RLN[Bn256],
|
proc verify*(ctx: RLN[Bn256],
|
||||||
proof_buffer: ptr Buffer,
|
proof_buffer: ptr Buffer,
|
||||||
result_ptr: ptr uint32): bool {.importc: "verify".}
|
result_ptr: ptr uint32): bool {.importc: "verify".}
|
||||||
#----------------------------------------------------------------------------------------------
|
#----------------------------------------------------------------------------------------------
|
||||||
|
@ -54,9 +55,9 @@ proc verify*(ctx: ptr RLN[Bn256],
|
||||||
|
|
||||||
proc new_circuit_from_params*(merkle_depth: uint,
|
proc new_circuit_from_params*(merkle_depth: uint,
|
||||||
parameters_buffer: ptr Buffer,
|
parameters_buffer: ptr Buffer,
|
||||||
ctx: ptr (ptr RLN[Bn256])): bool {.importc: "new_circuit_from_params".}
|
ctx: ptr RLN[Bn256]): bool {.importc: "new_circuit_from_params".}
|
||||||
|
|
||||||
proc hash*(ctx: ptr RLN[Bn256],
|
proc hash*(ctx: RLN[Bn256],
|
||||||
inputs_buffer: ptr Buffer,
|
inputs_buffer: ptr Buffer,
|
||||||
input_len: uint,
|
input_len: uint,
|
||||||
output_buffer: ptr Buffer): bool {.importc: "hash".}
|
output_buffer: ptr Buffer): bool {.importc: "hash".}
|
||||||
|
|
|
@ -8,7 +8,8 @@ import
|
||||||
## Bn256 and RLN are Nim wrappers for the data types used in
|
## Bn256 and RLN are Nim wrappers for the data types used in
|
||||||
## the rln library https://github.com/kilic/rln/blob/3bbec368a4adc68cd5f9bfae80b17e1bbb4ef373/src/ffi.rs
|
## the rln library https://github.com/kilic/rln/blob/3bbec368a4adc68cd5f9bfae80b17e1bbb4ef373/src/ffi.rs
|
||||||
type Bn256* = pointer
|
type Bn256* = pointer
|
||||||
type RLN*[E] {.incompleteStruct.} = object
|
type RLN*[E] = pointer
|
||||||
|
|
||||||
|
|
||||||
# Custom data types defined for waku rln relay -------------------------
|
# Custom data types defined for waku rln relay -------------------------
|
||||||
type MembershipKeyPair* = object
|
type MembershipKeyPair* = object
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import
|
import
|
||||||
chronicles, options, chronos, stint,
|
chronicles, options, chronos, stint,
|
||||||
web3,
|
web3,
|
||||||
|
stew/results,
|
||||||
stew/byteutils,
|
stew/byteutils,
|
||||||
rln,
|
rln,
|
||||||
waku_rln_relay_types
|
waku_rln_relay_types
|
||||||
|
@ -10,19 +11,20 @@ import
|
||||||
logScope:
|
logScope:
|
||||||
topics = "wakurlnrelayutils"
|
topics = "wakurlnrelayutils"
|
||||||
|
|
||||||
|
type RLNResult* = Result[RLN[Bn256], string]
|
||||||
# membership contract interface
|
# membership contract interface
|
||||||
contract(MembershipContract):
|
contract(MembershipContract):
|
||||||
# TODO define a return type of bool for register method to signify a successful registration
|
# TODO define a return type of bool for register method to signify a successful registration
|
||||||
proc register(pubkey: Uint256) # external payable
|
proc register(pubkey: Uint256) # external payable
|
||||||
|
|
||||||
proc createRLNInstance*(d: int, ctxPtr: var ptr RLN[Bn256]): bool
|
proc createRLNInstance*(d: int): RLNResult
|
||||||
{.raises: [Defect, IOError].} =
|
{.raises: [Defect, IOError].} =
|
||||||
|
|
||||||
## generates an instance of RLN
|
## generates an instance of RLN
|
||||||
## An RLN instance supports both zkSNARKs logics and Merkle tree data structure and operations
|
## An RLN instance supports both zkSNARKs logics and Merkle tree data structure and operations
|
||||||
## d indicates the depth of Merkle tree
|
## d indicates the depth of Merkle tree
|
||||||
var
|
var
|
||||||
ctxPtrPtr = addr(ctxPtr)
|
rlnInstance: RLN[Bn256]
|
||||||
merkleDepth: csize_t = uint(d)
|
merkleDepth: csize_t = uint(d)
|
||||||
# parameters.key contains the parameters related to the Poseidon hasher
|
# parameters.key contains the parameters related to the Poseidon hasher
|
||||||
# to generate this file, clone this repo https://github.com/kilic/rln
|
# to generate this file, clone this repo https://github.com/kilic/rln
|
||||||
|
@ -37,17 +39,17 @@ proc createRLNInstance*(d: int, ctxPtr: var ptr RLN[Bn256]): bool
|
||||||
# check the parameters.key is not empty
|
# check the parameters.key is not empty
|
||||||
if (pbytes.len == 0):
|
if (pbytes.len == 0):
|
||||||
debug "error in parameters.key"
|
debug "error in parameters.key"
|
||||||
return false
|
return err("error in parameters.key")
|
||||||
|
|
||||||
# create an instance of RLN
|
# create an instance of RLN
|
||||||
let res = new_circuit_from_params(merkleDepth, addr parametersBuffer, ctxPtrPtr)
|
let res = new_circuit_from_params(merkleDepth, addr parametersBuffer, addr rlnInstance)
|
||||||
# check whether the circuit parameters are generated successfully
|
# check whether the circuit parameters are generated successfully
|
||||||
if (res == false):
|
if (res == false):
|
||||||
debug "error in parameters generation"
|
debug "error in parameters generation"
|
||||||
return false
|
return err("error in parameters generation")
|
||||||
return true
|
return ok(rlnInstance)
|
||||||
|
|
||||||
proc membershipKeyGen*(ctxPtr: ptr RLN[Bn256]): Option[MembershipKeyPair] =
|
proc membershipKeyGen*(ctxPtr: RLN[Bn256]): Option[MembershipKeyPair] =
|
||||||
## generates a MembershipKeyPair that can be used for the registration into the rln membership contract
|
## generates a MembershipKeyPair that can be used for the registration into the rln membership contract
|
||||||
|
|
||||||
# keysBufferPtr will hold the generated key pairs i.e., secret and public keys
|
# keysBufferPtr will hold the generated key pairs i.e., secret and public keys
|
||||||
|
@ -79,7 +81,6 @@ proc membershipKeyGen*(ctxPtr: ptr RLN[Bn256]): Option[MembershipKeyPair] =
|
||||||
|
|
||||||
|
|
||||||
return some(keypair)
|
return some(keypair)
|
||||||
|
|
||||||
proc register*(rlnPeer: WakuRLNRelay): Future[bool] {.async.} =
|
proc register*(rlnPeer: WakuRLNRelay): Future[bool] {.async.} =
|
||||||
## registers the public key of the rlnPeer which is rlnPeer.membershipKeyPair.publicKey
|
## registers the public key of the rlnPeer which is rlnPeer.membershipKeyPair.publicKey
|
||||||
## into the membership contract whose address is in rlnPeer.membershipContractAddress
|
## into the membership contract whose address is in rlnPeer.membershipContractAddress
|
||||||
|
|
Loading…
Reference in New Issue