crypto_bigint/macros.rs
1//! Macro definitions which are a part of the public API.
2
3/// Calculate the number of limbs required to represent the given number of bits.
4// TODO(tarcieri): replace with `generic_const_exprs` (rust-lang/rust#76560) when stable
5#[macro_export]
6macro_rules! nlimbs {
7 ($bits:expr) => {
8 u32::div_ceil($bits, $crate::Limb::BITS) as usize
9 };
10}
11
12/// Calculate the number of 62-bit unsaturated limbs required to represent the given number of bits when performing
13/// Bernstein-Yang inversions.
14///
15/// We need to ensure that:
16///
17/// ```text
18/// $bits <= (safegcd_nlimbs($bits) * 62) - 64
19/// ```
20// TODO(tarcieri): replace with `generic_const_exprs` (rust-lang/rust#76560) when stable
21macro_rules! safegcd_nlimbs {
22 ($bits:expr) => {
23 ($bits + 64).div_ceil(62)
24 };
25}
26
27#[cfg(test)]
28mod tests {
29 #[cfg(target_pointer_width = "32")]
30 #[test]
31 fn nlimbs_for_bits_macro() {
32 assert_eq!(nlimbs!(64), 2);
33 assert_eq!(nlimbs!(128), 4);
34 assert_eq!(nlimbs!(192), 6);
35 assert_eq!(nlimbs!(256), 8);
36 }
37
38 #[cfg(target_pointer_width = "64")]
39 #[test]
40 fn nlimbs_for_bits_macro() {
41 assert_eq!(nlimbs!(64), 1);
42 assert_eq!(nlimbs!(128), 2);
43 assert_eq!(nlimbs!(192), 3);
44 assert_eq!(nlimbs!(256), 4);
45 }
46}