Skip to content

Commit 9d48ea5

Browse files
committed
return slices and typos
1 parent 3dd4ed1 commit 9d48ea5

1 file changed

Lines changed: 33 additions & 34 deletions

File tree

proof/verify.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ func RootFromSubtreeConsistencyProof(hasher merkle.LogHasher, start, end, size u
168168
}
169169

170170
func rootFromSubtreeConsistencyProof(hasher merkle.LogHasher, start, end, size uint64, proof [][]byte, subRoot []byte) ([]byte, error) {
171-
// If the right end of the subtree overlaps with the right end of the parent
172-
// tree, the proof allows reconstructing the tree root directly from the
173-
// argument subtree root |subRoot|.
171+
// If the right end of the subtree is also the right end of the parent tree,
172+
// the proof allows reconstructing the tree root directly from the argument
173+
// subtree root |subRoot|.
174174
if end == size {
175175
// Find the root of the subtree.
176176
// xor trims the common prefix between the first and last entry. The bit len
@@ -187,27 +187,27 @@ func rootFromSubtreeConsistencyProof(hasher merkle.LogHasher, start, end, size u
187187
}
188188

189189
// Otherwise, we need to:
190-
// - Verify that nodes in the proof that belongs to the subtree are consistent
190+
// - Verify that nodes in the proof that belong to the subtree are consistent
191191
// with the argument subtree root |subRoot|.
192-
// - Reconstruct the parent tree root from the the argument subtree root
192+
// - Reconstruct the parent tree root from the argument subtree root
193193
// grown into a subtree of the parent tree of size |size|.
194194
//
195195
// Split the proof in two, where paths to leaves |end-1| and |size-1| diverge.
196196
forkLevel, border := decompInclProof(end-1, size)
197-
// Height of the rightmost full subtree within the argument subtree.
197+
// Height of the rightmost perfect subtree within the argument subtree.
198198
// The proof starts at this level.
199199
shift := bits.TrailingZeros64(end - start)
200-
// Number of level between the root of that subtree and the end the the first
200+
// Number of levels between the root of that subtree and the end of the first
201201
// part of the proof.
202202
inner := forkLevel - shift // Note: shift < inner if end < size.
203203

204204
// The first node of the proof is the root of the rightmost subtree within
205205
// the argument subtree.
206206
seed, pStart := proof[0], 1
207-
// Unless the argument subtree is full, in which case that rightmost subtree
208-
// is the argument subtree itself. Its root is not included in the proof
209-
// since a client verifying a subtree inclusion proof is expected to already
210-
// know what the root of that subtree is.
207+
// Unless the argument subtree is perfect, in which case that rightmost
208+
// subtree is the argument subtree itself. Its root is not included in the
209+
// proof since a client verifying a subtree inclusion proof is expected to
210+
// already know what the root of that subtree is.
211211
if (end - start) == 1<<uint(shift) {
212212
seed, pStart = subRoot, 0
213213
}
@@ -220,13 +220,13 @@ func rootFromSubtreeConsistencyProof(hasher merkle.LogHasher, start, end, size u
220220

221221
mask := (end - 1) >> uint(shift) // Start chaining from level |shift|.
222222

223-
// If the argument subtree is not full, the proof includes somes nodes that
223+
// If the argument subtree is not perfect, the proof includes some nodes that
224224
// belong to the subtree. We must verify that these nodes chain correctly to
225225
// the argument subtree root.
226226
if pStart == 1 {
227-
subInner, innerIdx, borderEnd := decompSubtreeProof(start, end, size, border)
228-
hash1 := chainInnerRight(hasher, seed, proof[:subInner], mask)
229-
hash1 = chainBorderRight(hasher, hash1, proof[innerIdx:borderEnd])
227+
subInner, subBorder := decompSubtreeProof(start, end, size, border, proof)
228+
hash1 := chainInnerRight(hasher, seed, subInner, mask)
229+
hash1 = chainBorderRight(hasher, hash1, subBorder)
230230
if err := verifyMatch(hash1, subRoot); err != nil {
231231
return nil, err
232232
}
@@ -254,44 +254,43 @@ func decompInclProof(index, size uint64) (int, int) {
254254
}
255255

256256
// decompSubtreeProof computes the exact proof slice indices needed to
257-
// reconstruct the [start, end) subtree root
258-
// - subInner: end index of inner proof hashes inside the subtree
259-
// (proof[:subInner]), regardless of the size of the parent tree.
260-
// These nodes can be left or right siblings depending on the level at
261-
// which they are used in the proof. Only left siblings are required to
262-
// reconstruct the subtree root.
263-
// - inner: end index of inner proof hashes inside the tree, and potentially
264-
// outside of the subtree. It is also the start index of border proof
265-
// hashes (proof[inner:]). Border proof hashes are left siblings only.
266-
// There's one per level and they can be inside or outside of the subtree.
267-
// - inner+subBorder: end index of border proof hashes inside the subtree.
268-
// (proof[inner:subBorderEnd]). These nodes are left siblings only.
269-
func decompSubtreeProof(start, end, size uint64, border int) (subInner int, inner int, subBorderEnd int) {
257+
// reconstruct the [start, end) subtree root.
258+
// - proof[0:subInner]: inner proof hashes under the subtree root node
259+
// in a tree of size |end| or |size|. These nodes can be inside or outside
260+
// of the subtree, and left or right siblings depending on the level at
261+
// which they are used in the proof. Only left siblings (except possibly
262+
// the first node) are required to reconstruct the subtree root hash, and
263+
// right siblings can be ignored.
264+
// - proof[inner:inner+subBorder]: border proof hashes, inside the subtree.
265+
// These nodes are left siblings only, and there's one hash per level.
266+
func decompSubtreeProof(start, end, size uint64, border int, proof [][]byte) (subInnerProof [][]byte, subBorderProof [][]byte) {
270267
// xor trims the common prefix between the first and last entry. The bit len
271268
// of the result is the height of the subtree.
272269
h := bits.Len64((end - 1) ^ start)
273270
// Using a similar technique, we compute where paths to leaves |end-1| and
274271
// |size-1| diverge.
275272
forkLevel := bits.Len64((end - 1) ^ (size - 1))
276-
// Height of the rightmost full subtree within the argument subtree.
273+
// Height of the rightmost perfect subtree within the argument subtree.
277274
// The proof starts at this level.
278275
shift := bits.TrailingZeros64(end - start)
279276

280277
// Number of inner proof nodes from level |shift| up to subtree height |h|
281278
// (clamped at |forkLevel|) that belong inside the [start, end) subtree.
282279
// We take the minimum between h and forkLevel to make sure that subInner
283-
// isn't higher than the subtree root. Substract shift since the proof
280+
// isn't higher than the subtree root. Subtract shift since the proof
284281
// starts at this level.
285-
subInner = min(h, forkLevel) - shift
282+
subInner := min(h, forkLevel) - shift
286283
// Total number of inner proof nodes between level |shift| and |forkLevel|.
287-
inner = forkLevel - shift
284+
// It delimits the end of the inner proof nodes, and the beginning of the
285+
// border nodes.
286+
inner := forkLevel - shift
288287
// Number of border proof nodes above |forkLevel| and below subtree height |h|
289288
// that belong inside the [start, end) subtree. Use max as the border proof
290-
// might not include any node that belong to the subtree.
289+
// might not include any nodes that belong to the subtree.
291290
subBorder := max(0, border-bits.OnesCount64((end-1)>>uint(h)))
292291
// The proof slice indices needed to reconstruct hash1 are [0:subInner] for
293292
// inner-right chaining and [inner:inner+subBorder] for border chaining.
294-
return subInner, inner, inner + subBorder
293+
return proof[0:subInner], proof[inner : inner+subBorder]
295294
}
296295

297296
func innerProofSize(index, size uint64) int {

0 commit comments

Comments
 (0)