Add pure Go hash implementation

This commit is contained in:
Evgenii 2019-07-19 18:59:41 +03:00
parent 33f1403c28
commit 6c75cc0871
5 changed files with 105 additions and 2 deletions

View file

@ -6,7 +6,7 @@
tmpfile=$1 tmpfile=$1
go build ./cmd/tzsum && \ go build ./cmd/tzsum && \
for impl in avx avx2 avx2inline; do for impl in avx avx2 avx2inline purego; do
echo $impl implementation: echo $impl implementation:
/usr/bin/env time -f "time: %e seconds" ./tzsum -name $tmpfile -impl $impl /usr/bin/env time -f "time: %e seconds" ./tzsum -name $tmpfile -impl $impl
echo echo

View file

@ -17,7 +17,7 @@ var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile = flag.String("memprofile", "", "write memory profile to `file`") memprofile = flag.String("memprofile", "", "write memory profile to `file`")
filename = flag.String("name", "-", "file to use") filename = flag.String("name", "-", "file to use")
hashimpl = flag.String("impl", "avx2inline", "implementation to use") hashimpl = flag.String("impl", "", "implementation to use")
) )
func main() { func main() {
@ -54,6 +54,8 @@ func main() {
h = tz.NewWith(tz.AVX2) h = tz.NewWith(tz.AVX2)
case "avx2inline": case "avx2inline":
h = tz.NewWith(tz.AVX2Inline) h = tz.NewWith(tz.AVX2Inline)
case "purego":
h = tz.NewWith(tz.PureGo)
default: default:
h = tz.New() h = tz.New()
} }

View file

@ -18,6 +18,7 @@ const (
AVX AVX
AVX2 AVX2
AVX2Inline AVX2Inline
PureGo
) )
func (impl Implementation) String() string { func (impl Implementation) String() string {
@ -28,6 +29,8 @@ func (impl Implementation) String() string {
return "AVX2" return "AVX2"
case AVX2Inline: case AVX2Inline:
return "AVX2Inline" return "AVX2Inline"
case PureGo:
return "PureGo"
default: default:
return "UNKNOWN" return "UNKNOWN"
} }
@ -41,6 +44,8 @@ func NewWith(impl Implementation) hash.Hash {
return newAVX2() return newAVX2()
case AVX2Inline: case AVX2Inline:
return newAVX2Inline() return newAVX2Inline()
case PureGo:
return newPure()
default: default:
return New() return New()
} }

View file

@ -15,6 +15,7 @@ var providers = []Implementation{
AVX, AVX,
AVX2, AVX2,
AVX2Inline, AVX2Inline,
PureGo,
} }
func TestNewWith(t *testing.T) { func TestNewWith(t *testing.T) {
@ -26,6 +27,9 @@ func TestNewWith(t *testing.T) {
d = NewWith(AVX2Inline) d = NewWith(AVX2Inline)
require.IsType(t, (*digest3)(nil), d) require.IsType(t, (*digest3)(nil), d)
d = NewWith(PureGo)
require.IsType(t, (*digestp)(nil), d)
} }
var testCases = []struct { var testCases = []struct {

92
tz/pure.go Normal file
View file

@ -0,0 +1,92 @@
package tz
import (
"github.com/nspcc-dev/tzhash/gogf127"
)
type digestp struct {
x [4]gogf127.GF127
}
// New returns a new hash.Hash computing the Tillich-Zémor checksum.
func newPure() *digestp {
d := new(digestp)
d.Reset()
return d
}
func (d *digestp) Sum(in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing.
d0 := *d
h := d0.checkSum()
return append(in, h[:]...)
}
func (d *digestp) checkSum() [hashSize]byte {
return d.byteArray()
}
func (d *digestp) byteArray() (b [hashSize]byte) {
for i := 0; i < 4; i++ {
t := d.x[i].ByteArray()
copy(b[i*16:], t[:])
}
return
}
func (d *digestp) Reset() {
d.x[0] = gogf127.GF127{1, 0}
d.x[1] = gogf127.GF127{0, 0}
d.x[2] = gogf127.GF127{0, 0}
d.x[3] = gogf127.GF127{1, 0}
}
func (d *digestp) Write(data []byte) (n int, err error) {
n = len(data)
for _, b := range data {
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x80 != 0)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x40 != 0)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x20 != 0)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x10 != 0)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x08 != 0)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x04 != 0)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x02 != 0)
mulBitRightPure(&d.x[0], &d.x[1], &d.x[2], &d.x[3], b&0x01 != 0)
}
return
}
func (d *digestp) Size() int {
return hashSize
}
func (d *digestp) BlockSize() int {
return hashBlockSize
}
func mulBitRightPure(c00, c01, c10, c11 *gogf127.GF127, bit bool) {
var tmp gogf127.GF127
if bit {
tmp = *c00
gogf127.Mul10(c00, c00)
gogf127.Add(c00, c01, c00)
gogf127.Mul11(&tmp, &tmp)
gogf127.Add(c01, &tmp, c01)
tmp = *c10
gogf127.Mul10(c10, c10)
gogf127.Add(c10, c11, c10)
gogf127.Mul11(&tmp, &tmp)
gogf127.Add(c11, &tmp, c11)
} else {
tmp = *c00
gogf127.Mul10(c00, c00)
gogf127.Add(c00, c01, c00)
*c01 = tmp
tmp = *c10
gogf127.Mul10(c10, c10)
gogf127.Add(c10, c11, c10)
*c11 = tmp
}
}