This commit is contained in:
decanus 2021-09-13 22:50:57 +02:00
parent 2606c6d870
commit c298e971b1
No known key found for this signature in database
GPG Key ID: 3730AAF5D6589867
2 changed files with 14 additions and 10 deletions

View File

@ -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 {

View File

@ -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)
}
}