Skip to main content

malachite_base/num/arithmetic/
mod_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    ModAbsSquared, ModAbsSquaredAssign, ModSquare, ModSquareAssign,
11};
12
13macro_rules! impl_mod_abs_squared {
14    ($t:ident) => {
15        impl ModAbsSquared for $t {
16            type Output = $t;
17
18            /// Computes the squared absolute value of a number modulo another number $m$, which for
19            /// an unsigned number is its square modulo $m$. The input must be already reduced
20            /// modulo $m$.
21            ///
22            /// $f(x, m) = y$, where $x, y < m$ and $|x|^2 = x^2 \equiv y \mod m$.
23            ///
24            /// # Worst-case complexity
25            /// Constant time and additional memory.
26            ///
27            /// # Panics
28            /// Panics if `self` is greater than or equal to `m`.
29            ///
30            /// # Examples
31            /// See [here](super::mod_abs_squared#mod_abs_squared).
32            #[inline]
33            fn mod_abs_squared(self, m: $t) -> $t {
34                self.mod_square(m)
35            }
36        }
37
38        impl ModAbsSquaredAssign for $t {
39            /// Replaces a number with its squared absolute value modulo another number $m$, which
40            /// for an unsigned number is its square modulo $m$. The input must be already reduced
41            /// modulo $m$.
42            ///
43            /// $x \gets y$, where $x, y < m$ and $|x|^2 = x^2 \equiv y \mod m$.
44            ///
45            /// # Worst-case complexity
46            /// Constant time and additional memory.
47            ///
48            /// # Panics
49            /// Panics if `self` is greater than or equal to `m`.
50            ///
51            /// # Examples
52            /// See [here](super::mod_abs_squared#mod_abs_squared_assign).
53            #[inline]
54            fn mod_abs_squared_assign(&mut self, m: $t) {
55                self.mod_square_assign(m);
56            }
57        }
58    };
59}
60apply_to_unsigneds!(impl_mod_abs_squared);