enhancement/rln-hash-test (#3)

* started hash test

* playing around

* fix

* fix

* fix
This commit is contained in:
Dean Eigenmann 2021-09-14 14:56:23 +02:00 committed by GitHub
parent 5d7294a0cd
commit 1340b2e4ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 5 deletions

View File

@ -66,12 +66,16 @@ func (r *RLN) GenerateKey() (*KeyPair, error) {
// Hash hashes a given input using the underlying function.
func (r *RLN) Hash(input []byte) ([]byte, error) {
size := int(unsafe.Sizeof(C.Buffer{}))
in := (*C.Buffer)(C.malloc(C.size_t(size)))
*in = toBuffer(input)
out := (*C.Buffer)(C.malloc(C.size_t(size)))
if !bool(C.hash(r.ptr, in, in.len, out)) {
buf := toBuffer(input)
size := int(unsafe.Sizeof(buf))
in := (*C.Buffer)(C.malloc(C.size_t(size)))
*in = buf
var output []byte
out := toBuffer(output)
if !bool(C.hash(r.ptr, in, 1, &out)) {
return nil, errors.New("failed to hash")
}

View File

@ -1,6 +1,7 @@
package rln_test
import (
"encoding/hex"
"io/ioutil"
"reflect"
"testing"
@ -44,3 +45,40 @@ func TestGenerateKey(t *testing.T) {
t.Fatal("k.Commitment was empty")
}
}
func TestRLN_Hash(t *testing.T) {
// This test is based on tests from:
// https://github.com/status-im/nim-waku/blob/b7998de09d1ef04599a699938da69aecfa63cc6f/tests/v2/test_waku_rln_relay.nim#L527
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)
}
input := byteArray(32, 1)
output, err := r.Hash(input)
if err != nil {
t.Fatal(err)
}
expected := "53a6338cdbf02f0563cec1898e354d0d272c8f98b606c538945c6f41ef101828"
if expected != hex.EncodeToString(output) {
t.Fatalf("value %x did not match expected %s", output, expected)
}
}
func byteArray(length int, value byte) []byte {
arr := make([]byte, length)
for i := 0; i < length; i++ {
arr[i] = value
}
return arr
}