1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::integer_polynomial::arithmetic::pow_truncated::pow_truncated_ref;
use crate::integer_polynomial::arithmetic::vec::max_bits::vec_max_bits;
use crate::natural::Natural;
use crate::natural_polynomial::NaturalPolynomial;
use crate::natural_polynomial::arithmetic::mod_power_of_2_mul_truncated::*;
use crate::natural_polynomial::arithmetic::mod_power_of_2_pow::power_needs_no_reduction;
use crate::natural_polynomial::arithmetic::mod_power_of_2_square::assert_reduced;
use crate::natural_polynomial::arithmetic::mod_power_of_2_square_truncated::*;
use alloc::vec;
use alloc::vec::Vec;
use malachite_base::num::arithmetic::traits::{ModPowerOf2Pow, ModPowerOf2PowAssign};
use malachite_base::num::basic::traits::{One, Zero};
use malachite_base::num::conversion::traits::{ExactFrom, SaturatingFrom};
use malachite_base::polynomial::{
ModPowerOf2PowTruncated, ModPowerOf2PowTruncatedAssign, Polynomial, pow_binexp_trimmed,
};
// The coefficients, without zeros at the end, of the `e`th power modulo $2^k$, where $k$ is `pow`,
// of the polynomial with coefficients `xs`, which has length at least 2, a nonzero first element,
// and coefficients reduced modulo $2^k$, keeping only the coefficients of $x^i$ for $i$ less than
// `len`, which is at least 2, where `e` is at least 3, by binary exponentiation with truncated
// squaring and multiplication, each reduced and trimmed.
//
// This is equivalent to `_fmpz_mod_poly_pow_trunc_binexp` from `fmpz_mod_poly/pow_trunc_binexp.c`,
// FLINT 3.6.0, with the modulus $2^k$, except that the polynomial is not padded to length `len` and
// the intermediate powers are trimmed.
crate_test_fn! {mod_power_of_2_pow_truncated_binexp(
xs: &[Natural],
e: u64,
len: u64,
pow: u64,
) -> Vec<Natural> {
pow_binexp_trimmed(
xs,
e,
|r| mod_power_of_2_square_truncated_ref(r, len, pow).into_coefficients_asc(),
|r, xs| mod_power_of_2_mul_truncated_ref_ref(r, xs, len, pow).into_coefficients_asc(),
)
}}
// The `e`th power modulo $2^k$, where $k$ is `pow`, of the polynomial with coefficients `xs`, which
// has no zeros at the end and coefficients reduced modulo $2^k$, keeping only the coefficients of
// $x^i$ for $i$ less than `len`.
//
// Writing the polynomial modulo $x^n$ as $x^\ell q$, with $q_0 \neq 0$, its power is $x^{e\ell}
// (q^e \bmod x^{n - e\ell})$, or 0 if $e\ell \geq n$. When the power of $q$ over the integers
// already has every coefficient less than $2^k$, it is computed with `pow_truncated`; otherwise by
// binary exponentiation modulo $2^k$.
//
// This is equivalent to `fmpz_mod_poly_pow_trunc` from `fmpz_mod_poly/pow_trunc.c`, FLINT 3.6.0,
// with the modulus $2^k$, except for the removal of the factor of $x^\ell$ and the integer power,
// and except that the zeroth power of the zero polynomial is 1, where FLINT gives 0.
pub(crate) fn mod_power_of_2_pow_truncated_ref(
xs: &[Natural],
e: u64,
len: u64,
pow: u64,
) -> NaturalPolynomial {
if pow == 0 || len == 0 {
return NaturalPolynomial::ZERO;
}
if e == 0 {
return NaturalPolynomial::one();
}
let n = usize::saturating_from(len);
let xs = &xs[..xs.len().min(n)];
let Some(low) = xs.iter().position(|x| *x != 0u32) else {
return NaturalPolynomial::ZERO;
};
let shift = usize::saturating_from(e).saturating_mul(low);
if shift >= n {
return NaturalPolynomial::ZERO;
}
let q = &xs[low..];
let q_len = n - shift;
let q_len_u64 = u64::exact_from(q_len);
let mut power = match (q.len().min(q_len), e) {
(1, _) => {
let c = (&q[0]).mod_power_of_2_pow(Natural::from(e), pow);
if c == 0u32 { Vec::new() } else { vec![c] }
}
(m, 1) => {
let mut power = q[..m].to_vec();
while power.last() == Some(&Natural::ZERO) {
power.pop();
}
power
}
_ if power_needs_no_reduction(q.len(), vec_max_bits(q).0, e, pow) => {
pow_truncated_ref(q, e, q_len_u64)
}
(_, 2) => mod_power_of_2_square_truncated_ref(q, q_len_u64, pow).into_coefficients_asc(),
_ => mod_power_of_2_pow_truncated_binexp(q, e, q_len_u64, pow),
};
if power.is_empty() {
return NaturalPolynomial::ZERO;
}
power.splice(0..0, core::iter::repeat_n(Natural::ZERO, shift));
NaturalPolynomial {
coefficients: power,
}
}
impl ModPowerOf2PowTruncated for NaturalPolynomial {
type Output = Self;
/// Raises a [`NaturalPolynomial`] to a power modulo $2^k$, keeping only the coefficients of
/// $x^i$ for $i$ less than `len`, taking it by value. Its coefficients must already be reduced
/// modulo $2^k$.
///
/// $$
/// f(p, e, n, k) = (p^e \bmod x^n) \bmod 2^k.
/// $$
///
/// The polynomial need not already be truncated: only its first `len` coefficients are read.
/// The zeroth power of every polynomial is 1, truncated to 0 when `len` is 0 and reduced to 0
/// when $k$ is 0. The power is computed by repeated truncated squaring modulo $2^k$, unless no
/// coefficient of the power over the integers reaches $2^k$, in which case it is computed as in
/// [`PowTruncated`](malachite_base::polynomial::PowTruncated).
///
/// # Worst-case complexity
/// $T(n, m) = O(n(m + \log n) \log (nm) \log\log (nm) \log e)$
///
/// $M(n, m) = O(n(m + \log n) \log (nm))$
///
/// where $T$ is time, $M$ is additional memory, $n$ is `len`, $m$ is `pow`, and $e$ is `exp`.
///
/// # Panics
/// Panics if `self` is not reduced modulo $2^k$.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::polynomial::ModPowerOf2PowTruncated;
/// use malachite_nz::natural_polynomial::NaturalPolynomial;
///
/// assert_eq!(
/// (NaturalPolynomial::from_str("x+1").unwrap())
/// .mod_power_of_2_pow_truncated(5, 3, 3)
/// .to_string(),
/// "2*x^2+5*x+1"
/// );
/// // The power is a multiple of x^4.
/// assert_eq!(
/// (NaturalPolynomial::from_str("x^2+x").unwrap())
/// .mod_power_of_2_pow_truncated(4, 4, 3)
/// .to_string(),
/// "0"
/// );
/// ```
///
/// This is equivalent to `fmpz_mod_poly_pow_trunc` from `fmpz_mod_poly/pow_trunc.c`, FLINT
/// 3.6.0, with the modulus $2^k$, except that a factor of $x^\ell$ is removed before powering,
/// that the intermediate powers are trimmed rather than padded to `len`, that a power needing
/// no reduction is computed over the integers, and that the zeroth power of the zero polynomial
/// is 1 (modulo $2^k$), where FLINT gives 0.
#[inline]
fn mod_power_of_2_pow_truncated(mut self, exp: u64, len: u64, pow: u64) -> Self {
self.mod_power_of_2_pow_truncated_assign(exp, len, pow);
self
}
}
impl ModPowerOf2PowTruncated for &NaturalPolynomial {
type Output = NaturalPolynomial;
/// Raises a [`NaturalPolynomial`] to a power modulo $2^k$, keeping only the coefficients of
/// $x^i$ for $i$ less than `len`, taking it by reference. Its coefficients must already be
/// reduced modulo $2^k$.
///
/// $$
/// f(p, e, n, k) = (p^e \bmod x^n) \bmod 2^k.
/// $$
///
/// The polynomial need not already be truncated: only its first `len` coefficients are read.
/// The zeroth power of every polynomial is 1, truncated to 0 when `len` is 0 and reduced to 0
/// when $k$ is 0. The power is computed by repeated truncated squaring modulo $2^k$, unless no
/// coefficient of the power over the integers reaches $2^k$, in which case it is computed as in
/// [`PowTruncated`](malachite_base::polynomial::PowTruncated).
///
/// # Worst-case complexity
/// $T(n, m) = O(n(m + \log n) \log (nm) \log\log (nm) \log e)$
///
/// $M(n, m) = O(n(m + \log n) \log (nm))$
///
/// where $T$ is time, $M$ is additional memory, $n$ is `len`, $m$ is `pow`, and $e$ is `exp`.
///
/// # Panics
/// Panics if `self` is not reduced modulo $2^k$.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::polynomial::ModPowerOf2PowTruncated;
/// use malachite_nz::natural_polynomial::NaturalPolynomial;
///
/// assert_eq!(
/// (&NaturalPolynomial::from_str("x+1").unwrap())
/// .mod_power_of_2_pow_truncated(5, 3, 3)
/// .to_string(),
/// "2*x^2+5*x+1"
/// );
/// // The power is a multiple of x^4.
/// assert_eq!(
/// (&NaturalPolynomial::from_str("x^2+x").unwrap())
/// .mod_power_of_2_pow_truncated(4, 4, 3)
/// .to_string(),
/// "0"
/// );
/// ```
///
/// This is equivalent to `fmpz_mod_poly_pow_trunc` from `fmpz_mod_poly/pow_trunc.c`, FLINT
/// 3.6.0, with the modulus $2^k$, except that a factor of $x^\ell$ is removed before powering,
/// that the intermediate powers are trimmed rather than padded to `len`, that a power needing
/// no reduction is computed over the integers, and that the zeroth power of the zero polynomial
/// is 1 (modulo $2^k$), where FLINT gives 0.
fn mod_power_of_2_pow_truncated(self, exp: u64, len: u64, pow: u64) -> NaturalPolynomial {
assert_reduced(self, pow);
mod_power_of_2_pow_truncated_ref(&self.coefficients, exp, len, pow)
}
}
impl ModPowerOf2PowTruncatedAssign for NaturalPolynomial {
/// Raises a [`NaturalPolynomial`] to a power modulo $2^k$ in place, keeping only the
/// coefficients of $x^i$ for $i$ less than `len`. Its coefficients must already be reduced
/// modulo $2^k$.
///
/// $$
/// p \gets (p^e \bmod x^n) \bmod 2^k.
/// $$
///
/// The polynomial need not already be truncated: only its first `len` coefficients are read.
/// The zeroth power of every polynomial is 1, truncated to 0 when `len` is 0 and reduced to 0
/// when $k$ is 0. The power is computed by repeated truncated squaring modulo $2^k$, unless no
/// coefficient of the power over the integers reaches $2^k$, in which case it is computed as in
/// [`PowTruncated`](malachite_base::polynomial::PowTruncated).
///
/// # Worst-case complexity
/// $T(n, m) = O(n(m + \log n) \log (nm) \log\log (nm) \log e)$
///
/// $M(n, m) = O(n(m + \log n) \log (nm))$
///
/// where $T$ is time, $M$ is additional memory, $n$ is `len`, $m$ is `pow`, and $e$ is `exp`.
///
/// # Panics
/// Panics if `self` is not reduced modulo $2^k$.
///
/// # Examples
/// ```
/// use core::str::FromStr;
/// use malachite_base::polynomial::ModPowerOf2PowTruncatedAssign;
/// use malachite_nz::natural_polynomial::NaturalPolynomial;
///
/// let mut p = NaturalPolynomial::from_str("x+1").unwrap();
/// p.mod_power_of_2_pow_truncated_assign(5, 3, 3);
/// assert_eq!(p.to_string(), "2*x^2+5*x+1");
///
/// // The power is a multiple of x^4.
/// let mut p = NaturalPolynomial::from_str("x^2+x").unwrap();
/// p.mod_power_of_2_pow_truncated_assign(4, 4, 3);
/// assert_eq!(p.to_string(), "0");
/// ```
///
/// This is equivalent to `fmpz_mod_poly_pow_trunc` from `fmpz_mod_poly/pow_trunc.c`, FLINT
/// 3.6.0, with the modulus $2^k$, except that a factor of $x^\ell$ is removed before powering,
/// that the intermediate powers are trimmed rather than padded to `len`, that a power needing
/// no reduction is computed over the integers, and that the zeroth power of the zero polynomial
/// is 1 (modulo $2^k$), where FLINT gives 0.
fn mod_power_of_2_pow_truncated_assign(&mut self, exp: u64, len: u64, pow: u64) {
assert_reduced(self, pow);
let xs = &mut self.coefficients;
match (xs.len(), exp, len, pow) {
(_, _, 0, _) | (_, _, _, 0) => xs.clear(),
(0, 0, _, _) => xs.push(Natural::ONE),
(_, 0, _, _) => {
xs.truncate(1);
xs[0] = Natural::ONE;
}
(0, _, _, _) => {}
(_, 1, _, _) => self.truncate_assign(len),
(1, _, _, _) => {
xs[0].mod_power_of_2_pow_assign(Natural::from(exp), pow);
if xs[0] == 0u32 {
xs.clear();
}
}
_ => *self = mod_power_of_2_pow_truncated_ref(xs, exp, len, pow),
}
}
}