From c298e971b1f2863e5bcdd048c22b8435fb3c0067 Mon Sep 17 00:00:00 2001 From: decanus <7621705+decanus@users.noreply.github.com> Date: Mon, 13 Sep 2021 22:50:57 +0200 Subject: [PATCH] fix --- rln/rln.go | 13 ++++++++----- rln/rln_test.go | 11 ++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/rln/rln.go b/rln/rln.go index 8ce4c73..7ad1b81 100644 --- a/rln/rln.go +++ b/rln/rln.go @@ -6,6 +6,7 @@ package rln import "C" import ( + "errors" "unsafe" ) @@ -13,7 +14,7 @@ type RLN struct { ptr *C.RLN_Bn256 } -func New(depth int, parameters []byte) *RLN { +func New(depth int, parameters []byte) (*RLN, error) { r := &RLN{} buf := toBuffer(parameters) @@ -22,9 +23,11 @@ func New(depth int, parameters []byte) *RLN { in := (*C.Buffer)(C.malloc(C.size_t(size))) *in = buf - C.new_circuit_from_params(C.ulong(depth), in, &r.ptr) + if bool(C.new_circuit_from_params(C.ulong(depth), in, &r.ptr)) { + return nil, errors.New("failed to initialize") + } - return r + return r, nil } func (r *RLN) Hash(input []byte) []byte { @@ -56,11 +59,11 @@ func (r *RLN) GenerateKey(buf []byte) bool { return bool(C.key_gen(r.ptr, &buffer)) } -func (r *RLN) Verify(proof []byte, publicInputs []byte, result uint32) { +func (r *RLN) Verify(proof []byte, publicInputs []byte, result uint32) bool { proofBuf := toBuffer(proof) inputs := toBuffer(publicInputs) res := C.uint(result) - C.verify(r.ptr, &proofBuf, &inputs, &res) + return bool(C.verify(r.ptr, &proofBuf, &inputs, &res)) } func toBuffer(data []byte) C.Buffer { diff --git a/rln/rln_test.go b/rln/rln_test.go index 2cb3052..d6af0c9 100644 --- a/rln/rln_test.go +++ b/rln/rln_test.go @@ -1,8 +1,10 @@ -package rln +package rln_test import ( "io/ioutil" "testing" + + "github.com/decanus/go-rln/rln" ) func TestNew(t *testing.T) { @@ -11,9 +13,8 @@ func TestNew(t *testing.T) { t.Fatal(err) } - rln := New(32, params) - - if rln.ptr == nil { - t.Fatal("pointer not initialized.") + _, err = rln.New(32, params) + if err != nil { + t.Fatal(err) } }