@@ -193,9 +193,17 @@ func rootFromSubtreeConsistencyProof(hasher merkle.LogHasher, start, end, size u
193193 //
194194 // Split the proof in two, where paths to leaves |end-1| and |size-1| diverge.
195195 forkLevel := bits .Len64 ((end - 1 ) ^ (size - 1 ))
196+ // Height of the rightmost full subtree within the argument subtree.
197+ // The proof does not contain any node below this level.
196198 shift := bits .TrailingZeros64 (end - start )
197199
200+ // The first node of the proof is the root of the rightmost subtree within
201+ // the argument subtree.
198202 seed , pStart := proof [0 ], 1
203+ // Unless the argument subtree is full, in which case that rightmost subtree
204+ // is the argument subtree itself. Its root is not included in the proof
205+ // since a client verifying a subtree inclusion proof is expected to already
206+ // know what the root of that subtree is.
199207 if (end - start ) == 1 << uint (shift ) {
200208 seed , pStart = root1 , 0
201209 }
@@ -205,15 +213,24 @@ func rootFromSubtreeConsistencyProof(hasher merkle.LogHasher, start, end, size u
205213 }
206214 proof = proof [pStart :]
207215
216+ // Compute the root of the [start, end) subtree for trees of sizes
217+ // |end| and |size|.
208218 subtreeRoot , grownSubtreeRoot , remainingProof := chainSubtree (hasher , seed , proof , start , end , size )
209219 if err := verifyMatch (subtreeRoot , root1 ); err != nil {
210220 return nil , err
211221 }
212222
213- h := bits .Len64 ((end - 1 ) ^ start )
214- macroIndex := start >> uint (h )
215- macroSize := ((size - 1 ) >> uint (h )) + 1
216- return RootFromInclusionProof (hasher , macroIndex , macroSize , grownSubtreeRoot , remainingProof )
223+ // The remainder of the proof is an inclusion proof for grownSubtreeRoot
224+ // into the parent tree of size |size|.
225+ // Shift the tree down for that node to be a leaf.
226+ // xor trims the common prefix between the first and last entry. The bit len
227+ // of the result is the height of the subtree.
228+ srHeight := bits .Len64 ((end - 1 ) ^ start )
229+ // shifting indexes to the srHeight times gives the size of the tree at level
230+ // srHeight.
231+ srIndex := start >> uint (srHeight )
232+ macroSize := ((size - 1 ) >> uint (srHeight )) + 1
233+ return RootFromInclusionProof (hasher , srIndex , macroSize , grownSubtreeRoot , remainingProof )
217234}
218235
219236// chainSubtree hashes nodes from proof up to subtree [start, end)'s' root
0 commit comments