2018-12-29 13:04:17 +00:00
|
|
|
package tz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/tzhash/gf127"
|
2019-10-15 09:10:39 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/gf127/avx"
|
2019-05-29 11:10:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-29 13:04:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2019-01-29 13:11:48 +00:00
|
|
|
func random() (a *sl2) {
|
|
|
|
a = new(sl2)
|
|
|
|
a[0][0] = *gf127.Random()
|
|
|
|
a[0][1] = *gf127.Random()
|
|
|
|
a[1][0] = *gf127.Random()
|
|
|
|
|
|
|
|
// so that result is in SL2
|
|
|
|
// d = a^-1*(1+b*c)
|
2019-10-15 09:10:39 +00:00
|
|
|
avx.Mul(&a[0][1], &a[1][0], &a[1][1])
|
|
|
|
avx.Add(&a[1][1], gf127.New(1, 0), &a[1][1])
|
2019-01-29 13:11:48 +00:00
|
|
|
|
|
|
|
t := gf127.New(0, 0)
|
2019-10-15 09:10:39 +00:00
|
|
|
avx.Inv(&a[0][0], t)
|
|
|
|
avx.Mul(t, &a[1][1], &a[1][1])
|
2019-01-29 13:11:48 +00:00
|
|
|
|
|
|
|
return
|
2018-12-29 13:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSL2_MarshalBinary(t *testing.T) {
|
2019-01-29 13:11:48 +00:00
|
|
|
var (
|
|
|
|
a = random()
|
|
|
|
b = new(sl2)
|
|
|
|
)
|
2018-12-29 13:04:17 +00:00
|
|
|
|
|
|
|
data, err := a.MarshalBinary()
|
2019-05-29 11:10:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-29 13:04:17 +00:00
|
|
|
|
|
|
|
err = b.UnmarshalBinary(data)
|
2019-05-29 11:10:17 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2019-05-29 11:10:17 +00:00
|
|
|
require.Equal(t, a, b)
|
2018-12-29 13:04:17 +00:00
|
|
|
}
|
2019-01-29 13:11:48 +00:00
|
|
|
|
|
|
|
func TestInv(t *testing.T) {
|
2019-05-29 11:10:17 +00:00
|
|
|
var a, b, c *sl2
|
2019-01-29 13:11:48 +00:00
|
|
|
|
|
|
|
c = new(sl2)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
a = random()
|
|
|
|
b = Inv(a)
|
|
|
|
c = c.Mul(a, b)
|
|
|
|
|
2019-05-29 11:10:17 +00:00
|
|
|
require.Equal(t, id, *c)
|
2019-01-29 13:11:48 +00:00
|
|
|
}
|
|
|
|
}
|