go-ethereum/nomt/core/hasher.go
weiihann 7aebfb3c71 nomt/core: add Phase 1 core primitives for NOMT binary merkle trie
Implement foundational types and algorithms for the NOMT storage engine:
- node.go: Node/KeyPath/ValueHash types with MSB-based kind discrimination
- hasher.go: Keccak256 hashing with leaf/internal MSB labeling
- page.go: 4096-byte RawPage layout (126 nodes + elided children + pageID)
- pageid.go: PageID encode/decode with shift-then-add encoding
- triepos.go: TriePosition navigation (Down/Up/Sibling/PageID/NodeIndex)
- pagediff.go: 128-bit PageDiff bitfield for tracking changed nodes
- update.go: BuildTrie 3-pointer left-frontier algorithm, LeafOpsSpliced

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 17:05:00 +08:00

46 lines
1 KiB
Go

package core
import "golang.org/x/crypto/sha3"
// HashLeaf computes the hash of a leaf node: keccak256(keyPath || valueHash)
// with the MSB of byte 0 set to 1.
func HashLeaf(data *LeafData) Node {
h := sha3.NewLegacyKeccak256()
h.Write(data.KeyPath[:])
h.Write(data.ValueHash[:])
var out Node
h.Sum(out[:0])
setMSB(&out)
return out
}
// HashInternal computes the hash of an internal node: keccak256(left || right)
// with the MSB of byte 0 cleared to 0.
func HashInternal(data *InternalData) Node {
h := sha3.NewLegacyKeccak256()
h.Write(data.Left[:])
h.Write(data.Right[:])
var out Node
h.Sum(out[:0])
clearMSB(&out)
return out
}
// HashValue computes keccak256 of an arbitrary-length value.
func HashValue(value []byte) ValueHash {
h := sha3.NewLegacyKeccak256()
h.Write(value)
var out ValueHash
h.Sum(out[:0])
return out
}
// setMSB sets the most significant bit (bit 7 of byte 0) to 1.
func setMSB(n *Node) {
n[0] |= 0x80
}
// clearMSB clears the most significant bit (bit 7 of byte 0) to 0.
func clearMSB(n *Node) {
n[0] &= 0x7F
}