Skip to main content

crypto_bigint/uint/
from.rs

1//! `From`-like conversions for [`UInt`].
2
3use crate::{limb, Limb, Split, UInt, U128, U64};
4
5impl<const LIMBS: usize> UInt<LIMBS> {
6    /// Create a [`UInt`] from a `u8` (const-friendly)
7    // TODO(tarcieri): replace with `const impl From<u8>` when stable
8    pub const fn from_u8(n: u8) -> Self {
9        const_assert!(LIMBS >= 1, "number of limbs must be greater than zero");
10        let mut limbs = [Limb::ZERO; LIMBS];
11        limbs[0].0 = n as limb::Inner;
12        Self { limbs }
13    }
14
15    /// Create a [`UInt`] from a `u16` (const-friendly)
16    // TODO(tarcieri): replace with `const impl From<u16>` when stable
17    pub const fn from_u16(n: u16) -> Self {
18        const_assert!(LIMBS >= 1, "number of limbs must be greater than zero");
19        let mut limbs = [Limb::ZERO; LIMBS];
20        limbs[0].0 = n as limb::Inner;
21        Self { limbs }
22    }
23
24    /// Create a [`UInt`] from a `u32` (const-friendly)
25    // TODO(tarcieri): replace with `const impl From<u32>` when stable
26    pub const fn from_u32(n: u32) -> Self {
27        const_assert!(LIMBS >= 1, "number of limbs must be greater than zero");
28        let mut limbs = [Limb::ZERO; LIMBS];
29        limbs[0].0 = n as limb::Inner;
30        Self { limbs }
31    }
32
33    /// Create a [`UInt`] from a `u64` (const-friendly)
34    // TODO(tarcieri): replace with `const impl From<u64>` when stable
35    #[cfg(target_pointer_width = "32")]
36    pub const fn from_u64(n: u64) -> Self {
37        const_assert!(LIMBS >= 2, "number of limbs must be two or greater");
38        let mut limbs = [Limb::ZERO; LIMBS];
39        limbs[0].0 = (n & 0xFFFFFFFF) as u32;
40        limbs[1].0 = (n >> 32) as u32;
41        Self { limbs }
42    }
43
44    /// Create a [`UInt`] from a `u64` (const-friendly)
45    // TODO(tarcieri): replace with `const impl From<u64>` when stable
46    #[cfg(target_pointer_width = "64")]
47    pub const fn from_u64(n: u64) -> Self {
48        const_assert!(LIMBS >= 1, "number of limbs must be greater than zero");
49        let mut limbs = [Limb::ZERO; LIMBS];
50        limbs[0].0 = n as limb::Inner;
51        Self { limbs }
52    }
53
54    /// Create a [`UInt`] from a `u128` (const-friendly)
55    // TODO(tarcieri): replace with `const impl From<u128>` when stable
56    pub const fn from_u128(n: u128) -> Self {
57        const_assert!(
58            LIMBS >= (128 / limb::BIT_SIZE),
59            "number of limbs must be greater than zero"
60        );
61
62        let lo = U64::from_u64((n & 0xffff_ffff_ffff_ffff) as u64);
63        let hi = U64::from_u64((n >> 64) as u64);
64
65        let mut limbs = [Limb::ZERO; LIMBS];
66
67        let mut i = 0;
68        while i < lo.limbs.len() {
69            limbs[i] = lo.limbs[i];
70            i += 1;
71        }
72
73        let mut j = 0;
74        while j < hi.limbs.len() {
75            limbs[i + j] = hi.limbs[j];
76            j += 1;
77        }
78
79        Self { limbs }
80    }
81
82    /// Create a [`UInt`] from an array of the [`limb::Inner`]
83    /// unsigned integer type.
84    // TODO(tarcieri): replace with `const impl From<[limb::Inner; LIMBS]>` when stable
85    #[inline]
86    pub const fn from_uint_array(arr: [limb::Inner; LIMBS]) -> Self {
87        let mut limbs = [Limb::ZERO; LIMBS];
88        let mut i = 0;
89
90        while i < LIMBS {
91            limbs[i] = Limb(arr[i]);
92            i += 1;
93        }
94
95        Self { limbs }
96    }
97
98    /// Create an array of [`limb::Inner`] unsigned integer type from a [`UInt`].
99    #[inline]
100    // TODO(tarcieri): replace with `const impl From<Self> for [limb::Inner; LIMBS]` when stable
101    pub const fn to_uint_array(self) -> [limb::Inner; LIMBS] {
102        let mut arr = [0; LIMBS];
103        let mut i = 0;
104
105        while i < LIMBS {
106            arr[i] = self.limbs[i].0;
107            i += 1;
108        }
109
110        arr
111    }
112}
113
114impl<const LIMBS: usize> From<u8> for UInt<LIMBS> {
115    fn from(n: u8) -> Self {
116        // TODO(tarcieri): const where clause when possible
117        debug_assert!(LIMBS > 0, "limbs must be non-zero");
118        Self::from_u8(n)
119    }
120}
121
122impl<const LIMBS: usize> From<u16> for UInt<LIMBS> {
123    fn from(n: u16) -> Self {
124        // TODO(tarcieri): const where clause when possible
125        debug_assert!(LIMBS > 0, "limbs must be non-zero");
126        Self::from_u16(n)
127    }
128}
129
130impl<const LIMBS: usize> From<u32> for UInt<LIMBS> {
131    fn from(n: u32) -> Self {
132        // TODO(tarcieri): const where clause when possible
133        debug_assert!(LIMBS > 0, "limbs must be non-zero");
134        Self::from_u32(n)
135    }
136}
137
138impl<const LIMBS: usize> From<u64> for UInt<LIMBS> {
139    fn from(n: u64) -> Self {
140        // TODO(tarcieri): const where clause when possible
141        debug_assert!(LIMBS >= (64 / limb::BIT_SIZE), "not enough limbs");
142        Self::from_u64(n)
143    }
144}
145
146impl<const LIMBS: usize> From<u128> for UInt<LIMBS> {
147    fn from(n: u128) -> Self {
148        // TODO(tarcieri): const where clause when possible
149        debug_assert!(LIMBS >= (128 / limb::BIT_SIZE), "not enough limbs");
150        Self::from_u128(n)
151    }
152}
153
154#[cfg(target_pointer_width = "32")]
155#[cfg_attr(docsrs, doc(cfg(target_pointer_width = "32")))]
156impl From<U64> for u64 {
157    fn from(n: U64) -> u64 {
158        (n.limbs[0].0 as u64) | ((n.limbs[1].0 as u64) << 32)
159    }
160}
161
162#[cfg(target_pointer_width = "64")]
163#[cfg_attr(docsrs, doc(cfg(target_pointer_width = "64")))]
164impl From<U64> for u64 {
165    fn from(n: U64) -> u64 {
166        n.limbs[0].into()
167    }
168}
169
170impl From<U128> for u128 {
171    fn from(n: U128) -> u128 {
172        let (hi, lo) = n.split();
173        (u64::from(hi) as u128) << 64 | (u64::from(lo) as u128)
174    }
175}
176
177impl<const LIMBS: usize> From<[limb::Inner; LIMBS]> for UInt<LIMBS> {
178    fn from(arr: [limb::Inner; LIMBS]) -> Self {
179        Self::from_uint_array(arr)
180    }
181}
182
183impl<const LIMBS: usize> From<UInt<LIMBS>> for [limb::Inner; LIMBS] {
184    fn from(n: UInt<LIMBS>) -> [limb::Inner; LIMBS] {
185        n.to_uint_array()
186    }
187}
188
189impl<const LIMBS: usize> From<[Limb; LIMBS]> for UInt<LIMBS> {
190    fn from(limbs: [Limb; LIMBS]) -> Self {
191        Self { limbs }
192    }
193}
194
195impl<const LIMBS: usize> From<UInt<LIMBS>> for [Limb; LIMBS] {
196    fn from(n: UInt<LIMBS>) -> [Limb; LIMBS] {
197        n.limbs
198    }
199}
200
201impl<const LIMBS: usize> From<Limb> for UInt<LIMBS> {
202    fn from(limb: Limb) -> Self {
203        limb.0.into()
204    }
205}
206
207#[cfg(test)]
208mod tests {
209    use crate::{limb, Limb, U128};
210
211    #[cfg(target_pointer_width = "32")]
212    use crate::U64 as UIntEx;
213
214    #[cfg(target_pointer_width = "64")]
215    use crate::U128 as UIntEx;
216
217    #[test]
218    fn from_u8() {
219        let n = UIntEx::from(42u8);
220        assert_eq!(n.limbs(), &[Limb(42), Limb(0)]);
221    }
222
223    #[test]
224    fn from_u16() {
225        let n = UIntEx::from(42u16);
226        assert_eq!(n.limbs(), &[Limb(42), Limb(0)]);
227    }
228
229    #[test]
230    fn from_u64() {
231        let n = UIntEx::from(42u64);
232        assert_eq!(n.limbs(), &[Limb(42), Limb(0)]);
233    }
234
235    #[test]
236    fn from_u128() {
237        let n = U128::from(42u128);
238        assert_eq!(&n.limbs()[..2], &[Limb(42), Limb(0)]);
239        assert_eq!(u128::from(n), 42u128);
240    }
241
242    #[test]
243    fn array_round_trip() {
244        let arr1 = [1, 2];
245        let n = UIntEx::from(arr1);
246        let arr2: [limb::Inner; 2] = n.into();
247        assert_eq!(arr1, arr2);
248    }
249}