2022-02-25 11:50:48 +00:00
|
|
|
// Package tz contains Tillich-Zemor checksum implementations
|
|
|
|
// using different backends.
|
2018-12-29 13:04:17 +00:00
|
|
|
//
|
2022-02-25 11:50:48 +00:00
|
|
|
// Copyright 2022 (c) NSPCC
|
2018-12-29 13:04:17 +00:00
|
|
|
package tz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"hash"
|
2019-10-09 14:51:41 +00:00
|
|
|
|
2020-01-16 07:54:54 +00:00
|
|
|
"golang.org/x/sys/cpu"
|
2018-12-29 13:04:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-19 14:52:46 +00:00
|
|
|
type Implementation int
|
|
|
|
|
2018-12-29 13:04:17 +00:00
|
|
|
const (
|
2022-02-25 11:48:11 +00:00
|
|
|
// Size is the size of a Tillich-Zemor hash sum in bytes.
|
|
|
|
Size = 64
|
2018-12-29 13:04:17 +00:00
|
|
|
hashBlockSize = 128
|
|
|
|
|
2019-07-19 14:52:46 +00:00
|
|
|
_ Implementation = iota
|
|
|
|
AVX
|
|
|
|
AVX2
|
|
|
|
AVX2Inline
|
2019-07-19 15:59:41 +00:00
|
|
|
PureGo
|
2019-10-15 11:37:59 +00:00
|
|
|
AVXInline
|
2018-12-29 13:04:17 +00:00
|
|
|
)
|
|
|
|
|
2019-09-04 07:28:51 +00:00
|
|
|
var (
|
2020-01-16 07:54:54 +00:00
|
|
|
hasAVX = cpu.X86.HasAVX
|
2019-10-09 14:52:58 +00:00
|
|
|
// Having AVX2 does not guarantee
|
|
|
|
// that AVX is also present.
|
2020-01-16 07:54:54 +00:00
|
|
|
hasAVX2 = cpu.X86.HasAVX2 && hasAVX
|
2019-09-04 07:28:51 +00:00
|
|
|
)
|
|
|
|
|
2019-07-19 14:52:46 +00:00
|
|
|
func (impl Implementation) String() string {
|
|
|
|
switch impl {
|
|
|
|
case AVX:
|
|
|
|
return "AVX"
|
2019-10-15 11:37:59 +00:00
|
|
|
case AVXInline:
|
|
|
|
return "AVXInline"
|
2019-07-19 14:52:46 +00:00
|
|
|
case AVX2:
|
|
|
|
return "AVX2"
|
|
|
|
case AVX2Inline:
|
|
|
|
return "AVX2Inline"
|
2019-07-19 15:59:41 +00:00
|
|
|
case PureGo:
|
|
|
|
return "PureGo"
|
2019-07-19 14:52:46 +00:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
2018-12-29 13:04:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:52:46 +00:00
|
|
|
func NewWith(impl Implementation) hash.Hash {
|
|
|
|
switch impl {
|
|
|
|
case AVX:
|
|
|
|
return newAVX()
|
2019-10-15 11:37:59 +00:00
|
|
|
case AVXInline:
|
|
|
|
return newAVXInline()
|
2019-07-19 14:52:46 +00:00
|
|
|
case AVX2:
|
|
|
|
return newAVX2()
|
|
|
|
case AVX2Inline:
|
|
|
|
return newAVX2Inline()
|
2019-07-19 15:59:41 +00:00
|
|
|
case PureGo:
|
|
|
|
return newPure()
|
2019-07-19 14:52:46 +00:00
|
|
|
default:
|
|
|
|
return New()
|
|
|
|
}
|
2018-12-29 13:04:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:52:46 +00:00
|
|
|
// New returns a new hash.Hash computing the Tillich-Zémor checksum.
|
|
|
|
func New() hash.Hash {
|
2019-09-04 07:28:51 +00:00
|
|
|
if hasAVX2 {
|
|
|
|
return newAVX2Inline()
|
|
|
|
} else if hasAVX {
|
2019-10-16 11:52:16 +00:00
|
|
|
return newAVXInline()
|
2019-09-04 07:28:51 +00:00
|
|
|
} else {
|
|
|
|
return newPure()
|
|
|
|
}
|
2018-12-29 13:04:17 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 20:10:08 +00:00
|
|
|
// Sum returns Tillich-Zémor checksum of data.
|
2022-02-25 11:48:11 +00:00
|
|
|
func Sum(data []byte) [Size]byte {
|
2019-10-04 14:57:53 +00:00
|
|
|
if hasAVX2 {
|
|
|
|
d := newAVX2Inline()
|
|
|
|
_, _ = d.Write(data) // no errors
|
|
|
|
return d.checkSum()
|
|
|
|
} else if hasAVX {
|
2019-10-16 11:52:16 +00:00
|
|
|
d := newAVXInline()
|
2019-10-04 14:57:53 +00:00
|
|
|
_, _ = d.Write(data) // no errors
|
|
|
|
return d.checkSum()
|
|
|
|
} else {
|
|
|
|
d := newPure()
|
|
|
|
_, _ = d.Write(data) // no errors
|
|
|
|
return d.checkSum()
|
|
|
|
}
|
2019-06-21 20:10:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 13:04:17 +00:00
|
|
|
// Concat performs combining of hashes based on homomorphic property.
|
|
|
|
func Concat(hs [][]byte) ([]byte, error) {
|
|
|
|
var b, c sl2
|
|
|
|
|
|
|
|
b = id
|
|
|
|
for i := range hs {
|
|
|
|
if err := c.UnmarshalBinary(hs[i]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.Mul(&b, &c)
|
|
|
|
}
|
|
|
|
return b.MarshalBinary()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks if hashes in hs combined are equal to h.
|
|
|
|
func Validate(h []byte, hs [][]byte) (bool, error) {
|
|
|
|
var (
|
|
|
|
b []byte
|
2022-02-25 11:48:11 +00:00
|
|
|
got, expected [Size]byte
|
2018-12-29 13:04:17 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-02-25 11:48:11 +00:00
|
|
|
if len(h) != Size {
|
2018-12-29 13:04:17 +00:00
|
|
|
return false, errors.New("invalid hash")
|
|
|
|
} else if len(hs) == 0 {
|
|
|
|
return false, errors.New("empty slice")
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(expected[:], h)
|
|
|
|
|
|
|
|
b, err = Concat(hs)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("cant concatenate hashes")
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(got[:], b)
|
|
|
|
|
|
|
|
return expected == got, nil
|
|
|
|
}
|
|
|
|
|
2019-01-29 13:11:48 +00:00
|
|
|
// SubtractR returns hash a, such that Concat(a, b) == c
|
|
|
|
// This is possible, because Tillich-Zemor hash is actually a matrix
|
|
|
|
// which can be inversed.
|
|
|
|
func SubtractR(c, b []byte) (a []byte, err error) {
|
|
|
|
var p1, p2, r sl2
|
|
|
|
|
|
|
|
if err = r.UnmarshalBinary(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = p2.UnmarshalBinary(b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p1 = *Inv(&p2)
|
|
|
|
p1.Mul(&r, &p1)
|
|
|
|
|
|
|
|
return p1.MarshalBinary()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubtractL returns hash b, such that Concat(a, b) == c
|
|
|
|
// This is possible, because Tillich-Zemor hash is actually a matrix
|
|
|
|
// which can be inversed.
|
|
|
|
func SubtractL(c, a []byte) (b []byte, err error) {
|
|
|
|
var p1, p2, r sl2
|
|
|
|
|
|
|
|
if err = r.UnmarshalBinary(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = p1.UnmarshalBinary(a); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p2 = *Inv(&p1)
|
|
|
|
p2.Mul(&p2, &r)
|
|
|
|
|
|
|
|
return p2.MarshalBinary()
|
|
|
|
}
|