Remove non-AVX parts from avx package

Remove Inv(), Mul1(), And() because right now
they have no AVX optimizations.
This commit is contained in:
Evgenii Stratonikov 2019-10-15 12:25:15 +03:00
parent 0f8b498b58
commit 63834fe8c1
4 changed files with 6 additions and 100 deletions

View file

@ -8,8 +8,6 @@
package avx
import (
"math/bits"
"github.com/nspcc-dev/tzhash/gf127"
)
@ -21,76 +19,8 @@ const msb64 = uint64(1) << 63
var (
// x127x63 represents x^127 + x^63. Used in assembly file.
x127x63 = GF127{msb64, msb64}
// x126x631 is reduction polynomial x^127+x^63+1
x127x631 = GF127{msb64 + 1, msb64}
)
// Inv sets b to a^-1
// Algorithm is based on Extended Euclidean Algorithm
// and is described by Hankerson, Hernandez, Menezes in
// https://link.springer.com/content/pdf/10.1007/3-540-44499-8_1.pdf
func Inv(a, b *GF127) {
var (
v = x127x631
u = *a
c, d = &GF127{1, 0}, &GF127{0, 0}
t = new(GF127)
x *GF127
)
// degree of polynomial is a position of most significant bit
for du, dv := msb(&u), msb(&v); du != 0; du, dv = msb(&u), msb(&v) {
if du < dv {
v, u = u, v
dv, du = du, dv
d, c = c, d
}
x = xN(du - dv)
Mul(x, &v, t)
Add(&u, t, &u)
// becasuse mul performs reduction on t, we need
// manually reduce u at first step
if msb(&u) == 127 {
Add(&u, &x127x631, &u)
}
Mul(x, d, t)
Add(c, t, c)
}
*b = *c
}
func xN(n int) *GF127 {
if n < 64 {
return &GF127{1 << uint(n), 0}
}
return &GF127{0, 1 << uint(n-64)}
}
func msb(a *GF127) (x int) {
x = bits.LeadingZeros64(a[1])
if x == 64 {
x = bits.LeadingZeros64(a[0]) + 64
}
return 127 - x
}
// Mul1 copies a to b.
func Mul1(a, b *GF127) {
b[0] = a[0]
b[1] = a[1]
}
// And sets c to a & b (bitwise-and).
func And(a, b, c *GF127) {
c[0] = a[0] & b[0]
c[1] = a[1] & b[1]
}
// Add sets c to a+b.
func Add(a, b, c *GF127)

View file

@ -66,27 +66,3 @@ func TestMul11(t *testing.T) {
require.Equal(t, tc[1], c)
}
}
var testCasesInv = [][2]*GF127{
{&GF127{1, 0}, &GF127{1, 0}},
{&GF127{3, 0}, &GF127{msb64, ^msb64}},
{&GF127{54321, 12345}, &GF127{8230555108620784737, 3929873967650665114}},
}
func TestInv(t *testing.T) {
var a, b, c = new(GF127), new(GF127), new(GF127)
for _, tc := range testCasesInv {
Inv(tc[0], c)
require.Equal(t, tc[1], c)
}
for i := 0; i < 3; i++ {
// 0 has no inverse
if a = gf127.Random(); a.Equals(&GF127{0, 0}) {
continue
}
Inv(a, b)
Mul(a, b, c)
require.Equal(t, &GF127{1, 0}, c)
}
}

View file

@ -130,11 +130,11 @@ func (c *sl2) MulA() *sl2 {
var a GF127
avx.Mul10(&c[0][0], &a)
avx.Mul1(&c[0][0], &c[0][1])
gf127.Mul1(&c[0][0], &c[0][1])
avx.Add(&a, &c[0][1], &c[0][0])
avx.Mul10(&c[1][0], &a)
avx.Mul1(&c[1][0], &c[1][1])
gf127.Mul1(&c[1][0], &c[1][1])
avx.Add(&a, &c[1][1], &c[1][0])
return c
@ -143,12 +143,12 @@ func (c *sl2) MulA() *sl2 {
func (c *sl2) MulB() *sl2 {
var a GF127
avx.Mul1(&c[0][0], &a)
gf127.Mul1(&c[0][0], &a)
avx.Mul10(&c[0][0], &c[0][0])
avx.Add(&c[0][1], &c[0][0], &c[0][0])
avx.Add(&c[0][0], &a, &c[0][1])
avx.Mul1(&c[1][0], &a)
gf127.Mul1(&c[1][0], &a)
avx.Mul10(&c[1][0], &c[1][0])
avx.Add(&c[1][1], &c[1][0], &c[1][0])
avx.Add(&c[1][0], &a, &c[1][1])
@ -172,7 +172,7 @@ func inv(a, b *sl2, t *[2]GF127) {
avx.Mul(&a[0][0], &a[1][1], &t[0])
avx.Mul(&a[0][1], &a[1][0], &t[1])
avx.Add(&t[0], &t[1], &t[0])
avx.Inv(&t[0], &t[1])
gf127.Inv(&t[0], &t[1])
avx.Mul(&t[1], &a[0][0], &b[1][1])
avx.Mul(&t[1], &a[0][1], &b[0][1])

View file

@ -26,7 +26,7 @@ func random() (a *sl2) {
avx.Add(&a[1][1], gf127.New(1, 0), &a[1][1])
t := gf127.New(0, 0)
avx.Inv(&a[0][0], t)
gf127.Inv(&a[0][0], t)
avx.Mul(t, &a[1][1], &a[1][1])
return