2018-12-29 13:04:17 +00:00
|
|
|
package tz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2022-12-10 10:15:37 +00:00
|
|
|
"github.com/TrueCloudLab/tzhash/gf127"
|
2018-12-29 13:04:17 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 09:20:36 +00:00
|
|
|
type (
|
|
|
|
GF127 = gf127.GF127
|
|
|
|
|
|
|
|
sl2 [2][2]GF127
|
|
|
|
)
|
2018-12-29 13:04:17 +00:00
|
|
|
|
|
|
|
var id = sl2{
|
2019-10-15 09:20:36 +00:00
|
|
|
{GF127{1, 0}, GF127{0, 0}},
|
|
|
|
{GF127{0, 0}, GF127{1, 0}},
|
2018-12-29 13:04:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 13:58:21 +00:00
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler.
|
2018-12-29 13:04:17 +00:00
|
|
|
func (c *sl2) MarshalBinary() (data []byte, err error) {
|
2022-03-09 14:01:50 +00:00
|
|
|
s := c.Bytes()
|
2018-12-29 13:04:17 +00:00
|
|
|
return s[:], nil
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:58:21 +00:00
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
|
2018-12-29 13:04:17 +00:00
|
|
|
func (c *sl2) UnmarshalBinary(data []byte) (err error) {
|
|
|
|
if len(data) != 64 {
|
|
|
|
return errors.New("data must be 64-bytes long")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c[0][0].UnmarshalBinary(data[:16]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = c[0][1].UnmarshalBinary(data[16:32]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = c[1][0].UnmarshalBinary(data[32:48]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = c[1][1].UnmarshalBinary(data[48:64]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-29 10:19:45 +00:00
|
|
|
func (c *sl2) mulStrassen(a, b *sl2, x *[8]GF127) *sl2 { //nolint:unused
|
2018-12-29 13:04:17 +00:00
|
|
|
// strassen algorithm
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&a[0][0], &a[1][1], &x[0])
|
|
|
|
gf127.Add(&b[0][0], &b[1][1], &x[1])
|
|
|
|
gf127.Mul(&x[0], &x[1], &x[0])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&a[1][0], &a[1][1], &x[1])
|
|
|
|
gf127.Mul(&x[1], &b[0][0], &x[1])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&b[0][1], &b[1][1], &x[2])
|
|
|
|
gf127.Mul(&x[2], &a[0][0], &x[2])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&b[1][0], &b[0][0], &x[3])
|
|
|
|
gf127.Mul(&x[3], &a[1][1], &x[3])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&a[0][0], &a[0][1], &x[4])
|
|
|
|
gf127.Mul(&x[4], &b[1][1], &x[4])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&a[1][0], &a[0][0], &x[5])
|
|
|
|
gf127.Add(&b[0][0], &b[0][1], &x[6])
|
|
|
|
gf127.Mul(&x[5], &x[6], &x[5])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&a[0][1], &a[1][1], &x[6])
|
|
|
|
gf127.Add(&b[1][0], &b[1][1], &x[7])
|
|
|
|
gf127.Mul(&x[6], &x[7], &x[6])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&x[2], &x[4], &c[0][1])
|
|
|
|
gf127.Add(&x[1], &x[3], &c[1][0])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&x[4], &x[6], &x[4])
|
|
|
|
gf127.Add(&x[0], &x[3], &c[0][0])
|
|
|
|
gf127.Add(&c[0][0], &x[4], &c[0][0])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&x[0], &x[1], &x[0])
|
|
|
|
gf127.Add(&x[2], &x[5], &c[1][1])
|
|
|
|
gf127.Add(&c[1][1], &x[0], &c[1][1])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sl2) MulA() *sl2 {
|
2019-10-15 09:20:36 +00:00
|
|
|
var a GF127
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Mul10(&c[0][0], &a)
|
2019-10-15 09:25:15 +00:00
|
|
|
gf127.Mul1(&c[0][0], &c[0][1])
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&a, &c[0][1], &c[0][0])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Mul10(&c[1][0], &a)
|
2019-10-15 09:25:15 +00:00
|
|
|
gf127.Mul1(&c[1][0], &c[1][1])
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Add(&a, &c[1][1], &c[1][0])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sl2) MulB() *sl2 {
|
2019-10-15 09:20:36 +00:00
|
|
|
var a GF127
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2019-10-15 09:25:15 +00:00
|
|
|
gf127.Mul1(&c[0][0], &a)
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Mul10(&c[0][0], &c[0][0])
|
|
|
|
gf127.Add(&c[0][1], &c[0][0], &c[0][0])
|
|
|
|
gf127.Add(&c[0][0], &a, &c[0][1])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2019-10-15 09:25:15 +00:00
|
|
|
gf127.Mul1(&c[1][0], &a)
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Mul10(&c[1][0], &c[1][0])
|
|
|
|
gf127.Add(&c[1][1], &c[1][0], &c[1][0])
|
|
|
|
gf127.Add(&c[1][0], &a, &c[1][1])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:58:21 +00:00
|
|
|
// Mul returns a * b in GL_2(GF(2^127))
|
2018-12-29 13:04:17 +00:00
|
|
|
func (c *sl2) Mul(a, b *sl2) *sl2 {
|
2022-03-09 12:17:18 +00:00
|
|
|
var x [4]GF127
|
|
|
|
|
|
|
|
gf127.Mul(&a[0][0], &b[0][0], &x[0])
|
|
|
|
gf127.Mul(&a[0][0], &b[0][1], &x[1])
|
|
|
|
gf127.Mul(&a[1][0], &b[0][0], &x[2])
|
|
|
|
gf127.Mul(&a[1][0], &b[0][1], &x[3])
|
|
|
|
|
|
|
|
gf127.Mul(&a[0][1], &b[1][0], &c[0][0])
|
|
|
|
gf127.Add(&c[0][0], &x[0], &c[0][0])
|
|
|
|
gf127.Mul(&a[0][1], &b[1][1], &c[0][1])
|
|
|
|
gf127.Add(&c[0][1], &x[1], &c[0][1])
|
|
|
|
gf127.Mul(&a[1][1], &b[1][0], &c[1][0])
|
|
|
|
gf127.Add(&c[1][0], &x[2], &c[1][0])
|
|
|
|
gf127.Mul(&a[1][1], &b[1][1], &c[1][1])
|
|
|
|
gf127.Add(&c[1][1], &x[3], &c[1][1])
|
2019-10-09 08:37:22 +00:00
|
|
|
return c
|
2018-12-29 13:04:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 13:11:48 +00:00
|
|
|
// Inv returns inverse of a in GL_2(GF(2^127))
|
|
|
|
func Inv(a *sl2) (b *sl2) {
|
|
|
|
b = new(sl2)
|
2019-10-15 09:20:36 +00:00
|
|
|
inv(a, b, new([2]GF127))
|
2019-01-29 13:11:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:20:36 +00:00
|
|
|
func inv(a, b *sl2, t *[2]GF127) {
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Mul(&a[0][0], &a[1][1], &t[0])
|
|
|
|
gf127.Mul(&a[0][1], &a[1][0], &t[1])
|
|
|
|
gf127.Add(&t[0], &t[1], &t[0])
|
2019-10-15 09:25:15 +00:00
|
|
|
gf127.Inv(&t[0], &t[1])
|
2019-10-15 09:10:39 +00:00
|
|
|
|
2022-03-09 12:17:18 +00:00
|
|
|
gf127.Mul(&t[1], &a[0][0], &b[1][1])
|
|
|
|
gf127.Mul(&t[1], &a[0][1], &b[0][1])
|
|
|
|
gf127.Mul(&t[1], &a[1][0], &b[1][0])
|
|
|
|
gf127.Mul(&t[1], &a[1][1], &b[0][0])
|
2019-01-29 13:11:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 13:04:17 +00:00
|
|
|
func (c *sl2) String() string {
|
|
|
|
return c[0][0].String() + c[0][1].String() +
|
|
|
|
c[1][0].String() + c[1][1].String()
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:01:50 +00:00
|
|
|
func (c *sl2) Bytes() (b [Size]byte) {
|
|
|
|
t := c[0][0].Bytes()
|
2022-03-09 13:58:21 +00:00
|
|
|
copy(b[:], t[:])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 14:01:50 +00:00
|
|
|
t = c[0][1].Bytes()
|
2022-03-09 13:58:21 +00:00
|
|
|
copy(b[16:], t[:])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 14:01:50 +00:00
|
|
|
t = c[1][0].Bytes()
|
2022-03-09 13:58:21 +00:00
|
|
|
copy(b[32:], t[:])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
2022-03-09 14:01:50 +00:00
|
|
|
t = c[1][1].Bytes()
|
2022-03-09 13:58:21 +00:00
|
|
|
copy(b[48:], t[:])
|
2018-12-29 13:04:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|