Alias gf127.GF127

This commit is contained in:
Evgenii Stratonikov 2019-10-15 12:20:36 +03:00
parent d891a9c591
commit 0f8b498b58
8 changed files with 71 additions and 68 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/tzhash/gf127" "github.com/nspcc-dev/tzhash/gf127"
) )
// GF127 is an alias for a main type.
type GF127 = gf127.GF127 type GF127 = gf127.GF127
const msb64 = uint64(1) << 63 const msb64 = uint64(1) << 63

View file

@ -7,16 +7,19 @@ import (
"github.com/nspcc-dev/tzhash/gf127" "github.com/nspcc-dev/tzhash/gf127"
) )
// GF127 is an alias for a main type.
type GF127 = gf127.GF127
// GF127x2 represents a pair of elements of GF(2^127) stored together. // GF127x2 represents a pair of elements of GF(2^127) stored together.
type GF127x2 [2]gf127.GF127 type GF127x2 [2]GF127
// Split returns 2 components of pair without additional allocations. // Split returns 2 components of pair without additional allocations.
func Split(a *GF127x2) (*gf127.GF127, *gf127.GF127) { func Split(a *GF127x2) (*GF127, *GF127) {
return &a[0], &a[1] return &a[0], &a[1]
} }
// CombineTo 2 elements of GF(2^127) to the respective components of pair. // CombineTo 2 elements of GF(2^127) to the respective components of pair.
func CombineTo(a *gf127.GF127, b *gf127.GF127, c *GF127x2) { func CombineTo(a *GF127, b *GF127, c *GF127x2) {
c[0] = *a c[0] = *a
c[1] = *b c[1] = *b
} }

View file

@ -3,7 +3,6 @@ package avx2
import ( import (
"testing" "testing"
"github.com/nspcc-dev/tzhash/gf127"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -11,11 +10,11 @@ const maxUint64 = ^uint64(0)
var testCasesSplit = []struct { var testCasesSplit = []struct {
num *GF127x2 num *GF127x2
h1 *gf127.GF127 h1 *GF127
h2 *gf127.GF127 h2 *GF127
}{ }{
{&GF127x2{gf127.GF127{123, 31}, gf127.GF127{141, 9}}, &gf127.GF127{123, 31}, &gf127.GF127{141, 9}}, {&GF127x2{GF127{123, 31}, GF127{141, 9}}, &GF127{123, 31}, &GF127{141, 9}},
{&GF127x2{gf127.GF127{maxUint64, 0}, gf127.GF127{0, maxUint64}}, &gf127.GF127{maxUint64, 0}, &gf127.GF127{0, maxUint64}}, {&GF127x2{GF127{maxUint64, 0}, GF127{0, maxUint64}}, &GF127{maxUint64, 0}, &GF127{0, maxUint64}},
} }
func TestSplit(t *testing.T) { func TestSplit(t *testing.T) {
@ -36,16 +35,16 @@ func TestCombineTo(t *testing.T) {
var testCasesMul10x2 = [][2]*GF127x2{ var testCasesMul10x2 = [][2]*GF127x2{
{ {
&GF127x2{gf127.GF127{123, 0}, gf127.GF127{123, 0}}, &GF127x2{GF127{123, 0}, GF127{123, 0}},
&GF127x2{gf127.GF127{246, 0}, gf127.GF127{246, 0}}, &GF127x2{GF127{246, 0}, GF127{246, 0}},
}, },
{ {
&GF127x2{gf127.GF127{maxUint64, 2}, gf127.GF127{0, 1}}, &GF127x2{GF127{maxUint64, 2}, GF127{0, 1}},
&GF127x2{gf127.GF127{maxUint64 - 1, 5}, gf127.GF127{0, 2}}, &GF127x2{GF127{maxUint64 - 1, 5}, GF127{0, 2}},
}, },
{ {
&GF127x2{gf127.GF127{0, maxUint64 >> 1}, gf127.GF127{maxUint64, 2}}, &GF127x2{GF127{0, maxUint64 >> 1}, GF127{maxUint64, 2}},
&GF127x2{gf127.GF127{1 + 1<<63, maxUint64>>1 - 1}, gf127.GF127{maxUint64 - 1, 5}}, &GF127x2{GF127{1 + 1<<63, maxUint64>>1 - 1}, GF127{maxUint64 - 1, 5}},
}, },
} }
@ -59,16 +58,16 @@ func TestMul10x2(t *testing.T) {
var testCasesMul11x2 = [][2]*GF127x2{ var testCasesMul11x2 = [][2]*GF127x2{
{ {
&GF127x2{gf127.GF127{123, 0}, gf127.GF127{123, 0}}, &GF127x2{GF127{123, 0}, GF127{123, 0}},
&GF127x2{gf127.GF127{141, 0}, gf127.GF127{141, 0}}, &GF127x2{GF127{141, 0}, GF127{141, 0}},
}, },
{ {
&GF127x2{gf127.GF127{maxUint64, 2}, gf127.GF127{0, 1}}, &GF127x2{GF127{maxUint64, 2}, GF127{0, 1}},
&GF127x2{gf127.GF127{1, 7}, gf127.GF127{0, 3}}, &GF127x2{GF127{1, 7}, GF127{0, 3}},
}, },
{ {
&GF127x2{gf127.GF127{0, maxUint64 >> 1}, gf127.GF127{maxUint64, 2}}, &GF127x2{GF127{0, maxUint64 >> 1}, GF127{maxUint64, 2}},
&GF127x2{gf127.GF127{1 + 1<<63, 1}, gf127.GF127{1, 7}}, &GF127x2{GF127{1 + 1<<63, 1}, GF127{1, 7}},
}, },
} }

View file

@ -6,20 +6,18 @@ package tz
import ( import (
"hash" "hash"
"math" "math"
"github.com/nspcc-dev/tzhash/gf127"
) )
type digest struct { type digest struct {
x [4]gf127.GF127 x [4]GF127
} }
// type assertion // type assertion
var _ hash.Hash = (*digest)(nil) var _ hash.Hash = (*digest)(nil)
var ( var (
minmax = [2]gf127.GF127{{0, 0}, {math.MaxUint64, math.MaxUint64}} minmax = [2]GF127{{0, 0}, {math.MaxUint64, math.MaxUint64}}
x127x63 = gf127.GF127{1 << 63, 1 << 63} x127x63 = GF127{1 << 63, 1 << 63}
) )
func newAVX() *digest { func newAVX() *digest {
@ -48,10 +46,10 @@ func (d *digest) byteArray() (b [hashSize]byte) {
} }
func (d *digest) Reset() { func (d *digest) Reset() {
d.x[0] = gf127.GF127{1, 0} d.x[0] = GF127{1, 0}
d.x[1] = gf127.GF127{0, 0} d.x[1] = GF127{0, 0}
d.x[2] = gf127.GF127{0, 0} d.x[2] = GF127{0, 0}
d.x[3] = gf127.GF127{1, 0} d.x[3] = GF127{1, 0}
} }
func (d *digest) Write(data []byte) (n int, err error) { func (d *digest) Write(data []byte) (n int, err error) {
@ -77,4 +75,4 @@ func (d *digest) BlockSize() int {
return hashBlockSize return hashBlockSize
} }
func mulBitRight(c00, c01, c10, c11, e *gf127.GF127) func mulBitRight(c00, c01, c10, c11, e *GF127)

View file

@ -6,7 +6,6 @@ package tz
import ( import (
"hash" "hash"
"github.com/nspcc-dev/tzhash/gf127"
"github.com/nspcc-dev/tzhash/gf127/avx2" "github.com/nspcc-dev/tzhash/gf127/avx2"
) )
@ -45,8 +44,8 @@ func (d *digest2) Sum(in []byte) []byte {
return append(in, h[:]...) return append(in, h[:]...)
} }
func (d *digest2) Reset() { func (d *digest2) Reset() {
d.x[0] = avx2.GF127x2{gf127.GF127{1, 0}, gf127.GF127{0, 0}} d.x[0] = avx2.GF127x2{GF127{1, 0}, GF127{0, 0}}
d.x[1] = avx2.GF127x2{gf127.GF127{0, 0}, gf127.GF127{1, 0}} d.x[1] = avx2.GF127x2{GF127{0, 0}, GF127{1, 0}}
} }
func (d *digest2) Size() int { return hashSize } func (d *digest2) Size() int { return hashSize }
func (d *digest2) BlockSize() int { return hashBlockSize } func (d *digest2) BlockSize() int { return hashBlockSize }
@ -63,4 +62,4 @@ func (d *digest2) checkSum() (b [hashSize]byte) {
return return
} }
func mulBitRightx2(c00c10 *avx2.GF127x2, c01c11 *avx2.GF127x2, e *gf127.GF127) func mulBitRightx2(c00c10 *avx2.GF127x2, c01c11 *avx2.GF127x2, e *GF127)

View file

@ -7,7 +7,6 @@ package tz
import ( import (
"hash" "hash"
"github.com/nspcc-dev/tzhash/gf127"
"github.com/nspcc-dev/tzhash/gf127/avx2" "github.com/nspcc-dev/tzhash/gf127/avx2"
) )
@ -39,8 +38,8 @@ func (d *digest3) Sum(in []byte) []byte {
return append(in, h[:]...) return append(in, h[:]...)
} }
func (d *digest3) Reset() { func (d *digest3) Reset() {
d.x[0] = avx2.GF127x2{gf127.GF127{1, 0}, gf127.GF127{0, 0}} d.x[0] = avx2.GF127x2{GF127{1, 0}, GF127{0, 0}}
d.x[1] = avx2.GF127x2{gf127.GF127{0, 0}, gf127.GF127{1, 0}} d.x[1] = avx2.GF127x2{GF127{0, 0}, GF127{1, 0}}
} }
func (d *digest3) Size() int { return hashSize } func (d *digest3) Size() int { return hashSize }
func (d *digest3) BlockSize() int { return hashBlockSize } func (d *digest3) BlockSize() int { return hashBlockSize }

View file

@ -5,7 +5,7 @@ import (
) )
type digestp struct { type digestp struct {
x [4]gf127.GF127 x [4]GF127
} }
// New returns a new hash.Hash computing the Tillich-Zémor checksum. // New returns a new hash.Hash computing the Tillich-Zémor checksum.
@ -35,15 +35,15 @@ func (d *digestp) byteArray() (b [hashSize]byte) {
} }
func (d *digestp) Reset() { func (d *digestp) Reset() {
d.x[0] = gf127.GF127{1, 0} d.x[0] = GF127{1, 0}
d.x[1] = gf127.GF127{0, 0} d.x[1] = GF127{0, 0}
d.x[2] = gf127.GF127{0, 0} d.x[2] = GF127{0, 0}
d.x[3] = gf127.GF127{1, 0} d.x[3] = GF127{1, 0}
} }
func (d *digestp) Write(data []byte) (n int, err error) { func (d *digestp) Write(data []byte) (n int, err error) {
n = len(data) n = len(data)
tmp := new(gf127.GF127) tmp := new(GF127)
for _, b := range data { for _, b := range data {
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x80 != 0, tmp) mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x80 != 0, tmp)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x40 != 0, tmp) mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x40 != 0, tmp)
@ -65,7 +65,7 @@ func (d *digestp) BlockSize() int {
return hashBlockSize return hashBlockSize
} }
func mulBitRightPure(c00, c01, c10, c11 *gf127.GF127, bit bool, tmp *gf127.GF127) { func mulBitRightPure(c00, c01, c10, c11 *GF127, bit bool, tmp *GF127) {
if bit { if bit {
*tmp = *c00 *tmp = *c00
gf127.Mul10(c00, c00) gf127.Mul10(c00, c00)

View file

@ -7,14 +7,18 @@ import (
"github.com/nspcc-dev/tzhash/gf127/avx" "github.com/nspcc-dev/tzhash/gf127/avx"
) )
type sl2 [2][2]gf127.GF127 type (
GF127 = gf127.GF127
sl2 [2][2]GF127
)
var id = sl2{ var id = sl2{
{gf127.GF127{1, 0}, gf127.GF127{0, 0}}, {GF127{1, 0}, GF127{0, 0}},
{gf127.GF127{0, 0}, gf127.GF127{1, 0}}, {GF127{0, 0}, GF127{1, 0}},
} }
var mul func(a, b, c *sl2, x *[4]gf127.GF127) var mul func(a, b, c *sl2, x *[4]GF127)
func init() { func init() {
if hasAVX { if hasAVX {
@ -50,7 +54,7 @@ func (c *sl2) UnmarshalBinary(data []byte) (err error) {
return return
} }
func (c *sl2) mulStrassen(a, b *sl2, x *[8]gf127.GF127) *sl2 { func (c *sl2) mulStrassen(a, b *sl2, x *[8]GF127) *sl2 {
// strassen algorithm // strassen algorithm
avx.Add(&a[0][0], &a[1][1], &x[0]) avx.Add(&a[0][0], &a[1][1], &x[0])
avx.Add(&b[0][0], &b[1][1], &x[1]) avx.Add(&b[0][0], &b[1][1], &x[1])
@ -90,7 +94,7 @@ func (c *sl2) mulStrassen(a, b *sl2, x *[8]gf127.GF127) *sl2 {
return c return c
} }
func mulSL2AVX(a, b, c *sl2, x *[4]gf127.GF127) { func mulSL2AVX(a, b, c *sl2, x *[4]GF127) {
avx.Mul(&a[0][0], &b[0][0], &x[0]) avx.Mul(&a[0][0], &b[0][0], &x[0])
avx.Mul(&a[0][0], &b[0][1], &x[1]) avx.Mul(&a[0][0], &b[0][1], &x[1])
avx.Mul(&a[1][0], &b[0][0], &x[2]) avx.Mul(&a[1][0], &b[0][0], &x[2])
@ -106,24 +110,24 @@ func mulSL2AVX(a, b, c *sl2, x *[4]gf127.GF127) {
avx.Add(&c[1][1], &x[3], &c[1][1]) avx.Add(&c[1][1], &x[3], &c[1][1])
} }
func mulSL2Pure(a, b, c *sl2, x *[4]gf127.GF127) { func mulSL2Pure(a, b, c *sl2, x *[4]GF127) {
gf127.Mul((*gf127.GF127)(&a[0][0]), (*gf127.GF127)(&b[0][0]), (*gf127.GF127)(&x[0])) gf127.Mul((*GF127)(&a[0][0]), (*GF127)(&b[0][0]), (*GF127)(&x[0]))
gf127.Mul((*gf127.GF127)(&a[0][0]), (*gf127.GF127)(&b[0][1]), (*gf127.GF127)(&x[1])) gf127.Mul((*GF127)(&a[0][0]), (*GF127)(&b[0][1]), (*GF127)(&x[1]))
gf127.Mul((*gf127.GF127)(&a[1][0]), (*gf127.GF127)(&b[0][0]), (*gf127.GF127)(&x[2])) gf127.Mul((*GF127)(&a[1][0]), (*GF127)(&b[0][0]), (*GF127)(&x[2]))
gf127.Mul((*gf127.GF127)(&a[1][0]), (*gf127.GF127)(&b[0][1]), (*gf127.GF127)(&x[3])) gf127.Mul((*GF127)(&a[1][0]), (*GF127)(&b[0][1]), (*GF127)(&x[3]))
gf127.Mul((*gf127.GF127)(&a[0][1]), (*gf127.GF127)(&b[1][0]), (*gf127.GF127)(&c[0][0])) gf127.Mul((*GF127)(&a[0][1]), (*GF127)(&b[1][0]), (*GF127)(&c[0][0]))
gf127.Add((*gf127.GF127)(&c[0][0]), (*gf127.GF127)(&x[0]), (*gf127.GF127)(&c[0][0])) gf127.Add((*GF127)(&c[0][0]), (*GF127)(&x[0]), (*GF127)(&c[0][0]))
gf127.Mul((*gf127.GF127)(&a[0][1]), (*gf127.GF127)(&b[1][1]), (*gf127.GF127)(&c[0][1])) gf127.Mul((*GF127)(&a[0][1]), (*GF127)(&b[1][1]), (*GF127)(&c[0][1]))
gf127.Add((*gf127.GF127)(&c[0][1]), (*gf127.GF127)(&x[1]), (*gf127.GF127)(&c[0][1])) gf127.Add((*GF127)(&c[0][1]), (*GF127)(&x[1]), (*GF127)(&c[0][1]))
gf127.Mul((*gf127.GF127)(&a[1][1]), (*gf127.GF127)(&b[1][0]), (*gf127.GF127)(&c[1][0])) gf127.Mul((*GF127)(&a[1][1]), (*GF127)(&b[1][0]), (*GF127)(&c[1][0]))
gf127.Add((*gf127.GF127)(&c[1][0]), (*gf127.GF127)(&x[2]), (*gf127.GF127)(&c[1][0])) gf127.Add((*GF127)(&c[1][0]), (*GF127)(&x[2]), (*GF127)(&c[1][0]))
gf127.Mul((*gf127.GF127)(&a[1][1]), (*gf127.GF127)(&b[1][1]), (*gf127.GF127)(&c[1][1])) gf127.Mul((*GF127)(&a[1][1]), (*GF127)(&b[1][1]), (*GF127)(&c[1][1]))
gf127.Add((*gf127.GF127)(&c[1][1]), (*gf127.GF127)(&x[3]), (*gf127.GF127)(&c[1][1])) gf127.Add((*GF127)(&c[1][1]), (*GF127)(&x[3]), (*GF127)(&c[1][1]))
} }
func (c *sl2) MulA() *sl2 { func (c *sl2) MulA() *sl2 {
var a gf127.GF127 var a GF127
avx.Mul10(&c[0][0], &a) avx.Mul10(&c[0][0], &a)
avx.Mul1(&c[0][0], &c[0][1]) avx.Mul1(&c[0][0], &c[0][1])
@ -137,7 +141,7 @@ func (c *sl2) MulA() *sl2 {
} }
func (c *sl2) MulB() *sl2 { func (c *sl2) MulB() *sl2 {
var a gf127.GF127 var a GF127
avx.Mul1(&c[0][0], &a) avx.Mul1(&c[0][0], &a)
avx.Mul10(&c[0][0], &c[0][0]) avx.Mul10(&c[0][0], &c[0][0])
@ -153,18 +157,18 @@ func (c *sl2) MulB() *sl2 {
} }
func (c *sl2) Mul(a, b *sl2) *sl2 { func (c *sl2) Mul(a, b *sl2) *sl2 {
mul(a, b, c, new([4]gf127.GF127)) mul(a, b, c, new([4]GF127))
return c return c
} }
// Inv returns inverse of a in GL_2(GF(2^127)) // Inv returns inverse of a in GL_2(GF(2^127))
func Inv(a *sl2) (b *sl2) { func Inv(a *sl2) (b *sl2) {
b = new(sl2) b = new(sl2)
inv(a, b, new([2]gf127.GF127)) inv(a, b, new([2]GF127))
return return
} }
func inv(a, b *sl2, t *[2]gf127.GF127) { func inv(a, b *sl2, t *[2]GF127) {
avx.Mul(&a[0][0], &a[1][1], &t[0]) avx.Mul(&a[0][0], &a[1][1], &t[0])
avx.Mul(&a[0][1], &a[1][0], &t[1]) avx.Mul(&a[0][1], &a[1][0], &t[1])
avx.Add(&t[0], &t[1], &t[0]) avx.Add(&t[0], &t[1], &t[0])