diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:35 +0000 |
commit | 7e5d7eea9c580ef4b41a765bde624af431942b96 (patch) | |
tree | 2c0d9ca12878fc4525650aa4e54d77a81a07cc09 /vendor/crypto-bigint/src/uint/concat.rs | |
parent | Adding debian version 1.70.0+dfsg1-9. (diff) | |
download | rustc-7e5d7eea9c580ef4b41a765bde624af431942b96.tar.xz rustc-7e5d7eea9c580ef4b41a765bde624af431942b96.zip |
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/crypto-bigint/src/uint/concat.rs')
-rw-r--r-- | vendor/crypto-bigint/src/uint/concat.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/crypto-bigint/src/uint/concat.rs b/vendor/crypto-bigint/src/uint/concat.rs new file mode 100644 index 000000000..e92960da7 --- /dev/null +++ b/vendor/crypto-bigint/src/uint/concat.rs @@ -0,0 +1,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") + ); + } +} |