malachite_base/num/arithmetic/
mod_power_of_2_square.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::{
10    ModPowerOf2Mul, ModPowerOf2MulAssign, ModPowerOf2Square, ModPowerOf2SquareAssign,
11};
12
13macro_rules! impl_mod_power_of_2_square {
14    ($t:ident) => {
15        impl ModPowerOf2Square for $t {
16            type Output = $t;
17
18            /// Squares a number modulo another number $2^k$. The input must be already reduced
19            /// modulo $2^k$.
20            ///
21            /// $f(x, k) = y$, where $x, y < 2^k$ and $x^2 \equiv y \mod 2^k$.
22            ///
23            /// # Worst-case complexity
24            /// Constant time and additional memory.
25            ///
26            /// # Panics
27            /// Panics if `pow` is greater than `Self::WIDTH` or if `self` is greater than or equal
28            /// to $2^k$.
29            ///
30            /// # Examples
31            /// See [here](super::mod_power_of_2_square#mod_power_of_2_square).
32            #[inline]
33            fn mod_power_of_2_square(self, pow: u64) -> $t {
34                self.mod_power_of_2_mul(self, pow)
35            }
36        }
37
38        impl ModPowerOf2SquareAssign for $t {
39            /// Squares a number modulo another number $2^k$, in place. The input must be already
40            /// reduced modulo $2^k$.
41            ///
42            /// $x \gets y$, where $x, y < 2^k$ and $x^2 \equiv y \mod 2^k$.
43            ///
44            /// # Worst-case complexity
45            /// Constant time and additional memory.
46            ///
47            /// # Panics
48            /// Panics if `pow` is greater than `Self::WIDTH` or if `self` is greater than or equal
49            /// to $2^k$.
50            ///
51            /// # Examples
52            /// See [here](super::mod_power_of_2_square#mod_power_of_2_square_assign).
53            #[inline]
54            fn mod_power_of_2_square_assign(&mut self, pow: u64) {
55                self.mod_power_of_2_mul_assign(*self, pow);
56            }
57        }
58    };
59}
60apply_to_unsigneds!(impl_mod_power_of_2_square);