malachite_base/num/arithmetic/
mod_power_of_2_pow.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::arithmetic::traits::{ModPowerOf2Pow, ModPowerOf2PowAssign};
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11use crate::num::logic::traits::BitIterable;
12
13fn mod_power_of_2_pow<T: PrimitiveUnsigned>(x: T, exp: u64, pow: u64) -> T {
14    assert!(pow <= T::WIDTH);
15    assert!(
16        x.significant_bits() <= pow,
17        "x must be reduced mod 2^pow, but {x} >= 2^{pow}"
18    );
19    if pow == 0 {
20        return T::ZERO;
21    }
22    let mut out = T::ONE;
23    for bit in exp.bits().rev() {
24        out.mod_power_of_2_mul_assign(out, pow);
25        if bit {
26            out.mod_power_of_2_mul_assign(x, pow);
27        }
28    }
29    out
30}
31
32macro_rules! impl_mod_power_of_2_pow {
33    ($t:ident) => {
34        impl ModPowerOf2Pow<u64> for $t {
35            type Output = $t;
36
37            /// Raises a number to a power modulo another number $2^k$. The base must be already
38            /// reduced modulo $2^k$.
39            ///
40            /// $f(x, n, k) = y$, where $x, y < 2^k$ and $x^n \equiv y \mod 2^k$.
41            ///
42            /// # Worst-case complexity
43            /// $T(n) = O(n)$
44            ///
45            /// $M(n) = O(1)$
46            ///
47            /// where $T$ is time, $M$ is additional memory, and $n$ is `exp.significant_bits()`.
48            ///
49            /// # Panics
50            /// Panics if `pow` is greater than `Self::WIDTH` or if `self` is greater than or equal
51            /// to $2^k$.
52            ///
53            /// # Examples
54            /// See [here](super::mod_power_of_2_pow#mod_power_of_2_pow).
55            #[inline]
56            fn mod_power_of_2_pow(self, exp: u64, pow: u64) -> $t {
57                mod_power_of_2_pow(self, exp, pow)
58            }
59        }
60
61        impl ModPowerOf2PowAssign<u64> for $t {
62            /// Raises a number to a power modulo another number $2^k$, in place. The base must be
63            /// already reduced modulo $2^k$.
64            ///
65            /// $x \gets y$, where $x, y < 2^k$ and $x^n \equiv y \mod 2^k$.
66            ///
67            /// # Worst-case complexity
68            /// $T(n) = O(n)$
69            ///
70            /// $M(n) = O(1)$
71            ///
72            /// where $T$ is time, $M$ is additional memory, and $n$ is `exp.significant_bits()`.
73            ///
74            /// # Panics
75            /// Panics if `pow` is greater than `Self::WIDTH` or if `self` is greater than or equal
76            /// to $2^k$.
77            ///
78            /// # Examples
79            /// See [here](super::mod_power_of_2_pow#mod_power_of_2_pow_assign).
80            #[inline]
81            fn mod_power_of_2_pow_assign(&mut self, exp: u64, pow: u64) {
82                *self = self.mod_power_of_2_pow(exp, pow);
83            }
84        }
85    };
86}
87apply_to_unsigneds!(impl_mod_power_of_2_pow);