diff --git a/.gitmodules b/.gitmodules index ddce3de..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "zerokit"] - path = zerokit - url = git@github.com:vacp2p/zerokit.git diff --git a/Makefile b/Makefile deleted file mode 100644 index 8c16d40..0000000 --- a/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -.PHONY: rlnlib - -SHELL := bash # the shell used internally by Make - -GOBIN ?= $(shell which go) - -rlnlib: - scripts/build.sh - cd zerokit/rln && cbindgen --config ../../cbindgen.toml --crate rln --output ../../rln/librln.h --lang c - -test: - go test ./... -count 1 -v \ No newline at end of file diff --git a/README.md b/README.md index c9781df..1b64cbc 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,3 @@ # go-zerokit-rln Go wrappers for [zerokit's RLN](https://github.com/vacp2p/zerokit) - -### Building this library -``` -git clone https://github.com/waku-org/go-zerokit-rln -cd go-zerokit-rln -git submodule init -git submodule update --recursive -make -``` - -To generate smaller static libraries, before `make`, edit `./zerokit/rln/Cargo.toml` and use `branch = "no-ethers-core"` for `ark-circom` diff --git a/go.mod b/go.mod index 11e3af4..e367c6a 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,12 @@ module github.com/waku-org/go-zerokit-rln go 1.18 -require github.com/stretchr/testify v1.7.2 +require ( + github.com/stretchr/testify v1.7.2 + github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230331231302-258cacb91327 + github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230331223149-f90e66aebb0d + github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230331181847-cba74520bae9 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 4ef46d0..a9aca9e 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230331223042-3d24f0c813c3 h1:9V9Za2c8vaWM2ta/hrLibqUQw/KRgdZQdKejn/Q9exo= +github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230331223042-3d24f0c813c3/go.mod h1:KYykqtdApHVYZ3G0spwMnoxc5jH5eI3jyO9SwsSfi48= +github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230331231302-258cacb91327 h1:Q5XQqo+PEmvrybT8D7BEsKCwIYDi80s+00Q49cfm9Gs= +github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230331231302-258cacb91327/go.mod h1:KYykqtdApHVYZ3G0spwMnoxc5jH5eI3jyO9SwsSfi48= +github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230331223149-f90e66aebb0d h1:Kcg85Y2xGU6hqZ/kMfkLQF2jAog8vt+tw1/VNidzNtE= +github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230331223149-f90e66aebb0d/go.mod h1:7cSGUoGVIla1IpnChrLbkVjkYgdOcr7rcifEfh4ReR4= +github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230331181847-cba74520bae9 h1:u+YUlWDltHiK5upSb7M6mStc84zdc4vTCNNOz7R5RaY= +github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230331181847-cba74520bae9/go.mod h1:+LeEYoW5/uBUTVjtBGLEVCUe9mOYAlu5ZPkIxLOSr5Y= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/libs/aarch64-apple-darwin/librln.a b/libs/aarch64-apple-darwin/librln.a deleted file mode 100644 index 76bdd31..0000000 Binary files a/libs/aarch64-apple-darwin/librln.a and /dev/null differ diff --git a/libs/aarch64-unknown-linux-gnu/librln.a b/libs/aarch64-unknown-linux-gnu/librln.a deleted file mode 100644 index 5daa9ff..0000000 Binary files a/libs/aarch64-unknown-linux-gnu/librln.a and /dev/null differ diff --git a/libs/arm-unknown-linux-gnueabi/librln.a b/libs/arm-unknown-linux-gnueabi/librln.a deleted file mode 100644 index 4476aee..0000000 Binary files a/libs/arm-unknown-linux-gnueabi/librln.a and /dev/null differ diff --git a/libs/arm-unknown-linux-gnueabihf/librln.a b/libs/arm-unknown-linux-gnueabihf/librln.a deleted file mode 100644 index bcfcc34..0000000 Binary files a/libs/arm-unknown-linux-gnueabihf/librln.a and /dev/null differ diff --git a/libs/i686-unknown-linux-gnu/librln.a b/libs/i686-unknown-linux-gnu/librln.a deleted file mode 100644 index 654a767..0000000 Binary files a/libs/i686-unknown-linux-gnu/librln.a and /dev/null differ diff --git a/libs/x86_64-apple-darwin/librln.a b/libs/x86_64-apple-darwin/librln.a deleted file mode 100644 index b2d58d1..0000000 Binary files a/libs/x86_64-apple-darwin/librln.a and /dev/null differ diff --git a/libs/x86_64-pc-windows-gnu/librln.a b/libs/x86_64-pc-windows-gnu/librln.a deleted file mode 100644 index 0b841b9..0000000 Binary files a/libs/x86_64-pc-windows-gnu/librln.a and /dev/null differ diff --git a/libs/x86_64-unknown-linux-gnu/librln.a b/libs/x86_64-unknown-linux-gnu/librln.a deleted file mode 100644 index b433dde..0000000 Binary files a/libs/x86_64-unknown-linux-gnu/librln.a and /dev/null differ diff --git a/libs/x86_64-unknown-linux-musl/librln.a b/libs/x86_64-unknown-linux-musl/librln.a deleted file mode 100644 index d697a9f..0000000 Binary files a/libs/x86_64-unknown-linux-musl/librln.a and /dev/null differ diff --git a/rln/librln.h b/rln/librln.h deleted file mode 100644 index b7c6a71..0000000 --- a/rln/librln.h +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include - -#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 - * - * - * Also heavily inspired by - */ -typedef struct Buffer { - const uint8_t *ptr; - uintptr_t len; -} Buffer; - -bool new(uintptr_t tree_height, const struct Buffer *input_buffer, struct RLN **ctx); - -bool new_with_params(uintptr_t tree_height, - const struct Buffer *circom_buffer, - const struct Buffer *zkey_buffer, - const struct Buffer *vk_buffer, - struct RLN **ctx); - -bool set_tree(struct RLN *ctx, uintptr_t tree_height); - -bool delete_leaf(struct RLN *ctx, uintptr_t index); - -bool set_leaf(struct RLN *ctx, uintptr_t index, const struct Buffer *input_buffer); - -bool set_next_leaf(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); - -bool get_proof(const struct RLN *ctx, uintptr_t index, struct Buffer *output_buffer); - -bool prove(struct RLN *ctx, const struct Buffer *input_buffer, struct Buffer *output_buffer); - -bool verify(const struct RLN *ctx, const struct Buffer *proof_buffer, bool *proof_is_valid_ptr); - -bool generate_rln_proof(struct RLN *ctx, - const struct Buffer *input_buffer, - struct Buffer *output_buffer); - -bool verify_rln_proof(const struct RLN *ctx, - const struct Buffer *proof_buffer, - bool *proof_is_valid_ptr); - -bool verify_with_roots(const struct RLN *ctx, - const struct Buffer *proof_buffer, - const struct Buffer *roots_buffer, - bool *proof_is_valid_ptr); - -bool key_gen(const struct RLN *ctx, struct Buffer *output_buffer); - -bool seeded_key_gen(const struct RLN *ctx, - const struct Buffer *input_buffer, - struct Buffer *output_buffer); - -bool extended_key_gen(const struct RLN *ctx, struct Buffer *output_buffer); - -bool seeded_extended_key_gen(const struct RLN *ctx, - const struct Buffer *input_buffer, - struct Buffer *output_buffer); - -bool recover_id_secret(const struct RLN *ctx, - const struct Buffer *input_proof_buffer_1, - const struct Buffer *input_proof_buffer_2, - struct Buffer *output_buffer); - -bool hash(const struct Buffer *input_buffer, struct Buffer *output_buffer); - -bool poseidon_hash(const struct Buffer *input_buffer, struct Buffer *output_buffer); diff --git a/rln/link.go b/rln/link.go deleted file mode 100644 index 11f8ae0..0000000 --- a/rln/link.go +++ /dev/null @@ -1,16 +0,0 @@ -package rln - -/* -#cgo LDFLAGS:-lrln -ldl -lm -#cgo linux,arm LDFLAGS:-L${SRCDIR}/../libs/armv7-linux-androideabi -#cgo linux,arm64 LDFLAGS:-L${SRCDIR}/../libs/aarch64-unknown-linux-gnu -#cgo linux,amd64,musl,!android LDFLAGS:-L${SRCDIR}/../libs/x86_64-unknown-linux-musl -#cgo linux,amd64,!musl,!android LDFLAGS:-L${SRCDIR}/../libs/x86_64-unknown-linux-gnu -#cgo linux,386 LDFLAGS:-L${SRCDIR}/../libs/i686-unknown-linux-gnu -#cgo windows,386 LDFLAGS:-L${SRCDIR}/../libs/i686-pc-windows-gnu -lrln -lm -lws2_32 -luserenv -#cgo windows,amd64 LDFLAGS:-L${SRCDIR}/../libs/x86_64-pc-windows-gnu -lrln -lm -lws2_32 -luserenv -#cgo darwin,386,!ios LDFLAGS:-L${SRCDIR}/../libs/i686-apple-darwin -#cgo darwin,arm64,!ios LDFLAGS:-L${SRCDIR}/../libs/aarch64-apple-darwin -#cgo darwin,amd64,!ios LDFLAGS:-L${SRCDIR}/../libs/x86_64-apple-darwin -*/ -import "C" diff --git a/rln/link/apple.go b/rln/link/apple.go new file mode 100644 index 0000000..7936aea --- /dev/null +++ b/rln/link/apple.go @@ -0,0 +1,64 @@ +//go:build (386 || arm64 || amd64) && darwin && !ios +// +build 386 arm64 amd64 +// +build darwin +// +build !ios + +package link + +import r "github.com/waku-org/go-zerokit-rln-apple/rln" + +type RLNWrapper struct { + ffi *r.RLN +} + +func NewWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RLNWrapper, error) { + rln, err := r.NewWithParams(depth, wasm, zkey, verifKey) + if err != nil { + return nil, err + } + return &RLNWrapper{ffi: rln}, nil +} + +func NewWithFolder(depth int, resourcesFolderPath string) (*RLNWrapper, error) { + rln, err := r.NewWithFolder(depth, resourcesFolderPath) + if err != nil { + return nil, err + } + return &RLNWrapper{ffi: rln}, nil +} + +func (i RLNWrapper) ExtendedKeyGen() []byte { + return i.ffi.ExtendedKeyGen() +} + +func (i RLNWrapper) Hash(input []byte) ([]byte, error) { + return i.ffi.Hash(input) +} + +func (i RLNWrapper) PoseidonHash(input []byte) ([]byte, error) { + return i.ffi.PoseidonHash(input) +} + +func (i RLNWrapper) SetNextLeaf(idcommitment []byte) bool { + return i.ffi.SetNextLeaf(idcommitment) +} + +func (i RLNWrapper) SetLeavesFrom(index uint, idcommitments []byte) bool { + return i.ffi.SetLeavesFrom(index, idcommitments) +} + +func (i RLNWrapper) DeleteLeaf(index uint) bool { + return i.ffi.DeleteLeaf(index) +} + +func (i RLNWrapper) GetRoot() ([]byte, error) { + return i.ffi.GetRoot() +} + +func (i RLNWrapper) GenerateRLNProof(input []byte) ([]byte, error) { + return i.ffi.GenerateRLNProof(input) +} + +func (i RLNWrapper) VerifyWithRoots(input []byte, roots []byte) (bool, error) { + return i.ffi.VerifyWithRoots(input, roots) +} diff --git a/rln/link/arm.go b/rln/link/arm.go new file mode 100644 index 0000000..557cca6 --- /dev/null +++ b/rln/link/arm.go @@ -0,0 +1,63 @@ +//go:build (arm || arm64) && linux +// +build arm arm64 +// +build linux + +package link + +import r "github.com/waku-org/go-zerokit-rln-arm/rln" + +type RLNWrapper struct { + ffi *r.RLN +} + +func NewWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RLNWrapper, error) { + rln, err := r.NewWithParams(depth, wasm, zkey, verifKey) + if err != nil { + return nil, err + } + return &RLNWrapper{ffi: rln}, nil +} + +func NewWithFolder(depth int, resourcesFolderPath string) (*RLNWrapper, error) { + rln, err := r.NewWithFolder(depth, resourcesFolderPath) + if err != nil { + return nil, err + } + return &RLNWrapper{ffi: rln}, nil +} + +func (i RLNWrapper) ExtendedKeyGen() []byte { + return i.ffi.ExtendedKeyGen() +} + +func (i RLNWrapper) Hash(input []byte) ([]byte, error) { + return i.ffi.Hash(input) +} + +func (i RLNWrapper) PoseidonHash(input []byte) ([]byte, error) { + return i.ffi.PoseidonHash(input) +} + +func (i RLNWrapper) SetNextLeaf(idcommitment []byte) bool { + return i.ffi.SetNextLeaf(idcommitment) +} + +func (i RLNWrapper) SetLeavesFrom(index uint, idcommitments []byte) bool { + return i.ffi.SetLeavesFrom(index, idcommitments) +} + +func (i RLNWrapper) DeleteLeaf(index uint) bool { + return i.ffi.DeleteLeaf(index) +} + +func (i RLNWrapper) GetRoot() ([]byte, error) { + return i.ffi.GetRoot() +} + +func (i RLNWrapper) GenerateRLNProof(input []byte) ([]byte, error) { + return i.ffi.GenerateRLNProof(input) +} + +func (i RLNWrapper) VerifyWithRoots(input []byte, roots []byte) (bool, error) { + return i.ffi.VerifyWithRoots(input, roots) +} diff --git a/rln/link/x86_64.go b/rln/link/x86_64.go new file mode 100644 index 0000000..eee9e0c --- /dev/null +++ b/rln/link/x86_64.go @@ -0,0 +1,64 @@ +//go:build (linux || windows) && amd64 && !android +// +build linux windows +// +build amd64 +// +build !android + +package link + +import r "github.com/waku-org/go-zerokit-rln-x86_64/rln" + +type RLNWrapper struct { + ffi *r.RLN +} + +func NewWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RLNWrapper, error) { + rln, err := r.NewWithParams(depth, wasm, zkey, verifKey) + if err != nil { + return nil, err + } + return &RLNWrapper{ffi: rln}, nil +} + +func NewWithFolder(depth int, resourcesFolderPath string) (*RLNWrapper, error) { + rln, err := r.NewWithFolder(depth, resourcesFolderPath) + if err != nil { + return nil, err + } + return &RLNWrapper{ffi: rln}, nil +} + +func (i RLNWrapper) ExtendedKeyGen() []byte { + return i.ffi.ExtendedKeyGen() +} + +func (i RLNWrapper) Hash(input []byte) ([]byte, error) { + return i.ffi.Hash(input) +} + +func (i RLNWrapper) PoseidonHash(input []byte) ([]byte, error) { + return i.ffi.PoseidonHash(input) +} + +func (i RLNWrapper) SetNextLeaf(idcommitment []byte) bool { + return i.ffi.SetNextLeaf(idcommitment) +} + +func (i RLNWrapper) SetLeavesFrom(index uint, idcommitments []byte) bool { + return i.ffi.SetLeavesFrom(index, idcommitments) +} + +func (i RLNWrapper) DeleteLeaf(index uint) bool { + return i.ffi.DeleteLeaf(index) +} + +func (i RLNWrapper) GetRoot() ([]byte, error) { + return i.ffi.GetRoot() +} + +func (i RLNWrapper) GenerateRLNProof(input []byte) ([]byte, error) { + return i.ffi.GenerateRLNProof(input) +} + +func (i RLNWrapper) VerifyWithRoots(input []byte, roots []byte) (bool, error) { + return i.ffi.VerifyWithRoots(input, roots) +} diff --git a/rln/rln.go b/rln/rln.go index 8b830c7..61698da 100644 --- a/rln/rln.go +++ b/rln/rln.go @@ -1,27 +1,23 @@ package rln -/* -#include "./librln.h" -*/ import "C" import ( "encoding/binary" "errors" "fmt" - "unsafe" + "github.com/waku-org/go-zerokit-rln/rln/link" "github.com/waku-org/go-zerokit-rln/rln/resources" ) // RLN represents the context used for rln. type RLN struct { - ptr *C.RLN + w *link.RLNWrapper } // NewRLN generates an instance of RLN. An instance supports both zkSNARKs logics // and Merkle tree data structure and operations. It uses a depth of 20 by default func NewRLN() (*RLN, error) { - wasm, err := resources.Asset("tree_height_20/rln.wasm") if err != nil { return nil, err @@ -41,12 +37,9 @@ func NewRLN() (*RLN, error) { depth := 20 - wasmBuffer := toCBufferPtr(wasm) - zkeyBuffer := toCBufferPtr(zkey) - verifKeyBuffer := toCBufferPtr(verifKey) - - if !bool(C.new_with_params(C.uintptr_t(depth), wasmBuffer, zkeyBuffer, verifKeyBuffer, &r.ptr)) { - return nil, errors.New("failed to initialize") + r.w, err = link.NewWithParams(depth, wasm, zkey, verifKey) + if err != nil { + return nil, err } return r, nil @@ -56,13 +49,11 @@ func NewRLN() (*RLN, error) { // and Merkle tree data structure and operations. The parameter `depth“ indicates the depth of Merkle tree func NewRLNWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RLN, error) { r := &RLN{} + var err error - wasmBuffer := toCBufferPtr(wasm) - zkeyBuffer := toCBufferPtr(zkey) - verifKeyBuffer := toCBufferPtr(verifKey) - - if !bool(C.new_with_params(C.uintptr_t(depth), wasmBuffer, zkeyBuffer, verifKeyBuffer, &r.ptr)) { - return nil, errors.New("failed to initialize") + r.w, err = link.NewWithParams(depth, wasm, zkey, verifKey) + if err != nil { + return nil, err } return r, nil @@ -74,30 +65,21 @@ func NewRLNWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RL func NewRLNWithFolder(depth int, resourcesFolderPath string) (*RLN, error) { r := &RLN{} - pathBuffer := toCBufferPtr([]byte(resourcesFolderPath)) + var err error - if !bool(C.new(C.uintptr_t(depth), pathBuffer, &r.ptr)) { - return nil, errors.New("failed to initialize") + r.w, err = link.NewWithFolder(depth, resourcesFolderPath) + if err != nil { + return nil, err } return r, nil } -func toCBufferPtr(input []byte) *C.Buffer { - buf := toBuffer(input) - - size := int(unsafe.Sizeof(buf)) - in := (*C.Buffer)(C.malloc(C.size_t(size))) - *in = buf - - return in -} - // MembershipKeyGen generates a IdentityCredential that can be used for the // registration into the rln membership contract. Returns an error if the key generation fails func (r *RLN) MembershipKeyGen() (*IdentityCredential, error) { - buffer := toBuffer([]byte{}) - if !bool(C.extended_key_gen(r.ptr, &buffer)) { + generatedKeys := r.w.ExtendedKeyGen() + if generatedKeys == nil { return nil, errors.New("error in key generation") } @@ -108,8 +90,6 @@ func (r *RLN) MembershipKeyGen() (*IdentityCredential, error) { IDCommitment: [32]byte{}, } - // the public and secret keys together are 64 bytes - generatedKeys := C.GoBytes(unsafe.Pointer(buffer.ptr), C.int(buffer.len)) if len(generatedKeys) != 32*4 { return nil, errors.New("generated keys are of invalid length") } @@ -124,68 +104,41 @@ func (r *RLN) MembershipKeyGen() (*IdentityCredential, error) { // appendLength returns length prefixed version of the input with the following format // [len<8>|input], the len is a 8 byte value serialized in little endian + func appendLength(input []byte) []byte { inputLen := make([]byte, 8) binary.LittleEndian.PutUint64(inputLen, uint64(len(input))) return append(inputLen, input...) } -// toBuffer converts the input to a buffer object that is used to communicate data with the rln lib -func toBuffer(data []byte) C.Buffer { - dataPtr, dataLen := sliceToPtr(data) - return C.Buffer{ - ptr: dataPtr, - len: C.uintptr_t(dataLen), - } -} - -func sliceToPtr(slice []byte) (*C.uchar, C.int) { - if len(slice) == 0 { - return nil, 0 - } else { - return (*C.uchar)(unsafe.Pointer(&slice[0])), C.int(len(slice)) - } -} - func (r *RLN) Sha256(data []byte) (MerkleNode, error) { lenPrefData := appendLength(data) - hashInputBuffer := toCBufferPtr(lenPrefData) - - var output []byte - out := toBuffer(output) - - if !bool(C.hash(hashInputBuffer, &out)) { - return MerkleNode{}, errors.New("failed to hash") + b, err := r.w.Hash(lenPrefData) + if err != nil { + return MerkleNode{}, err } - b := C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)) - var result MerkleNode copy(result[:], b) return result, nil } -func (r *RLN) Poseidon(input ...[]byte) ([32]byte, error) { +func (r *RLN) Poseidon(input ...[]byte) (MerkleNode, error) { data := serializeSlice(input) inputLen := make([]byte, 8) binary.LittleEndian.PutUint64(inputLen, uint64(len(input))) lenPrefData := append(inputLen, data...) - hashInputBuffer := toCBufferPtr(lenPrefData) - var output []byte - out := toBuffer(output) - - if !bool(C.poseidon_hash(hashInputBuffer, &out)) { - return [32]byte{}, errors.New("error in poseidon hash") + b, err := r.w.PoseidonHash(lenPrefData) + if err != nil { + return MerkleNode{}, err } - b := C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)) - - var result [32]byte + var result MerkleNode copy(result[:], b) return result, nil @@ -213,17 +166,11 @@ func ExtractMetadata(proof RateLimitProof) (ProofMetadata, error) { // integers wrapped in <> indicate value sizes in bytes func (r *RLN) GenerateProof(data []byte, key IdentityCredential, index MembershipIndex, epoch Epoch) (*RateLimitProof, error) { input := serialize(key.IDSecretHash, index, epoch, data) - inputBuffer := toCBufferPtr(input) - - var output []byte - out := toBuffer(output) - - if !bool(C.generate_rln_proof(r.ptr, inputBuffer, &out)) { - return nil, errors.New("could not generate the proof") + proofBytes, err := r.w.GenerateRLNProof(input) + if err != nil { + return nil, err } - proofBytes := C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)) - if len(proofBytes) != 320 { return nil, errors.New("invalid proof generated") } @@ -300,14 +247,11 @@ func serializeCommitments(commitments []IDCommitment) []byte { // As default, it is set to an empty sequence of roots. This implies that the validity check for the proof's root is skipped func (r *RLN) Verify(data []byte, proof RateLimitProof, roots ...[32]byte) (bool, error) { proofBytes := proof.serialize(data) - proofBuf := toCBufferPtr(proofBytes) - rootBytes := serialize32(roots) - rootBuf := toCBufferPtr(rootBytes) - res := C.bool(false) - if !bool(C.verify_with_roots(r.ptr, proofBuf, rootBuf, &res)) { - return false, errors.New("could not verify with roots") + res, err := r.w.VerifyWithRoots(proofBytes, rootBytes) + if err != nil { + return false, err } return bool(res), nil @@ -315,8 +259,7 @@ func (r *RLN) Verify(data []byte, proof RateLimitProof, roots ...[32]byte) (bool // InsertMember adds the member to the tree func (r *RLN) InsertMember(idComm IDCommitment) error { - idCommBuffer := toCBufferPtr(idComm[:]) - insertionSuccess := bool(C.set_next_leaf(r.ptr, idCommBuffer)) + insertionSuccess := r.w.SetNextLeaf(idComm[:]) if !insertionSuccess { return errors.New("could not insert member") } @@ -327,8 +270,7 @@ func (r *RLN) InsertMember(idComm IDCommitment) error { // 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)) + insertionSuccess := r.w.SetLeavesFrom(index, idCommBytes) if !insertionSuccess { return errors.New("could not insert members") } @@ -339,7 +281,7 @@ func (r *RLN) InsertMembers(index MembershipIndex, idComms []IDCommitment) error // 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 func (r *RLN) DeleteMember(index MembershipIndex) error { - deletionSuccess := bool(C.delete_leaf(r.ptr, C.uintptr_t(index))) + deletionSuccess := r.w.DeleteLeaf(index) if !deletionSuccess { return errors.New("could not delete member") } @@ -348,15 +290,11 @@ func (r *RLN) DeleteMember(index MembershipIndex) error { // GetMerkleRoot reads the Merkle Tree root after insertion func (r *RLN) GetMerkleRoot() (MerkleNode, error) { - var output []byte - out := toBuffer(output) - - if !bool(C.get_root(r.ptr, &out)) { - return MerkleNode{}, errors.New("could not get the root") + b, err := r.w.GetRoot() + if err != nil { + return MerkleNode{}, err } - b := C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)) - if len(b) != 32 { return MerkleNode{}, errors.New("wrong output size") } diff --git a/scripts/Cross.toml b/scripts/Cross.toml deleted file mode 100644 index 6a32952..0000000 --- a/scripts/Cross.toml +++ /dev/null @@ -1,32 +0,0 @@ -[target.x86_64-pc-windows-gnu] -image = "ghcr.io/cross-rs/x86_64-pc-windows-gnu:local" - -[target.aarch64-unknown-linux-gnu] -image = "ghcr.io/cross-rs/aarch64-unknown-linux-gnu:local" - -[target.x86_64-unknown-linux-gnu] -image = "ghcr.io/cross-rs/x86_64-unknown-linux-gnu:local" - -[target.arm-unknown-linux-gnueabi] -image = "ghcr.io/cross-rs/arm-unknown-linux-gnueabi:local" - -[target.i686-pc-windows-gnu] -image = "ghcr.io/cross-rs/i686-pc-windows-gnu:local" - -[target.i686-unknown-linux-gnu] -image = "ghcr.io/cross-rs/i686-unknown-linux-gnu:local" - -[target.arm-unknown-linux-gnueabihf] -image = "ghcr.io/cross-rs/arm-unknown-linux-gnueabihf:local" - -[target.mips-unknown-linux-gnu] -image = "ghcr.io/cross-rs/mips-unknown-linux-gnu:local" - -[target.mips64-unknown-linux-gnuabi64] -image = "ghcr.io/cross-rs/mips64-unknown-linux-gnuabi64:local" - -[target.mips64el-unknown-linux-gnuabi64] -image = "ghcr.io/cross-rs/mips64el-unknown-linux-gnuabi64:local" - -[target.mipsel-unknown-linux-gnu] -image = "ghcr.io/cross-rs/mipsel-unknown-linux-gnu:local" diff --git a/scripts/build.sh b/scripts/build.sh deleted file mode 100755 index f6787cd..0000000 --- a/scripts/build.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash - -DIRECTORY=./libs -if [[ -d "$DIRECTORY" ]] -then - echo "$DIRECTORY exists on your filesystem. Delete it and run the script again." - exit 0 -fi - -export RUSTFLAGS="-Ccodegen-units=1" - -rustup default stable - -cargo install cross --git https://github.com/cross-rs/cross --branch main - -pushd zerokit/rln - -cargo clean - - -cross build --release --lib --target=aarch64-unknown-linux-gnu -cross build --release --lib --target=arm-unknown-linux-gnueabi -cross build --release --lib --target=arm-unknown-linux-gnueabihf -#cross build --release --lib --target=i686-pc-windows-gnu -cross build --release --lib --target=i686-unknown-linux-gnu -cross build --release --lib --target=x86_64-pc-windows-gnu -cross build --release --lib --target=x86_64-unknown-linux-gnu -cross build --release --lib --target=x86_64-unknown-linux-musl -#cross build --release --lib --target=aarch64-linux-android -#cross build --release --lib --target=armv7-linux-androideabi -#cross build --release --lib --target=i686-linux-android -#cross build --release --lib --target=x86_64-linux-android - -# TODO: these work only on iOS -cargo install cargo-lipo -rustup target add aarch64-apple-ios x86_64-apple-ios x86_64-apple-darwin aarch64-apple-darwin -cargo build --release --target=x86_64-apple-darwin --lib -cargo build --release --target=aarch64-apple-darwin --lib -#cargo build --release --target=x86_64-apple-ios --lib -#cargo build --release --target=aarch64-apple-ios --lib -cargo lipo --release - -popd - -TOOLS_DIR=`dirname $0` -COMPILE_DIR=${TOOLS_DIR}/../zerokit/target -rm -rf $COMPILE_DIR/x86_64-apple-ios $COMPILE_DIR/aarch64-apple-ios -for platform in `ls ${COMPILE_DIR} | grep -v release | grep -v debug` -do - PLATFORM_DIR=${DIRECTORY}/$platform - mkdir -p ${PLATFORM_DIR} - cp ${COMPILE_DIR}/$platform/release/librln.a ${PLATFORM_DIR} -done diff --git a/zerokit b/zerokit deleted file mode 160000 index c319f32..0000000 --- a/zerokit +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c319f32a1e4a3471554006523c90856a943b366e