go-rln/rln/rln.go

158 lines
3.4 KiB
Go
Raw Normal View History

2021-09-13 23:10:24 +02:00
// Package rln contains bindings for https://github.com/kilic/rln
2021-09-13 18:41:52 +02:00
package rln
/*
#include "./librln.h"
*/
import "C"
import (
2021-09-13 22:50:57 +02:00
"errors"
2021-09-13 18:41:52 +02:00
"unsafe"
)
2021-09-13 23:10:24 +02:00
// RLN represents the context used for rln.
2021-09-13 18:41:52 +02:00
type RLN struct {
ptr *C.RLN_Bn256
}
2021-09-13 23:10:24 +02:00
// 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
}
2021-09-14 13:13:16 +02:00
// @TODO THINK ABOUT AUTH OBJECT
2021-09-13 23:10:24 +02:00
// New returns a new RLN generated from the passed depth and parameters.
2021-09-13 22:50:57 +02:00
func New(depth int, parameters []byte) (*RLN, error) {
2021-09-13 22:20:32 +02:00
r := &RLN{}
buf := toBuffer(parameters)
2021-09-13 22:41:27 +02:00
size := int(unsafe.Sizeof(buf))
in := (*C.Buffer)(C.malloc(C.size_t(size)))
*in = buf
2021-09-13 23:10:24 +02:00
if !bool(C.new_circuit_from_params(C.ulong(depth), in, &r.ptr)) {
2021-09-13 22:50:57 +02:00
return nil, errors.New("failed to initialize")
}
2021-09-13 22:20:32 +02:00
2021-09-13 22:50:57 +02:00
return r, nil
2021-09-13 22:20:32 +02:00
}
2021-09-13 23:13:04 +02:00
// GenerateKey generates a KeyPair for an RLN.
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:64])
return key, nil
}
2021-09-13 23:15:01 +02:00
// Hash hashes a given input using the underlying function.
func (r *RLN) Hash(input []byte) ([]byte, error) {
size := int(unsafe.Sizeof(C.Buffer{}))
2021-09-13 22:06:26 +02:00
in := (*C.Buffer)(C.malloc(C.size_t(size)))
2021-09-13 22:20:32 +02:00
*in = toBuffer(input)
2021-09-13 18:41:52 +02:00
2021-09-13 22:06:26 +02:00
out := (*C.Buffer)(C.malloc(C.size_t(size)))
2021-09-14 13:13:16 +02:00
if !bool(C.hash(r.ptr, in, in.len, out)) {
2021-09-13 23:15:01 +02:00
return nil, errors.New("failed to hash")
}
2021-09-13 18:41:52 +02:00
2021-09-13 23:15:01 +02:00
return C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)), nil
2021-09-13 18:41:52 +02:00
}
2021-09-14 13:13:16 +02:00
// GenerateProof generates a proof for the RLN given a KeyPair and the index in a merkle tree.
func (r *RLN) GenerateProof(input []byte, key *KeyPair, index uint) ([]byte, error) {
2021-09-13 18:41:52 +02:00
inputBuf := toBuffer(input)
2021-09-13 23:31:42 +02:00
var output []byte
out := toBuffer(output)
2021-09-14 13:13:16 +02:00
keybuf := toBuffer(key.Key[:])
auth := &C.Auth{
secret_buffer: &keybuf,
index: C.ulong(index),
}
if !bool(C.generate_proof(r.ptr, &inputBuf, auth, &out)) {
2021-09-13 23:31:42 +02:00
return nil, errors.New("failed to generate proof")
}
return C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)), nil
2021-09-13 18:41:52 +02:00
}
2021-09-13 23:31:42 +02:00
// Verify verifies a proof generated for the RLN.
2021-09-14 13:13:16 +02:00
func (r *RLN) Verify(proof []byte) bool {
2021-09-13 18:41:52 +02:00
proofBuf := toBuffer(proof)
2021-09-13 23:31:42 +02:00
result := uint32(0)
2021-09-13 18:41:52 +02:00
res := C.uint(result)
2021-09-14 13:13:16 +02:00
if !bool(C.verify(r.ptr, &proofBuf, &res)) {
2021-09-13 23:31:42 +02:00
// @TODO THINK ABOUT ERROR?
return false
}
return uint32(res) == 0
2021-09-13 18:41:52 +02:00
}
2021-09-14 13:13:16 +02:00
func (r *RLN) UpdateNextMember(input []byte) error {
buf := toBuffer(input)
if !bool(C.update_next_member(r.ptr, &buf)) {
return errors.New("failed to update next member")
}
return nil
}
func (r *RLN) DeleteMember(index int) error {
if !bool(C.delete_member(r.ptr, C.ulong(index))) {
return errors.New("failed to delete member")
}
return nil
}
func (r *RLN) GetRoot() ([]byte, error) {
var output []byte
out := toBuffer(output)
if !bool(C.get_root(r.ptr, &out)) {
return nil, errors.New("failed to get root")
}
return C.GoBytes(unsafe.Pointer(out.ptr), C.int(out.len)), nil
}
2021-09-13 18:41:52 +02:00
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))
}
}