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
//! C509 Unwrapped CBOR Unsigned Bignum (~biguint)
//!
//! Please refer to [CDDL Wrapping](https://datatracker.ietf.org/doc/html/rfc8610#section-3.7)
//! for unwrapped types.

// cspell: words Bignum bignum biguint

use minicbor::{encode::Write, Decode, Decoder, Encode, Encoder};
use serde::{Deserialize, Serialize};

/// A struct representing an unwrapped CBOR unsigned bignum.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct UnwrappedBigUint(u64);

impl UnwrappedBigUint {
    /// Create a new instance of `UnwrappedBigUint`.
    #[must_use]
    pub fn new(uint: u64) -> Self {
        Self(uint)
    }
}

impl Encode<()> for UnwrappedBigUint {
    fn encode<W: Write>(
        &self, e: &mut Encoder<W>, _ctx: &mut (),
    ) -> Result<(), minicbor::encode::Error<W::Error>> {
        let bytes = self.0.to_be_bytes();
        // Trim leading zeros
        let significant_bytes = bytes
            .iter()
            .skip_while(|&&b| b == 0)
            .copied()
            .collect::<Vec<u8>>();

        e.bytes(&significant_bytes)?;
        Ok(())
    }
}

impl Decode<'_, ()> for UnwrappedBigUint {
    fn decode(d: &mut Decoder<'_>, _ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
        // Turn bytes into u64
        let b = d
            .bytes()?
            .iter()
            .fold(0, |acc, &b| (acc << 8) | u64::from(b));
        Ok(UnwrappedBigUint::new(b))
    }
}

#[cfg(test)]
mod test_big_uint {

    use super::*;

    // Test reference https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/09/
    // A.1.  Example RFC 7925 profiled X.509 Certificate
    #[test]
    fn test_encode_decode() {
        let mut buffer = Vec::new();
        let mut encoder = minicbor::Encoder::new(&mut buffer);
        // Serial Number: 128269 (0x1f50d)
        let b_uint = UnwrappedBigUint::new(128_269);
        b_uint
            .encode(&mut encoder, &mut ())
            .expect("Failed to encode UnwrappedBigUint");
        assert_eq!(hex::encode(buffer.clone()), "4301f50d");

        let mut decoder = minicbor::Decoder::new(&buffer);
        let decoded_b_uint = UnwrappedBigUint::decode(&mut decoder, &mut ())
            .expect("Failed to decode UnwrappedBigUint");

        assert_eq!(decoded_b_uint, b_uint);
    }

    // Test reference https://datatracker.ietf.org/doc/draft-ietf-cose-cbor-encoded-cert/09/
    // A.2.  Example IEEE 802.1AR profiled X.509 Certificate
    #[test]
    fn test_encode_decode_2() {
        let mut buffer = Vec::new();
        let mut encoder = minicbor::Encoder::new(&mut buffer);
        // Serial Number: 9112578475118446130 (0x7e7661d7b54e4632)
        let b_uint = UnwrappedBigUint::new(9_112_578_475_118_446_130);
        b_uint
            .encode(&mut encoder, &mut ())
            .expect("Failed to encode UnwrappedBigUint");
        assert_eq!(hex::encode(buffer.clone()), "487e7661d7b54e4632");

        let mut decoder = minicbor::Decoder::new(&buffer);
        let decoded_b_uint = UnwrappedBigUint::decode(&mut decoder, &mut ())
            .expect("Failed to decode UnwrappedBigUint");

        assert_eq!(decoded_b_uint, b_uint);
    }
}