forked from TrueCloudLab/tzhash
Remove usage of unsafe
This commit is contained in:
parent
5142f695cf
commit
f296adb043
4 changed files with 39 additions and 24 deletions
|
@ -3,28 +3,25 @@ package gf127
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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 [4]uint64
|
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) {
|
func Split(a *GF127x2) (*GF127, *GF127) {
|
||||||
return (*GF127)(unsafe.Pointer(a)), (*GF127)(unsafe.Pointer(&(*a)[2]))
|
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, b *GF127, c *GF127x2) {
|
func CombineTo(a *GF127, b *GF127, c *GF127x2) {
|
||||||
c[0] = a[0]
|
c[0] = *a
|
||||||
c[1] = a[1]
|
c[1] = *b
|
||||||
c[2] = b[0]
|
|
||||||
c[3] = b[1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equal checks if both elements of GF(2^127) pair are equal.
|
// Equal checks if both elements of GF(2^127) pair are equal.
|
||||||
func (a *GF127x2) Equal(b *GF127x2) bool {
|
func (a *GF127x2) Equal(b *GF127x2) bool {
|
||||||
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]
|
return a[0] == b[0] && a[1] == b[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns hex-encoded representation, starting with MSB.
|
// String returns hex-encoded representation, starting with MSB.
|
||||||
|
@ -37,10 +34,10 @@ func (a *GF127x2) String() string {
|
||||||
// ByteArray represents element of GF(2^127) as byte array of length 32.
|
// ByteArray represents element of GF(2^127) as byte array of length 32.
|
||||||
func (a *GF127x2) ByteArray() (buf []byte) {
|
func (a *GF127x2) ByteArray() (buf []byte) {
|
||||||
buf = make([]byte, 32)
|
buf = make([]byte, 32)
|
||||||
binary.BigEndian.PutUint64(buf, a[1])
|
binary.BigEndian.PutUint64(buf, a[0][1])
|
||||||
binary.BigEndian.PutUint64(buf[8:], a[0])
|
binary.BigEndian.PutUint64(buf[8:], a[0][0])
|
||||||
binary.BigEndian.PutUint64(buf[16:], a[3])
|
binary.BigEndian.PutUint64(buf[16:], a[1][1])
|
||||||
binary.BigEndian.PutUint64(buf[24:], a[2])
|
binary.BigEndian.PutUint64(buf[24:], a[1][0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@ var testCasesSplit = []struct {
|
||||||
h1 *GF127
|
h1 *GF127
|
||||||
h2 *GF127
|
h2 *GF127
|
||||||
}{
|
}{
|
||||||
{&GF127x2{123, 31, 141, 9}, &GF127{123, 31}, &GF127{141, 9}},
|
{&GF127x2{GF127{123, 31}, GF127{141, 9}}, &GF127{123, 31}, &GF127{141, 9}},
|
||||||
{&GF127x2{maxUint64, 0, 0, maxUint64}, &GF127{maxUint64, 0}, &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) {
|
||||||
|
@ -32,9 +32,18 @@ func TestCombineTo(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var testCasesMul10x2 = [][2]*GF127x2{
|
var testCasesMul10x2 = [][2]*GF127x2{
|
||||||
{&GF127x2{123, 0, 123, 0}, &GF127x2{246, 0, 246, 0}},
|
{
|
||||||
{&GF127x2{maxUint64, 2, 0, 1}, &GF127x2{maxUint64 - 1, 5, 0, 2}},
|
&GF127x2{GF127{123, 0}, GF127{123, 0}},
|
||||||
{&GF127x2{0, maxUint64 >> 1, maxUint64, 2}, &GF127x2{1 + 1<<63, maxUint64>>1 - 1, maxUint64 - 1, 5}},
|
&GF127x2{GF127{246, 0}, GF127{246, 0}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
&GF127x2{GF127{maxUint64, 2}, GF127{0, 1}},
|
||||||
|
&GF127x2{GF127{maxUint64 - 1, 5}, GF127{0, 2}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
&GF127x2{GF127{0, maxUint64 >> 1}, GF127{maxUint64, 2}},
|
||||||
|
&GF127x2{GF127{1 + 1<<63, maxUint64>>1 - 1}, GF127{maxUint64 - 1, 5}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMul10x2(t *testing.T) {
|
func TestMul10x2(t *testing.T) {
|
||||||
|
@ -46,9 +55,18 @@ func TestMul10x2(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var testCasesMul11x2 = [][2]*GF127x2{
|
var testCasesMul11x2 = [][2]*GF127x2{
|
||||||
{&GF127x2{123, 0, 123, 0}, &GF127x2{141, 0, 141, 0}},
|
{
|
||||||
{&GF127x2{maxUint64, 2, 0, 1}, &GF127x2{1, 7, 0, 3}},
|
&GF127x2{GF127{123, 0}, GF127{123, 0}},
|
||||||
{&GF127x2{0, maxUint64 >> 1, maxUint64, 2}, &GF127x2{1 + 1<<63, 1, 1, 7}},
|
&GF127x2{GF127{141, 0}, GF127{141, 0}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
&GF127x2{GF127{maxUint64, 2}, GF127{0, 1}},
|
||||||
|
&GF127x2{GF127{1, 7}, GF127{0, 3}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
&GF127x2{GF127{0, maxUint64 >> 1}, GF127{maxUint64, 2}},
|
||||||
|
&GF127x2{GF127{1 + 1<<63, 1}, GF127{1, 7}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMul11x2(t *testing.T) {
|
func TestMul11x2(t *testing.T) {
|
||||||
|
|
|
@ -44,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] = gf127.GF127x2{1, 0, 0, 0}
|
d.x[0] = gf127.GF127x2{gf127.GF127{1, 0}, gf127.GF127{0, 0}}
|
||||||
d.x[1] = gf127.GF127x2{0, 0, 1, 0}
|
d.x[1] = gf127.GF127x2{gf127.GF127{0, 0}, gf127.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 }
|
||||||
|
|
|
@ -38,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] = gf127.GF127x2{1, 0, 0, 0}
|
d.x[0] = gf127.GF127x2{gf127.GF127{1, 0}, gf127.GF127{0, 0}}
|
||||||
d.x[1] = gf127.GF127x2{0, 0, 1, 0}
|
d.x[1] = gf127.GF127x2{gf127.GF127{0, 0}, gf127.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 }
|
||||||
|
|
Loading…
Reference in a new issue