penumbra_tct/
index.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
//! Types to distinguish between different kinds of indices, to prevent them from being confused for
//! each other internally.
//!
//! Methods that take `Into<u64>` as an index argument can be given types from the [`within`]
//! module, which are all `Into<u64>`. They can be constructed from types in this module, which are
//! all `From<u16>`.

use serde::{Deserialize, Serialize};

/// The index of an individual item in a block.
///
/// Create this using `From<u16>`.
#[derive(
    Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Derivative, Serialize, Deserialize,
)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(proptest_derive::Arbitrary))]
#[derivative(Debug = "transparent")]
pub struct Commitment(u16);

impl Commitment {
    /// Increment the commitment.
    pub fn increment(&mut self) {
        self.0
            .checked_add(1)
            .expect("block index should never overflow");
    }

    /// The maximum representable commitment index.
    pub const MAX: Self = Self(u16::MAX);
}

impl From<u16> for Commitment {
    fn from(index: u16) -> Self {
        Self(index)
    }
}

impl From<Commitment> for u16 {
    fn from(commitment: Commitment) -> Self {
        commitment.0
    }
}

/// The index of an individual block in an epoch.
///
/// Create this using `From<u16>`.
#[derive(
    Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Derivative, Serialize, Deserialize,
)]
#[derivative(Debug = "transparent")]
#[cfg_attr(any(test, feature = "arbitrary"), derive(proptest_derive::Arbitrary))]
pub struct Block(u16);

impl From<u16> for Block {
    fn from(index: u16) -> Self {
        Self(index)
    }
}

impl From<Block> for u16 {
    fn from(block: Block) -> Self {
        block.0
    }
}

impl Block {
    /// Increment the block.
    pub fn increment(&mut self) {
        self.0
            .checked_add(1)
            .expect("block index should never overflow");
    }

    /// The maximum representable block index.
    pub const MAX: Self = Self(u16::MAX);
}

/// The index of an individual epoch in a tree.
///
/// Create this using `From<u16>`.
#[derive(
    Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Derivative, Serialize, Deserialize,
)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(proptest_derive::Arbitrary))]
#[derivative(Debug = "transparent")]
pub struct Epoch(u16);

impl From<u16> for Epoch {
    fn from(index: u16) -> Self {
        Self(index)
    }
}

impl From<Epoch> for u16 {
    fn from(epoch: Epoch) -> Self {
        epoch.0
    }
}

impl Epoch {
    /// Increment the epoch.
    pub fn increment(&mut self) {
        self.0
            .checked_add(1)
            .expect("block index should never overflow");
    }

    /// The maximum epoch index representable.
    pub const MAX: Self = Self(u16::MAX);
}

/// Indices of individual items within larger structures.
pub mod within {
    use super::*;

    /// The index of an individual item within a block.
    #[derive(
        Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
    )]
    #[cfg_attr(any(test, feature = "arbitrary"), derive(proptest_derive::Arbitrary))]
    pub struct Block {
        /// The index of the item within its block.
        pub commitment: super::Commitment,
    }

    impl Block {
        /// The maximum representable index within a block.
        pub const MAX: Self = Self {
            commitment: Commitment::MAX,
        };
    }

    impl From<Block> for u16 {
        fn from(
            Block {
                commitment: Commitment(item),
            }: Block,
        ) -> Self {
            item
        }
    }

    impl From<u16> for Block {
        fn from(position: u16) -> Self {
            Self {
                commitment: Commitment(position),
            }
        }
    }

    impl From<Block> for u64 {
        fn from(block: Block) -> Self {
            u16::from(block) as u64
        }
    }

    /// The index of an individual item within an epoch.
    #[derive(
        Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
    )]
    #[cfg_attr(any(test, feature = "arbitrary"), derive(proptest_derive::Arbitrary))]
    pub struct Epoch {
        /// The index of the block within its epoch.
        pub block: super::Block,
        /// The index of the item within its block.
        pub commitment: super::Commitment,
    }

    impl Epoch {
        /// The maximum representable index within an epoch.
        pub const MAX: Self = Self {
            block: super::Block::MAX,
            commitment: Commitment::MAX,
        };
    }

    impl From<Epoch> for u32 {
        fn from(
            Epoch {
                block: super::Block(block),
                commitment: Commitment(item),
            }: Epoch,
        ) -> Self {
            ((block as u32) << 16) | item as u32
        }
    }

    impl From<u32> for Epoch {
        fn from(position: u32) -> Self {
            let block = (position >> 16) as u16;
            let commitment = position as u16;
            Self {
                block: super::Block(block),
                commitment: Commitment(commitment),
            }
        }
    }

    impl From<Epoch> for u64 {
        fn from(epoch: Epoch) -> Self {
            u32::from(epoch) as u64
        }
    }

    /// The index of an individual item within a tree.
    #[derive(
        Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
    )]
    #[cfg_attr(any(test, feature = "arbitrary"), derive(proptest_derive::Arbitrary))]
    pub struct Tree {
        /// The index of the epoch within its tree.
        pub epoch: super::Epoch,
        /// The index of the block within its epoch.
        pub block: super::Block,
        /// The index of the item within its block.
        pub commitment: super::Commitment,
    }

    impl Tree {
        /// The maximum representable index within a tree.
        pub const MAX: Self = Self {
            epoch: super::Epoch::MAX,
            block: super::Block::MAX,
            commitment: Commitment::MAX,
        };
    }

    impl From<Tree> for u64 {
        fn from(
            Tree {
                epoch: super::Epoch(epoch),
                block: super::Block(block),
                commitment: super::Commitment(item),
            }: Tree,
        ) -> Self {
            ((epoch as u64) << 32) | ((block as u64) << 16) | item as u64
        }
    }

    impl From<u64> for Tree {
        fn from(position: u64) -> Self {
            let epoch = (position >> 32) as u16;
            let block = (position >> 16) as u16;
            let commitment = position as u16;
            Self {
                epoch: super::Epoch(epoch),
                block: super::Block(block),
                commitment: super::Commitment(commitment),
            }
        }
    }
}

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

    proptest! {
        #[test]
        fn u64_convert_eternity_inverse(e in 0u16..u16::MAX, b in 0u16..u16::MAX, c in 0u16..u16::MAX) {
            let tree = within::Tree { epoch: e.into(), block: b.into(), commitment: c.into() };
            let position: u64 = tree.into();
            let back_again = position.into();
            assert_eq!(tree, back_again);
        }

        #[test]
        fn u32_convert_epoch_inverse(b in 0u16..u16::MAX, c in 0u16..u16::MAX) {
            let epoch = within::Epoch { block: b.into(), commitment: c.into() };
            let position: u32 = epoch.into();
            let back_again = position.into();
            assert_eq!(epoch, back_again);
        }

        #[test]
        fn u16_convert_block_inverse(c in 0u16..u16::MAX) {
            let block = within::Block { commitment: c.into() };
            let position: u16 = block.into();
            let back_again = position.into();
            assert_eq!(block, back_again);
        }
    }
}