diff --git a/Makefile b/Makefile index 13dafb0..a9db647 100644 --- a/Makefile +++ b/Makefile @@ -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. \ No newline at end of file diff --git a/README.md b/README.md index fe38dc0..3296672 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/lib/librln.d b/lib/librln.d deleted file mode 100644 index 863f40b..0000000 --- a/lib/librln.d +++ /dev/null @@ -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 diff --git a/lib/librln.rlib b/lib/librln.rlib deleted file mode 100644 index efe37ed..0000000 Binary files a/lib/librln.rlib and /dev/null differ diff --git a/pkg/rln/rln.go b/pkg/rln/rln.go deleted file mode 100644 index b6b8b7b..0000000 --- a/pkg/rln/rln.go +++ /dev/null @@ -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)) - } -} diff --git a/rln.go b/rln.go index 4c8a123..930f79c 100644 --- a/rln.go +++ b/rln.go @@ -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 } diff --git a/lib/librln.h b/rln/librln.h similarity index 100% rename from lib/librln.h rename to rln/librln.h diff --git a/rln/rln.go b/rln/rln.go new file mode 100644 index 0000000..45039ab --- /dev/null +++ b/rln/rln.go @@ -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)) + } +} diff --git a/rln/rln_android.go b/rln/rln_android.go new file mode 100644 index 0000000..bf0aa51 --- /dev/null +++ b/rln/rln_android.go @@ -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" diff --git a/rln/rln_arm.go b/rln/rln_arm.go new file mode 100644 index 0000000..db93508 --- /dev/null +++ b/rln/rln_arm.go @@ -0,0 +1,8 @@ +// +build linux,arm,!arm7 + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/arm-unknown-linux-gnueabi -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_arm64.go b/rln/rln_arm64.go new file mode 100644 index 0000000..ebc3a50 --- /dev/null +++ b/rln/rln_arm64.go @@ -0,0 +1,8 @@ +// +build linux,arm64 + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/aarch64-unknown-linux-gnu -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_arm7.go b/rln/rln_arm7.go new file mode 100644 index 0000000..a097ad0 --- /dev/null +++ b/rln/rln_arm7.go @@ -0,0 +1,8 @@ +// +build arm7 + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/arm-unknown-linux-gnueabihf -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_darwin.go b/rln/rln_darwin.go new file mode 100644 index 0000000..619467c --- /dev/null +++ b/rln/rln_darwin.go @@ -0,0 +1,8 @@ +// +build darwin,386,!ios + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/i686-apple-darwin -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_darwin64.go b/rln/rln_darwin64.go new file mode 100644 index 0000000..71c3db0 --- /dev/null +++ b/rln/rln_darwin64.go @@ -0,0 +1,8 @@ +// +build darwin,amd64,!ios + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-apple-darwin -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_darwin64_m1.go b/rln/rln_darwin64_m1.go new file mode 100644 index 0000000..eb1ea49 --- /dev/null +++ b/rln/rln_darwin64_m1.go @@ -0,0 +1,8 @@ +// +build darwin,arm64,!ios + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/aarch64-apple-darwin -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_ios.go b/rln/rln_ios.go new file mode 100644 index 0000000..9d1ea94 --- /dev/null +++ b/rln/rln_ios.go @@ -0,0 +1,8 @@ +// +build ios + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/universal -lrln -ldl -lm -framework Security -framework Foundation +*/ +import "C" diff --git a/rln/rln_linux386.go b/rln/rln_linux386.go new file mode 100644 index 0000000..f5d0c08 --- /dev/null +++ b/rln/rln_linux386.go @@ -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" diff --git a/rln/rln_linux64.go b/rln/rln_linux64.go new file mode 100644 index 0000000..4b1afcd --- /dev/null +++ b/rln/rln_linux64.go @@ -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" diff --git a/rln/rln_linux_musl.go b/rln/rln_linux_musl.go new file mode 100644 index 0000000..086e65f --- /dev/null +++ b/rln/rln_linux_musl.go @@ -0,0 +1,8 @@ +// +build !android,musl + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/x86_64-unknown-linux-musl -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_mips.go b/rln/rln_mips.go new file mode 100644 index 0000000..59ddb87 --- /dev/null +++ b/rln/rln_mips.go @@ -0,0 +1,8 @@ +// +build linux,mips + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/mips-unknown-linux-gnu -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_mips64.go b/rln/rln_mips64.go new file mode 100644 index 0000000..31ad25d --- /dev/null +++ b/rln/rln_mips64.go @@ -0,0 +1,8 @@ +// +build linux,mips64 + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/mips64-unknown-linux-gnuabi64 -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_mips64le.go b/rln/rln_mips64le.go new file mode 100644 index 0000000..1b6feab --- /dev/null +++ b/rln/rln_mips64le.go @@ -0,0 +1,8 @@ +// +build linux,mips64le + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/mips64el-unknown-linux-gnuabi64 -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_mipsle.go b/rln/rln_mipsle.go new file mode 100644 index 0000000..7c43cbc --- /dev/null +++ b/rln/rln_mipsle.go @@ -0,0 +1,8 @@ +// +build linux,mipsle + +package rln + +/* +#cgo LDFLAGS: -L${SRCDIR}/../libs/mipsel-unknown-linux-gnu -lrln -ldl -lm +*/ +import "C" diff --git a/rln/rln_windows.go b/rln/rln_windows.go new file mode 100644 index 0000000..f23b3e8 --- /dev/null +++ b/rln/rln_windows.go @@ -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" diff --git a/rln/rln_windows64.go b/rln/rln_windows64.go new file mode 100644 index 0000000..04f1a23 --- /dev/null +++ b/rln/rln_windows64.go @@ -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" diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100644 index 0000000..5d6924d --- /dev/null +++ b/scripts/build.sh @@ -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