malachite_base/num/arithmetic/mod_power_of_2_abs_squared.rs
1// Copyright © 2026 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 ModPowerOf2AbsSquared, ModPowerOf2AbsSquaredAssign, ModPowerOf2Square, ModPowerOf2SquareAssign,
11};
12
13macro_rules! impl_mod_power_of_2_abs_squared {
14 ($t:ident) => {
15 impl ModPowerOf2AbsSquared for $t {
16 type Output = $t;
17
18 /// Computes the squared absolute value of a number modulo $2^k$, which for an unsigned
19 /// number is its square modulo $2^k$. The input must be already reduced modulo $2^k$.
20 ///
21 /// $f(x, k) = y$, where $x, y < 2^k$ and $|x|^2 = 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_abs_squared#mod_power_of_2_abs_squared).
32 #[inline]
33 fn mod_power_of_2_abs_squared(self, pow: u64) -> $t {
34 self.mod_power_of_2_square(pow)
35 }
36 }
37
38 impl ModPowerOf2AbsSquaredAssign for $t {
39 /// Replaces a number with its squared absolute value modulo $2^k$, which for an
40 /// unsigned number is its square modulo $2^k$. The input must be already reduced modulo
41 /// $2^k$.
42 ///
43 /// $x \gets y$, where $x, y < 2^k$ and $|x|^2 = x^2 \equiv y \mod 2^k$.
44 ///
45 /// # Worst-case complexity
46 /// Constant time and additional memory.
47 ///
48 /// # Panics
49 /// Panics if `pow` is greater than `Self::WIDTH` or if `self` is greater than or equal
50 /// to $2^k$.
51 ///
52 /// # Examples
53 /// See [here](super::mod_power_of_2_abs_squared#mod_power_of_2_abs_squared_assign).
54 #[inline]
55 fn mod_power_of_2_abs_squared_assign(&mut self, pow: u64) {
56 self.mod_power_of_2_square_assign(pow);
57 }
58 }
59 };
60}
61apply_to_unsigneds!(impl_mod_power_of_2_abs_squared);