-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompact_merkle_tree.rs
1614 lines (1469 loc) · 59.4 KB
/
compact_merkle_tree.rs
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
use crate::errors::{MerkleTreeError, MerkleTreeErrorKind};
use crate::hasher::Arity2Hasher;
use std::marker::PhantomData;
// Code inspired from Google's certificate-transparency https://github.com/google/certificate-transparency/blob/master/python/ct/crypto/merkle.py
// , Evernym's enhancements and Google's trillian
// TODO: Make it changable by a feature
// Tree with size 2^64 should be good for most purposes.
type TreeSizeType = u64;
/// Returns the number of bits set in `n`
fn count_set_bits(mut n: TreeSizeType) -> u8 {
// Brian Kernighan's was https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
let mut count = 0;
while n != 0 {
n &= n - 1;
count += 1
}
count
}
/// Number of bits required to represent `n`
fn num_bits(mut n: TreeSizeType) -> u8 {
if n == 0 {
return 0;
}
let mut index = 0;
while n != 0 {
index += 1;
n >>= 1;
}
index
}
/// Least significant bit set in `n`,
fn least_significant_set_bit(n: TreeSizeType) -> u8 {
if n == 0 {
return 0;
}
n.trailing_zeros() as u8
}
/// Largest power of 2 less than `n`, 2^k < n <= 2^{k+1}, return 2^k
fn largest_power_of_2_less_than(n: TreeSizeType) -> TreeSizeType {
if n < 2 {
return 0;
}
let mut cur = 1u64;
let mut largest = 1u64;
while cur < n {
largest = cur;
let r = cur.wrapping_shl(1);
if r < cur {
// `cur` has wrapped around
break;
} else {
cur = r;
}
}
largest
}
/// Break a number on decreasing powers of 2, eg 4 -> 4, 5 -> [4, 1], 6 -> [4, 2], 7 -> [4, 2, 1], 8 -> [8]
fn powers_of_2(mut n: TreeSizeType) -> Vec<TreeSizeType> {
if n == 0 {
return vec![];
}
let mut powers = vec![];
loop {
if n.is_power_of_two() {
powers.push(n);
break;
} else {
let p = largest_power_of_2_less_than(n);
n = n - p;
powers.push(p);
}
}
powers
}
/// Interface for the database used to store the leaf and node hashes
pub trait HashDb<H> {
/// The database stores all leaves
fn add_leaf(&mut self, leaf_hash: H) -> Result<(), MerkleTreeError>;
/// The database stores roots of all full subtrees of the datbase
fn add_full_subtree_root(&mut self, node_hash: H) -> Result<(), MerkleTreeError>;
fn get_leaf(&self, leaf_index: TreeSizeType) -> Result<H, MerkleTreeError>;
fn get_full_subtree_root(&self, node_index: TreeSizeType) -> Result<H, MerkleTreeError>;
}
/// Uses an in-memory vectors for storing leaf and node hashes. Used for testing.
#[derive(Clone, Debug)]
pub struct InMemoryHashDb<H> {
leaves: Vec<H>,
nodes: Vec<H>,
}
impl<H: Clone> HashDb<H> for InMemoryHashDb<H> {
fn add_leaf(&mut self, leaf_hash: H) -> Result<(), MerkleTreeError> {
self.leaves.push(leaf_hash);
Ok(())
}
fn add_full_subtree_root(&mut self, node_hash: H) -> Result<(), MerkleTreeError> {
self.nodes.push(node_hash);
Ok(())
}
fn get_leaf(&self, leaf_index: TreeSizeType) -> Result<H, MerkleTreeError> {
let i = leaf_index as usize;
if i >= self.leaves.len() {
Err(MerkleTreeError::from_kind(
MerkleTreeErrorKind::LeafIndexNotFoundInDB { index: i as u64 },
))
} else {
Ok(self.leaves[i].clone())
}
}
fn get_full_subtree_root(&self, node_index: TreeSizeType) -> Result<H, MerkleTreeError> {
let i = node_index as usize;
if i >= self.nodes.len() {
Err(MerkleTreeError::from_kind(
MerkleTreeErrorKind::NodeIndexNotFoundInDB { index: i as u64 },
))
} else {
Ok(self.nodes[i].clone())
}
}
}
impl<H> InMemoryHashDb<H> {
pub fn new() -> Self {
Self {
leaves: vec![],
nodes: vec![],
}
}
pub fn leaf_count(&self) -> TreeSizeType {
self.leaves.len() as TreeSizeType
}
pub fn node_count(&self) -> TreeSizeType {
self.nodes.len() as TreeSizeType
}
}
/// Compact merkle tree can be seen as a list of trees. A leaf is added to a tree until its full (it has 2^k leaves)
/// Once a tree is full, start adding to a new tree until that is full. Once the current full tree has the same height
/// as the previous full tree, combine both full trees to form a new full tree such that the previous and current form
/// the left and right subtrees of the new full tree respectively. When the various trees don't form a full tree, they
/// can be seen as subtrees of a big n-ary tree. The root of this n-ary tree at any time is the hash of the
/// concatenated roots of all full trees. eg, if there are 4 leaves in total, the n-ary tree is a full binary tree. When
/// there are 5 leaves, the n-ary tree contains 2 subtrees of size 4 and 1. With 6 leaves, the n-ary tree contains 2
/// subtrees of size 4 and 2. With 7 leaves, the n-ary tree contains 3 subtrees of size 4, 3 and 1. With 8 leaves, the
/// the n-ary tree is a full binary tree of size 8. Lets say we want to insert a few leaves l_0, l_1, l_2,..l_n in
/// an empty tree. Inserting l_0 makes the tree full (with 2^0=1 leaf) with root T_0 so l_1 will be inserted
/// in a new tree with root T_1, making it full as well. Note that since roots T_0 and T_1 have only 1 element
/// so the root hash is same as the leaf hash, i.e. T_0 = hash(l_0) and T_1 = hash(l_1). Now we have 2 full
/// trees of same height (1 in this case) so we combine them to make a new full tree of height 1 more than
/// their height (new tree has height 2 in this case). The root of this new tree is T_2 and T_2 = hash(T_0, T_1).
/// Since T_2 is full, l_2 will be inserted into a new tree T_3 and T_3 = hash(l_2). Also, T_3 is full so l_3
/// will be inserted in a new tree with root T_4 and T_4 = hash(l_3). We again have 2 tree of same height (1 here)
/// T3, T_4 so we combine them to form a tree of bigger height and root T_5 = hash(T_3, T_4). Now we have 2 more
/// trees of same height, T_2 and T_5. We combine them to form a bigger full tree with root T_6 = hash(T_2, T_5).
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CompactMerkleTree<D: Clone, H: Clone, MTH>
where
MTH: Arity2Hasher<D, H>,
{
/// Number of leaves in the tree
pub size: TreeSizeType,
/// Contains root of all the full subtrees, from largest subtree to smallest
pub full_subtree_roots: Vec<H>,
pub hasher: MTH,
pub phantom: PhantomData<D>,
}
impl<D: Clone, H: Clone + PartialEq, MTH> CompactMerkleTree<D, H, MTH>
where
MTH: Arity2Hasher<D, H>,
{
pub fn new(hasher: MTH) -> Self {
Self {
size: 0,
full_subtree_roots: vec![],
hasher,
phantom: PhantomData,
}
}
/// Takes a hash db and returns a new tree of the size `tree_size` based on the leaves and nodes
/// present in hash db.
pub fn new_from_hash_db(
hasher: MTH,
tree_size: TreeSizeType,
hash_db: &dyn HashDb<H>,
) -> Result<Self, MerkleTreeError> {
// The full subtree roots will form the inclusion path of the next leaf that will be inserted
// making the new tree's size `tree_size+1`
let mut full_subtree_roots = Self::get_leaf_inclusion_proof_for_tree_size(
&hasher,
tree_size,
tree_size + 1,
hash_db,
)?;
full_subtree_roots.reverse();
let mut new_tree = Self::new(hasher);
new_tree.size = tree_size;
new_tree.full_subtree_roots = full_subtree_roots;
Ok(new_tree)
}
/// Append a leaf and return the inclusion proof of the leaf, path goes from leaf to root
pub fn append(
&mut self,
leaf_data: D,
hash_db: &mut dyn HashDb<H>,
) -> Result<Vec<H>, MerkleTreeError> {
// Inclusion proof (audit path in RFC 6982) of a leaf contains the nodes that when hashed
// together with the leaf result in the root hash. Thus the inclusion proof would be the roots
// of all the subtrees from larger to smaller trees.
let mut inclusion_proof = self.full_subtree_roots.clone();
// We need the path from smaller to larger.
inclusion_proof.reverse();
// A single leaf forms a full subtree with 2^0 = 1 leaf
self.push_full_subtree(vec![leaf_data], hash_db)?;
Ok(inclusion_proof)
}
/// Append multiple leaves
pub fn extend(
&mut self,
mut leaves: Vec<D>,
hash_db: &mut dyn HashDb<H>,
) -> Result<(), MerkleTreeError> {
if leaves.is_empty() {
return Err(MerkleTreeErrorKind::NoLeafProvided.into());
}
let mut num_leaves_remaining = leaves.len() as TreeSizeType;
if self.size > 0 {
// The existing tree is non empty
loop {
// Try to create full tree from leaves, of the same size as the smallest full subtree
// of the current tree
let max_leaves_to_insert = 1 << self.smallest_subtree_height() as TreeSizeType;
if max_leaves_to_insert <= num_leaves_remaining {
let to_insert = leaves.drain(0..max_leaves_to_insert as usize).collect();
self.push_full_subtree(to_insert, hash_db)?;
num_leaves_remaining -= max_leaves_to_insert;
} else {
// The leaves cannot form a full tree of the size of the smallest full subtree
// of the current tree
break;
}
}
}
if num_leaves_remaining > 0 {
// No subtree exists in the current tree smaller or of same size as the number of remaining leaves so
// create full trees from the remaining leaves and add store their roots.
let (leaf_hashes, node_hashes) =
Self::hash_leaves(&self.hasher, leaves.drain(0..).collect())?;
// `full_subtree_roots` should contain the roots of the full subtrees
let mut idx = 0;
for p in powers_of_2(num_leaves_remaining) {
// `p` would be the size of the full tree
if p == 1 {
// Hash of the subtree of size 1 is the leaf hash in it
self.added_subtree(1, leaf_hashes[(num_leaves_remaining - 1) as usize].clone());
} else {
// pick root from `node_hashes` by calculating the index of the root; the root
// of tree of size p will be at index p-1 since there are p-1 inner nodes in a tree
// of p leaves
idx += (p - 1);
self.added_subtree(p, node_hashes[(idx - 1) as usize].clone());
}
}
for l in leaf_hashes {
hash_db.add_leaf(l)?;
}
for n in node_hashes {
hash_db.add_full_subtree_root(n)?;
}
}
Ok(())
}
pub fn get_root_hash(&self) -> Result<H, MerkleTreeError> {
if self.size == 0 {
return Err(MerkleTreeErrorKind::CannotQueryEmptyTree.into());
}
// Hash the roots of subtrees, starting from the root of the lower subtree.
// In case of a single subtree (a full tree), the root of the subtree will be the root of
// the full tree
let mut cur_root = self.full_subtree_roots[self.full_subtree_roots.len() - 1].clone();
for i in (0..self.full_subtree_roots.len() - 1).rev() {
cur_root = self
.hasher
.hash_tree_nodes(self.full_subtree_roots[i].clone(), cur_root)?;
}
Ok(cur_root)
}
/// Get a proof that the leaf at index `leaf_index` is present in the current tree. Called `audit path` in RFC 6982
pub fn get_leaf_inclusion_proof(
&self,
leaf_index: TreeSizeType,
hash_db: &dyn HashDb<H>,
) -> Result<Vec<H>, MerkleTreeError> {
Self::get_leaf_inclusion_proof_for_tree_size(&self.hasher, leaf_index, self.size, hash_db)
}
/// Get a proof that the leaf at index `leaf_index` is present in the tree of size `tree_size`. Called `audit path` in RFC 6982
pub fn get_leaf_inclusion_proof_for_tree_size(
hasher: &MTH,
leaf_index: TreeSizeType,
tree_size: TreeSizeType,
hash_db: &dyn HashDb<H>,
) -> Result<Vec<H>, MerkleTreeError> {
if leaf_index >= tree_size {
return Err(MerkleTreeErrorKind::TreeSmallerThanExpected {
expected: leaf_index + 1,
given: tree_size,
}
.into());
}
Self::path(hasher, leaf_index, 0, tree_size, hash_db)
}
/// Get a proof that the a shorter tree with size `old_tree_size` is consistent with the current
/// tree, i.e. the shorter tree is contained in the new tree
pub fn get_consistency_proof(
&self,
old_tree_size: TreeSizeType,
hash_db: &dyn HashDb<H>,
) -> Result<Vec<H>, MerkleTreeError> {
Self::get_consistency_proof_for_tree_size(&self.hasher, old_tree_size, self.size, hash_db)
}
/// Get a proof that the a shorter tree with size `old_tree_size` is consistent with tree of size
/// `new_tree_size`, i.e. the shorter tree is contained in the new tree
pub fn get_consistency_proof_for_tree_size(
hasher: &MTH,
old_tree_size: TreeSizeType,
new_tree_size: TreeSizeType,
hash_db: &dyn HashDb<H>,
) -> Result<Vec<H>, MerkleTreeError> {
if old_tree_size > new_tree_size {
return Err(MerkleTreeErrorKind::TreeSmallerThanExpected {
expected: old_tree_size,
given: new_tree_size,
}
.into());
}
if old_tree_size == new_tree_size {
Ok(vec![])
} else {
let proof = Self::subproof(hasher, old_tree_size, 0, new_tree_size, true, hash_db)?;
Ok(proof)
}
}
/// Verify the proof generated by `Self::get_leaf_inclusion_proof_*`
pub fn verify_leaf_inclusion_proof(
hasher: &MTH,
leaf_index: TreeSizeType,
leaf_val: D,
tree_size: TreeSizeType,
root: &H,
proof: Vec<H>,
) -> Result<bool, MerkleTreeError> {
if leaf_index >= tree_size {
return Err(MerkleTreeErrorKind::TreeSmallerThanExpected {
expected: leaf_index + 1,
given: tree_size,
}
.into());
}
// start from the leaf hash
let cur_hash = hasher.hash_leaf_data(leaf_val)?;
let (right_border_len, inner_path_len) =
Self::get_right_border_and_inner_node_count(leaf_index, tree_size);
if (right_border_len + inner_path_len) != proof.len() as u8 {
return Err(MerkleTreeErrorKind::ShorterInclusionProof {
expected: right_border_len + inner_path_len,
given: proof.len() as u8,
}
.into());
}
Self::_verify_leaf_inclusion_proof(
hasher,
leaf_index,
inner_path_len,
cur_hash,
proof,
root,
)
}
/// Verify the proof generated by `Self::get_consistency_proof_*`
pub fn verify_consistency_proof(
hasher: &MTH,
old_tree_size: TreeSizeType,
new_tree_size: TreeSizeType,
old_root: &H,
new_root: &H,
mut proof: Vec<H>,
) -> Result<bool, MerkleTreeError> {
if old_tree_size > new_tree_size {
return Err(MerkleTreeErrorKind::ConsistencyProofIncorrectTreeSize {
new: new_tree_size,
old: old_tree_size,
}
.into());
}
if old_tree_size == 0 || new_tree_size == 0 {
return Err(MerkleTreeErrorKind::ConsistencyProofWithEmptyTree.into());
}
if old_tree_size == new_tree_size {
return Ok(old_root == new_root);
}
// Start hashing from the node with hash `start_hash`
let start_hash = if old_tree_size.is_power_of_two() {
// If the old tree was a full tree, then its root hash will not be part of the proof
old_root.clone()
} else {
proof.remove(0)
};
let (right_border_len, mut inner_path_len) =
Self::get_right_border_and_inner_node_count(old_tree_size - 1, new_tree_size);
// height of the smallest full subtree of the old tree
let shift = least_significant_set_bit(old_tree_size);
inner_path_len -= shift;
if (right_border_len + inner_path_len) != proof.len() as u8 {
return Err(MerkleTreeErrorKind::ShorterConsistencyProof {
expected: right_border_len + inner_path_len,
given: proof.len() as u8,
}
.into());
}
// A consistency proof is a leaf inclusion proof for the node with index `old_tree_size - 1` in
// the new tree that also proves inclusion in the old tree.
let node_index = (old_tree_size - 1) >> shift as TreeSizeType;
// verify inclusion in the old tree
let mut index_for_old = node_index.clone();
let mut expected_old_root = start_hash.clone();
for p in proof.iter().take(inner_path_len as usize) {
if index_for_old % 2 == 1 {
// the proof contains only left nodes, i.e. nodes present in the old tree
expected_old_root = hasher.hash_tree_nodes(p.clone(), expected_old_root)?;
}
index_for_old >>= 1;
}
for p in proof.iter().skip(inner_path_len as usize) {
expected_old_root = hasher.hash_tree_nodes(p.clone(), expected_old_root)?;
}
if expected_old_root != *old_root {
return Err(MerkleTreeErrorKind::InconsistentOldRoot.into());
}
// verify inclusion in the new tree
Self::_verify_leaf_inclusion_proof(
hasher,
node_index,
inner_path_len,
start_hash,
proof,
new_root,
)
}
/// Helper for `Self::verify_leaf_inclusion_proof`
fn _verify_leaf_inclusion_proof(
hasher: &MTH,
mut leaf_index: TreeSizeType,
inner_path_len: u8,
mut cur_hash: H,
mut proof: Vec<H>,
expected_root: &H,
) -> Result<bool, MerkleTreeError> {
for p in proof.drain(0..inner_path_len as usize) {
if leaf_index % 2 == 1 {
cur_hash = hasher.hash_tree_nodes(p, cur_hash)?;
} else {
cur_hash = hasher.hash_tree_nodes(cur_hash, p)?;
}
leaf_index >>= 1;
}
// the nodes on the right border will always be roots of the right subtree and hence on the right
// side while hashing
for p in proof.drain(0..) {
cur_hash = hasher.hash_tree_nodes(p, cur_hash)?;
}
Ok(cur_hash == *expected_root)
}
/// Add a full subtree formed from the given leaves. The number of leaves should be a power of 2.
fn push_full_subtree(
&mut self,
leaves: Vec<D>,
hash_db: &mut dyn HashDb<H>,
) -> Result<(), MerkleTreeError> {
if !leaves.len().is_power_of_two() {
return Err(MerkleTreeErrorKind::ErrorInsertingSubtree {
msg: format!(
"Leaves should form a full subtree but only {} leaves given",
leaves.len()
),
}
.into());
}
// leaves form a full subtree.
let new_subtree_height = leaves.len().trailing_zeros() as u8;
let min_subtree_height = self.smallest_subtree_height();
if (self.size != 0) && (new_subtree_height > min_subtree_height) {
return Err(MerkleTreeErrorKind::ErrorInsertingSubtree {msg: format!("Leaves should form full subtree of height at most {} but form subtree of height {}", min_subtree_height, new_subtree_height)}.into());
}
let (leaf_hashes, node_hashes) = Self::hash_leaves(&self.hasher, leaves)?;
// Root of a subtree with a single leaf is the leaf itself.
let subtree_root = if node_hashes.is_empty() {
leaf_hashes.last().unwrap().clone()
} else {
node_hashes.last().unwrap().clone()
};
for l in leaf_hashes {
hash_db.add_leaf(l)?;
}
for n in node_hashes {
hash_db.add_full_subtree_root(n)?;
}
// roots of all the new subtrees created from merging
let new_roots = self.push_full_subtree_hash(new_subtree_height, subtree_root)?;
for n in new_roots {
hash_db.add_full_subtree_root(n)?;
}
Ok(())
}
/// Take a subtree hash and add it to the tree. This might lead to merging of existing subtrees
/// since the given subtree can be of the same size as the smallest subtree in the tree which will
/// result in merging of those together forming a larger subtree. That larger subtree might turn out
/// to be of the same size as another existing subtree again resulting in a merge.
/// Return the roots of all the new subtrees created from merging.
fn push_full_subtree_hash(
&mut self,
subtree_height: u8,
subtree_hash: H,
) -> Result<Vec<H>, MerkleTreeError> {
let min_subtree_height = self.smallest_subtree_height();
let subtree_size = 1 << (subtree_height as TreeSizeType);
if (self.size != 0) && (subtree_height > min_subtree_height) {
return Err(MerkleTreeErrorKind::ErrorInsertingSubtree {msg: format!("Leaves should form full subtree of height at most {} but form subtree of height {}", min_subtree_height, subtree_height)}.into());
}
if self.size == 0 || subtree_height < min_subtree_height {
// Either the tree is empty or given subtree is smaller than the smallest subtree in the tree
self.added_subtree(subtree_size, subtree_hash);
Ok(vec![])
} else {
// The given subtree is of the size as the smallest subtree in the tree, hence merge them.
// Take the smallest subtree hash.
let removed_subtree_hash = self.removed_subtree(subtree_size)?;
// Hash both subtree roots to create root of the new bigger subtree
let next_root = self
.hasher
.hash_tree_nodes(removed_subtree_hash, subtree_hash)?;
// Check if any more merges need to happen
let mut remain_roots =
self.push_full_subtree_hash(subtree_height + 1, next_root.clone())?;
remain_roots.insert(0, next_root);
Ok(remain_roots)
}
}
/// A single full subtree was added.
fn added_subtree(&mut self, subtree_size: TreeSizeType, subtree_root: H) {
self.size += subtree_size;
self.full_subtree_roots.push(subtree_root);
}
/// A single full subtree was removed.
fn removed_subtree(&mut self, subtree_size: TreeSizeType) -> Result<H, MerkleTreeError> {
if self.size < subtree_size {
return Err(MerkleTreeErrorKind::TreeSmallerThanExpected {
expected: subtree_size,
given: self.size,
}
.into());
}
self.size -= subtree_size;
Ok(self.full_subtree_roots.pop().unwrap())
}
/// Arrange leaves in a merkle tree and return hashes of all leaves and hash of all full subtrees
/// with size > 1. The first return value is the list of hashes, one for each leaf. The second return
/// value is the list of root hashes of all full subtrees.
fn hash_leaves(hasher: &MTH, mut leaves: Vec<D>) -> Result<(Vec<H>, Vec<H>), MerkleTreeError> {
if leaves.is_empty() {
return Err(MerkleTreeErrorKind::NoLeafProvided.into());
}
if leaves.len() == 1 {
Ok((vec![hasher.hash_leaf_data(leaves.remove(0))?], vec![]))
} else {
// left subtree will be a full subtree
let left_subtree_size = largest_power_of_2_less_than(leaves.len() as TreeSizeType);
// right subtree might be a full subtree
let right_subtree_size = leaves.len() as TreeSizeType - left_subtree_size;
let right_subtree_leaves = leaves.split_off(left_subtree_size as usize);
let (mut left_leaf_hashes, mut left_node_hashes) = Self::hash_leaves(hasher, leaves)?;
let (mut right_leaf_hashes, mut right_node_hashes) =
Self::hash_leaves(hasher, right_subtree_leaves)?;
// If the left and right subtree have same size then the root of the subtree needs to be returned
let root = if left_subtree_size == right_subtree_size {
// When there is only 1 leaf, the root hash is the leaf hash.
let left_root = if left_node_hashes.is_empty() {
left_leaf_hashes.last().unwrap().clone()
} else {
left_node_hashes.last().unwrap().clone()
};
let right_root = if right_node_hashes.is_empty() {
right_leaf_hashes.last().unwrap().clone()
} else {
right_node_hashes.last().unwrap().clone()
};
let root = hasher.hash_tree_nodes(left_root, right_root)?;
Some(root)
} else {
None
};
let mut all_leaf_hashes = vec![];
all_leaf_hashes.append(&mut left_leaf_hashes);
all_leaf_hashes.append(&mut right_leaf_hashes);
let mut all_node_hashes = vec![];
all_node_hashes.append(&mut left_node_hashes);
all_node_hashes.append(&mut right_node_hashes);
if left_subtree_size == right_subtree_size {
all_node_hashes.push(root.unwrap());
}
Ok((all_leaf_hashes, all_node_hashes))
}
}
/// Given an ordered list of n inputs to the tree, D[n] = {d(0), ..., d(n-1)}, the Merkle audit
/// path PATH(m, D[n]) for the (m+1)th input d(m), 0 <= m < n, is defined as follows:
/// For n = 1, path is empty. D[1] = {d(0)}, PATH(0, {d(0)}) = {}
/// For n > 1 and m < n, let k be the largest power of two smaller than n:
/// PATH(m, D[n]) = PATH(m, D[0:k]) : MTH(D[k:n]) for m < k; and
/// PATH(m, D[n]) = PATH(m - k, D[k:n]) : MTH(D[0:k]) for m >= k,
/// where ":" means concatenation and MTH is the merkle tree hash of the sublist
/// Path goes from leaf to root
fn path(
hasher: &MTH,
leaf_index: TreeSizeType,
from: TreeSizeType,
to: TreeSizeType,
hash_db: &dyn HashDb<H>,
) -> Result<Vec<H>, MerkleTreeError> {
// size of the subtree being processed
let subtree_size = to - from;
if subtree_size == 1 {
return Ok(vec![]);
}
// Split the subtree into 2 smaller subtrees of sizes [from, k) and [k, to)
let k = largest_power_of_2_less_than(subtree_size);
let (mut path, mth) = if leaf_index < k {
// leaf_index is in the left half of the subtree, find path in the left half
let path = Self::path(hasher, leaf_index, from, from + k, hash_db)?;
// take root of the right half
let mth = Self::subtree_root_hash_from_db(hasher, from + k, to, hash_db)?;
(path, mth)
} else {
// leaf_index is in the right half of the subtree, find path in the right half
let path = Self::path(hasher, leaf_index - k, from + k, to, hash_db)?;
// take root of the left half
let mth = Self::subtree_root_hash_from_db(hasher, from, from + k, hash_db)?;
(path, mth)
};
path.push(mth);
Ok(path)
}
/// a consistency proof must contain a set of intermediate nodes sufficient to verify
/// MTH(D[n]), such that (a subset of) the same nodes can be used to verify MTH(D[0:m]).
/// PROOF(m, D[n]) = SUBPROOF(m, D[n], true), the boolean indicates whether the root at m is part of the old tree
/// SUBPROOF(m, D[m], true) = {} if root of m was part of old tree
/// SUBPROOF(m, D[m], false) = {MTH(D[m])} if root of m was not part of old tree
/// For m < n, let k be the largest power of two smaller than n. The subproof is then defined recursively.
/// SUBPROOF(m, D[n], b) = SUBPROOF(m, D[0:k], b) : MTH(D[k:n]) for m <= k
/// SUBPROOF(m, D[n], b) = SUBPROOF(m - k, D[k:n], false) : MTH(D[0:k]) for m > k
/// /// where ":" means concatenation and MTH is the merkle tree hash of the sublist
fn subproof(
hasher: &MTH,
old_tree_size: TreeSizeType,
from: TreeSizeType,
to: TreeSizeType,
root_present_in_old_tree: bool,
hash_db: &dyn HashDb<H>,
) -> Result<Vec<H>, MerkleTreeError> {
// size of the subtree being processed
let subtree_size = to - from;
if subtree_size == old_tree_size && root_present_in_old_tree {
Ok(vec![])
} else if subtree_size == old_tree_size && !root_present_in_old_tree {
Ok(vec![Self::subtree_root_hash_from_db(
hasher, from, to, hash_db,
)?])
} else {
// Split the subtree into 2 smaller subtrees of sizes [from, k) and [k, to)
let k = largest_power_of_2_less_than(subtree_size);
let (mut subproof, mth) = if old_tree_size <= k {
// the right subtree is only present in the new tree so take root of the right
// subtree and get subproof in the left subtree
let subproof = Self::subproof(
hasher,
old_tree_size,
from,
from + k,
root_present_in_old_tree,
hash_db,
)?;
let mth = Self::subtree_root_hash_from_db(hasher, from + k, to, hash_db)?;
(subproof, mth)
} else {
// left subtree is completely present in the old tree and some of the right subtree
// so take root of the left subtree and get subproof in the right subtree ignoring
// the part already present in the old tree.
let subproof =
Self::subproof(hasher, old_tree_size - k, from + k, to, false, hash_db)?;
let mth = Self::subtree_root_hash_from_db(hasher, from, from + k, hash_db)?;
(subproof, mth)
};
subproof.push(mth);
Ok(subproof)
}
}
/// Reads leaves and nodes from database to create a root hash over leaves [`from`, `to`)
fn subtree_root_hash_from_db(
hasher: &MTH,
from: TreeSizeType,
to: TreeSizeType,
hash_db: &dyn HashDb<H>,
) -> Result<H, MerkleTreeError> {
if from >= to {
return Err(MerkleTreeErrorKind::IncorrectSpan { from, to }.into());
}
let subtree_size = to - from;
if subtree_size == 1 {
hash_db.get_leaf(from)
} else if subtree_size.is_power_of_two() {
let node_index = Self::get_subtree_root_index(from, to);
hash_db.get_full_subtree_root(node_index)
} else {
// Divide the subtree into 2 subtrees and hash their roots
// left subtree will be a full subtree
let left_subtree_size = largest_power_of_2_less_than(subtree_size);
let left_root =
Self::subtree_root_hash_from_db(hasher, from, from + left_subtree_size, hash_db)?;
let right_root =
Self::subtree_root_hash_from_db(hasher, from + left_subtree_size, to, hash_db)?;
hasher.hash_tree_nodes(left_root, right_root)
}
}
/// Get subtree root index where subtree is over leaves [`from`, `to`). Assumes `from` and `to` form
/// a full subtree and `to` > `from`
fn get_subtree_root_index(from: TreeSizeType, to: TreeSizeType) -> TreeSizeType {
fn root_index(m: TreeSizeType, from: TreeSizeType, to: TreeSizeType) -> TreeSizeType {
if from == m {
// take all nodes between from and to which in a full subtree is always `no. of leaves - 1`
// last -1 because nodes are indexed from 0
to - from - 1 - 1
} else {
let d = to - from;
let k = largest_power_of_2_less_than(d);
(k - 1) + root_index(m, from + k, to)
}
}
root_index(from, 0, to)
}
/// Height of the smallest subtree in the current tree
fn smallest_subtree_height(&self) -> u8 {
least_significant_set_bit(self.size)
}
/// For a tree size and a given leaf index, compare the paths from root to the leaf at index `leaf_index`
/// and the last leaf and split the path of `leaf_index` where it diverges from path to last leaf.
fn get_right_border_and_inner_node_count(
leaf_index: TreeSizeType,
size: TreeSizeType,
) -> (u8, u8) {
let last_leaf_idx = (size - 1);
// The bit representation of `leaf_index` and `leaf_index` represent which subtrees they lie in
// and their paths from root to leaf. XOR will set those bits 1 where `leaf_index` and `leaf_index`
// are different with the MSB being the first node where they diverge.
// Taking `num_bits` will return the number of nodes on the path from the diversion (including the diverging node).
let inner_path_len = num_bits(leaf_index ^ last_leaf_idx);
// `leaf_index >> inner_path_len` will have those nodes . The nodes on the right border
let right_border_path_len = count_set_bits(leaf_index >> inner_path_len as u64);
(right_border_path_len, inner_path_len)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hasher::Sha256Hasher;
#[test]
fn test_largest_power_of_2_less_than() {
assert_eq!(largest_power_of_2_less_than(0), 0);
assert_eq!(largest_power_of_2_less_than(1), 0);
assert_eq!(largest_power_of_2_less_than(2), 1);
assert_eq!(largest_power_of_2_less_than(3), 2);
assert_eq!(largest_power_of_2_less_than(4), 2);
assert_eq!(largest_power_of_2_less_than(5), 4);
assert_eq!(largest_power_of_2_less_than(6), 4);
assert_eq!(largest_power_of_2_less_than(7), 4);
assert_eq!(largest_power_of_2_less_than(8), 4);
// 2^63 = 9223372036854775808, 2^62 = 4611686018427387904
assert_eq!(
largest_power_of_2_less_than(u64::max_value()),
9223372036854775808
);
assert_eq!(
largest_power_of_2_less_than(u64::max_value() - 1),
9223372036854775808
);
assert_eq!(
largest_power_of_2_less_than(9223372036854775808u64),
4611686018427387904u64
);
assert_eq!(
largest_power_of_2_less_than(9223372036854775808u64 - 1),
4611686018427387904u64
);
assert_eq!(
largest_power_of_2_less_than(9223372036854775808u64 + 1),
9223372036854775808u64
);
}
#[test]
fn test_count_set_bits() {
assert_eq!(count_set_bits(0), 0);
assert_eq!(count_set_bits(1), 1);
assert_eq!(count_set_bits(2), 1);
assert_eq!(count_set_bits(3), 2);
assert_eq!(count_set_bits(4), 1);
assert_eq!(count_set_bits(u64::max_value()), 64);
// 2^63 = 9223372036854775808
assert_eq!(count_set_bits(9223372036854775808), 1);
assert_eq!(count_set_bits(9223372036854775808 - 1), 63);
}
#[test]
fn test_least_significant_set_bit() {
assert_eq!(least_significant_set_bit(0), 0);
assert_eq!(least_significant_set_bit(1), 0);
assert_eq!(least_significant_set_bit(2), 1);
assert_eq!(least_significant_set_bit(3), 0);
assert_eq!(least_significant_set_bit(4), 2);
assert_eq!(least_significant_set_bit(u64::max_value()), 0);
// 2^63 = 9223372036854775808
assert_eq!(least_significant_set_bit(9223372036854775808), 63);
assert_eq!(least_significant_set_bit(9223372036854775808 - 1), 0);
}
#[test]
fn test_num_bits() {
assert_eq!(num_bits(0), 0);
assert_eq!(num_bits(1), 1);
assert_eq!(num_bits(2), 2);
assert_eq!(num_bits(3), 2);
assert_eq!(num_bits(4), 3);
assert_eq!(num_bits(5), 3);
assert_eq!(num_bits(6), 3);
assert_eq!(num_bits(7), 3);
assert_eq!(num_bits(8), 4);
assert_eq!(num_bits(u64::max_value()), 64);
// 2^63 = 9223372036854775808
assert_eq!(num_bits(9223372036854775808), 64);
assert_eq!(num_bits(9223372036854775808 - 1), 63);
}
#[test]
fn test_break_in_powers_of_2() {
//assert_eq!(break_in_powers_of_2(0), vec![]);
assert_eq!(powers_of_2(1), vec![1]);
assert_eq!(powers_of_2(2), vec![2]);
assert_eq!(powers_of_2(3), vec![2, 1]);
assert_eq!(powers_of_2(4), vec![4]);
assert_eq!(powers_of_2(5), vec![4, 1]);
assert_eq!(powers_of_2(6), vec![4, 2]);
assert_eq!(powers_of_2(7), vec![4, 2, 1]);
assert_eq!(powers_of_2(8), vec![8]);
assert_eq!(powers_of_2(9), vec![8, 1]);
assert_eq!(powers_of_2(10), vec![8, 2]);
assert_eq!(powers_of_2(11), vec![8, 2, 1]);
assert_eq!(powers_of_2(12), vec![8, 4]);
assert_eq!(powers_of_2(13), vec![8, 4, 1]);
assert_eq!(powers_of_2(14), vec![8, 4, 2]);
assert_eq!(powers_of_2(15), vec![8, 4, 2, 1]);
assert_eq!(powers_of_2(16), vec![16]);
}
#[test]
fn test_get_subtree_root_index() {
// For tree of 8 leaves
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(0, 2),
0
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(2, 4),
1
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(0, 4),
2
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(4, 6),
3
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(6, 8),
4
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(4, 8),
5
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(0, 8),
6
);
// For tree of 16 leaves
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(8, 10),
7
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(10, 12),
8
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(8, 12),
9
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(12, 14),
10
);
assert_eq!(
CompactMerkleTree::<&str, Vec<u8>, Sha256Hasher>::get_subtree_root_index(14, 16),