2019-07-19 14:52:46 +00:00
|
|
|
// Copyright 2019 (c) NSPCC
|
|
|
|
//
|
|
|
|
// This file contains AVX2 implementation.
|
2019-06-21 19:29:08 +00:00
|
|
|
package tz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hash"
|
|
|
|
|
2019-10-15 09:10:39 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/gf127/avx2"
|
2019-06-21 19:29:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type digest2 struct {
|
2019-10-15 09:10:39 +00:00
|
|
|
x [2]avx2.GF127x2
|
2019-06-21 19:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:52:46 +00:00
|
|
|
// type assertion
|
2019-06-24 07:07:16 +00:00
|
|
|
var _ hash.Hash = (*digest2)(nil)
|
2019-06-21 19:29:08 +00:00
|
|
|
|
2019-07-19 14:52:46 +00:00
|
|
|
func newAVX2() *digest2 {
|
2019-07-19 13:11:01 +00:00
|
|
|
d := new(digest2)
|
|
|
|
d.Reset()
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:29:08 +00:00
|
|
|
func (d *digest2) Write(data []byte) (n int, err error) {
|
|
|
|
n = len(data)
|
|
|
|
for _, b := range data {
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>7)&1])
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>6)&1])
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>5)&1])
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>4)&1])
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>3)&1])
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>2)&1])
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>1)&1])
|
|
|
|
mulBitRightx2(&d.x[0], &d.x[1], &minmax[(b>>0)&1])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *digest2) 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 *digest2) Reset() {
|
2019-10-15 09:20:36 +00:00
|
|
|
d.x[0] = avx2.GF127x2{GF127{1, 0}, GF127{0, 0}}
|
|
|
|
d.x[1] = avx2.GF127x2{GF127{0, 0}, GF127{1, 0}}
|
2019-06-21 19:29:08 +00:00
|
|
|
}
|
|
|
|
func (d *digest2) Size() int { return hashSize }
|
|
|
|
func (d *digest2) BlockSize() int { return hashBlockSize }
|
|
|
|
func (d *digest2) checkSum() (b [hashSize]byte) {
|
|
|
|
// Matrix is stored transposed,
|
|
|
|
// but we need to use order consistent with digest.
|
|
|
|
h := d.x[0].ByteArray()
|
2019-06-21 20:10:08 +00:00
|
|
|
copy(b[:], h[:16])
|
|
|
|
copy(b[32:], h[16:])
|
2019-06-21 19:29:08 +00:00
|
|
|
|
|
|
|
h = d.x[1].ByteArray()
|
2019-06-21 20:10:08 +00:00
|
|
|
copy(b[16:], h[:16])
|
|
|
|
copy(b[48:], h[16:])
|
2019-06-21 19:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:20:36 +00:00
|
|
|
func mulBitRightx2(c00c10 *avx2.GF127x2, c01c11 *avx2.GF127x2, e *GF127)
|