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
use std::hash::{Hash, Hasher};
use std::cmp::Ordering;
use ring::digest::Algorithm;
use tree::{Tree, LeavesIterator, LeavesIntoIterator};
use hashutils::{Hashable, HashUtils};
use proof::{Proof, Lemma};
#[derive(Clone, Debug)]
pub struct MerkleTree<T> {
pub algorithm: &'static Algorithm,
root: Tree<T>,
height: usize,
count: usize,
}
impl<T: PartialEq> PartialEq for MerkleTree<T> {
#[allow(trivial_casts)]
fn eq(&self, other: &MerkleTree<T>) -> bool {
self.root == other.root && self.height == other.height && self.count == other.count &&
(self.algorithm as *const Algorithm) == (other.algorithm as *const Algorithm)
}
}
impl<T: Eq> Eq for MerkleTree<T> {}
impl<T: Ord> PartialOrd for MerkleTree<T> {
fn partial_cmp(&self, other: &MerkleTree<T>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: Ord> Ord for MerkleTree<T> {
#[allow(trivial_casts)]
fn cmp(&self, other: &MerkleTree<T>) -> Ordering {
self.height
.cmp(&other.height)
.then(self.count.cmp(&other.count))
.then((self.algorithm as *const Algorithm).cmp(
&(other.algorithm as
*const Algorithm),
))
.then_with(|| self.root.cmp(&other.root))
}
}
impl<T: Hash> Hash for MerkleTree<T> {
#[allow(trivial_casts)]
fn hash<H: Hasher>(&self, state: &mut H) {
<Tree<T> as Hash>::hash(&self.root, state);
self.height.hash(state);
self.count.hash(state);
(self.algorithm as *const Algorithm).hash(state);
}
}
impl<T> MerkleTree<T> {
pub fn from_vec(algorithm: &'static Algorithm, values: Vec<T>) -> Self
where
T: Hashable,
{
if values.is_empty() {
return MerkleTree {
algorithm: algorithm,
root: Tree::empty(algorithm.hash_empty()),
height: 0,
count: 0,
};
}
let count = values.len();
let mut height = 0;
let mut cur = Vec::with_capacity(count);
for v in values {
let leaf = Tree::new_leaf(algorithm, v);
cur.push(leaf);
}
while cur.len() > 1 {
let mut next = Vec::new();
while !cur.is_empty() {
if cur.len() == 1 {
next.push(cur.remove(0));
} else {
let left = cur.remove(0);
let right = cur.remove(0);
let combined_hash = algorithm.hash_nodes(left.hash(), right.hash());
let node = Tree::Node {
hash: combined_hash.as_ref().into(),
left: Box::new(left),
right: Box::new(right),
};
next.push(node);
}
}
height += 1;
cur = next;
}
debug_assert!(cur.len() == 1);
let root = cur.remove(0);
MerkleTree {
algorithm: algorithm,
root: root,
height: height,
count: count,
}
}
pub fn root_hash(&self) -> &Vec<u8> {
self.root.hash()
}
pub fn height(&self) -> usize {
self.height
}
pub fn count(&self) -> usize {
self.count
}
pub fn is_empty(&self) -> bool {
self.count() == 0
}
pub fn gen_proof(&self, value: T) -> Option<Proof<T>>
where
T: Hashable,
{
let root_hash = self.root_hash().clone();
let leaf_hash = self.algorithm.hash_leaf(&value);
Lemma::new(&self.root, leaf_hash.as_ref()).map(|lemma| {
Proof::new(self.algorithm, root_hash, lemma, value)
})
}
pub fn iter(&self) -> LeavesIterator<T> {
self.root.iter()
}
}
impl<T> IntoIterator for MerkleTree<T> {
type Item = T;
type IntoIter = LeavesIntoIterator<T>;
fn into_iter(self) -> Self::IntoIter {
self.root.into_iter()
}
}
impl<'a, T> IntoIterator for &'a MerkleTree<T> {
type Item = &'a T;
type IntoIter = LeavesIterator<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.root.iter()
}
}