├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .gitmodules ├── LICENSE ├── Makefile ├── README.md ├── go.mod ├── go.sum ├── lib └── cbindgen.toml ├── libs ├── aarch64-apple-darwin │ ├── librln.d │ └── librln.rlib ├── x86_64-apple-darwin │ ├── librln.d │ └── librln.rlib └── x86_64-unknown-linux-musl │ ├── librln.d │ └── librln.rlib ├── rln ├── librln.h ├── rln.go ├── rln_android.go ├── rln_arm.go ├── rln_arm64.go ├── rln_arm7.go ├── rln_darwin.go ├── rln_darwin64.go ├── rln_darwin64_m1.go ├── rln_ios.go ├── rln_linux386.go ├── rln_linux64.go ├── rln_linux_musl.go ├── rln_mips.go ├── rln_mips64.go ├── rln_mips64le.go ├── rln_mipsle.go ├── rln_test.go ├── rln_windows.go ├── rln_windows64.go └── testdata │ └── parameters.key └── scripts └── build.sh /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | 9 | jobs: 10 | test: 11 | strategy: 12 | matrix: 13 | go-version: [1.16.x] 14 | platform: [ubuntu-latest] 15 | runs-on: ${{ matrix.platform }} 16 | steps: 17 | - name: Install Go 18 | uses: actions/setup-go@v2 19 | with: 20 | go-version: ${{ matrix.go-version }} 21 | - name: Checkout code 22 | uses: actions/checkout@v2 23 | - name: Test 24 | run: go test ./... 25 | lint: 26 | strategy: 27 | matrix: 28 | go-version: [1.15.x, 1.16.x] 29 | platform: [ubuntu-latest] 30 | runs-on: ${{ matrix.platform }} 31 | steps: 32 | - uses: actions/checkout@master 33 | - uses: reviewdog/action-golangci-lint@v1 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "rln"] 2 | path = lib/rln 3 | url = https://github.com/kilic/rln 4 | [submodule "lib/rln"] 5 | path = lib/rln 6 | url = git@github.com:kilic/rln.git 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Dean Eigenmann 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: rlnlib 2 | 3 | rlnlib: 4 | sh scripts/build.sh 5 | cd lib/rln && cbindgen --config ../cbindgen.toml --crate rln --output ../../rln/librln.h --lang c 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-rln 2 | 3 | Wrappers for [kilic/rln](https://github.com/kilic/rln) along with an implementation for rate-limiting using RLN inspired 4 | by the [Waku v2 RLN Relay](https://rfc.vac.dev/spec/17/) built by [Status](https://status.im). 5 | 6 | Further research can be found here: 7 | - https://forum.vac.dev/t/vac-3-zk/97 8 | - https://github.com/vacp2p/research/tree/master/rln-research 9 | - https://ethresear.ch/t/semaphore-rln-rate-limiting-nullifier-for-spam-prevention-in-anonymous-p2p-setting/5009 10 | 11 | The goal of this is to create a rate-limiter for blockchains where block production is cheap. I started playing around with this 12 | after talking to the team at [Celestia](https://celestia.org/). 13 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/decanus/go-rln 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/cbergoon/merkletree v0.2.0 h1:Bttqr3OuoiZEo4ed1L7fTasHka9II+BF9fhBfbNEEoQ= 2 | github.com/cbergoon/merkletree v0.2.0/go.mod h1:5c15eckUgiucMGDOCanvalj/yJnD+KAZj1qyJtRW5aM= 3 | -------------------------------------------------------------------------------- /lib/cbindgen.toml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decanus/go-rln/e0e31478e351ec601321f719a205deaf9722b8a4/lib/cbindgen.toml -------------------------------------------------------------------------------- /libs/aarch64-apple-darwin/librln.d: -------------------------------------------------------------------------------- 1 | /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/target/aarch64-apple-darwin/release/librln.dylib: /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/mod.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/polynomial.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/poseidon.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/rln.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/ffi.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/lib.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/merkle.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/poseidon.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/public.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/utils.rs 2 | -------------------------------------------------------------------------------- /libs/aarch64-apple-darwin/librln.rlib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decanus/go-rln/e0e31478e351ec601321f719a205deaf9722b8a4/libs/aarch64-apple-darwin/librln.rlib -------------------------------------------------------------------------------- /libs/x86_64-apple-darwin/librln.d: -------------------------------------------------------------------------------- 1 | /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/target/x86_64-apple-darwin/release/librln.dylib: /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/mod.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/polynomial.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/poseidon.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/rln.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/ffi.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/lib.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/merkle.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/poseidon.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/public.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/utils.rs 2 | -------------------------------------------------------------------------------- /libs/x86_64-apple-darwin/librln.rlib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decanus/go-rln/e0e31478e351ec601321f719a205deaf9722b8a4/libs/x86_64-apple-darwin/librln.rlib -------------------------------------------------------------------------------- /libs/x86_64-unknown-linux-musl/librln.d: -------------------------------------------------------------------------------- 1 | /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/target/x86_64-unknown-linux-musl/release/librln.rlib: /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/mod.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/polynomial.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/poseidon.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/circuit/rln.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/ffi.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/lib.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/merkle.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/poseidon.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/public.rs /Users/deaneigenmann/go/src/github.com/decanus/go-rln/lib/rln/src/utils.rs 2 | -------------------------------------------------------------------------------- /libs/x86_64-unknown-linux-musl/librln.rlib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decanus/go-rln/e0e31478e351ec601321f719a205deaf9722b8a4/libs/x86_64-unknown-linux-musl/librln.rlib -------------------------------------------------------------------------------- /rln/librln.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef struct RLN_Bn256 RLN_Bn256; 7 | 8 | /** 9 | * Buffer struct is taken from 10 | * https://github.com/celo-org/celo-threshold-bls-rs/blob/master/crates/threshold-bls-ffi/src/ffi.rs 11 | */ 12 | typedef struct Buffer { 13 | const uint8_t *ptr; 14 | uintptr_t len; 15 | } Buffer; 16 | 17 | typedef struct Auth { 18 | const struct Buffer *secret_buffer; 19 | uintptr_t index; 20 | } Auth; 21 | 22 | bool new_circuit_from_params(uintptr_t merkle_depth, 23 | const struct Buffer *parameters_buffer, 24 | struct RLN_Bn256 **ctx); 25 | 26 | bool get_root(const struct RLN_Bn256 *ctx, struct Buffer *output_buffer); 27 | 28 | bool update_next_member(struct RLN_Bn256 *ctx, const struct Buffer *input_buffer); 29 | 30 | bool delete_member(struct RLN_Bn256 *ctx, uintptr_t index); 31 | 32 | bool generate_proof(const struct RLN_Bn256 *ctx, 33 | const struct Buffer *input_buffer, 34 | const struct Auth *auth, 35 | struct Buffer *output_buffer); 36 | 37 | bool verify(const struct RLN_Bn256 *ctx, struct Buffer *proof_buffer, uint32_t *result_ptr); 38 | 39 | bool hash(const struct RLN_Bn256 *ctx, 40 | const struct Buffer *inputs_buffer, 41 | uintptr_t input_len, 42 | struct Buffer *output_buffer); 43 | 44 | bool key_gen(const struct RLN_Bn256 *ctx, struct Buffer *keypair_buffer); 45 | -------------------------------------------------------------------------------- /rln/rln.go: -------------------------------------------------------------------------------- 1 | // Package rln contains bindings for https://github.com/kilic/rln 2 | package rln 3 | 4 | /* 5 | #include "./librln.h" 6 | */ 7 | import "C" 8 | 9 | import ( 10 | "errors" 11 | "unsafe" 12 | ) 13 | 14 | // RLN represents the context used for rln. 15 | type RLN struct { 16 | ptr *C.RLN_Bn256 17 | } 18 | 19 | // KeyPair generated by the GenerateKey function for RLN values. 20 | type KeyPair struct { 21 | // Key represents the secret key. 22 | Key [32]byte 23 | 24 | // Commitment hash of the Key generated by a hash function in the rln lib. 25 | Commitment [32]byte 26 | } 27 | 28 | // @TODO THINK ABOUT AUTH OBJECT 29 | 30 | // New returns a new RLN generated from the passed depth and parameters. 31 | func New(depth int, parameters []byte) (*RLN, error) { 32 | r := &RLN{} 33 | 34 | buf := toBuffer(parameters) 35 | 36 | size := int(unsafe.Sizeof(buf)) 37 | in := (*C.Buffer)(C.malloc(C.size_t(size))) 38 | *in = buf 39 | 40 | if !bool(C.new_circuit_from_params(C.ulong(depth), in, &r.ptr)) { 41 | return nil, errors.New("failed to initialize") 42 | } 43 | 44 | return r, nil 45 | } 46 | 47 | // GenerateKey generates a KeyPair for an RLN. 48 | func (r *RLN) GenerateKey() (*KeyPair, error) { 49 | buffer := toBuffer([]byte{}) 50 | if !bool(C.key_gen(r.ptr, &buffer)) { 51 | return nil, errors.New("failed to genenrate key") 52 | } 53 | 54 | key := &KeyPair{ 55 | Key: [32]byte{}, 56 | Commitment: [32]byte{}, 57 | } 58 | 59 | b := C.GoBytes(unsafe.Pointer(buffer.ptr), C.int(buffer.len)) 60 | 61 | copy(key.Key[:], b[:32]) 62 | copy(key.Commitment[:], b[32:64]) 63 | 64 | return key, nil 65 | } 66 | 67 | // Hash hashes a given input using the underlying function. 68 | func (r *RLN) Hash(input []byte) ([]byte, error) { 69 | 70 | buf := toBuffer(input) 71 | size := int(unsafe.Sizeof(buf)) 72 | in := (*C.Buffer)(C.malloc(C.size_t(size))) 73 | *in = buf 74 | 75 | var output []byte 76 | out := toBuffer(output) 77 | 78 | if !bool(C.hash(r.ptr, in, 1, &out)) { 79 | return nil, errors.New("failed to hash") 80 | } 81 | 82 | return C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)), nil 83 | } 84 | 85 | // GenerateProof generates a proof for the RLN given a KeyPair and the index in a merkle tree. 86 | func (r *RLN) GenerateProof(input []byte, key *KeyPair, index uint) ([]byte, error) { 87 | inputBuf := toBuffer(input) 88 | 89 | var output []byte 90 | out := toBuffer(output) 91 | 92 | keybuf := toBuffer(key.Key[:]) 93 | auth := &C.Auth{ 94 | secret_buffer: &keybuf, 95 | index: C.ulong(index), 96 | } 97 | 98 | if !bool(C.generate_proof(r.ptr, &inputBuf, auth, &out)) { 99 | return nil, errors.New("failed to generate proof") 100 | } 101 | 102 | return C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)), nil 103 | } 104 | 105 | // Verify verifies a proof generated for the RLN. 106 | func (r *RLN) Verify(proof []byte) bool { 107 | proofBuf := toBuffer(proof) 108 | 109 | result := uint32(0) 110 | res := C.uint(result) 111 | if !bool(C.verify(r.ptr, &proofBuf, &res)) { 112 | // @TODO THINK ABOUT ERROR? 113 | return false 114 | } 115 | 116 | return uint32(res) == 0 117 | } 118 | 119 | func (r *RLN) UpdateNextMember(input []byte) error { 120 | buf := toBuffer(input) 121 | if !bool(C.update_next_member(r.ptr, &buf)) { 122 | return errors.New("failed to update next member") 123 | } 124 | 125 | return nil 126 | } 127 | 128 | func (r *RLN) DeleteMember(index int) error { 129 | if !bool(C.delete_member(r.ptr, C.ulong(index))) { 130 | return errors.New("failed to delete member") 131 | } 132 | 133 | return nil 134 | } 135 | 136 | func (r *RLN) GetRoot() ([]byte, error) { 137 | var output []byte 138 | out := toBuffer(output) 139 | 140 | if !bool(C.get_root(r.ptr, &out)) { 141 | return nil, errors.New("failed to get root") 142 | } 143 | 144 | return C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)), nil 145 | } 146 | 147 | func toBuffer(data []byte) C.Buffer { 148 | dataPtr, dataLen := sliceToPtr(data) 149 | return C.Buffer{ 150 | ptr: dataPtr, 151 | len: C.ulong(dataLen), 152 | } 153 | } 154 | 155 | func sliceToPtr(slice []byte) (*C.uchar, C.int) { 156 | if len(slice) == 0 { 157 | return nil, 0 158 | } else { 159 | return (*C.uchar)(unsafe.Pointer(&slice[0])), C.int(len(slice)) 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /rln/rln_android.go: -------------------------------------------------------------------------------- 1 | // +build android 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/aarch64-linux-android -L${SRCDIR}/../libs/armv7-linux-androideabi -L${SRCDIR}/../libs/i686-linux-android -L${SRCDIR}/../libs/x86_64-linux-android -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_arm.go: -------------------------------------------------------------------------------- 1 | // +build linux,arm,!arm7 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/arm-unknown-linux-gnueabi -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_arm64.go: -------------------------------------------------------------------------------- 1 | // +build linux,arm64 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/aarch64-unknown-linux-gnu -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_arm7.go: -------------------------------------------------------------------------------- 1 | // +build arm7 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/arm-unknown-linux-gnueabihf -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_darwin.go: -------------------------------------------------------------------------------- 1 | // +build darwin,386,!ios 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/i686-apple-darwin -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_darwin64.go: -------------------------------------------------------------------------------- 1 | // +build darwin,amd64,!ios 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-apple-darwin -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_darwin64_m1.go: -------------------------------------------------------------------------------- 1 | // +build darwin,arm64,!ios 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/aarch64-apple-darwin -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_ios.go: -------------------------------------------------------------------------------- 1 | // +build ios 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/universal -lrln -ldl -lm -framework Security -framework Foundation 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_linux386.go: -------------------------------------------------------------------------------- 1 | // +build !android,linux,386,!musl 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/i686-unknown-linux-gnu -lrln -ldl -lm -lpthread 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_linux64.go: -------------------------------------------------------------------------------- 1 | // +build !android,linux,amd64,!musl 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-unknown-linux-gnu -lrln -ldl -lm -lpthread 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_linux_musl.go: -------------------------------------------------------------------------------- 1 | // +build !android,musl 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-unknown-linux-musl -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_mips.go: -------------------------------------------------------------------------------- 1 | // +build linux,mips 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/mips-unknown-linux-gnu -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_mips64.go: -------------------------------------------------------------------------------- 1 | // +build linux,mips64 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/mips64-unknown-linux-gnuabi64 -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_mips64le.go: -------------------------------------------------------------------------------- 1 | // +build linux,mips64le 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/mips64el-unknown-linux-gnuabi64 -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_mipsle.go: -------------------------------------------------------------------------------- 1 | // +build linux,mipsle 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/mipsel-unknown-linux-gnu -lrln -ldl -lm 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_test.go: -------------------------------------------------------------------------------- 1 | package rln_test 2 | 3 | import ( 4 | "encoding/hex" 5 | "io/ioutil" 6 | "reflect" 7 | "testing" 8 | 9 | "github.com/decanus/go-rln/rln" 10 | ) 11 | 12 | func TestNew(t *testing.T) { 13 | params, err := ioutil.ReadFile("./testdata/parameters.key") 14 | if err != nil { 15 | t.Fatal(err) 16 | } 17 | 18 | _, err = rln.New(32, params) 19 | if err != nil { 20 | t.Fatal(err) 21 | } 22 | } 23 | 24 | func TestGenerateKey(t *testing.T) { 25 | params, err := ioutil.ReadFile("./testdata/parameters.key") 26 | if err != nil { 27 | t.Fatal(err) 28 | } 29 | 30 | r, err := rln.New(32, params) 31 | if err != nil { 32 | t.Fatal(err) 33 | } 34 | 35 | k, err := r.GenerateKey() 36 | if err != nil { 37 | t.Fatal(err) 38 | } 39 | 40 | if reflect.DeepEqual(k.Key, [32]byte{}) { 41 | t.Fatal("k.Key was empty") 42 | } 43 | 44 | if reflect.DeepEqual(k.Commitment, [32]byte{}) { 45 | t.Fatal("k.Commitment was empty") 46 | } 47 | } 48 | 49 | func TestRLN_Hash(t *testing.T) { 50 | // This test is based on tests from: 51 | // https://github.com/status-im/nim-waku/blob/b7998de09d1ef04599a699938da69aecfa63cc6f/tests/v2/test_waku_rln_relay.nim#L527 52 | 53 | params, err := ioutil.ReadFile("./testdata/parameters.key") 54 | if err != nil { 55 | t.Fatal(err) 56 | } 57 | 58 | r, err := rln.New(32, params) 59 | if err != nil { 60 | t.Fatal(err) 61 | } 62 | 63 | input := byteArray(32, 1) 64 | 65 | output, err := r.Hash(input) 66 | if err != nil { 67 | t.Fatal(err) 68 | } 69 | 70 | expected := "53a6338cdbf02f0563cec1898e354d0d272c8f98b606c538945c6f41ef101828" 71 | if expected != hex.EncodeToString(output) { 72 | t.Fatalf("value %x did not match expected %s", output, expected) 73 | } 74 | } 75 | 76 | func TestRLN_GetRoot(t *testing.T) { 77 | // This test is based on tests from: 78 | // https://github.com/status-im/nim-waku/blob/b7998de09d1ef04599a699938da69aecfa63cc6f/tests/v2/test_waku_rln_relay.nim#L320 79 | 80 | params, err := ioutil.ReadFile("./testdata/parameters.key") 81 | if err != nil { 82 | t.Fatal(err) 83 | } 84 | 85 | r, err := rln.New(32, params) 86 | if err != nil { 87 | t.Fatal(err) 88 | } 89 | 90 | root1, err := r.GetRoot() 91 | if err != nil { 92 | t.Fatal(err) 93 | } 94 | 95 | root2, err := r.GetRoot() 96 | if err != nil { 97 | t.Fatal(err) 98 | } 99 | 100 | if hex.EncodeToString(root1) != hex.EncodeToString(root2) { 101 | t.Fatalf("value %x did not match expected %x", root1, root2) 102 | } 103 | } 104 | 105 | func byteArray(length int, value byte) []byte { 106 | arr := make([]byte, length) 107 | 108 | for i := 0; i < length; i++ { 109 | arr[i] = value 110 | } 111 | 112 | return arr 113 | } 114 | -------------------------------------------------------------------------------- /rln/rln_windows.go: -------------------------------------------------------------------------------- 1 | // +build windows,386 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/i686-pc-windows-gnu -lrln -lm -lws2_32 -luserenv 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/rln_windows64.go: -------------------------------------------------------------------------------- 1 | // +build windows,amd64 2 | 3 | package rln 4 | 5 | /* 6 | #cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-pc-windows-gnu -lrln -lm -lws2_32 -luserenv 7 | */ 8 | import "C" 9 | -------------------------------------------------------------------------------- /rln/testdata/parameters.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/decanus/go-rln/e0e31478e351ec601321f719a205deaf9722b8a4/rln/testdata/parameters.key -------------------------------------------------------------------------------- /scripts/build.sh: -------------------------------------------------------------------------------- 1 | DIRECTORY=./libs 2 | if [[ -d "$DIRECTORY" ]] 3 | then 4 | echo "$DIRECTORY exists on your filesystem. Delete it and run the script again." 5 | exit 0 6 | fi 7 | 8 | pushd lib/rln 9 | 10 | export RUSTFLAGS="-Ccodegen-units=1" 11 | rustup default 1.52.1 12 | cargo install cargo-lipo -f 13 | cargo install cargo-strip -f 14 | 15 | rustup target add x86_64-unknown-linux-gnu 16 | rustup target add aarch64-unknown-linux-gnu 17 | rustup target add x86_64-apple-darwin 18 | rustup target add aarch64-apple-darwin 19 | rustup target add x86_64-pc-windows-gnu 20 | rustup target add aarch64-linux-android 21 | rustup target add armv7-linux-androideabi 22 | rustup target add i686-linux-android 23 | rustup target add x86_64-linux-android 24 | rustup target add x86_64-unknown-linux-musl 25 | rustup target add aarch64-apple-ios x86_64-apple-ios 26 | 27 | cargo build --release --target=aarch64-linux-android --lib 28 | cargo strip --target aarch64-linux-android 29 | cargo build --release --target=armv7-linux-androideabi --lib 30 | cargo strip --target armv7-linux-androideabi 31 | cargo build --release --target=i686-linux-android --lib 32 | cargo strip --target i686-linux-android 33 | cargo build --release --target=x86_64-linux-android --lib 34 | cargo strip --target x86_64-linux-android 35 | cargo build --target=x86_64-unknown-linux-gnu --release 36 | cargo strip --target x86_64-unknown-linux-gnu 37 | cargo build --target=aarch64-unknown-linux-gnu --release 38 | cargo strip --target aarch64-unknown-linux-gnu 39 | cargo build --target=x86_64-apple-darwin --release 40 | cargo strip --target x86_64-apple-darwin 41 | cargo build --target=aarch64-apple-darwin --release 42 | cargo strip --target aarch64-apple-darwin 43 | cargo build --target=x86_64-pc-windows-gnu --release 44 | cargo strip --target x86_64-pc-windows-gnu 45 | cargo build --target=x86_64-unknown-linux-musl --release 46 | cargo strip --target x86_64-unknown-linux-musl 47 | cargo lipo --release --targets=aarch64-apple-ios,x86_64-apple-ios 48 | 49 | popd 50 | 51 | TOOLS_DIR=`dirname $0` 52 | COMPILE_DIR=${TOOLS_DIR}/../lib/rln/target 53 | rm -rf $COMPILE_DIR/x86_64-apple-ios $COMPILE_DIR/aarch64-apple-ios 54 | for platform in `ls ${COMPILE_DIR} | grep -v release | grep -v debug` 55 | do 56 | PLATFORM_DIR=${DIRECTORY}/$platform 57 | mkdir -p ${PLATFORM_DIR} 58 | cp ${COMPILE_DIR}/$platform/release/librln.* ${PLATFORM_DIR} 59 | done 60 | --------------------------------------------------------------------------------