Skip to main content

crypto_bigint/int/
encoding.rs

1//! Const-friendly decoding/encoding operations for [`Int`].
2
3use crate::{EncodedUint, Encoding, Int, Uint};
4
5impl<const LIMBS: usize> Int<LIMBS> {
6    /// Create a new [`Int`] from the provided big endian hex string.
7    ///
8    /// See [`Uint::from_be_hex`] for more details.
9    ///
10    /// # Panics
11    /// - if the hex is malformed or not zero-padded accordingly for the size.
12    #[must_use]
13    pub const fn from_be_hex(hex: &str) -> Self {
14        Self(Uint::from_be_hex(hex))
15    }
16}
17
18impl<const LIMBS: usize> Encoding for Int<LIMBS> {
19    type Repr = EncodedUint<LIMBS>;
20
21    #[inline]
22    fn from_be_bytes(bytes: Self::Repr) -> Self {
23        Self(Uint::from_be_bytes(bytes))
24    }
25
26    #[inline]
27    fn from_le_bytes(bytes: Self::Repr) -> Self {
28        Self(Uint::from_le_bytes(bytes))
29    }
30
31    #[inline]
32    fn to_be_bytes(&self) -> Self::Repr {
33        self.0.to_be_bytes()
34    }
35
36    #[inline]
37    fn to_le_bytes(&self) -> Self::Repr {
38        self.0.to_le_bytes()
39    }
40}