mirror of
https://github.com/logos-messaging/go-rln.git
synced 2026-01-04 05:53:06 +00:00
enhancement/rln-hash-test (#3)
* started hash test * playing around * fix * fix * fix
This commit is contained in:
parent
5d7294a0cd
commit
1340b2e4ba
14
rln/rln.go
14
rln/rln.go
@ -66,12 +66,16 @@ func (r *RLN) GenerateKey() (*KeyPair, error) {
|
|||||||
|
|
||||||
// Hash hashes a given input using the underlying function.
|
// Hash hashes a given input using the underlying function.
|
||||||
func (r *RLN) Hash(input []byte) ([]byte, error) {
|
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)))
|
buf := toBuffer(input)
|
||||||
if !bool(C.hash(r.ptr, in, in.len, out)) {
|
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")
|
return nil, errors.New("failed to hash")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package rln_test
|
package rln_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@ -44,3 +45,40 @@ func TestGenerateKey(t *testing.T) {
|
|||||||
t.Fatal("k.Commitment was empty")
|
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
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user