proof.go (3096B)
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 "bytes" 19 "errors" 20 "math/bits" 21 22 "certichain/internal/crypto" 23 ) 24 25 // Direction indicates where the sibling node 26 // is located relative to the current hash. 27 type Direction byte 28 29 const ( 30 DirLeft Direction = iota 31 DirRight 32 ) 33 34 // ProofStep represents a single step in the Merkle proof. 35 // 36 // SiblingHash is the hash paired with the current hash at 37 // this tree level. 38 // 39 // Direction indicates whether the sibling hash was on the 40 // left or right side during the parent hash computation. 41 type ProofStep struct { 42 SiblingHash crypto.Hash 43 Direction Direction 44 } 45 46 // Proof represents a Merkle inclusion proof. 47 // 48 // Steps contain the ordered proof path from leaf to the 49 // root. 50 // 51 // Index is the original leaf index the proof was generated 52 // for. 53 type Proof struct { 54 Steps []ProofStep 55 Index int 56 } 57 58 // GenerateProof method generates an inclusion proof for the 59 // leaf at the provided index. 60 func (t *Tree) GenerateProof(leafIndex int) (*Proof, error) { 61 if t == nil || t.root == nil { 62 return nil, errors.New("merkle: nil tree") 63 } 64 65 if leafIndex < 0 || leafIndex >= t.leafCount { 66 return nil, errors.New("merkle: leaf index out of bound") 67 } 68 69 current := t.leaves[leafIndex] 70 71 depth := bits.Len(uint(t.leafCount - 1)) 72 proof := &Proof{ 73 Index: leafIndex, 74 Steps: make([]ProofStep, 0, depth), 75 } 76 77 for current.parent != nil { 78 parent := current.parent 79 80 if current == parent.left { 81 proof.Steps = append(proof.Steps, ProofStep{ 82 SiblingHash: parent.right.hash, 83 Direction: DirRight, 84 }) 85 } else { 86 proof.Steps = append(proof.Steps, ProofStep{ 87 SiblingHash: parent.left.hash, 88 Direction: DirLeft, 89 }) 90 } 91 92 current = parent 93 } 94 95 return proof, nil 96 } 97 98 // Verify checks whether a leaf is included in a Merkle tree 99 // with the given root using an RFC 6962 inclusion proof. 100 func Verify(leafData []byte, proof *Proof, root []byte) (bool, error) { 101 if leafData == nil { 102 return false, errors.New("merkle: nil leaf data") 103 } 104 105 if proof == nil { 106 return false, errors.New("merkle: nil proof") 107 } 108 109 if len(root) != crypto.HashSize { 110 return false, errors.New("merkle: invalid root size") 111 } 112 113 current := hashLeaf(leafData) 114 115 for _, step := range proof.Steps { 116 switch step.Direction { 117 case DirLeft: 118 current = hashNode(step.SiblingHash, current) 119 120 case DirRight: 121 current = hashNode(current, step.SiblingHash) 122 123 default: 124 return false, errors.New("merkle: invalid proof direction") 125 } 126 } 127 128 return bytes.Equal(current[:], root), nil 129 }