erigon-pulse/cl/merkle_tree/primitives.go
a 37da9ec1e3
[caplin] ssz byteobjects (#7454)
instead of converting from ssz -> struct -> ssz, it may be better to
just stay as ssz, then use methods to read the data.

this pr explores this concept, while maintaining compatiblity with the
existing codebase.
2023-05-10 21:37:50 +02:00

47 lines
1004 B
Go

package merkle_tree
import (
"encoding/binary"
libcommon "github.com/ledgerwatch/erigon-lib/common"
)
// Uint64Root retrieves the root hash of a uint64 value by converting it to a byte array and returning it as a hash.
func Uint64Root(val uint64) libcommon.Hash {
var root libcommon.Hash
binary.LittleEndian.PutUint64(root[:], val)
return root
}
func BoolRoot(b bool) (root libcommon.Hash) {
if b {
root[0] = 1
}
return
}
func SignatureRoot(signature [96]byte) (libcommon.Hash, error) {
return ArraysRoot([][32]byte{
libcommon.BytesToHash(signature[0:32]),
libcommon.BytesToHash(signature[32:64]),
libcommon.BytesToHash(signature[64:]),
}, 4)
}
func PublicKeyRoot(key [48]byte) (libcommon.Hash, error) {
var lastByte [32]byte
copy(lastByte[:], key[32:])
return ArraysRoot([][32]byte{
libcommon.BytesToHash(key[:32]),
lastByte,
}, 2)
}
func InPlacePublicKeyRoot(key []byte) error {
err := MerkleRootFromFlatLeaves(key, key)
if err != nil {
return err
}
return nil
}