2021-11-29 16:30:17 +00:00
|
|
|
package stateutil
|
2021-11-17 09:04:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/crypto/hash"
|
|
|
|
"github.com/prysmaticlabs/prysm/encoding/ssz"
|
|
|
|
)
|
|
|
|
|
2022-02-22 09:27:51 +00:00
|
|
|
func arraysRoot(input [][]byte, length uint64) ([32]byte, error) {
|
2021-11-17 09:04:49 +00:00
|
|
|
hashFunc := hash.CustomSHA256Hasher()
|
|
|
|
|
|
|
|
leaves := make([][32]byte, length)
|
|
|
|
for i, chunk := range input {
|
|
|
|
copy(leaves[i][:], chunk)
|
|
|
|
}
|
2022-02-22 09:27:51 +00:00
|
|
|
res, err := merkleize(leaves, length, hashFunc)
|
2021-11-17 09:04:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:27:51 +00:00
|
|
|
return res, nil
|
2021-11-17 09:04:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 09:27:51 +00:00
|
|
|
func merkleize(leaves [][32]byte, length uint64,
|
|
|
|
hasher func([]byte) [32]byte) ([32]byte, error) {
|
2021-11-17 09:04:49 +00:00
|
|
|
if len(leaves) == 0 {
|
|
|
|
return [32]byte{}, errors.New("zero leaves provided")
|
|
|
|
}
|
|
|
|
if len(leaves) == 1 {
|
|
|
|
return leaves[0], nil
|
|
|
|
}
|
|
|
|
hashLayer := leaves
|
|
|
|
layers := make([][][32]byte, ssz.Depth(length)+1)
|
|
|
|
layers[0] = hashLayer
|
|
|
|
var err error
|
2022-02-22 09:27:51 +00:00
|
|
|
_, hashLayer, err = MerkleizeTrieLeaves(layers, hashLayer, hasher)
|
2021-11-17 09:04:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
root := hashLayer[0]
|
|
|
|
return root, nil
|
|
|
|
}
|