crypto_bigint/uint/concat.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// TODO(tarcieri): use `const_evaluatable_checked` when stable to make generic around bits.
macro_rules! impl_concat {
($(($name:ident, $bits:expr)),+) => {
$(
impl $name {
/// Concatenate the two values, with `self` as most significant and `rhs`
/// as the least significant.
pub const fn concat(&self, rhs: &Self) -> UInt<{nlimbs!($bits) * 2}> {
let mut limbs = [Limb::ZERO; nlimbs!($bits) * 2];
let mut i = 0;
let mut j = 0;
while j < nlimbs!($bits) {
limbs[i] = rhs.limbs[j];
i += 1;
j += 1;
}
j = 0;
while j < nlimbs!($bits) {
limbs[i] = self.limbs[j];
i += 1;
j += 1;
}
UInt { limbs }
}
}
impl Concat for $name {
type Output = UInt<{nlimbs!($bits) * 2}>;
fn concat(&self, rhs: &Self) -> Self::Output {
self.concat(rhs)
}
}
impl From<($name, $name)> for UInt<{nlimbs!($bits) * 2}> {
fn from(nums: ($name, $name)) -> UInt<{nlimbs!($bits) * 2}> {
nums.0.concat(&nums.1)
}
}
)+
};
}
#[cfg(test)]
mod tests {
use crate::{U128, U64};
#[test]
fn concat() {
let hi = U64::from_u64(0x0011223344556677);
let lo = U64::from_u64(0x8899aabbccddeeff);
assert_eq!(
hi.concat(&lo),
U128::from_be_hex("00112233445566778899aabbccddeeff")
);
}
}