github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/crypto/merkle/proof.go (about) 1 package merkle 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 8 "github.com/badrootd/nibiru-cometbft/crypto/tmhash" 9 cmtcrypto "github.com/badrootd/nibiru-cometbft/proto/tendermint/crypto" 10 ) 11 12 const ( 13 // MaxAunts is the maximum number of aunts that can be included in a Proof. 14 // This corresponds to a tree of size 2^100, which should be sufficient for all conceivable purposes. 15 // This maximum helps prevent Denial-of-Service attacks by limitting the size of the proofs. 16 MaxAunts = 100 17 ) 18 19 // Proof represents a Merkle proof. 20 // NOTE: The convention for proofs is to include leaf hashes but to 21 // exclude the root hash. 22 // This convention is implemented across IAVL range proofs as well. 23 // Keep this consistent unless there's a very good reason to change 24 // everything. This also affects the generalized proof system as 25 // well. 26 type Proof struct { 27 Total int64 `json:"total"` // Total number of items. 28 Index int64 `json:"index"` // Index of item to prove. 29 LeafHash []byte `json:"leaf_hash"` // Hash of item value. 30 Aunts [][]byte `json:"aunts"` // Hashes from leaf's sibling to a root's child. 31 } 32 33 // ProofsFromByteSlices computes inclusion proof for given items. 34 // proofs[0] is the proof for items[0]. 35 func ProofsFromByteSlices(items [][]byte) (rootHash []byte, proofs []*Proof) { 36 trails, rootSPN := trailsFromByteSlices(items) 37 rootHash = rootSPN.Hash 38 proofs = make([]*Proof, len(items)) 39 for i, trail := range trails { 40 proofs[i] = &Proof{ 41 Total: int64(len(items)), 42 Index: int64(i), 43 LeafHash: trail.Hash, 44 Aunts: trail.FlattenAunts(), 45 } 46 } 47 return 48 } 49 50 // Verify that the Proof proves the root hash. 51 // Check sp.Index/sp.Total manually if needed 52 func (sp *Proof) Verify(rootHash []byte, leaf []byte) error { 53 if rootHash == nil { 54 return fmt.Errorf("invalid root hash: cannot be nil") 55 } 56 if sp.Total < 0 { 57 return errors.New("proof total must be positive") 58 } 59 if sp.Index < 0 { 60 return errors.New("proof index cannot be negative") 61 } 62 leafHash := leafHash(leaf) 63 if !bytes.Equal(sp.LeafHash, leafHash) { 64 return fmt.Errorf("invalid leaf hash: wanted %X got %X", leafHash, sp.LeafHash) 65 } 66 computedHash, err := sp.computeRootHash() 67 if err != nil { 68 return fmt.Errorf("compute root hash: %w", err) 69 } 70 if !bytes.Equal(computedHash, rootHash) { 71 return fmt.Errorf("invalid root hash: wanted %X got %X", rootHash, computedHash) 72 } 73 return nil 74 } 75 76 // Compute the root hash given a leaf hash. Panics in case of errors. 77 func (sp *Proof) ComputeRootHash() []byte { 78 computedHash, err := sp.computeRootHash() 79 if err != nil { 80 panic(fmt.Errorf("ComputeRootHash errored %w", err)) 81 } 82 return computedHash 83 } 84 85 // Compute the root hash given a leaf hash. 86 func (sp *Proof) computeRootHash() ([]byte, error) { 87 return computeHashFromAunts( 88 sp.Index, 89 sp.Total, 90 sp.LeafHash, 91 sp.Aunts, 92 ) 93 } 94 95 // String implements the stringer interface for Proof. 96 // It is a wrapper around StringIndented. 97 func (sp *Proof) String() string { 98 return sp.StringIndented("") 99 } 100 101 // StringIndented generates a canonical string representation of a Proof. 102 func (sp *Proof) StringIndented(indent string) string { 103 return fmt.Sprintf(`Proof{ 104 %s Aunts: %X 105 %s}`, 106 indent, sp.Aunts, 107 indent) 108 } 109 110 // ValidateBasic performs basic validation. 111 // NOTE: it expects the LeafHash and the elements of Aunts to be of size tmhash.Size, 112 // and it expects at most MaxAunts elements in Aunts. 113 func (sp *Proof) ValidateBasic() error { 114 if sp.Total < 0 { 115 return errors.New("negative Total") 116 } 117 if sp.Index < 0 { 118 return errors.New("negative Index") 119 } 120 if len(sp.LeafHash) != tmhash.Size { 121 return fmt.Errorf("expected LeafHash size to be %d, got %d", tmhash.Size, len(sp.LeafHash)) 122 } 123 if len(sp.Aunts) > MaxAunts { 124 return fmt.Errorf("expected no more than %d aunts, got %d", MaxAunts, len(sp.Aunts)) 125 } 126 for i, auntHash := range sp.Aunts { 127 if len(auntHash) != tmhash.Size { 128 return fmt.Errorf("expected Aunts#%d size to be %d, got %d", i, tmhash.Size, len(auntHash)) 129 } 130 } 131 return nil 132 } 133 134 func (sp *Proof) ToProto() *cmtcrypto.Proof { 135 if sp == nil { 136 return nil 137 } 138 pb := new(cmtcrypto.Proof) 139 140 pb.Total = sp.Total 141 pb.Index = sp.Index 142 pb.LeafHash = sp.LeafHash 143 pb.Aunts = sp.Aunts 144 145 return pb 146 } 147 148 func ProofFromProto(pb *cmtcrypto.Proof) (*Proof, error) { 149 if pb == nil { 150 return nil, errors.New("nil proof") 151 } 152 153 sp := new(Proof) 154 155 sp.Total = pb.Total 156 sp.Index = pb.Index 157 sp.LeafHash = pb.LeafHash 158 sp.Aunts = pb.Aunts 159 160 return sp, sp.ValidateBasic() 161 } 162 163 // Use the leafHash and innerHashes to get the root merkle hash. 164 // If the length of the innerHashes slice isn't exactly correct, the result is nil. 165 // Recursive impl. 166 func computeHashFromAunts(index, total int64, leafHash []byte, innerHashes [][]byte) ([]byte, error) { 167 if index >= total || index < 0 || total <= 0 { 168 return nil, fmt.Errorf("invalid index %d and/or total %d", index, total) 169 } 170 switch total { 171 case 0: 172 panic("Cannot call computeHashFromAunts() with 0 total") 173 case 1: 174 if len(innerHashes) != 0 { 175 return nil, fmt.Errorf("unexpected inner hashes") 176 } 177 return leafHash, nil 178 default: 179 if len(innerHashes) == 0 { 180 return nil, fmt.Errorf("expected at least one inner hash") 181 } 182 numLeft := getSplitPoint(total) 183 if index < numLeft { 184 leftHash, err := computeHashFromAunts(index, numLeft, leafHash, innerHashes[:len(innerHashes)-1]) 185 if err != nil { 186 return nil, err 187 } 188 189 return innerHash(leftHash, innerHashes[len(innerHashes)-1]), nil 190 } 191 rightHash, err := computeHashFromAunts(index-numLeft, total-numLeft, leafHash, innerHashes[:len(innerHashes)-1]) 192 if err != nil { 193 return nil, err 194 } 195 return innerHash(innerHashes[len(innerHashes)-1], rightHash), nil 196 } 197 } 198 199 // ProofNode is a helper structure to construct merkle proof. 200 // The node and the tree is thrown away afterwards. 201 // Exactly one of node.Left and node.Right is nil, unless node is the root, in which case both are nil. 202 // node.Parent.Hash = hash(node.Hash, node.Right.Hash) or 203 // hash(node.Left.Hash, node.Hash), depending on whether node is a left/right child. 204 type ProofNode struct { 205 Hash []byte 206 Parent *ProofNode 207 Left *ProofNode // Left sibling (only one of Left,Right is set) 208 Right *ProofNode // Right sibling (only one of Left,Right is set) 209 } 210 211 // FlattenAunts will return the inner hashes for the item corresponding to the leaf, 212 // starting from a leaf ProofNode. 213 func (spn *ProofNode) FlattenAunts() [][]byte { 214 // Nonrecursive impl. 215 innerHashes := [][]byte{} 216 for spn != nil { 217 switch { 218 case spn.Left != nil: 219 innerHashes = append(innerHashes, spn.Left.Hash) 220 case spn.Right != nil: 221 innerHashes = append(innerHashes, spn.Right.Hash) 222 default: 223 break 224 } 225 spn = spn.Parent 226 } 227 return innerHashes 228 } 229 230 // trails[0].Hash is the leaf hash for items[0]. 231 // trails[i].Parent.Parent....Parent == root for all i. 232 func trailsFromByteSlices(items [][]byte) (trails []*ProofNode, root *ProofNode) { 233 // Recursive impl. 234 switch len(items) { 235 case 0: 236 return []*ProofNode{}, &ProofNode{emptyHash(), nil, nil, nil} 237 case 1: 238 trail := &ProofNode{leafHash(items[0]), nil, nil, nil} 239 return []*ProofNode{trail}, trail 240 default: 241 k := getSplitPoint(int64(len(items))) 242 lefts, leftRoot := trailsFromByteSlices(items[:k]) 243 rights, rightRoot := trailsFromByteSlices(items[k:]) 244 rootHash := innerHash(leftRoot.Hash, rightRoot.Hash) 245 root := &ProofNode{rootHash, nil, nil, nil} 246 leftRoot.Parent = root 247 leftRoot.Right = rightRoot 248 rightRoot.Parent = root 249 rightRoot.Left = leftRoot 250 return append(lefts, rights...), root 251 } 252 }