Skip to main content

crypto_bigint/
macros.rs

1//! Macros
2
3/// Constant panicking assertion.
4// TODO(tarcieri): use const panic when stable.
5// See: https://github.com/rust-lang/rust/issues/51999
6macro_rules! const_assert {
7    ($bool:expr, $msg:expr) => {
8        [$msg][!$bool as usize]
9    };
10}
11
12/// Calculate the number of limbs required to represent the given number of bits.
13// TODO(tarcieri): replace with `const_evaluatable_checked` (e.g. a `const fn`) when stable
14#[macro_export]
15macro_rules! nlimbs {
16    ($bits:expr) => {
17        $bits / $crate::limb::BIT_SIZE
18    };
19}
20
21#[cfg(test)]
22mod tests {
23    #[cfg(target_pointer_width = "32")]
24    #[test]
25    fn nlimbs_for_bits_macro() {
26        assert_eq!(nlimbs!(64), 2);
27        assert_eq!(nlimbs!(128), 4);
28        assert_eq!(nlimbs!(192), 6);
29        assert_eq!(nlimbs!(256), 8);
30    }
31
32    #[cfg(target_pointer_width = "64")]
33    #[test]
34    fn nlimbs_for_bits_macro() {
35        assert_eq!(nlimbs!(64), 1);
36        assert_eq!(nlimbs!(128), 2);
37        assert_eq!(nlimbs!(192), 3);
38        assert_eq!(nlimbs!(256), 4);
39    }
40}