forked from ethereum/go-verkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
1201 lines (1039 loc) · 31.1 KB
/
tree.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <https://unlicense.org>
package verkle
import (
"bytes"
"errors"
"fmt"
"github.com/crate-crypto/go-ipa/banderwagon"
)
type (
NodeFlushFn func(VerkleNode)
NodeResolverFn func([]byte) ([]byte, error)
)
// Committer represents an object that is able to create the
// commitment to a polynomial.
type Committer interface {
CommitToPoly([]Fr, int) *Point
}
type keylist [][]byte
func (kl keylist) Len() int {
return len(kl)
}
func (kl keylist) Less(i, j int) bool {
return bytes.Compare(kl[i], kl[j]) == -1
}
func (kl keylist) Swap(i, j int) {
kl[i], kl[j] = kl[j], kl[i]
}
type VerkleNode interface {
// Insert or Update value into the tree
Insert([]byte, []byte, NodeResolverFn) error
// Insert "à la" Stacktrie. Same thing as insert, except that
// values are expected to be ordered, and the commitments and
// hashes for each subtrie are computed online, as soon as it
// is clear that no more values will be inserted in there.
InsertOrdered([]byte, []byte, NodeFlushFn) error
// Delete a leaf with the given key
Delete([]byte, NodeResolverFn) error
// Get value at a given key
Get([]byte, NodeResolverFn) ([]byte, error)
// Commit computes the commitment of the node. The
// result (the curve point) is cached.
Commit() *Point
// Commitment is a getter for the cached commitment
// to this node.
Commitment() *Point
// Hash returns the field representation of the commitment.
Hash() *Fr
// GetProofItems collects the various proof elements, and
// returns them breadth-first. On top of that, it returns
// one "extension status" per stem, and an alternate stem
// if the key is missing but another stem has been found.
GetProofItems(keylist) (*ProofElements, []byte, [][]byte)
// Serialize encodes the node to RLP.
Serialize() ([]byte, error)
// Copy a node and its children
Copy() VerkleNode
// toDot returns a string representing this subtree in DOT language
toDot(string, string) string
setDepth(depth byte)
}
// ProofElements gathers the elements needed to build a proof.
type ProofElements struct {
Cis []*Point
Zis []byte
Yis []*Fr
Fis [][]Fr
ByPath map[string]*Point // Gather commitments by path
// dedups flags the presence of each (Ci,zi) tuple
dedups map[*Point]map[byte]struct{}
}
// Merge merges the elements of two proofs and removes duplicates.
func (pe *ProofElements) Merge(other *ProofElements) {
// Build the local map if it's missing
if pe.dedups == nil {
pe.dedups = make(map[*Point]map[byte]struct{})
for i, ci := range pe.Cis {
if _, ok := pe.dedups[ci]; !ok {
pe.dedups[ci] = make(map[byte]struct{})
}
pe.dedups[ci][pe.Zis[i]] = struct{}{}
}
}
for i, ci := range other.Cis {
if _, ok := pe.dedups[ci]; !ok {
// First time this commitment has been seen, create
// the map and flatten the zi.
pe.dedups[ci] = make(map[byte]struct{})
}
if _, ok := pe.dedups[ci][other.Zis[i]]; ok {
// duplicate, skip
continue
}
pe.dedups[ci][other.Zis[i]] = struct{}{}
pe.Cis = append(pe.Cis, ci)
pe.Zis = append(pe.Zis, other.Zis[i])
pe.Yis = append(pe.Yis, other.Yis[i])
if pe.Fis != nil {
pe.Fis = append(pe.Fis, other.Fis[i])
}
}
for path, C := range other.ByPath {
if _, ok := pe.ByPath[path]; !ok {
pe.ByPath[path] = C
}
}
}
const (
// These types will distinguish internal
// and leaf nodes when decoding from RLP.
internalRLPType byte = 1
leafRLPType byte = 2
)
type (
// Represents an internal node at any level
InternalNode struct {
// List of child nodes of this internal node.
children []VerkleNode
// node depth in the tree, in bits
depth byte
// Cache the commitment value
commitment *Point
cow map[byte]*Point
}
LeafNode struct {
stem []byte
values [][]byte
commitment *Point
c1, c2 *Point
depth byte
}
)
func newInternalNode(depth byte) VerkleNode {
node := new(InternalNode)
node.children = make([]VerkleNode, NodeWidth)
for idx := range node.children {
node.children[idx] = Empty(struct{}{})
}
node.depth = depth
node.commitment = new(Point).Identity()
return node
}
// New creates a new tree root
func New() VerkleNode {
return newInternalNode(0)
}
// New creates a new leaf node
func NewLeafNode(stem []byte, values [][]byte) *LeafNode {
leaf := &LeafNode{
// depth will be 0, but the commitment calculation
// does not need it, and so it won't be free.
values: values,
stem: stem[:31], // enforce a 31-byte length
c1: Generator(),
c2: Generator(),
}
// Initialize the commitment with the extension tree
// marker and the stem.
cfg := GetConfig()
count := 0
var poly, c1poly, c2poly [256]Fr
poly[0].SetUint64(1)
StemFromBytes(&poly[1], leaf.stem)
count = fillSuffixTreePoly(c1poly[:], values[:128])
leaf.c1 = cfg.CommitToPoly(c1poly[:], 256-count)
toFr(&poly[2], leaf.c1)
count = fillSuffixTreePoly(c2poly[:], values[128:])
leaf.c2 = cfg.CommitToPoly(c2poly[:], 256-count)
toFr(&poly[3], leaf.c2)
leaf.commitment = cfg.CommitToPoly(poly[:], 252)
return leaf
}
func (n *InternalNode) Children() []VerkleNode {
return n.children
}
func (n *InternalNode) SetChild(i int, c VerkleNode) error {
if i >= NodeWidth-1 {
return errors.New("child index higher than node width")
}
n.children[i] = c
return nil
}
func (n *InternalNode) cowChild(index byte) {
if n.cow == nil {
n.cow = make(map[byte]*Point)
}
if n.cow[index] == nil {
n.cow[index] = new(Point)
CopyPoint(n.cow[index], n.children[index].Commitment())
}
}
func (n *InternalNode) Insert(key []byte, value []byte, resolver NodeResolverFn) error {
values := make([][]byte, NodeWidth)
values[key[31]] = value
return n.InsertStem(key[:31], values, resolver)
}
func (n *InternalNode) InsertStem(stem []byte, values [][]byte, resolver NodeResolverFn) error {
nChild := offset2key(stem, n.depth) // index of the child pointed by the next byte in the key
n.cowChild(nChild)
switch child := n.children[nChild].(type) {
case Empty:
n.children[nChild] = NewLeafNode(stem, values)
n.children[nChild].setDepth(n.depth + 1)
case *HashedNode:
if resolver == nil {
return errInsertIntoHash
}
hash := child.commitment
serialized, err := resolver(hash)
if err != nil {
return fmt.Errorf("verkle tree: error resolving node %x at depth %d: %w", stem, n.depth, err)
}
resolved, err := ParseNode(serialized, n.depth+1, hash)
if err != nil {
return fmt.Errorf("verkle tree: error parsing resolved node %x: %w", stem, err)
}
n.children[nChild] = resolved
// recurse to handle the case of a LeafNode child that
// splits.
return n.InsertStem(stem, values, resolver)
case *LeafNode:
if equalPaths(child.stem, stem) {
return child.insertMultiple(stem, values)
}
// A new branch node has to be inserted. Depending
// on the next word in both keys, a recursion into
// the moved leaf node can occur.
nextWordInExistingKey := offset2key(child.stem, n.depth+1)
newBranch := newInternalNode(n.depth + 1).(*InternalNode)
newBranch.cowChild(nextWordInExistingKey)
n.children[nChild] = newBranch
newBranch.children[nextWordInExistingKey] = child
child.depth += 1
nextWordInInsertedKey := offset2key(stem, n.depth+1)
if nextWordInInsertedKey == nextWordInExistingKey {
return newBranch.InsertStem(stem, values, resolver)
}
// Next word differs, so this was the last level.
// Insert it directly into its final slot.
leaf := NewLeafNode(stem, values)
leaf.setDepth(n.depth + 2)
newBranch.cowChild(nextWordInInsertedKey)
newBranch.children[nextWordInInsertedKey] = leaf
case *InternalNode:
return child.InsertStem(stem, values, resolver)
default: // StatelessNode
return errStatelessAndStatefulMix
}
return nil
}
func (n *InternalNode) toHashedNode() *HashedNode {
if n.commitment == nil {
panic("nil commitment")
}
comm := n.commitment.Bytes()
return &HashedNode{commitment: comm[:]}
}
func (n *InternalNode) InsertOrdered(key []byte, value []byte, flush NodeFlushFn) error {
values := make([][]byte, NodeWidth)
values[key[31]] = value
return n.InsertStemOrdered(key[:31], values, flush)
}
// InsertStemOrdered does the same thing as InsertOrdered but is meant to insert a pre-build
// LeafNode at a given stem, instead of individual leaves.
func (n *InternalNode) InsertStemOrdered(key []byte, values [][]byte, flush NodeFlushFn) error {
nChild := offset2key(key, n.depth)
n.cowChild(nChild)
switch child := n.children[nChild].(type) {
case Empty:
// Insert into a new subtrie, which means that the
// subtree directly preceding this new one, can
// safely be flushed.
searchFirstNonEmptyChild:
for i := int(nChild) - 1; i >= 0; i-- {
switch child := n.children[i].(type) {
case Empty:
continue
case *LeafNode:
child.Commit()
if flush != nil {
flush(child)
}
n.children[i] = child.ToHashedNode()
break searchFirstNonEmptyChild
case *HashedNode:
break searchFirstNonEmptyChild
case *InternalNode:
n.children[i].Commit()
if flush != nil {
child.Flush(flush)
}
n.children[i] = child.toHashedNode()
break searchFirstNonEmptyChild
}
}
// NOTE: these allocations are inducing a noticeable slowdown
lastNode := NewLeafNode(key[:31], values)
lastNode.setDepth(n.depth + 1)
n.children[nChild] = lastNode
// If the node was already created, then there was at least one
// child. As a result, inserting this new leaf means there are
// now more than one child in this node.
case *HashedNode:
return errInsertIntoHash
case *LeafNode:
// Need to add a new branch node to differentiate
// between two keys, if the keys are different.
// Otherwise, just update the key.
if equalPaths(child.stem, key) {
// TODO when LeafNode no longer updates on insert,
// just set the values here.
child.updateMultipleLeaves(values)
} else {
// A new branch node has to be inserted. Depending
// on the next word in both keys, a recursion into
// the moved leaf node can occur.
nextWordInExistingKey := offset2key(child.stem, n.depth+1)
newBranch := newInternalNode(n.depth + 1).(*InternalNode)
newBranch.cowChild(nextWordInExistingKey)
n.children[nChild] = newBranch
nextWordInInsertedKey := offset2key(key, n.depth+1)
if nextWordInInsertedKey != nextWordInExistingKey {
// Directly hash the (left) node that was already
// inserted. In case the commitment update should
// not be updated, the left node's commitment has
// to be calculated anyways, in order to flush it
// to disk.
child.Commit()
if flush != nil {
flush(child)
}
newBranch.children[nextWordInExistingKey] = child.ToHashedNode()
// Next word differs, so this was the last level.
// Insert it directly into its final slot.
lastNode := NewLeafNode(key[:31], values)
lastNode.setDepth(n.depth + 1)
newBranch.cowChild(nextWordInInsertedKey)
newBranch.children[nextWordInInsertedKey] = lastNode
} else {
// Reinsert the leaf in order to recurse
newBranch.children[nextWordInExistingKey] = child
return newBranch.InsertStemOrdered(key, values, flush)
}
}
case *InternalNode: // InternalNode
return child.InsertStemOrdered(key, values, flush)
default: // StatelessNode
return errStatelessAndStatefulMix
}
return nil
}
func (n *InternalNode) Delete(key []byte, resolver NodeResolverFn) error {
nChild := offset2key(key, n.depth)
switch child := n.children[nChild].(type) {
case Empty:
return errDeleteNonExistent
case *HashedNode:
if resolver == nil {
return errDeleteHash
}
comm := child.commitment
payload, err := resolver(comm)
if err != nil {
return err
}
// deserialize the payload and set it as the child
c, err := ParseNode(payload, n.depth+1, comm)
if err != nil {
return err
}
n.children[nChild] = c
return n.Delete(key, resolver)
default:
n.cowChild(nChild)
return child.Delete(key, resolver)
}
}
// Flush hashes the children of an internal node and replaces them
// with HashedNode. It also sends the current node on the flush channel.
func (n *InternalNode) Flush(flush NodeFlushFn) {
n.Commit()
for i, child := range n.children {
if c, ok := child.(*InternalNode); ok {
c.Commit()
c.Flush(flush)
n.children[i] = c.toHashedNode()
} else if c, ok := child.(*LeafNode); ok {
c.Commit()
flush(n.children[i])
n.children[i] = c.ToHashedNode()
}
}
flush(n)
}
// FlushAtDepth goes over all internal nodes of a given depth, and
// flushes them to disk. Its purpose it to free up space if memory
// is running scarce.
func (n *InternalNode) FlushAtDepth(depth uint8, flush NodeFlushFn) {
for i, child := range n.children {
// Skip non-internal nodes
c, ok := child.(*InternalNode)
if !ok {
continue
}
// Not deep enough, recurse
if n.depth < depth {
c.FlushAtDepth(depth, flush)
continue
}
child.Commit()
c.Flush(flush)
n.children[i] = c.toHashedNode()
}
}
func (n *InternalNode) Get(k []byte, getter NodeResolverFn) ([]byte, error) {
nChild := offset2key(k, n.depth)
switch child := n.children[nChild].(type) {
case Empty, nil:
// Return nil as a signal that the value isn't
// present in the tree. This matches the behavior
// of SecureTrie in Geth.
return nil, nil
case *HashedNode:
// if a resolution function is set, resolve the
// current hash node.
if getter == nil {
return nil, errReadFromInvalid
}
payload, err := getter(child.commitment)
if err != nil {
return nil, err
}
// deserialize the payload and set it as the child
c, err := ParseNode(payload, n.depth+1, child.commitment)
if err != nil {
return nil, err
}
n.children[nChild] = c
return c.Get(k, getter)
default: // InternalNode
return child.Get(k, getter)
}
}
func (n *InternalNode) Hash() *Fr {
var hash Fr
toFr(&hash, n.Commitment())
return &hash
}
func (n *InternalNode) Commitment() *Point {
if n.commitment == nil {
panic("nil commitment")
}
return n.commitment
}
func (n *InternalNode) Commit() *Point {
poly := make([]Fr, NodeWidth)
emptyChildren := 256
if len(n.cow) != 0 {
for idx, comm := range n.cow {
emptyChildren--
var pre Fr
// TODO use kev's multimaptofield
toFr(&pre, comm)
// child in cow, so its child has also been
// modified, so call `Commit()` instead of
// `Commitment()`
toFr(&poly[idx], n.children[idx].Commit())
poly[idx].Sub(&poly[idx], &pre)
}
n.cow = nil
n.commitment.Add(n.commitment, GetConfig().CommitToPoly(poly, emptyChildren))
return n.commitment
}
return n.commitment
}
// groupKeys groups a set of keys based on their byte at a given depth.
func groupKeys(keys keylist, depth byte) []keylist {
// special case: no key
if len(keys) == 0 {
return []keylist{}
}
// special case: only one key left
if len(keys) == 1 {
return []keylist{keys}
}
// there are at least two keys left in the list at this depth
groups := make([]keylist, 0, len(keys))
firstkey, lastkey := 0, 1
for ; lastkey < len(keys); lastkey++ {
key := keys[lastkey]
keyidx := offset2key(key, depth)
previdx := offset2key(keys[lastkey-1], depth)
if keyidx != previdx {
groups = append(groups, keys[firstkey:lastkey])
firstkey = lastkey
}
}
groups = append(groups, keys[firstkey:lastkey])
return groups
}
func (n *InternalNode) GetProofItems(keys keylist) (*ProofElements, []byte, [][]byte) {
var (
groups = groupKeys(keys, n.depth)
pe = &ProofElements{
Cis: []*Point{},
Zis: []byte{},
Yis: []*Fr{}, // Should be 0
Fis: [][]Fr{},
ByPath: map[string]*Point{},
}
esses []byte = nil // list of extension statuses
poass [][]byte // list of proof-of-absence stems
)
// fill in the polynomial for this node
fi := make([]Fr, NodeWidth)
for i, child := range n.children {
toFr(&fi[i], child.Commitment())
}
for _, group := range groups {
childIdx := offset2key(group[0], n.depth)
// Build the list of elements for this level
var yi Fr
CopyFr(&yi, &fi[childIdx])
pe.Cis = append(pe.Cis, n.commitment)
pe.Zis = append(pe.Zis, childIdx)
pe.Yis = append(pe.Yis, &yi)
pe.Fis = append(pe.Fis, fi)
pe.ByPath[string(group[0][:n.depth])] = n.commitment
}
// Loop over again, collecting the children's proof elements
// This is because the order is breadth-first.
for _, group := range groups {
childIdx := offset2key(group[0], n.depth)
// Special case of a proof of absence: no children
// commitment, as the value is 0.
if _, ok := n.children[childIdx].(Empty); ok {
// A question arises here: what if this proof of absence
// corresponds to several stems? Should the ext status be
// repeated as many times? It would be wasteful, so the
// decoding code has to be aware of this corner case.
esses = append(esses, extStatusAbsentEmpty|((n.depth+1)<<3))
continue
}
pec, es, other := n.children[childIdx].GetProofItems(group)
pe.Merge(pec)
poass = append(poass, other...)
esses = append(esses, es...)
}
return pe, esses, poass
}
func (n *InternalNode) Serialize() ([]byte, error) {
var (
bitlist, hashlist [32]byte
nhashed int // number of children who are hashed nodes
)
commitments := make([]*Point, 0, NodeWidth)
for i, c := range n.children {
if _, ok := c.(Empty); !ok {
setBit(bitlist[:], i)
if _, ok := c.(*HashedNode); ok {
// don't trigger the commitment on hashed nodes,
// as they already hold a serialized version of
// their commitment. Instead, just mark them as
// hashes so they can be added directly.
setBit(hashlist[:], i)
nhashed++
} else {
commitments = append(commitments, c.Commitment())
}
}
}
children := make([]byte, 0, (len(commitments)+nhashed)*32)
bytecomms := banderwagon.ElementsToBytes(commitments)
consumed := 0
for i := 0; i < NodeWidth; i++ {
if bit(bitlist[:], i) {
// if a child is present and is a hash, add its
// internal, serialized representation directly.
if bit(hashlist[:], i) {
children = append(children, n.children[i].(*HashedNode).commitment...)
} else {
children = append(children, bytecomms[consumed][:]...)
consumed++
}
}
}
return append(append([]byte{internalRLPType}, bitlist[:]...), children...), nil
}
func (n *InternalNode) Copy() VerkleNode {
ret := &InternalNode{
children: make([]VerkleNode, len(n.children)),
commitment: new(Point),
depth: n.depth,
}
for i, child := range n.children {
ret.children[i] = child.Copy()
}
if n.commitment != nil {
CopyPoint(ret.commitment, n.commitment)
}
if n.cow != nil {
ret.cow = make(map[byte]*Point)
for k, v := range n.cow {
ret.cow[k] = new(Point)
CopyPoint(ret.cow[k], v)
}
}
return ret
}
func (n *InternalNode) toDot(parent, path string) string {
me := fmt.Sprintf("internal%s", path)
var hash Fr
toFr(&hash, n.commitment)
ret := fmt.Sprintf("%s [label=\"I: %x\"]\n", me, hash.BytesLE())
if len(parent) > 0 {
ret = fmt.Sprintf("%s %s -> %s\n", ret, parent, me)
}
for i, child := range n.children {
ret = fmt.Sprintf("%s%s", ret, child.toDot(me, fmt.Sprintf("%s%02x", path, i)))
}
return ret
}
func (n *InternalNode) setDepth(d byte) {
n.depth = d
}
// MergeTrees takes a series of subtrees that got filled following
// a command-and-conquer method, and merges them into a single tree.
func MergeTrees(subroots []*InternalNode) VerkleNode {
root := New().(*InternalNode)
for _, subroot := range subroots {
for i := 0; i < 256; i++ {
if _, ok := subroot.children[i].(Empty); ok {
continue
}
root.children[i] = subroot.children[i]
}
}
return root
}
func (n *LeafNode) ToHashedNode() *HashedNode {
if n.commitment == nil {
panic("nil commitment")
}
comm := n.commitment.Bytes()
return &HashedNode{commitment: comm[:]}
}
func (n *LeafNode) Insert(k []byte, value []byte, _ NodeResolverFn) error {
values := make([][]byte, NodeWidth)
values[k[31]] = value
return n.insertMultiple(k[:31], values)
}
func (n *LeafNode) insertMultiple(k []byte, values [][]byte) error {
// Sanity check: ensure the key header is the same:
if !equalPaths(k, n.stem) {
return errInsertIntoOtherStem
}
n.updateMultipleLeaves(values)
return nil
}
func (n *LeafNode) getOldCn(index byte) (*Point, *Fr) {
var (
c *Point
oldc Fr
)
if index < 128 {
c = n.c1
} else {
c = n.c2
}
toFr(&oldc, c)
return c, &oldc
}
func (n *LeafNode) updateC(index byte, c *Point, oldc *Fr) {
var (
newc Fr
diff Point
poly [256]Fr
)
toFr(&newc, c)
newc.Sub(&newc, oldc)
poly[2+(index/128)] = newc
diff = cfg.conf.Commit(poly[:])
n.commitment.Add(n.commitment, &diff)
}
func (n *LeafNode) updateCn(index byte, value []byte, c *Point) {
var (
old, newH [2]Fr
diff Point
poly [256]Fr
)
// Optimization idea:
// If the value is created (i.e. not overwritten), the leaf marker
// is already present in the commitment. In order to save computations,
// do not include it. The result should be the same,
// but the computation time should be faster as one doesn't need to
// compute 1 - 1 mod N.
leafToComms(old[:], n.values[index])
leafToComms(newH[:], value)
newH[0].Sub(&newH[0], &old[0])
poly[2*(index%128)] = newH[0]
diff = cfg.conf.Commit(poly[:])
poly[2*(index%128)].SetZero()
c.Add(c, &diff)
newH[1].Sub(&newH[1], &old[1])
poly[2*(index%128)+1] = newH[1]
diff = cfg.conf.Commit(poly[:])
c.Add(c, &diff)
}
func (n *LeafNode) updateLeaf(index byte, value []byte) {
c, oldc := n.getOldCn(index)
n.updateCn(index, value, c)
n.updateC(index, c, oldc)
n.values[index] = value
}
func (n *LeafNode) updateMultipleLeaves(values [][]byte) {
var c1, c2 *Point
var old1, old2 *Fr
for i, v := range values {
if len(v) != 0 && !bytes.Equal(v, n.values[i]) {
if i < 128 {
if c1 == nil {
c1, old1 = n.getOldCn(byte(i))
}
n.updateCn(byte(i), v, c1)
} else {
if c2 == nil {
c2, old2 = n.getOldCn(byte(i))
}
n.updateCn(byte(i), v, c2)
}
n.values[i] = v
}
}
if c1 != nil {
n.updateC(0, c1, old1)
}
if c2 != nil {
n.updateC(128, c2, old2)
}
}
func (n *LeafNode) InsertOrdered(key []byte, value []byte, _ NodeFlushFn) error {
// In the previous version, this value used to be flushed on insert.
// This is no longer the case, as all values at the last level get
// flushed at the same time.
return n.Insert(key, value, nil)
}
func (n *LeafNode) Delete(k []byte, _ NodeResolverFn) error {
// Sanity check: ensure the key header is the same:
if !equalPaths(k, n.stem) {
return errDeleteNonExistent
}
var zero [32]byte
n.updateLeaf(k[31], zero[:])
return nil
}
func (n *LeafNode) Get(k []byte, _ NodeResolverFn) ([]byte, error) {
if !equalPaths(k, n.stem) {
// If keys differ, return nil in order to
// signal that the key isn't present in the
// tree. Do not return an error, thus matching
// the behavior of Geth's SecureTrie.
return nil, nil
}
// value can be nil, as expected by geth
return n.values[k[31]], nil
}
func (n *LeafNode) Hash() *Fr {
// TODO cache this in a subsequent PR, not done here
// to reduce complexity.
// TODO use n.commitment once all Insert* are diff-inserts
var hash Fr
toFr(&hash, n.Commitment())
return &hash
}
func (n *LeafNode) Commitment() *Point {
if n.commitment == nil {
panic("nil commitment")
}
return n.commitment
}
func (n *LeafNode) Commit() *Point {
return n.commitment
}
// fillSuffixTreePoly takes one of the two suffix tree and
// builds the associated polynomial, to be used to compute
// the corresponding C{1,2} commitment.
func fillSuffixTreePoly(poly []Fr, values [][]byte) int {
count := 0
for idx, val := range values {
if val == nil {
continue
}
count++
leafToComms(poly[(idx<<1)&0xFF:], val)
}
return count
}
// leafToComms turns a leaf into two commitments of the suffix
// and extension tree.
func leafToComms(poly []Fr, val []byte) {
if len(val) == 0 {
return
}
if len(val) > 32 {
panic(fmt.Sprintf("invalid leaf length %d, %v", len(val), val))
}
var (
valLoWithMarker [17]byte
loEnd = 16
)
if len(val) < loEnd {
loEnd = len(val)
}
copy(valLoWithMarker[:loEnd], val[:loEnd])
valLoWithMarker[16] = 1 // 2**128
FromLEBytes(&poly[0], valLoWithMarker[:])
if len(val) >= 16 {
FromLEBytes(&poly[1], val[16:])
}
}
func (n *LeafNode) GetProofItems(keys keylist) (*ProofElements, []byte, [][]byte) {
var (
poly [256]Fr // top-level polynomial
pe = &ProofElements{
Cis: []*Point{n.commitment, n.commitment},
Zis: []byte{0, 1},
Yis: []*Fr{&poly[0], &poly[1]}, // Should be 0
Fis: [][]Fr{poly[:], poly[:]},
ByPath: map[string]*Point{},
}
esses []byte = nil // list of extension statuses
poass [][]byte // list of proof-of-absence stems
)
// Initialize the top-level polynomial with 1 + stem + C1 + C2
poly[0].SetUint64(1)
StemFromBytes(&poly[1], n.stem)
toFr(&poly[2], n.c1)