This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
proof_range.go
497 lines (446 loc) · 14.5 KB
/
proof_range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package iavl
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"
)
type RangeProof struct {
// You don't need the right path because
// it can be derived from what we have.
LeftPath PathToLeaf `json:"left_path"`
InnerNodes []PathToLeaf `json:"inner_nodes"`
Leaves []proofLeafNode `json:"leaves"`
// memoize
rootVerified bool
rootHash []byte // valid iff rootVerified is true
treeEnd bool // valid iff rootVerified is true
}
// Keys returns all the keys in the RangeProof. NOTE: The keys here may
// include more keys than provided by tree.GetRangeWithProof or
// MutableTree.GetVersionedRangeWithProof. The keys returned there are only
// in the provided [startKey,endKey){limit} range. The keys returned here may
// include extra keys, such as:
// - the key before startKey if startKey is provided and doesn't exist;
// - the key after a queried key with tree.GetWithProof, when the key is absent.
func (proof *RangeProof) Keys() (keys [][]byte) {
if proof == nil {
return nil
}
for _, leaf := range proof.Leaves {
keys = append(keys, leaf.Key)
}
return keys
}
// String returns a string representation of the proof.
func (proof *RangeProof) String() string {
if proof == nil {
return "<nil-RangeProof>"
}
return proof.StringIndented("")
}
func (proof *RangeProof) StringIndented(indent string) string {
istrs := make([]string, 0, len(proof.InnerNodes))
for _, ptl := range proof.InnerNodes {
istrs = append(istrs, ptl.stringIndented(indent+" "))
}
lstrs := make([]string, 0, len(proof.Leaves))
for _, leaf := range proof.Leaves {
lstrs = append(lstrs, leaf.stringIndented(indent+" "))
}
return fmt.Sprintf(`RangeProof{
%s LeftPath: %v
%s InnerNodes:
%s %v
%s Leaves:
%s %v
%s (rootVerified): %v
%s (rootHash): %X
%s (treeEnd): %v
%s}`,
indent, proof.LeftPath.stringIndented(indent+" "),
indent,
indent, strings.Join(istrs, "\n"+indent+" "),
indent,
indent, strings.Join(lstrs, "\n"+indent+" "),
indent, proof.rootVerified,
indent, proof.rootHash,
indent, proof.treeEnd,
indent)
}
// The index of the first leaf (of the whole tree).
// Returns -1 if the proof is nil.
func (proof *RangeProof) LeftIndex() int64 {
if proof == nil {
return -1
}
return proof.LeftPath.Index()
}
// Also see LeftIndex().
// Verify that a key has some value.
// Does not assume that the proof itself is valid, call Verify() first.
func (proof *RangeProof) VerifyItem(key, value []byte) error {
leaves := proof.Leaves
if proof == nil {
return cmn.ErrorWrap(ErrInvalidProof, "proof is nil")
}
if !proof.rootVerified {
return cmn.NewError("must call Verify(root) first.")
}
i := sort.Search(len(leaves), func(i int) bool {
return bytes.Compare(key, leaves[i].Key) <= 0
})
if i >= len(leaves) || !bytes.Equal(leaves[i].Key, key) {
return cmn.ErrorWrap(ErrInvalidProof, "leaf key not found in proof")
}
valueHash := tmhash.Sum(value)
if !bytes.Equal(leaves[i].ValueHash, valueHash) {
return cmn.ErrorWrap(ErrInvalidProof, "leaf value hash not same")
}
return nil
}
// Verify that proof is valid absence proof for key.
// Does not assume that the proof itself is valid.
// For that, use Verify(root).
func (proof *RangeProof) VerifyAbsence(key []byte) error {
if proof == nil {
return cmn.ErrorWrap(ErrInvalidProof, "proof is nil")
}
if !proof.rootVerified {
return cmn.NewError("must call Verify(root) first.")
}
cmp := bytes.Compare(key, proof.Leaves[0].Key)
if cmp < 0 {
if proof.LeftPath.isLeftmost() {
return nil
} else {
return cmn.NewError("absence not proved by left path")
}
} else if cmp == 0 {
return cmn.NewError("absence disproved via first item #0")
}
if len(proof.LeftPath) == 0 {
return nil // proof ok
}
if proof.LeftPath.isRightmost() {
return nil
}
// See if any of the leaves are greater than key.
for i := 1; i < len(proof.Leaves); i++ {
leaf := proof.Leaves[i]
cmp := bytes.Compare(key, leaf.Key)
if cmp < 0 {
return nil // proof ok
} else if cmp == 0 {
return cmn.NewError("absence disproved via item #%v", i)
} else {
if i == len(proof.Leaves)-1 {
// If last item, check whether
// it's the last item in teh tree.
}
continue
}
}
// It's still a valid proof if our last leaf is the rightmost child.
if proof.treeEnd {
return nil // OK!
}
// It's not a valid absence proof.
if len(proof.Leaves) < 2 {
return cmn.NewError("absence not proved by right leaf (need another leaf?)")
} else {
return cmn.NewError("absence not proved by right leaf")
}
}
// Verify that proof is valid.
func (proof *RangeProof) Verify(root []byte) error {
if proof == nil {
return cmn.ErrorWrap(ErrInvalidProof, "proof is nil")
}
err := proof.verify(root)
return err
}
func (proof *RangeProof) verify(root []byte) (err error) {
rootHash := proof.rootHash
if rootHash == nil {
derivedHash, err := proof.computeRootHash()
if err != nil {
return err
}
rootHash = derivedHash
}
if !bytes.Equal(rootHash, root) {
return cmn.ErrorWrap(ErrInvalidRoot, "root hash doesn't match")
} else {
proof.rootVerified = true
}
return nil
}
// ComputeRootHash computes the root hash with leaves.
// Returns nil if error or proof is nil.
// Does not verify the root hash.
func (proof *RangeProof) ComputeRootHash() []byte {
if proof == nil {
return nil
}
rootHash, _ := proof.computeRootHash()
return rootHash
}
func (proof *RangeProof) computeRootHash() (rootHash []byte, err error) {
rootHash, treeEnd, err := proof._computeRootHash()
if err == nil {
proof.rootHash = rootHash // memoize
proof.treeEnd = treeEnd // memoize
}
return rootHash, err
}
func (proof *RangeProof) _computeRootHash() (rootHash []byte, treeEnd bool, err error) {
if len(proof.Leaves) == 0 {
return nil, false, cmn.ErrorWrap(ErrInvalidProof, "no leaves")
}
if len(proof.InnerNodes)+1 != len(proof.Leaves) {
return nil, false, cmn.ErrorWrap(ErrInvalidProof, "InnerNodes vs Leaves length mismatch, leaves should be 1 more.")
}
// Start from the left path and prove each leaf.
// shared across recursive calls
var leaves = proof.Leaves
var innersq = proof.InnerNodes
var COMPUTEHASH func(path PathToLeaf, rightmost bool) (hash []byte, treeEnd bool, done bool, err error)
// rightmost: is the root a rightmost child of the tree?
// treeEnd: true iff the last leaf is the last item of the tree.
// Returns the (possibly intermediate, possibly root) hash.
COMPUTEHASH = func(path PathToLeaf, rightmost bool) (hash []byte, treeEnd bool, done bool, err error) {
// Pop next leaf.
nleaf, rleaves := leaves[0], leaves[1:]
leaves = rleaves
// Compute hash.
hash = (pathWithLeaf{
Path: path,
Leaf: nleaf,
}).computeRootHash()
// If we don't have any leaves left, we're done.
if len(leaves) == 0 {
rightmost = rightmost && path.isRightmost()
return hash, rightmost, true, nil
}
// Prove along path (until we run out of leaves).
for len(path) > 0 {
// Drop the leaf-most (last-most) inner nodes from path
// until we encounter one with a left hash.
// We assume that the left side is already verified.
// rpath: rest of path
// lpath: last path item
rpath, lpath := path[:len(path)-1], path[len(path)-1]
path = rpath
if len(lpath.Right) == 0 {
continue
}
// Pop next inners, a PathToLeaf (e.g. []proofInnerNode).
inners, rinnersq := innersq[0], innersq[1:]
innersq = rinnersq
// Recursively verify inners against remaining leaves.
derivedRoot, treeEnd, done, err := COMPUTEHASH(inners, rightmost && rpath.isRightmost())
if err != nil {
return nil, treeEnd, false, cmn.ErrorWrap(err, "recursive COMPUTEHASH call")
}
if !bytes.Equal(derivedRoot, lpath.Right) {
return nil, treeEnd, false, cmn.ErrorWrap(ErrInvalidRoot, "intermediate root hash %X doesn't match, got %X", lpath.Right, derivedRoot)
}
if done {
return hash, treeEnd, true, nil
}
}
// We're not done yet (leaves left over). No error, not done either.
// Technically if rightmost, we know there's an error "left over leaves
// -- malformed proof", but we return that at the top level, below.
return hash, false, false, nil
}
// Verify!
path := proof.LeftPath
rootHash, treeEnd, done, err := COMPUTEHASH(path, true)
if err != nil {
return nil, treeEnd, cmn.ErrorWrap(err, "root COMPUTEHASH call")
} else if !done {
return nil, treeEnd, cmn.ErrorWrap(ErrInvalidProof, "left over leaves -- malformed proof")
}
// Ok!
return rootHash, treeEnd, nil
}
///////////////////////////////////////////////////////////////////////////////
// keyStart is inclusive and keyEnd is exclusive.
// If keyStart or keyEnd don't exist, the leaf before keyStart
// or after keyEnd will also be included, but not be included in values.
// If keyEnd-1 exists, no later leaves will be included.
// If keyStart >= keyEnd and both not nil, panics.
// Limit is never exceeded.
func (t *ImmutableTree) getRangeProof(keyStart, keyEnd []byte, limit int) (proof *RangeProof, keys, values [][]byte, err error) {
if keyStart != nil && keyEnd != nil && bytes.Compare(keyStart, keyEnd) >= 0 {
panic("if keyStart and keyEnd are present, need keyStart < keyEnd.")
}
if limit < 0 {
panic("limit must be greater or equal to 0 -- 0 means no limit")
}
root := t.getRoot()
if root == nil {
return nil, nil, nil, nil
}
root.hashWithCount() // Ensure that all hashes are calculated.
// Get the first key/value pair proof, which provides us with the left key.
path, left, err := root.PathToLeaf(t, keyStart)
if err != nil {
// Key doesn't exist, but instead we got the prev leaf (or the
// first or last leaf), which provides proof of absence).
err = nil
}
startOK := keyStart == nil || bytes.Compare(keyStart, left.key) <= 0
endOK := keyEnd == nil || bytes.Compare(left.key, keyEnd) < 0
// If left.key is in range, add it to key/values.
if startOK && endOK {
keys = append(keys, left.key) // == keyStart
values = append(values, left.value)
}
// Either way, add to proof leaves.
var leaves = []proofLeafNode{proofLeafNode{
Key: left.key,
ValueHash: tmhash.Sum(left.value),
Version: left.version,
}}
// 1: Special case if limit is 1.
// 2: Special case if keyEnd is left.key+1.
_stop := false
if limit == 1 {
_stop = true // case 1
} else if keyEnd != nil && bytes.Compare(cpIncr(left.key), keyEnd) >= 0 {
_stop = true // case 2
}
if _stop {
return &RangeProof{
LeftPath: path,
Leaves: leaves,
}, keys, values, nil
}
// Get the key after left.key to iterate from.
afterLeft := cpIncr(left.key)
// Traverse starting from afterLeft, until keyEnd or the next leaf
// after keyEnd.
var innersq = []PathToLeaf(nil)
var inners = PathToLeaf(nil)
var leafCount = 1 // from left above.
var pathCount = 0
// var keys, values [][]byte defined as function outs.
root.traverseInRange(t, afterLeft, nil, true, false, 0,
func(node *Node, depth uint8) (stop bool) {
// Track when we diverge from path, or when we've exhausted path,
// since the first innersq shouldn't include it.
if pathCount != -1 {
if len(path) <= pathCount {
// We're done with path counting.
pathCount = -1
} else {
pn := path[pathCount]
if pn.Height != node.height ||
pn.Left != nil && !bytes.Equal(pn.Left, node.leftHash) ||
pn.Right != nil && !bytes.Equal(pn.Right, node.rightHash) {
// We've diverged, so start appending to inners.
pathCount = -1
} else {
pathCount += 1
}
}
}
if node.height == 0 {
// Leaf node.
// Append inners to innersq.
innersq = append(innersq, inners)
inners = PathToLeaf(nil)
// Append leaf to leaves.
leaves = append(leaves, proofLeafNode{
Key: node.key,
ValueHash: tmhash.Sum(node.value),
Version: node.version,
})
leafCount += 1
// Maybe terminate because we found enough leaves.
if limit > 0 && limit <= leafCount {
return true
}
// Terminate if we've found keyEnd or after.
if keyEnd != nil && bytes.Compare(node.key, keyEnd) >= 0 {
return true
}
// Value is in range, append to keys and values.
keys = append(keys, node.key)
values = append(values, node.value)
// Terminate if we've found keyEnd-1 or after.
// We don't want to fetch any leaves for it.
if keyEnd != nil && bytes.Compare(cpIncr(node.key), keyEnd) >= 0 {
return true
}
} else {
// Inner node.
if pathCount >= 0 {
// Skip redundant path items.
} else {
inners = append(inners, proofInnerNode{
Height: node.height,
Size: node.size,
Version: node.version,
Left: nil, // left is nil for range proof inners
Right: node.rightHash,
})
}
}
return false
},
)
return &RangeProof{
LeftPath: path,
InnerNodes: innersq,
Leaves: leaves,
}, keys, values, nil
}
//----------------------------------------
// GetWithProof gets the value under the key if it exists, or returns nil.
// A proof of existence or absence is returned alongside the value.
func (t *ImmutableTree) GetWithProof(key []byte) (value []byte, proof *RangeProof, err error) {
proof, _, values, err := t.getRangeProof(key, cpIncr(key), 2)
if err != nil {
return nil, nil, cmn.ErrorWrap(err, "constructing range proof")
}
if len(values) > 0 && bytes.Equal(proof.Leaves[0].Key, key) {
return values[0], proof, nil
}
return nil, proof, nil
}
// GetRangeWithProof gets key/value pairs within the specified range and limit.
func (t *ImmutableTree) GetRangeWithProof(startKey []byte, endKey []byte, limit int) (keys, values [][]byte, proof *RangeProof, err error) {
proof, keys, values, err = t.getRangeProof(startKey, endKey, limit)
return
}
// GetVersionedWithProof gets the value under the key at the specified version
// if it exists, or returns nil.
func (tree *MutableTree) GetVersionedWithProof(key []byte, version int64) ([]byte, *RangeProof, error) {
if tree.versions[version] {
t, err := tree.GetImmutable(version)
if err != nil {
return nil, nil, err
}
return t.GetWithProof(key)
}
return nil, nil, cmn.ErrorWrap(ErrVersionDoesNotExist, "")
}
// GetVersionedRangeWithProof gets key/value pairs within the specified range
// and limit.
func (tree *MutableTree) GetVersionedRangeWithProof(startKey, endKey []byte, limit int, version int64) (
keys, values [][]byte, proof *RangeProof, err error) {
if tree.versions[version] {
t, err := tree.GetImmutable(version)
if err != nil {
return nil, nil, nil, err
}
return t.GetRangeWithProof(startKey, endKey, limit)
}
return nil, nil, nil, cmn.ErrorWrap(ErrVersionDoesNotExist, "")
}