mirror of
https://github.com/logos-messaging/go-zerokit-rln.git
synced 2026-01-02 05:03:10 +00:00
refactor: split into submodules
* chore: remove zerokit submodule * refactor: split go-zerokit-rln into submodules
This commit is contained in:
parent
eaf62c1713
commit
f3f2c16093
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
||||
[submodule "zerokit"]
|
||||
path = zerokit
|
||||
url = git@github.com:vacp2p/zerokit.git
|
||||
12
Makefile
12
Makefile
@ -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
|
||||
11
README.md
11
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`
|
||||
|
||||
7
go.mod
7
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
|
||||
|
||||
8
go.sum
8
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=
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
88
rln/librln.h
88
rln/librln.h
@ -1,88 +0,0 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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>
|
||||
*
|
||||
* Also heavily inspired by <https://github.com/kilic/rln/blob/master/src/ffi.rs>
|
||||
*/
|
||||
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);
|
||||
16
rln/link.go
16
rln/link.go
@ -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"
|
||||
64
rln/link/apple.go
Normal file
64
rln/link/apple.go
Normal file
@ -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)
|
||||
}
|
||||
63
rln/link/arm.go
Normal file
63
rln/link/arm.go
Normal file
@ -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)
|
||||
}
|
||||
64
rln/link/x86_64.go
Normal file
64
rln/link/x86_64.go
Normal file
@ -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)
|
||||
}
|
||||
134
rln/rln.go
134
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<var>], 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")
|
||||
}
|
||||
|
||||
@ -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"
|
||||
@ -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
|
||||
1
zerokit
1
zerokit
@ -1 +0,0 @@
|
||||
Subproject commit c319f32a1e4a3471554006523c90856a943b366e
|
||||
Loading…
x
Reference in New Issue
Block a user