mirror of
https://github.com/logos-messaging/go-zerokit-rln.git
synced 2026-01-05 22:53:09 +00:00
45 lines
1.8 KiB
Go
45 lines
1.8 KiB
Go
package rln
|
|
|
|
import (
|
|
"bytes"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBigInt(t *testing.T) {
|
|
base := big.NewInt(2)
|
|
value := base.Exp(base, big.NewInt(248), nil)
|
|
value = value.Sub(value, big.NewInt(1)) // 2^248 - 1
|
|
|
|
b32Value := BigIntToBytes32(value)
|
|
require.True(t, bytes.Equal(b32Value[:], []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0}))
|
|
|
|
newValue := Bytes32ToBigInt(b32Value)
|
|
require.True(t, bytes.Equal(newValue.Bytes(), value.Bytes()))
|
|
}
|
|
|
|
func TestFlatten(t *testing.T) {
|
|
in1 := [][32]byte{[32]byte{}}
|
|
in2 := [][32]byte{[32]byte{0x00}, [32]byte{0x01}}
|
|
in3 := [][32]byte{[32]byte{0x01, 0x02, 0x03}, [32]byte{0x04, 0x05, 0x06}}
|
|
|
|
expected1 := []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
|
|
expected2 := []byte{
|
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
|
|
expected3 := []byte{
|
|
0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
|
0x4, 0x5, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
|
|
|
|
out1 := Flatten(in1)
|
|
require.Equal(t, expected1, out1)
|
|
|
|
out2 := Flatten(in2)
|
|
require.Equal(t, expected2, out2)
|
|
|
|
out3 := Flatten(in3)
|
|
require.Equal(t, expected3, out3)
|
|
}
|