tree.go (3595B)
1 // Copyright 2026 Shivakumar (Certichain) 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package merkle 16 17 import ( 18 "errors" 19 20 "certichain/internal/crypto" 21 ) 22 23 // RFC 6962 prefixes 24 // 25 // Leaf hashes are computed as: 26 // 27 // SHA256(0x00 | leaf_data) 28 // 29 // Node hashes are computed as: 30 // 31 // SHA256(0x01 | left_hash | right_hash) 32 const ( 33 leafHashPrefix byte = 0x00 34 nodeHashPrefix byte = 0x01 35 ) 36 37 // Node represents a single Merkle tree node. 38 // 39 // Leaf node have nil children. 40 // Internal nodes have pointers to left and right children. 41 type Node struct { 42 hash crypto.Hash 43 left *Node 44 right *Node 45 parent *Node 46 } 47 48 // Tree represents an RFC 6962 Merkle tree. 49 // 50 // The leaves slice preserves the original leaf ordering so 51 // inclusion proofs can later be generated efficiently without 52 // re-traversing the entire tree. 53 type Tree struct { 54 root *Node 55 leaves []*Node 56 leafCount int 57 } 58 59 // New constructs a new RFC 6962 Merkle tree from raw leaf data. 60 // 61 // The tree is constructed using the RFC 6962 canonical split rule: 62 // split of the largest power of two smaller than n. 63 func New(data [][]byte) (*Tree, error) { 64 if len(data) == 0 { 65 return nil, errors.New("merkle: empty input") 66 } 67 68 leaves := make([]*Node, 0, len(data)) 69 70 for _, datum := range data { 71 if datum == nil { 72 return nil, errors.New("merkle: empty leaf data") 73 } 74 75 leaf := &Node{ 76 hash: hashLeaf(datum), 77 } 78 leaves = append(leaves, leaf) 79 } 80 81 return &Tree{ 82 root: buildSubtree(leaves), 83 leaves: leaves, 84 leafCount: len(leaves), 85 }, nil 86 } 87 88 // buildSubtree recursively constructs an RFC 6962 Merkle subtree. 89 func buildSubtree(nodes []*Node) *Node { 90 if len(nodes) == 0 { 91 panic("merkle: buildSubtree called with empty nodes") 92 } 93 n := len(nodes) 94 95 if n == 1 { 96 return nodes[0] 97 } 98 99 k := 1 100 for k<<1 < n { 101 k <<= 1 102 } 103 104 left := buildSubtree(nodes[:k]) 105 right := buildSubtree(nodes[k:]) 106 107 parent := &Node{ 108 hash: hashNode(left.hash, right.hash), 109 left: left, 110 right: right, 111 } 112 113 left.parent, right.parent = parent, parent 114 115 return parent 116 } 117 118 // Root returns the copy of the Merkle root hash. 119 // 120 // A copy is returned to prevent callers from mutating 121 // internal tree state. 122 func (t *Tree) Root() []byte { 123 if t == nil || t.root == nil { 124 return nil 125 } 126 127 root := make([]byte, crypto.HashSize) 128 copy(root, t.root.hash[:]) 129 130 return root 131 } 132 133 // LeafCount returns the number of original leaves used to 134 // construct the tree. 135 func (t *Tree) LeafCount() int { 136 if t == nil { 137 return 0 138 } 139 140 return t.leafCount 141 } 142 143 // hashLeaf computes an RFC 6962 leaf hash: 144 // 145 // returns SHA256(0x00 | leaf_data) 146 func hashLeaf(data []byte) crypto.Hash { 147 buf := make([]byte, 1+len(data)) 148 149 buf[0] = leafHashPrefix 150 copy(buf[1:], data) 151 152 return crypto.HashBytes(buf) 153 } 154 155 // hashNode computes an RFC 6962 internal node hash: 156 // 157 // returns SHA256(0x01 | left_hash | right_hash) 158 func hashNode(left, right crypto.Hash) crypto.Hash { 159 var buf [1 + 2*crypto.HashSize]byte 160 161 buf[0] = nodeHashPrefix 162 copy(buf[1:1+crypto.HashSize], left[:]) 163 copy(buf[1+crypto.HashSize:], right[:]) 164 165 return crypto.HashBytes(buf[:]) 166 }