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
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
#![feature(external_doc)]
#![doc(include = "../README.md")]
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(clippy))]
extern crate flat_tree as flat;
extern crate sparse_bitfield as bitfield;
mod proof;
mod verification;
pub use self::bitfield::{Bitfield, Change};
pub use self::proof::Proof;
pub use self::verification::Verification;
pub struct TreeIndex {
bitfield: Bitfield,
}
impl TreeIndex {
pub fn new(bitfield: Bitfield) -> Self {
TreeIndex { bitfield }
}
pub fn get(&mut self, index: usize) -> bool {
self.bitfield.get(index)
}
pub fn set(&mut self, index: usize) -> Change {
if let Change::Unchanged = self.bitfield.set(index, true) {
return Change::Unchanged;
}
let mut index = index;
while self.bitfield.get(flat::sibling(index)) {
index = flat::parent(index);
if let Change::Unchanged = self.bitfield.set(index, true) {
break;
}
}
Change::Changed
}
pub fn proof(&mut self, index: usize, nodes: Vec<usize>) -> Option<Proof> {
if !self.get(index) {
return None;
}
let mut digest = shift_right(index);
let has_root = digest & 1;
let mut sibling = index;
let mut next = index;
let mut roots = Vec::new();
while digest > 0 {
if digest == 1 && has_root > 0 {
if self.get(next) {
}
let tmp = flat::sibling(next);
if tmp > next {
next = tmp
}
flat::full_roots(flat::right_span(next) + 2, &mut roots);
for root in roots {
if self.get(root) {
}
}
break;
}
sibling = flat::sibling(next);
if is_even(digest) {
if self.get(sibling) {
}
}
next = flat::parent(next);
digest = shift_right(digest);
}
Some(Proof {
nodes: nodes,
verified_by: 0,
})
}
pub fn digest(&self) {
unimplemented!();
}
pub fn blocks(&mut self) -> usize {
let mut top = 0;
let mut next = 0;
let max = self.bitfield.len();
while flat::right_span(next) < max {
next = flat::parent(next);
if self.get(next) {
top = next;
}
}
if self.get(top) {
self.verified_by(top).node / 2
} else {
0
}
}
pub fn roots(&mut self, roots: &mut Vec<usize>) {
flat::full_roots(2 * self.blocks(), roots)
}
pub fn verified_by(&mut self, index: usize) -> Verification {
let has_index = self.get(index);
if !has_index {
return Verification {
node: 0,
top: 0,
};
}
let mut depth = flat::depth(index);
let mut top = index;
let mut parent = flat::parent_with_depth(top, depth);
depth += 1;
while self.get(parent) && self.get(flat::sibling(top)) {
top = parent;
parent = flat::parent_with_depth(top, depth);
depth += 1;
}
depth -= 1;
while depth != 0 {
top = flat::left_child_with_depth(
flat::index(depth, flat::offset_with_depth(top, depth) + 1),
depth,
).unwrap();
depth -= 1;
while !self.get(top) && depth > 0 {
top = flat::left_child_with_depth(top, depth).unwrap();
depth -= 1;
}
}
let node = if self.get(top) {
top + 2
} else {
top
};
Verification { node, top }
}
}
impl Default for TreeIndex {
fn default() -> Self {
TreeIndex {
bitfield: Bitfield::new(1024),
}
}
}
#[inline]
fn is_even(n: usize) -> bool {
match n & 1 {
0 => true,
1 => false,
_ => panic!("Bitshift failure"),
}
}
#[inline]
fn shift_right(n: usize) -> usize {
(n - (n & 1)) / 2
}
#[test]
fn shifts_bits_right() {
assert_eq!(shift_right(9), 4);
assert_eq!(shift_right(12), 6);
assert_eq!(shift_right(13), 6);
assert_eq!(shift_right(14), 7);
}