2023-01-07 11:25:28 +00:00
|
|
|
package merkle_tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-05-23 18:58:34 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/length"
|
2023-01-07 11:25:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Uint64Root retrieves the root hash of a uint64 value by converting it to a byte array and returning it as a hash.
|
2023-01-13 18:12:18 +00:00
|
|
|
func Uint64Root(val uint64) libcommon.Hash {
|
|
|
|
var root libcommon.Hash
|
2023-01-07 11:25:28 +00:00
|
|
|
binary.LittleEndian.PutUint64(root[:], val)
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:41:16 +00:00
|
|
|
func BoolRoot(b bool) (root libcommon.Hash) {
|
|
|
|
if b {
|
|
|
|
root[0] = 1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:58:34 +00:00
|
|
|
func BytesRoot(b []byte) (out [32]byte, err error) {
|
|
|
|
leafCount := NextPowerOfTwo(uint64((len(b) + 31) / length.Hash))
|
|
|
|
leaves := make([]byte, leafCount*length.Hash)
|
|
|
|
copy(leaves, b)
|
|
|
|
if err = MerkleRootFromFlatLeaves(leaves, leaves); err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
copy(out[:], leaves)
|
|
|
|
return
|
2023-01-09 00:04:05 +00:00
|
|
|
}
|
2023-05-10 19:37:50 +00:00
|
|
|
|
2023-05-14 22:12:24 +00:00
|
|
|
func InPlaceRoot(key []byte) error {
|
2023-05-10 19:37:50 +00:00
|
|
|
err := MerkleRootFromFlatLeaves(key, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|