Some checks are pending
docker-build-cometbft / vars (push) Waiting to run
docker-build-cometbft / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-cometbft / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-cometbft / merge-images (push) Blocked by required conditions
docker-build-e2e-node / vars (push) Waiting to run
docker-build-e2e-node / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-e2e-node / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-e2e-node / merge-images (push) Blocked by required conditions
44 lines
848 B
Go
44 lines
848 B
Go
package merkle
|
|
|
|
import (
|
|
"hash"
|
|
|
|
"github.com/cometbft/cometbft/crypto/tmhash"
|
|
)
|
|
|
|
// TODO: make these have a large predefined capacity
|
|
var (
|
|
leafPrefix = []byte{0}
|
|
innerPrefix = []byte{1}
|
|
)
|
|
|
|
// returns tmhash(<empty>)
|
|
func emptyHash() []byte {
|
|
return tmhash.Sum([]byte{})
|
|
}
|
|
|
|
// returns tmhash(0x00 || leaf)
|
|
func leafHash(leaf []byte) []byte {
|
|
return tmhash.Sum(append(leafPrefix, leaf...))
|
|
}
|
|
|
|
// returns tmhash(0x00 || leaf)
|
|
func leafHashOpt(s hash.Hash, leaf []byte) []byte {
|
|
s.Reset()
|
|
s.Write(leafPrefix)
|
|
s.Write(leaf)
|
|
return s.Sum(nil)
|
|
}
|
|
|
|
// returns tmhash(0x01 || left || right)
|
|
func innerHash(left []byte, right []byte) []byte {
|
|
return tmhash.SumMany(innerPrefix, left, right)
|
|
}
|
|
|
|
func innerHashOpt(s hash.Hash, left []byte, right []byte) []byte {
|
|
s.Reset()
|
|
s.Write(innerPrefix)
|
|
s.Write(left)
|
|
s.Write(right)
|
|
return s.Sum(nil)
|
|
}
|