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
use crate::common::{
    node::{
        Node,
        ParentNode,
    },
    Bytes8,
    PositionPath,
};
use core::convert::Infallible;

use super::{
    node::{
        ChildError,
        ChildResult,
    },
    path::Side,
};

/// # Position
///
/// A `Position` represents a node's position in a binary tree by encapsulating
/// the node's index data. Indices are calculated through in-order traversal of
/// the nodes, starting with the first leaf node. Indexing starts at 0.
///
/// Merkle Trees
///
/// In the context of Merkle trees, trees are constructed "upwards" from leaf
/// nodes. Therefore, indexing is done from the bottom up, starting with the
/// leaves, rather than top down, starting with the root, and we can guarantee a
/// deterministic construction of index data.
///
/// ```text
///               07
///              /  \
///             /    \
///            /      \
///           /        \
///          /          \
///         /            \
///       03              11
///      /  \            /  \
///     /    \          /    \
///   01      05      09      13
///  /  \    /  \    /  \    /  \
/// 00  02  04  06  08  10  12  14
/// ```
///
/// In-order indices can be considered internal to the `Position` struct and are
/// used to facilitate the calculation of positional attributes and the
/// construction of other nodes. Leaf nodes have both an in-order index as part
/// of the tree, and a leaf index determined by its position in the bottom row.
/// Because of the in-order traversal used to calculate the in-order indices,
/// leaf nodes have the property that their in-order index is always equal to
/// their leaf index multiplied by 2.
///
/// ```text
///                    /  \    /  \    /  \    /  \
///     Leaf indices: 00  01  02  03  04  05  06  07
/// In-order indices: 00  02  04  06  08  10  12  14
/// ```
///
/// This allows us to construct a `Position` (and its in-order index) by
/// providing either an in-order index directly or, in the case of a leaf, a
/// leaf index. This functionality is captured by `from_in_order_index()` and
/// `from_leaf_index()` respectively.
///
/// Traversal of a Merkle Tree can be performed by the methods on a given
/// `Position` to retrieve its sibling, parent, or uncle `Position`.
///
/// Merkle Mountain Ranges
///
/// Because the `Position` indices are calculated from in-order traversal
/// starting with the leaves, the deterministic quality of the indices holds
/// true for imbalanced binary trees, including Merkle Mountain Ranges. Consider
/// the following binary tree construction composed of seven leaves (with leaf
/// indices 0 through 6):
///
/// ```text
///       03
///      /  \
///     /    \
///   01      05      09
///  /  \    /  \    /  \
/// 00  02  04  06  08  10  12
/// ```
///
/// Note the absence of internal nodes that would be present in a fully balanced
/// tree: inner nodes with indices 7 and 11 are absent. This is owing to the
/// fact that node indices are calculated deterministically through in-order
/// traversal, not calculated as a sequence.
///
/// Traversal of a Merkle Mountain Range is still done in the same manner as a
/// balanced Merkle tree, using methods to retrieve a `Position's` sibling,
/// parent, or uncle `Position`. However, in such cases, the corresponding
/// sibling or uncle nodes are not guaranteed to exist in the tree.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Position(u64);

impl Position {
    pub fn in_order_index(self) -> u64 {
        self.0
    }

    pub fn leaf_index(self) -> u64 {
        assert!(self.is_leaf());
        self.in_order_index() / 2
    }

    /// Construct a position from an in-order index.
    pub fn from_in_order_index(index: u64) -> Self {
        Position(index)
    }

    /// Construct a position from a leaf index. The in-order index corresponding
    /// to the leaf index will always equal the leaf index multiplied by 2.
    /// Panics if index is too large to fit into u64.
    pub fn from_leaf_index(index: u64) -> Option<Self> {
        Some(Position(index.checked_mul(2)?))
    }

    #[cfg(test)]
    pub(crate) fn from_leaf_index_unwrap(index: u64) -> Self {
        Self::from_leaf_index(index).expect("Index too large")
    }

    /// The parent position.
    /// The parent position has a height less 1 relative to this position.
    pub fn parent(self) -> Result<Self, GetNodeError> {
        let shift = 1u64
            .checked_shl(self.height())
            .ok_or(GetNodeError::CannotExist)?;
        let this = self.in_order_index();
        Ok(Self::from_in_order_index(match self.orientation()? {
            Side::Left => this.checked_sub(shift).ok_or(GetNodeError::CannotExist)?,
            Side::Right => this.checked_add(shift).ok_or(GetNodeError::CannotExist)?,
        }))
    }

    /// The sibling position.
    /// A position shares the same parent and height as its sibling.
    #[cfg(test)]
    pub fn sibling(self) -> Result<Self, GetNodeError> {
        #[allow(clippy::arithmetic_side_effects)] // height() <= 64
        let shift = 1u64
            .checked_shl(self.height() + 1)
            .ok_or(GetNodeError::CannotExist)?;
        let this = self.in_order_index();
        Ok(Self::from_in_order_index(match self.orientation()? {
            Side::Left => this.checked_sub(shift).ok_or(GetNodeError::CannotExist)?,
            Side::Right => this.checked_add(shift).ok_or(GetNodeError::CannotExist)?,
        }))
    }

    /// The uncle position.
    /// The uncle position is the sibling of the parent and has a height less 1
    /// relative to this position.
    #[cfg(test)]
    pub fn uncle(self) -> Result<Self, GetNodeError> {
        self.parent()?.sibling()
    }

    /// The child position of the current position given by the direction.
    /// A child position has a height less 1 than the current position.
    ///
    /// A child position is calculated as a function of the current position's
    /// index and height, and the supplied direction. The left child
    /// position has the in-order index arriving before the current index;
    /// the right child position has the in-order index arriving after the
    /// current index.
    pub fn child(self, side: Side) -> Result<Self, GetNodeError> {
        if !self.is_node() {
            return Err(GetNodeError::IsLeaf);
        }
        let shift = 1u64
            .checked_shl(
                self.height()
                    .checked_sub(1)
                    .ok_or(GetNodeError::CannotExist)?,
            )
            .ok_or(GetNodeError::CannotExist)?;
        let this = self.in_order_index();
        Ok(Self::from_in_order_index(match side {
            Side::Left => this.checked_sub(shift).ok_or(GetNodeError::CannotExist)?,
            Side::Right => this.checked_add(shift).ok_or(GetNodeError::CannotExist)?,
        }))
    }

    /// Returns the left child of the current position.
    pub fn left_child(self) -> Result<Self, GetNodeError> {
        self.child(Side::Left)
    }

    /// Returns the right child of the current position.
    pub fn right_child(self) -> Result<Self, GetNodeError> {
        self.child(Side::Right)
    }

    /// The height of the index in a binary tree.
    /// Leaf nodes represent height 0. A leaf's parent represents height 1.
    /// Height values monotonically increase as you ascend the tree.
    ///
    /// Height is deterministically calculated as the number of trailing zeros
    /// of the complement of the position's index. The following table
    /// demonstrates the relationship between a position's height and the
    /// trailing zeros.
    ///
    /// | Index (Dec) | Index (Bin) | !Index (Bin) | Trailing 0s | Height |
    /// |-------------|-------------|--------------|-------------|--------|
    /// |           0 |        0000 |         1111 |           0 |      0 |
    /// |           2 |        0010 |         1101 |           0 |      0 |
    /// |           4 |        0100 |         1011 |           0 |      0 |
    /// |           1 |        0001 |         1110 |           1 |      1 |
    /// |           5 |        0101 |         1010 |           1 |      1 |
    /// |           9 |        1001 |         0110 |           1 |      1 |
    /// |           3 |        0011 |         1100 |           2 |      2 |
    /// |          11 |        1011 |         0100 |           2 |      2 |
    pub fn height(self) -> u32 {
        (!self.in_order_index()).trailing_zeros()
    }

    /// Whether or not this position represents a leaf node.
    /// Returns `true` if the position is a leaf node.
    /// Returns `false` if the position is an internal node.
    ///
    /// A position is a leaf node if and only if its in-order index is even. A
    /// position is an internal node if and only if its in-order index is
    /// odd.
    pub fn is_leaf(self) -> bool {
        self.in_order_index() % 2 == 0
    }

    /// Whether or not this position represents an internal node.
    /// Returns `false` if the position is a leaf node.
    /// Returns `true` if the position is an internal node.
    ///
    /// When a position is an internal node, the position will have both a left
    /// and right child.
    pub fn is_node(self) -> bool {
        !self.is_leaf()
    }

    /// Given a leaf position and the total count of leaves in a tree, get the
    /// path from this position to the given leaf position. The shape of the
    /// tree is defined by the `leaves_count` parameter and constrains the
    /// path. See [PositionPath](crate::common::PositionPath).
    pub fn path(self, leaf: &Self, leaves_count: u64) -> PositionPath {
        debug_assert!(leaves_count > 0);
        PositionPath::new(self, *leaf, leaves_count)
    }

    /// Orientation of the position index relative to its parent.
    ///
    /// The orientation is determined by the reading the `n`th rightmost digit
    /// of the index's binary value, where `n` = the height of the position
    /// `+ 1`. The following table demonstrates the relationships between a
    /// position's index, height, and orientation.
    ///
    /// | Index (Dec) | Index (Bin) | Height | Orientation |
    /// |-------------|-------------|--------|-------------|
    /// |           0 |        0000 |      0 |           L |
    /// |           2 |        0010 |      0 |           R |
    /// |           4 |        0100 |      0 |           L |
    /// |           6 |        0110 |      0 |           R |
    /// |           1 |        0001 |      1 |           L |
    /// |           5 |        0101 |      1 |           R |
    /// |           9 |        1001 |      1 |           L |
    /// |          13 |        1101 |      1 |           R |
    fn orientation(self) -> Result<Side, GetNodeError> {
        #[allow(clippy::arithmetic_side_effects)] // height() <= 64
        let shift = 1u64
            .checked_shl(self.height() + 1)
            .ok_or(GetNodeError::CannotExist)?;
        Ok(match self.in_order_index() & shift {
            0 => Side::Right,
            _ => Side::Left,
        })
    }
}

impl Node for Position {
    type Key = Bytes8;

    fn height(&self) -> u32 {
        Position::height(*self)
    }

    #[allow(clippy::arithmetic_side_effects, clippy::cast_possible_truncation)] // const
    fn key_size_bits() -> u32 {
        core::mem::size_of::<Self::Key>() as u32 * 8
    }

    fn leaf_key(&self) -> Self::Key {
        Position::leaf_index(*self).to_be_bytes()
    }

    fn is_leaf(&self) -> bool {
        Position::is_leaf(*self)
    }

    fn is_node(&self) -> bool {
        Position::is_node(*self)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GetNodeError {
    /// The operation requires a node that can have children.
    /// This is a leaf, and cannot have children.
    IsLeaf,
    /// The requested node cannot exists as it would be out of bounds.
    CannotExist,
}

impl ParentNode for Position {
    type Error = Infallible;

    fn left_child(&self) -> ChildResult<Self> {
        match self.child(Side::Left) {
            Ok(child) => Ok(child),
            Err(GetNodeError::IsLeaf) => Err(ChildError::NodeIsLeaf),
            Err(GetNodeError::CannotExist) => Err(ChildError::ChildCannotExist),
        }
    }

    fn right_child(&self) -> ChildResult<Self> {
        match self.child(Side::Right) {
            Ok(child) => Ok(child),
            Err(GetNodeError::IsLeaf) => Err(ChildError::NodeIsLeaf),
            Err(GetNodeError::CannotExist) => Err(ChildError::ChildCannotExist),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_from_in_order_index() {
        assert_eq!(Position::from_in_order_index(0).in_order_index(), 0);
        assert_eq!(Position::from_in_order_index(1).in_order_index(), 1);
        assert_eq!(Position::from_in_order_index(!0u64).in_order_index(), !0u64);
    }

    #[test]
    fn test_from_leaf_index_unwrap() {
        assert_eq!(Position::from_leaf_index_unwrap(0).in_order_index(), 0);
        assert_eq!(Position::from_leaf_index_unwrap(1).in_order_index(), 2);
        assert_eq!(
            Position::from_leaf_index_unwrap((!0u64) >> 1).in_order_index(),
            !0u64 - 1
        );
    }

    #[test]
    fn test_equality_returns_true_for_two_equal_positions() {
        assert_eq!(Position(0), Position(0));
        assert_eq!(Position::from_in_order_index(0), Position(0));
        assert_eq!(Position::from_leaf_index_unwrap(1), Position(2));
    }

    #[test]
    fn test_equality_returns_false_for_two_unequal_positions() {
        assert_ne!(Position(0), Position(1));
        assert_ne!(Position::from_in_order_index(0), Position(1));
        assert_ne!(Position::from_leaf_index_unwrap(0), Position(2));
    }

    #[test]
    fn test_height() {
        assert_eq!(Position(0).height(), 0);
        assert_eq!(Position(2).height(), 0);
        assert_eq!(Position(4).height(), 0);

        assert_eq!(Position(1).height(), 1);
        assert_eq!(Position(5).height(), 1);
        assert_eq!(Position(9).height(), 1);

        assert_eq!(Position(3).height(), 2);
        assert_eq!(Position(11).height(), 2);
        assert_eq!(Position(19).height(), 2);
    }

    #[test]
    fn test_sibling() {
        assert_eq!(Position(0).sibling(), Ok(Position(2)));
        assert_eq!(Position(2).sibling(), Ok(Position(0)));

        assert_eq!(Position(1).sibling(), Ok(Position(5)));
        assert_eq!(Position(5).sibling(), Ok(Position(1)));

        assert_eq!(Position(3).sibling(), Ok(Position(11)));
        assert_eq!(Position(11).sibling(), Ok(Position(3)));
    }

    #[test]
    fn test_parent() {
        assert_eq!(Position(0).parent(), Ok(Position(1)));
        assert_eq!(Position(2).parent(), Ok(Position(1)));

        assert_eq!(Position(1).parent(), Ok(Position(3)));
        assert_eq!(Position(5).parent(), Ok(Position(3)));

        assert_eq!(Position(3).parent(), Ok(Position(7)));
        assert_eq!(Position(11).parent(), Ok(Position(7)));
    }

    #[test]
    fn test_uncle() {
        assert_eq!(Position(0).uncle(), Ok(Position(5)));
        assert_eq!(Position(2).uncle(), Ok(Position(5)));
        assert_eq!(Position(4).uncle(), Ok(Position(1)));
        assert_eq!(Position(6).uncle(), Ok(Position(1)));

        assert_eq!(Position(1).uncle(), Ok(Position(11)));
        assert_eq!(Position(5).uncle(), Ok(Position(11)));
        assert_eq!(Position(9).uncle(), Ok(Position(3)));
        assert_eq!(Position(13).uncle(), Ok(Position(3)));
    }

    #[test]
    fn test_left_child() {
        assert_eq!(Position(7).left_child(), Ok(Position(3)));
        assert_eq!(Position(3).left_child(), Ok(Position(1)));
        assert_eq!(Position(1).left_child(), Ok(Position(0)));
        assert_eq!(Position(11).left_child(), Ok(Position(9)));
        assert_eq!(Position(9).left_child(), Ok(Position(8)));
    }

    #[test]
    fn test_right_child() {
        assert_eq!(Position(7).right_child(), Ok(Position(11)));
        assert_eq!(Position(3).right_child(), Ok(Position(5)));
        assert_eq!(Position(1).right_child(), Ok(Position(2)));
        assert_eq!(Position(11).right_child(), Ok(Position(13)));
        assert_eq!(Position(9).right_child(), Ok(Position(10)));
    }

    #[test]
    fn test_is_leaf() {
        assert_eq!(Position(0).is_leaf(), true);
        assert_eq!(Position(2).is_leaf(), true);
        assert_eq!(Position(4).is_leaf(), true);
        assert_eq!(Position(6).is_leaf(), true);

        assert_eq!(Position(1).is_leaf(), false);
        assert_eq!(Position(5).is_leaf(), false);
        assert_eq!(Position(9).is_leaf(), false);
        assert_eq!(Position(13).is_leaf(), false);
    }

    #[test]
    fn test_is_node() {
        assert_eq!(Position(0).is_node(), false);
        assert_eq!(Position(2).is_node(), false);
        assert_eq!(Position(4).is_node(), false);
        assert_eq!(Position(6).is_node(), false);

        assert_eq!(Position(1).is_node(), true);
        assert_eq!(Position(5).is_node(), true);
        assert_eq!(Position(9).is_node(), true);
        assert_eq!(Position(13).is_node(), true);
    }
}