mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-22 03:30:35 +00:00
45bfd82c88
* ssz package * compile * htrutils * rem pkg doc * fix cloners_test.go * fix circular dep/build issues Co-authored-by: prestonvanloon <preston@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package ssz
|
|
|
|
import "encoding/binary"
|
|
|
|
// HashFn is the generic hash function signature.
|
|
type HashFn func(input []byte) [32]byte
|
|
|
|
// Hasher describes an interface through which we can
|
|
// perform hash operations on byte arrays,indices,etc.
|
|
type Hasher interface {
|
|
Hash(a []byte) [32]byte
|
|
Combi(a [32]byte, b [32]byte) [32]byte
|
|
MixIn(a [32]byte, i uint64) [32]byte
|
|
}
|
|
|
|
// HasherFunc defines a structure to hold a hash function and can be used for multiple rounds of
|
|
// hashing.
|
|
type HasherFunc struct {
|
|
b [64]byte
|
|
hashFunc HashFn
|
|
}
|
|
|
|
// NewHasherFunc is the constructor for the object
|
|
// that fulfills the Hasher interface.
|
|
func NewHasherFunc(h HashFn) *HasherFunc {
|
|
return &HasherFunc{
|
|
b: [64]byte{},
|
|
hashFunc: h,
|
|
}
|
|
}
|
|
|
|
// Hash utilizes the provided hash function for
|
|
// the object.
|
|
func (h *HasherFunc) Hash(a []byte) [32]byte {
|
|
return h.hashFunc(a)
|
|
}
|
|
|
|
// Combi appends the two inputs and hashes them.
|
|
func (h *HasherFunc) Combi(a, b [32]byte) [32]byte {
|
|
copy(h.b[:32], a[:])
|
|
copy(h.b[32:], b[:])
|
|
return h.Hash(h.b[:])
|
|
}
|
|
|
|
// MixIn works like Combi, but using an integer as the second input.
|
|
func (h *HasherFunc) MixIn(a [32]byte, i uint64) [32]byte {
|
|
copy(h.b[:32], a[:])
|
|
copy(h.b[32:], make([]byte, 32))
|
|
binary.LittleEndian.PutUint64(h.b[32:], i)
|
|
return h.Hash(h.b[:])
|
|
}
|