This commit is contained in:
decanus 2021-09-13 18:41:52 +02:00
parent 63ee41dab3
commit cd5c593d26
No known key found for this signature in database
GPG Key ID: 3730AAF5D6589867
26 changed files with 277 additions and 58 deletions

View File

@ -1,8 +1,9 @@
.PHONY: rlnlib
rlnlib:
cd lib/rln && cargo build --release --target=aarch64-apple-darwin
cbindgen --config ../cbindgen.toml --crate rln --output librln.h --lang c
mv lib/rln/target/release/librln.* lib/
sh scripts/build.sh
#cd lib/rln && cargo build --release --target=aarch64-apple-darwin
#cbindgen --config ../cbindgen.toml --crate rln --output librln.h --lang c
#mv lib/rln/target/release/librln.* lib/
# @TODO TARGET FOR BINDINGS.

View File

@ -1,2 +1,6 @@
# go-rln
RLN wrappers and implementation in Go
Wrappers for [kilic/rln](https://github.com/kilic/rln) along with an implementation for rate-limiting using RLN inspired
by the [Waku v2 RLN Relay](https://rfc.vac.dev/spec/17/) built by [Status](https://status.im).
The goal of this is to create a block submission rate-limiter for [Celestia](https://celestia.org/)

View File

@ -1 +0,0 @@
/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

Binary file not shown.

View File

@ -1,51 +0,0 @@
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../../lib/ -llibrln -ldl -lm
#include "./../../lib/librln.h"
*/
import "C"
import "unsafe"
type Buffer struct {
Ptr *[]byte
Len uint
}
type RLN struct {
ptr *C.RLN_Bn256
}
//func (r *RLN) CircuitFromParams(depth int, parameters []byte) bool {
// return C.new_circuit_from_params(depth, toBuffer(parameters), r.ptr)
//}
//
//func (r *RLN) GenerateProof(input, output []byte) bool {
// //bool generate_proof(const struct RLN_Bn256 *ctx,
// // const struct Buffer *input_buffer,
// //struct Buffer *output_buffer);
//
// return C.generate_proof(r.ptr, toBuffer(input), toBuffer(output));
//}
func (r *RLN) GenerateKey() {
C.key_gen(r.ptr, &Buffer{})
//bool key_gen(const struct RLN_Bn256 *ctx, struct Buffer *keypair_buffer);
}
func toBuffer(data []byte) C.Buffer {
dataPtr, dataLen := sliceToPtr(data)
return C.Buffer{
ptr: dataPtr,
len: C.int(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))
}
}

10
rln.go
View File

@ -1,9 +1,15 @@
package main
import "github.com/decanus/go-rln/pkg/rln"
import (
"fmt"
"github.com/decanus/go-rln/rln"
)
func main() {
r := &rln.RLN{}
r.GenerateKey()
test := []byte{}
r.GenerateKey(test)
fmt.Printf("Test: %v", test)
//C
}

65
rln/rln.go Normal file
View File

@ -0,0 +1,65 @@
package rln
/*
#include "./librln.h"
*/
import "C"
import (
"unsafe"
)
type RLN struct {
ptr *C.RLN_Bn256
}
func (r *RLN) Hash(input []byte) []byte {
out := &C.Buffer{}
in := toBuffer(input)
C.hash(r.ptr, &in, &in.len, out)
return []byte{}
}
func (r *RLN) CircuitFromParams(depth int, parameters []byte) bool {
ptr := r.ptr
buf := toBuffer(parameters)
return bool(C.new_circuit_from_params(C.ulong(depth), &buf, &ptr))
}
func (r *RLN) GenerateProof(input, output []byte) bool {
inputBuf := toBuffer(input)
outputBuf := toBuffer(output)
return bool(C.generate_proof(r.ptr, &inputBuf, &outputBuf))
}
func (r *RLN) GenerateKey(buf []byte) bool {
buffer := toBuffer(buf)
return bool(C.key_gen(r.ptr, &buffer))
}
func (r *RLN) Verify(proof []byte, publicInputs []byte, result uint32) {
proofBuf := toBuffer(proof)
inputs := toBuffer(publicInputs)
res := C.uint(result)
C.verify(r.ptr, &proofBuf, &inputs, &res)
}
func toBuffer(data []byte) C.Buffer {
dataPtr, dataLen := sliceToPtr(data)
return C.Buffer{
ptr: dataPtr,
len: C.ulong(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))
}
}

8
rln/rln_android.go Normal file
View File

@ -0,0 +1,8 @@
// +build android
package rln
/*
#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
*/
import "C"

8
rln/rln_arm.go Normal file
View File

@ -0,0 +1,8 @@
// +build linux,arm,!arm7
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/arm-unknown-linux-gnueabi -lrln -ldl -lm
*/
import "C"

8
rln/rln_arm64.go Normal file
View File

@ -0,0 +1,8 @@
// +build linux,arm64
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/aarch64-unknown-linux-gnu -lrln -ldl -lm
*/
import "C"

8
rln/rln_arm7.go Normal file
View File

@ -0,0 +1,8 @@
// +build arm7
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/arm-unknown-linux-gnueabihf -lrln -ldl -lm
*/
import "C"

8
rln/rln_darwin.go Normal file
View File

@ -0,0 +1,8 @@
// +build darwin,386,!ios
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/i686-apple-darwin -lrln -ldl -lm
*/
import "C"

8
rln/rln_darwin64.go Normal file
View File

@ -0,0 +1,8 @@
// +build darwin,amd64,!ios
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-apple-darwin -lrln -ldl -lm
*/
import "C"

8
rln/rln_darwin64_m1.go Normal file
View File

@ -0,0 +1,8 @@
// +build darwin,arm64,!ios
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/aarch64-apple-darwin -lrln -ldl -lm
*/
import "C"

8
rln/rln_ios.go Normal file
View File

@ -0,0 +1,8 @@
// +build ios
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/universal -lrln -ldl -lm -framework Security -framework Foundation
*/
import "C"

8
rln/rln_linux386.go Normal file
View File

@ -0,0 +1,8 @@
// +build !android,linux,386,!musl
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/i686-unknown-linux-gnu -lrln -ldl -lm -lpthread
*/
import "C"

8
rln/rln_linux64.go Normal file
View File

@ -0,0 +1,8 @@
// +build !android,linux,amd64,!musl
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-unknown-linux-gnu -lrln -ldl -lm -lpthread
*/
import "C"

8
rln/rln_linux_musl.go Normal file
View File

@ -0,0 +1,8 @@
// +build !android,musl
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-unknown-linux-musl -lrln -ldl -lm
*/
import "C"

8
rln/rln_mips.go Normal file
View File

@ -0,0 +1,8 @@
// +build linux,mips
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/mips-unknown-linux-gnu -lrln -ldl -lm
*/
import "C"

8
rln/rln_mips64.go Normal file
View File

@ -0,0 +1,8 @@
// +build linux,mips64
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/mips64-unknown-linux-gnuabi64 -lrln -ldl -lm
*/
import "C"

8
rln/rln_mips64le.go Normal file
View File

@ -0,0 +1,8 @@
// +build linux,mips64le
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/mips64el-unknown-linux-gnuabi64 -lrln -ldl -lm
*/
import "C"

8
rln/rln_mipsle.go Normal file
View File

@ -0,0 +1,8 @@
// +build linux,mipsle
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/mipsel-unknown-linux-gnu -lrln -ldl -lm
*/
import "C"

8
rln/rln_windows.go Normal file
View File

@ -0,0 +1,8 @@
// +build windows,386
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/i686-pc-windows-gnu -lrln -lm -lws2_32 -luserenv
*/
import "C"

8
rln/rln_windows64.go Normal file
View File

@ -0,0 +1,8 @@
// +build windows,amd64
package rln
/*
#cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-pc-windows-gnu -lrln -lm -lws2_32 -luserenv
*/
import "C"

59
scripts/build.sh Normal file
View File

@ -0,0 +1,59 @@
DIRECTORY=./libs
if [[ -d "$DIRECTORY" ]]
then
echo "$DIRECTORY exists on your filesystem. Delete it and run the script again."
exit 0
fi
pushd lib/rln
export RUSTFLAGS="-Ccodegen-units=1"
rustup default 1.52.1
cargo install cargo-lipo -f
cargo install cargo-strip -f
rustup target add x86_64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-gnu
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
rustup target add x86_64-pc-windows-gnu
rustup target add aarch64-linux-android
rustup target add armv7-linux-androideabi
rustup target add i686-linux-android
rustup target add x86_64-linux-android
rustup target add x86_64-unknown-linux-musl
rustup target add aarch64-apple-ios x86_64-apple-ios
cargo build --release --target=aarch64-linux-android --lib
cargo strip --target aarch64-linux-android
cargo build --release --target=armv7-linux-androideabi --lib
cargo strip --target armv7-linux-androideabi
cargo build --release --target=i686-linux-android --lib
cargo strip --target i686-linux-android
cargo build --release --target=x86_64-linux-android --lib
cargo strip --target x86_64-linux-android
cargo build --target=x86_64-unknown-linux-gnu --release
cargo strip --target x86_64-unknown-linux-gnu
cargo build --target=aarch64-unknown-linux-gnu --release
cargo strip --target aarch64-unknown-linux-gnu
cargo build --target=x86_64-apple-darwin --release
cargo strip --target x86_64-apple-darwin
cargo build --target=aarch64-apple-darwin --release
cargo strip --target aarch64-apple-darwin
cargo build --target=x86_64-pc-windows-gnu --release
cargo strip --target x86_64-pc-windows-gnu
cargo build --target=x86_64-unknown-linux-musl --release
cargo strip --target x86_64-unknown-linux-musl
cargo lipo --release --targets=aarch64-apple-ios,x86_64-apple-ios
popd
TOOLS_DIR=`dirname $0`
COMPILE_DIR=${TOOLS_DIR}/../lib/rln/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.* ${PLATFORM_DIR}
done