mirror of https://github.com/waku-org/go-rln.git
added better funcs
This commit is contained in:
parent
c298e971b1
commit
3482f3b885
34
rln/rln.go
34
rln/rln.go
|
@ -1,3 +1,4 @@
|
|||
// Package rln contains bindings for https://github.com/kilic/rln
|
||||
package rln
|
||||
|
||||
/*
|
||||
|
@ -10,10 +11,21 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
// RLN represents the context used for rln.
|
||||
type RLN struct {
|
||||
ptr *C.RLN_Bn256
|
||||
}
|
||||
|
||||
// KeyPair generated by the GenerateKey function for RLN values.
|
||||
type KeyPair struct {
|
||||
// Key represents the secret key.
|
||||
Key [32]byte
|
||||
|
||||
// Commitment hash of the Key generated by a hash function in the rln lib.
|
||||
Commitment [32]byte
|
||||
}
|
||||
|
||||
// New returns a new RLN generated from the passed depth and parameters.
|
||||
func New(depth int, parameters []byte) (*RLN, error) {
|
||||
r := &RLN{}
|
||||
|
||||
|
@ -23,7 +35,7 @@ func New(depth int, parameters []byte) (*RLN, error) {
|
|||
in := (*C.Buffer)(C.malloc(C.size_t(size)))
|
||||
*in = buf
|
||||
|
||||
if bool(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")
|
||||
}
|
||||
|
||||
|
@ -54,9 +66,23 @@ func (r *RLN) GenerateProof(input, output []byte) bool {
|
|||
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) GenerateKey() (*KeyPair, error) {
|
||||
buffer := toBuffer([]byte{})
|
||||
if !bool(C.key_gen(r.ptr, &buffer)) {
|
||||
return nil, errors.New("failed to genenrate key")
|
||||
}
|
||||
|
||||
key := &KeyPair{
|
||||
Key: [32]byte{},
|
||||
Commitment: [32]byte{},
|
||||
}
|
||||
|
||||
b := C.GoBytes(unsafe.Pointer(buffer.ptr), C.int(buffer.len))
|
||||
|
||||
copy(key.Key[:], b[:32])
|
||||
copy(key.Commitment[:], b[:32])
|
||||
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func (r *RLN) Verify(proof []byte, publicInputs []byte, result uint32) bool {
|
||||
|
|
|
@ -2,6 +2,7 @@ package rln_test
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/decanus/go-rln/rln"
|
||||
|
@ -18,3 +19,28 @@ func TestNew(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateKey(t *testing.T) {
|
||||
params, err := ioutil.ReadFile("./testdata/parameters.key")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := rln.New(32, params)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
k, err := r.GenerateKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(k.Key, [32]byte{}) {
|
||||
t.Fatal("k.Key was empty")
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(k.Commitment, [32]byte{}) {
|
||||
t.Fatal("k.Commitment was empty")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue