2021-09-21 15:02:48 +00:00
|
|
|
// Package ssz defines HashTreeRoot utility functions.
|
|
|
|
package ssz
|
2019-12-04 19:20:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
|
|
"github.com/minio/sha256-simd"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
2019-12-04 19:20:33 +00:00
|
|
|
)
|
|
|
|
|
2020-06-18 02:15:13 +00:00
|
|
|
const bytesPerChunk = 32
|
|
|
|
|
|
|
|
// BitlistRoot returns the mix in length of a bitwise Merkleized bitfield.
|
2023-03-17 11:41:02 +00:00
|
|
|
func BitlistRoot(bfield bitfield.Bitfield, maxCapacity uint64) ([32]byte, error) {
|
2019-12-04 19:20:33 +00:00
|
|
|
limit := (maxCapacity + 255) / 256
|
|
|
|
if bfield == nil || bfield.Len() == 0 {
|
|
|
|
length := make([]byte, 32)
|
2023-03-17 11:41:02 +00:00
|
|
|
root, err := BitwiseMerkleize([][32]byte{}, 0, limit)
|
2019-12-04 19:20:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
2020-06-18 02:15:13 +00:00
|
|
|
return MixInLength(root, length), nil
|
2019-12-04 19:20:33 +00:00
|
|
|
}
|
2022-03-04 15:19:07 +00:00
|
|
|
chunks, err := PackByChunk([][]byte{bfield.Bytes()})
|
2019-12-04 19:20:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := binary.Write(buf, binary.LittleEndian, bfield.Len()); err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
output := make([]byte, 32)
|
|
|
|
copy(output, buf.Bytes())
|
2023-03-17 11:41:02 +00:00
|
|
|
root, err := BitwiseMerkleize(chunks, uint64(len(chunks)), limit)
|
2019-12-04 19:20:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
2020-06-18 02:15:13 +00:00
|
|
|
return MixInLength(root, output), nil
|
2019-12-04 19:20:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 02:15:13 +00:00
|
|
|
// BitwiseMerkleize - given ordered BYTES_PER_CHUNK-byte chunks, if necessary utilize
|
|
|
|
// zero chunks so that the number of chunks is a power of two, Merkleize the chunks,
|
|
|
|
// and return the root.
|
2019-12-04 19:20:33 +00:00
|
|
|
// Note that merkleize on a single chunk is simply that chunk, i.e. the identity
|
|
|
|
// when the number of chunks is one.
|
2023-03-17 11:41:02 +00:00
|
|
|
func BitwiseMerkleize(chunks [][32]byte, count, limit uint64) ([32]byte, error) {
|
2020-01-31 20:57:01 +00:00
|
|
|
if count > limit {
|
|
|
|
return [32]byte{}, errors.New("merkleizing list that is too large, over limit")
|
|
|
|
}
|
2023-03-17 11:41:02 +00:00
|
|
|
return MerkleizeVector(chunks, limit), nil
|
2020-01-31 20:57:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 12:01:15 +00:00
|
|
|
// PackByChunk a given byte array's final chunk with zeroes if needed.
|
|
|
|
func PackByChunk(serializedItems [][]byte) ([][bytesPerChunk]byte, error) {
|
2022-12-22 09:20:10 +00:00
|
|
|
var emptyChunk [bytesPerChunk]byte
|
2021-11-19 12:01:15 +00:00
|
|
|
// If there are no items, we return an empty chunk.
|
|
|
|
if len(serializedItems) == 0 {
|
|
|
|
return [][bytesPerChunk]byte{emptyChunk}, nil
|
|
|
|
} else if len(serializedItems[0]) == bytesPerChunk {
|
|
|
|
// If each item has exactly BYTES_PER_CHUNK length, we return the list of serialized items.
|
|
|
|
chunks := make([][bytesPerChunk]byte, 0, len(serializedItems))
|
|
|
|
for _, c := range serializedItems {
|
|
|
|
chunks = append(chunks, bytesutil.ToBytes32(c))
|
|
|
|
}
|
|
|
|
return chunks, nil
|
|
|
|
}
|
|
|
|
// We flatten the list in order to pack its items into byte chunks correctly.
|
2022-03-23 11:57:48 +00:00
|
|
|
orderedItems := make([]byte, 0, len(serializedItems)*len(serializedItems[0]))
|
2021-11-19 12:01:15 +00:00
|
|
|
for _, item := range serializedItems {
|
|
|
|
orderedItems = append(orderedItems, item...)
|
|
|
|
}
|
|
|
|
// If all our serialized item slices are length zero, we
|
|
|
|
// exit early.
|
|
|
|
if len(orderedItems) == 0 {
|
|
|
|
return [][bytesPerChunk]byte{emptyChunk}, nil
|
|
|
|
}
|
|
|
|
numItems := len(orderedItems)
|
|
|
|
var chunks [][bytesPerChunk]byte
|
|
|
|
for i := 0; i < numItems; i += bytesPerChunk {
|
|
|
|
j := i + bytesPerChunk
|
|
|
|
// We create our upper bound index of the chunk, if it is greater than numItems,
|
|
|
|
// we set it as numItems itself.
|
|
|
|
if j > numItems {
|
|
|
|
j = numItems
|
|
|
|
}
|
|
|
|
// We create chunks from the list of items based on the
|
|
|
|
// indices determined above.
|
|
|
|
// Right-pad the last chunk with zero bytes if it does not
|
|
|
|
// have length bytesPerChunk from the helper.
|
|
|
|
// The ToBytes32 helper allocates a 32-byte array, before
|
|
|
|
// copying the ordered items in. This ensures that even if
|
|
|
|
// the last chunk is != 32 in length, we will right-pad it with
|
|
|
|
// zero bytes.
|
|
|
|
chunks = append(chunks, bytesutil.ToBytes32(orderedItems[i:j]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks, nil
|
|
|
|
}
|
|
|
|
|
2020-06-18 02:15:13 +00:00
|
|
|
// MixInLength appends hash length to root
|
|
|
|
func MixInLength(root [32]byte, length []byte) [32]byte {
|
2019-12-04 19:20:33 +00:00
|
|
|
var hash [32]byte
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write(root[:])
|
|
|
|
h.Write(length)
|
|
|
|
// The hash interface never returns an error, for that reason
|
|
|
|
// we are not handling the error below. For reference, it is
|
|
|
|
// stated here https://golang.org/pkg/hash/#Hash
|
|
|
|
// #nosec G104
|
|
|
|
h.Sum(hash[:0])
|
|
|
|
return hash
|
|
|
|
}
|