diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:25:56 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:25:56 +0000 |
commit | 018c4950b9406055dec02ef0fb52f132e2bb1e2c (patch) | |
tree | a835ebdf2088ef88fa681f8fad45f09922c1ae9a /vendor/crypto-bigint/src/macros.rs | |
parent | Adding debian version 1.75.0+dfsg1-5. (diff) | |
download | rustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.tar.xz rustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.zip |
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/crypto-bigint/src/macros.rs')
-rw-r--r-- | vendor/crypto-bigint/src/macros.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/vendor/crypto-bigint/src/macros.rs b/vendor/crypto-bigint/src/macros.rs new file mode 100644 index 000000000..7142c214d --- /dev/null +++ b/vendor/crypto-bigint/src/macros.rs @@ -0,0 +1,79 @@ +//! Macro definitions which are a part of the public API. + +/// Internal implementation detail of [`const_assert_eq`] and [`const_assert_ne`]. +#[doc(hidden)] +#[macro_export] +macro_rules! const_assert_n { + ($n:expr, $($arg:tt)*) => {{ + // TODO(tarcieri): gensym a name so it's unique per invocation of the macro? + mod __const_assert { + pub(super) struct Assert<const N: usize>; + + impl<const N: usize> Assert<N> { + pub(super) const ASSERT: () = assert!($($arg)*); + } + } + + __const_assert::Assert::<$n>::ASSERT + }}; +} + +/// Const-friendly assertion that two values are equal. +/// +/// ``` +/// const _: () = crypto_bigint::const_assert_eq!(0, 0, "zero equals zero"); +/// ``` +#[macro_export] +macro_rules! const_assert_eq { + ($left:expr, $right:expr $(,)?) => ( + $crate::const_assert_n!($left, $left == $right) + ); + ($left:expr, $right:expr, $($arg:tt)+) => ( + $crate::const_assert_n!($left, $left == $right, $($arg)+) + ); +} + +/// Const-friendly assertion that two values are NOT equal. +/// +/// ``` +/// const _: () = crypto_bigint::const_assert_ne!(0, 1, "zero is NOT equal to one"); +/// ``` +#[macro_export] +macro_rules! const_assert_ne { + ($left:expr, $right:expr $(,)?) => ( + $crate::const_assert_n!($left, $left != $right) + ); + ($left:expr, $right:expr, $($arg:tt)+) => ( + $crate::const_assert_n!($left, $left != $right, $($arg)+) + ); +} + +/// Calculate the number of limbs required to represent the given number of bits. +// TODO(tarcieri): replace with `generic_const_exprs` (rust-lang/rust#76560) when stable +#[macro_export] +macro_rules! nlimbs { + ($bits:expr) => { + $bits / $crate::Limb::BITS + }; +} + +#[cfg(test)] +mod tests { + #[cfg(target_pointer_width = "32")] + #[test] + fn nlimbs_for_bits_macro() { + assert_eq!(nlimbs!(64), 2); + assert_eq!(nlimbs!(128), 4); + assert_eq!(nlimbs!(192), 6); + assert_eq!(nlimbs!(256), 8); + } + + #[cfg(target_pointer_width = "64")] + #[test] + fn nlimbs_for_bits_macro() { + assert_eq!(nlimbs!(64), 1); + assert_eq!(nlimbs!(128), 2); + assert_eq!(nlimbs!(192), 3); + assert_eq!(nlimbs!(256), 4); + } +} |