feat: insert multiple members

This commit is contained in:
Richard Ramos 2022-11-26 13:58:25 -04:00
parent d338ed9d40
commit e1bed5294a
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C
13 changed files with 57 additions and 8 deletions

View File

@ -4,7 +4,7 @@ Go wrappers for [zerokit's RLN](https://github.com/vacp2p/zerokit)
### Building this library
```
git clone https://github.com/status-im/go-zerokit-rln
git clone https://github.com/waku-org/go-zerokit-rln
cd go-zerokit-rln
git submodule init
git submodule update --recursive

4
go.mod
View File

@ -1,6 +1,6 @@
module github.com/status-im/go-zerokit-rln
module github.com/waku-org/go-zerokit-rln
go 1.17
go 1.18
require github.com/stretchr/testify v1.7.2

Binary file not shown.

Binary file not shown.

View File

@ -5,13 +5,20 @@
#define TEST_PARAMETERS_INDEX 2
/**
* The RLN object.
*
* It implements the methods required to update the internal Merkle Tree, generate and verify RLN ZK proofs.
*
* I/O is mostly done using writers and readers implementing `std::io::Write` and `std::io::Read`, respectively.
*/
typedef struct RLN RLN;
/**
* Buffer struct is taken from
* https://github.com/celo-org/celo-threshold-bls-rs/blob/master/crates/threshold-bls-ffi/src/ffi.rs
* <https://github.com/celo-org/celo-threshold-bls-rs/blob/master/crates/threshold-bls-ffi/src/ffi.rs>
*
* Also heavily inspired by https://github.com/kilic/rln/blob/master/src/ffi.rs
* Also heavily inspired by <https://github.com/kilic/rln/blob/master/src/ffi.rs>
*/
typedef struct Buffer {
const uint8_t *ptr;
@ -34,7 +41,9 @@ bool set_leaf(struct RLN *ctx, uintptr_t index, const struct Buffer *input_buffe
bool set_next_leaf(struct RLN *ctx, const struct Buffer *input_buffer);
bool set_leaves(struct RLN *ctx, const struct Buffer *input_buffer);
bool set_leaves_from(struct RLN *ctx, uintptr_t index, const struct Buffer *input_buffer);
bool init_tree_with_leaves(struct RLN *ctx, const struct Buffer *input_buffer);
bool get_root(const struct RLN *ctx, struct Buffer *output_buffer);

View File

@ -9,7 +9,7 @@ import (
"errors"
"unsafe"
"github.com/status-im/go-zerokit-rln/rln/resources"
"github.com/waku-org/go-zerokit-rln/rln/resources"
)
// RLN represents the context used for rln.
@ -240,6 +240,23 @@ func serializeRoots(roots [][32]byte) []byte {
return result
}
func serializeCommitments(commitments []IDCommitment) []byte {
// serializes a seq of IDCommitments to a byte seq
// the serialization is based on https://github.com/status-im/nwaku/blob/37bd29fbc37ce5cf636734e7dd410b1ed27b88c8/waku/v2/protocol/waku_rln_relay/rln.nim#L142
// the order of serialization is |id_commitment_len<8>|id_commitment<var>|
var result []byte
inputLen := make([]byte, 8)
binary.LittleEndian.PutUint64(inputLen, uint64(len(commitments)))
result = append(result, inputLen...)
for _, idComm := range commitments {
result = append(result, idComm[:]...)
}
return result
}
func (r *RLN) VerifyWithRoots(data []byte, proof RateLimitProof, roots [][32]byte) (bool, error) {
proofBytes := proof.serialize(data)
proofBuf := toCBufferPtr(proofBytes)
@ -265,6 +282,18 @@ func (r *RLN) InsertMember(idComm IDCommitment) error {
return nil
}
// Insert multiple members i.e., identity commitments starting from index
// This proc is atomic, i.e., if any of the insertions fails, all the previous insertions are rolled back
func (r *RLN) InsertMembers(index MembershipIndex, idComms []IDCommitment) error {
idCommBytes := serializeCommitments(idComms)
idCommBuffer := toCBufferPtr(idCommBytes)
insertionSuccess := bool(C.set_leaves_from(r.ptr, C.uintptr_t(index), idCommBuffer))
if !insertionSuccess {
return errors.New("could not insert members")
}
return nil
}
// DeleteMember removes an IDCommitment key from the tree. The index
// parameter is the position of the id commitment key to be deleted from the tree.
// The deleted id commitment key is replaced with a zero leaf

View File

@ -57,6 +57,17 @@ func (s *RLNSuite) TestInsertMember() {
s.NoError(err)
}
func (s *RLNSuite) TestInsertMembers() {
rln, err := NewRLN()
s.NoError(err)
keypair, err := rln.MembershipKeyGen()
s.NoError(err)
err = rln.InsertMembers(0, []IDCommitment{keypair.IDCommitment})
s.NoError(err)
}
func (s *RLNSuite) TestRemoveMember() {
rln, err := NewRLN()
s.NoError(err)

@ -1 +1 @@
Subproject commit b95b151a1c2407c897d486dbab2c480684ae2b7e
Subproject commit 284e51483c73e956c2c318fe6f69d735412bdc08