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
use crate::{Limb, LimbUInt, UInt};

/// [`UInt`] decoder.
#[derive(Clone, Debug)]
pub(super) struct Decoder<const LIMBS: usize> {
    /// Limbs being decoded.
    ///
    /// Stored from least significant to most significant.
    limbs: [Limb; LIMBS],

    /// Current limb being decoded.
    index: usize,

    /// Total number of bytes consumed.
    bytes: usize,
}

impl<const LIMBS: usize> Decoder<LIMBS> {
    /// Create a new decoder.
    pub const fn new() -> Self {
        Self {
            limbs: [Limb::ZERO; LIMBS],
            index: 0,
            bytes: 0,
        }
    }

    /// Add a byte onto the [`UInt`] being decoded.
    pub const fn add_byte(mut self, byte: u8) -> Self {
        if self.bytes == Limb::BYTE_SIZE {
            const_assert!(self.index < LIMBS, "too many bytes in UInt");
            self.index += 1;
            self.bytes = 0;
        }

        self.limbs[self.index].0 |= (byte as LimbUInt) << (self.bytes * 8);
        self.bytes += 1;
        self
    }

    /// Finish decoding a [`UInt`], returning a decoded value only if we've
    /// received the expected number of bytes.
    pub const fn finish(self) -> UInt<LIMBS> {
        const_assert!(self.index == LIMBS - 1, "decoded UInt is missing limbs");
        const_assert!(
            self.bytes == Limb::BYTE_SIZE,
            "decoded UInt is missing bytes"
        );
        UInt { limbs: self.limbs }
    }
}

impl<const LIMBS: usize> Default for Decoder<LIMBS> {
    fn default() -> Self {
        Self::new()
    }
}