forked from TrueCloudLab/tzhash
add random GF(2^127)-element generator
This commit is contained in:
parent
b380bca1d2
commit
ebb124e812
2 changed files with 26 additions and 6 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GF127 represents element of GF(2^127)
|
// GF127 represents element of GF(2^127)
|
||||||
|
@ -36,6 +37,12 @@ func New(lo, hi uint64) *GF127 {
|
||||||
return &GF127{lo, hi}
|
return &GF127{lo, hi}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Random returns random element from GF(2^127).
|
||||||
|
// Is used mostly for testing.
|
||||||
|
func Random() *GF127 {
|
||||||
|
return &GF127{rand.Uint64(), rand.Uint64() >> 1}
|
||||||
|
}
|
||||||
|
|
||||||
// String returns hex-encoded representation, starting with MSB.
|
// String returns hex-encoded representation, starting with MSB.
|
||||||
func (c *GF127) String() string {
|
func (c *GF127) String() string {
|
||||||
return hex.EncodeToString(c.ByteArray())
|
return hex.EncodeToString(c.ByteArray())
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
package gf127
|
package gf127
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxUint64 = ^uint64(0)
|
const maxUint64 = ^uint64(0)
|
||||||
|
|
||||||
func TestAdd(t *testing.T) {
|
func TestAdd(t *testing.T) {
|
||||||
a := &GF127{rand.Uint64(), rand.Uint64() >> 1}
|
var (
|
||||||
b := &GF127{rand.Uint64(), rand.Uint64() >> 1}
|
a = Random()
|
||||||
e := &GF127{a[0] ^ b[0], a[1] ^ b[1]}
|
b = Random()
|
||||||
c := &GF127{0, 0}
|
e = &GF127{a[0] ^ b[0], a[1] ^ b[1]}
|
||||||
|
c = new(GF127)
|
||||||
|
)
|
||||||
c.Add(a, b)
|
c.Add(a, b)
|
||||||
if e[0] != c[0] || e[1] != c[1] {
|
if e[0] != c[0] || e[1] != c[1] {
|
||||||
t.Errorf("expected (%s), got (%s)", e.String(), c.String())
|
t.Errorf("expected (%s), got (%s)", e.String(), c.String())
|
||||||
|
@ -74,10 +75,22 @@ var testCasesInv = [][2]*GF127{
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInv(t *testing.T) {
|
func TestInv(t *testing.T) {
|
||||||
c := new(GF127)
|
var a, b, c = new(GF127), new(GF127), new(GF127)
|
||||||
for _, tc := range testCasesInv {
|
for _, tc := range testCasesInv {
|
||||||
if Inv(tc[0], c); !c.Equals(tc[1]) {
|
if Inv(tc[0], c); !c.Equals(tc[1]) {
|
||||||
t.Errorf("expected (%s), got (%s)", tc[1].String(), c.String())
|
t.Errorf("expected (%s), got (%s)", tc[1].String(), c.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
// 0 has no inverse
|
||||||
|
if a = Random(); a.Equals(&GF127{0, 0}) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
Inv(a, b)
|
||||||
|
Mul(a, b, c)
|
||||||
|
if !c.Equals(&GF127{1, 0}) {
|
||||||
|
t.Errorf("expected inverse of (%s), got (%s)", a.String(), b.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue