Skip to main content

crypto_bigint/uint/
array.rs

1//! `generic-array` integration with `UInt`.
2// TODO(tarcieri): completely phase out `generic-array` when const generics are powerful enough
3
4use crate::{ArrayEncoding, ByteArray};
5use generic_array::{typenum, GenericArray};
6
7macro_rules! impl_uint_array_encoding {
8    ($(($uint:ident, $bytes:path)),+) => {
9        $(
10            #[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))]
11            impl ArrayEncoding for super::$uint {
12                type ByteSize = $bytes;
13
14                #[inline]
15                fn from_be_byte_array(bytes: ByteArray<Self>) -> Self {
16                    Self::from_be_slice(&bytes)
17                }
18
19                #[inline]
20                fn from_le_byte_array(bytes: ByteArray<Self>) -> Self {
21                    Self::from_le_slice(&bytes)
22                }
23
24                #[inline]
25                fn to_be_byte_array(&self) -> ByteArray<Self> {
26                    let mut result = GenericArray::default();
27                    self.write_be_bytes(&mut result);
28                    result
29                }
30
31                #[inline]
32                fn to_le_byte_array(&self) -> ByteArray<Self> {
33                    let mut result = GenericArray::default();
34                    self.write_le_bytes(&mut result);
35                    result
36                }
37            }
38        )+
39     };
40}
41
42// TODO(tarcieri): use `const_evaluatable_checked` when stable to make generic around bits.
43impl_uint_array_encoding! {
44    (U64, typenum::U8),
45    (U128, typenum::U16),
46    (U192, typenum::U24),
47    (U256, typenum::U32),
48    (U384, typenum::U48),
49    (U448, typenum::U56),
50    (U512, typenum::U64),
51    (U768, typenum::U96),
52    (U896, typenum::U112),
53    (U1024, typenum::U128),
54    (U1536, typenum::U192),
55    (U1792, typenum::U224),
56    (U2048, typenum::U256),
57    (U3072, typenum::U384),
58    (U3584, typenum::U448),
59    (U4096, typenum::U512),
60    (U6144, typenum::U768),
61    (U8192, typenum::U1024)
62}
63
64#[cfg(test)]
65mod tests {
66    use crate::{ArrayEncoding, Limb};
67    use hex_literal::hex;
68
69    #[cfg(target_pointer_width = "32")]
70    use crate::U64 as UIntEx;
71
72    #[cfg(target_pointer_width = "64")]
73    use crate::U128 as UIntEx;
74
75    /// Byte array that corresponds to `UIntEx`
76    type ByteArray = crate::ByteArray<UIntEx>;
77
78    #[test]
79    #[cfg(target_pointer_width = "32")]
80    fn from_be_byte_array() {
81        let n = UIntEx::from_be_byte_array(hex!("0011223344556677").into());
82        assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]);
83    }
84
85    #[test]
86    #[cfg(target_pointer_width = "64")]
87    fn from_be_byte_array() {
88        let n = UIntEx::from_be_byte_array(hex!("00112233445566778899aabbccddeeff").into());
89        assert_eq!(
90            n.limbs(),
91            &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)]
92        );
93    }
94
95    #[test]
96    #[cfg(target_pointer_width = "32")]
97    fn from_le_byte_array() {
98        let n = UIntEx::from_le_byte_array(hex!("7766554433221100").into());
99        assert_eq!(n.limbs(), &[Limb(0x44556677), Limb(0x00112233)]);
100    }
101
102    #[test]
103    #[cfg(target_pointer_width = "64")]
104    fn from_le_byte_array() {
105        let n = UIntEx::from_le_byte_array(hex!("ffeeddccbbaa99887766554433221100").into());
106        assert_eq!(
107            n.limbs(),
108            &[Limb(0x8899aabbccddeeff), Limb(0x0011223344556677)]
109        );
110    }
111
112    #[test]
113    #[cfg(target_pointer_width = "32")]
114    fn to_be_byte_array() {
115        let expected_bytes = ByteArray::from(hex!("0011223344556677"));
116        let actual_bytes = UIntEx::from_be_byte_array(expected_bytes).to_be_byte_array();
117        assert_eq!(expected_bytes, actual_bytes);
118    }
119
120    #[test]
121    #[cfg(target_pointer_width = "64")]
122    fn to_be_byte_array() {
123        let expected_bytes = ByteArray::from(hex!("00112233445566778899aabbccddeeff"));
124        let actual_bytes = UIntEx::from_be_byte_array(expected_bytes).to_be_byte_array();
125        assert_eq!(expected_bytes, actual_bytes);
126    }
127
128    #[test]
129    #[cfg(target_pointer_width = "32")]
130    fn to_le_byte_array() {
131        let expected_bytes = ByteArray::from(hex!("7766554433221100"));
132        let actual_bytes = UIntEx::from_le_byte_array(expected_bytes).to_le_byte_array();
133        assert_eq!(expected_bytes, actual_bytes);
134    }
135
136    #[test]
137    #[cfg(target_pointer_width = "64")]
138    fn to_le_byte_array() {
139        let expected_bytes = ByteArray::from(hex!("ffeeddccbbaa99887766554433221100"));
140        let actual_bytes = UIntEx::from_le_byte_array(expected_bytes).to_le_byte_array();
141        assert_eq!(expected_bytes, actual_bytes);
142    }
143}