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
#[cfg(test)]
mod nibble_path_test;
use crate::nibble::{Nibble, ROOT_NIBBLE_HEIGHT};
use mirai_annotations::*;
#[cfg(any(test, feature = "fuzzing"))]
use proptest::{collection::vec, prelude::*};
use serde::{Deserialize, Serialize};
use std::{fmt, iter::FromIterator};
#[derive(Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub struct NibblePath {
num_nibbles: usize,
bytes: Vec<u8>,
}
impl fmt::Debug for NibblePath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.nibbles().try_for_each(|x| write!(f, "{:x}", x))
}
}
impl FromIterator<Nibble> for NibblePath {
fn from_iter<I: IntoIterator<Item = Nibble>>(iter: I) -> Self {
let mut nibble_path = NibblePath::new(vec![]);
for nibble in iter {
nibble_path.push(nibble);
}
nibble_path
}
}
#[cfg(any(test, feature = "fuzzing"))]
impl Arbitrary for NibblePath {
type Parameters = ();
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
arb_nibble_path().boxed()
}
type Strategy = BoxedStrategy<Self>;
}
#[cfg(any(test, feature = "fuzzing"))]
prop_compose! {
fn arb_nibble_path()(
mut bytes in vec(any::<u8>(), 0..=ROOT_NIBBLE_HEIGHT/2),
is_odd in any::<bool>()
) -> NibblePath {
if let Some(last_byte) = bytes.last_mut() {
if is_odd {
*last_byte &= 0xf0;
return NibblePath::new_odd(bytes);
}
}
NibblePath::new(bytes)
}
}
#[cfg(any(test, feature = "fuzzing"))]
prop_compose! {
fn arb_internal_nibble_path()(
nibble_path in arb_nibble_path().prop_filter(
"Filter out leaf paths.",
|p| p.num_nibbles() < ROOT_NIBBLE_HEIGHT,
)
) -> NibblePath {
nibble_path
}
}
impl NibblePath {
pub fn new(bytes: Vec<u8>) -> Self {
checked_precondition!(bytes.len() <= ROOT_NIBBLE_HEIGHT / 2);
let num_nibbles = bytes.len() * 2;
NibblePath { num_nibbles, bytes }
}
pub fn new_odd(bytes: Vec<u8>) -> Self {
checked_precondition!(bytes.len() <= ROOT_NIBBLE_HEIGHT / 2);
assert_eq!(
bytes.last().expect("Should have odd number of nibbles.") & 0x0f,
0,
"Last nibble must be 0."
);
let num_nibbles = bytes.len() * 2 - 1;
NibblePath { num_nibbles, bytes }
}
pub fn push(&mut self, nibble: Nibble) {
assert!(ROOT_NIBBLE_HEIGHT > self.num_nibbles);
if self.num_nibbles % 2 == 0 {
self.bytes.push(u8::from(nibble) << 4);
} else {
self.bytes[self.num_nibbles / 2] |= u8::from(nibble);
}
self.num_nibbles += 1;
}
pub fn pop(&mut self) -> Option<Nibble> {
let poped_nibble = if self.num_nibbles % 2 == 0 {
self.bytes.last_mut().map(|last_byte| {
let nibble = *last_byte & 0x0f;
*last_byte &= 0xf0;
Nibble::from(nibble)
})
} else {
self.bytes.pop().map(|byte| Nibble::from(byte >> 4))
};
if poped_nibble.is_some() {
self.num_nibbles -= 1;
}
poped_nibble
}
pub fn last(&self) -> Option<Nibble> {
let last_byte_option = self.bytes.last();
if self.num_nibbles % 2 == 0 {
last_byte_option.map(|last_byte| Nibble::from(*last_byte & 0x0f))
} else {
let last_byte = last_byte_option.expect("Last byte must exist if num_nibbles is odd.");
Some(Nibble::from(*last_byte >> 4))
}
}
fn get_bit(&self, i: usize) -> bool {
assert!(i < self.num_nibbles * 4);
let pos = i / 8;
let bit = 7 - i % 8;
((self.bytes[pos] >> bit) & 1) != 0
}
pub fn get_nibble(&self, i: usize) -> Nibble {
assert!(i < self.num_nibbles);
Nibble::from((self.bytes[i / 2] >> (if i % 2 == 1 { 0 } else { 4 })) & 0xf)
}
pub fn bits(&self) -> BitIterator {
assume!(self.num_nibbles <= ROOT_NIBBLE_HEIGHT); BitIterator {
nibble_path: self,
pos: (0..self.num_nibbles * 4),
}
}
pub fn nibbles(&self) -> NibbleIterator {
assume!(self.num_nibbles <= ROOT_NIBBLE_HEIGHT); NibbleIterator::new(self, 0, self.num_nibbles)
}
pub fn num_nibbles(&self) -> usize {
self.num_nibbles
}
pub fn is_empty(&self) -> bool {
self.num_nibbles() == 0
}
pub fn bytes(&self) -> &[u8] {
&self.bytes
}
}
pub trait Peekable: Iterator {
fn peek(&self) -> Option<Self::Item>;
}
pub struct BitIterator<'a> {
nibble_path: &'a NibblePath,
pos: std::ops::Range<usize>,
}
impl<'a> Peekable for BitIterator<'a> {
fn peek(&self) -> Option<Self::Item> {
if self.pos.start < self.pos.end {
Some(self.nibble_path.get_bit(self.pos.start))
} else {
None
}
}
}
impl<'a> Iterator for BitIterator<'a> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
self.pos.next().map(|i| self.nibble_path.get_bit(i))
}
}
impl<'a> DoubleEndedIterator for BitIterator<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.pos.next_back().map(|i| self.nibble_path.get_bit(i))
}
}
#[derive(Debug)]
pub struct NibbleIterator<'a> {
nibble_path: &'a NibblePath,
pos: std::ops::Range<usize>,
start: usize,
}
impl<'a> Iterator for NibbleIterator<'a> {
type Item = Nibble;
fn next(&mut self) -> Option<Self::Item> {
self.pos.next().map(|i| self.nibble_path.get_nibble(i))
}
}
impl<'a> Peekable for NibbleIterator<'a> {
fn peek(&self) -> Option<Self::Item> {
if self.pos.start < self.pos.end {
Some(self.nibble_path.get_nibble(self.pos.start))
} else {
None
}
}
}
impl<'a> NibbleIterator<'a> {
fn new(nibble_path: &'a NibblePath, start: usize, end: usize) -> Self {
precondition!(start <= end);
precondition!(start <= ROOT_NIBBLE_HEIGHT);
precondition!(end <= ROOT_NIBBLE_HEIGHT);
Self {
nibble_path,
pos: (start..end),
start,
}
}
pub fn visited_nibbles(&self) -> NibbleIterator<'a> {
assume!(self.start <= self.pos.start); assume!(self.pos.start <= ROOT_NIBBLE_HEIGHT); Self::new(self.nibble_path, self.start, self.pos.start)
}
pub fn remaining_nibbles(&self) -> NibbleIterator<'a> {
assume!(self.pos.start <= self.pos.end); assume!(self.pos.end <= ROOT_NIBBLE_HEIGHT); Self::new(self.nibble_path, self.pos.start, self.pos.end)
}
pub fn bits(&self) -> BitIterator<'a> {
assume!(self.pos.start <= self.pos.end); assume!(self.pos.end <= ROOT_NIBBLE_HEIGHT); BitIterator {
nibble_path: self.nibble_path,
pos: (self.pos.start * 4..self.pos.end * 4),
}
}
pub fn get_nibble_path(&self) -> NibblePath {
self.visited_nibbles()
.chain(self.remaining_nibbles())
.collect()
}
pub fn num_nibbles(&self) -> usize {
assume!(self.start <= self.pos.end); self.pos.end - self.start
}
pub fn is_finished(&self) -> bool {
self.peek().is_none()
}
}
pub fn skip_common_prefix<I1, I2>(x: &mut I1, y: &mut I2) -> usize
where
I1: Iterator + Peekable,
I2: Iterator + Peekable,
<I1 as Iterator>::Item: std::cmp::PartialEq<<I2 as Iterator>::Item>,
{
let mut count = 0;
loop {
let x_peek = x.peek();
let y_peek = y.peek();
if x_peek.is_none()
|| y_peek.is_none()
|| x_peek.expect("cannot be none") != y_peek.expect("cannot be none")
{
break;
}
count += 1;
x.next();
y.next();
}
count
}