crypto_bigint/uint/
resize.rs

1use super::Uint;
2
3impl<const LIMBS: usize> Uint<LIMBS> {
4    /// Construct a `Uint<T>` from the unsigned integer value,
5    /// truncating the upper bits if the value is too large to be
6    /// represented.
7    #[inline(always)]
8    pub const fn resize<const T: usize>(&self) -> Uint<T> {
9        let mut res = Uint::ZERO;
10        let mut i = 0;
11        let dim = if T < LIMBS { T } else { LIMBS };
12        while i < dim {
13            res.limbs[i] = self.limbs[i];
14            i += 1;
15        }
16        res
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use crate::{U128, U64};
23
24    #[test]
25    fn resize_larger() {
26        let u = U64::from_be_hex("AAAAAAAABBBBBBBB");
27        let u2: U128 = u.resize();
28        assert_eq!(u2, U128::from_be_hex("0000000000000000AAAAAAAABBBBBBBB"));
29    }
30
31    #[test]
32    fn resize_smaller() {
33        let u = U128::from_be_hex("AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD");
34        let u2: U64 = u.resize();
35        assert_eq!(u2, U64::from_be_hex("CCCCCCCCDDDDDDDD"));
36    }
37}