crypto_bigint/uint/
split.rs

1use crate::{Limb, Split, SplitMixed, Uint};
2
3impl<const I: usize> Uint<I> {
4    /// Split this number in half into low and high components.
5    pub const fn split<const O: usize>(&self) -> (Uint<O>, Uint<O>)
6    where
7        Self: Split<Output = Uint<O>>,
8    {
9        self.split_mixed()
10    }
11
12    /// Split this number into low and high components respectively.
13    #[inline]
14    pub const fn split_mixed<const L: usize, const H: usize>(&self) -> (Uint<L>, Uint<H>)
15    where
16        Self: SplitMixed<Uint<L>, Uint<H>>,
17    {
18        let top = L + H;
19        let top = if top < I { top } else { I };
20        let mut lo = [Limb::ZERO; L];
21        let mut hi = [Limb::ZERO; H];
22        let mut i = 0;
23
24        while i < top {
25            if i < L {
26                lo[i] = self.limbs[i];
27            } else {
28                hi[i - L] = self.limbs[i];
29            }
30            i += 1;
31        }
32
33        (Uint { limbs: lo }, Uint { limbs: hi })
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::{U128, U64};
40
41    #[test]
42    fn split() {
43        let (lo, hi) = U128::from_be_hex("00112233445566778899aabbccddeeff").split();
44        assert_eq!(lo, U64::from_u64(0x8899aabbccddeeff));
45        assert_eq!(hi, U64::from_u64(0x0011223344556677));
46    }
47}