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
// Copyright 2023 Marvin Beckmann
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! Implementations to manipulate a [`PolyOverZq`] polynomial.
use super::PolyOverZq;
use crate::{
integer::Z,
integer_mod_q::{Modulus, Zq},
macros::for_others::implement_for_owned,
traits::SetCoefficient,
};
use flint3_sys::{fmpz_mod_poly_set_coeff_fmpz, fmpz_mod_poly_set_fmpz_poly};
impl<Integer: Into<Z>> SetCoefficient<Integer> for PolyOverZq {
/// Sets the coefficient of a polynomial [`PolyOverZq`].
/// We advise to use small coefficients, since already 2^32 coefficients take space
/// of roughly 34 GB. If not careful, be prepared that memory problems can occur, if
/// the index is very high.
///
/// Parameters:
/// - `index`: the index of the coefficient to set (has to be positive)
/// - `value`: the new value the coefficient will be set to.
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::PolyOverZq;
/// use qfall_math::integer::Z;
/// use qfall_math::traits::*;
/// use std::str::FromStr;
///
/// let mut poly = PolyOverZq::from_str("4 0 1 2 3 mod 17").unwrap();
///
/// assert!(poly.set_coeff(4, 1000).is_ok());
/// unsafe{ poly.set_coeff_unchecked(5, 75) };
/// ```
///
/// # Safety
/// To use this function safely, make sure that the selected index
/// is greater or equal than `0` and that the provided value has
/// the same base so that they have a matching base.
unsafe fn set_coeff_unchecked(&mut self, index: i64, value: Integer) {
let value: Z = value.into();
unsafe {
fmpz_mod_poly_set_coeff_fmpz(
&mut self.poly,
index,
&value.value,
self.modulus.get_fmpz_mod_ctx_struct(),
);
}
}
}
impl SetCoefficient<&Zq> for PolyOverZq {
/// Sets the coefficient of a polynomial [`PolyOverZq`].
/// We advise to use small coefficients, since already 2^32 coefficients take space
/// of roughly 34 GB. If not careful, be prepared that memory problems can occur, if
/// the index is very high.
///
/// This function does not check if the modulus of the polynomial and the value match.
///
/// Parameters:
/// - `index`: the index of the coefficient to set (has to be positive)
/// - `value`: the new value the index should have from a borrowed [`Zq`].
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::PolyOverZq;
/// use qfall_math::integer_mod_q::Zq;
/// use qfall_math::traits::*;
/// use std::str::FromStr;
///
/// let mut poly = PolyOverZq::from_str("4 0 1 2 3 mod 17").unwrap();
/// let value = Zq::from((1000, 17));
///
/// assert!(poly.set_coeff(4, &value).is_ok());
/// unsafe{ poly.set_coeff_unchecked(5, &value) };
/// ```
///
/// # Safety
/// To use this function safely, make sure that the selected index
/// is greater or equal than `0` and that the provided value has
/// the same base so that they have a matching base.
unsafe fn set_coeff_unchecked(&mut self, index: i64, value: &Zq) {
unsafe { self.set_coeff_unchecked(index, &value.value) }
}
}
implement_for_owned!(Zq, PolyOverZq, SetCoefficient);
impl PolyOverZq {
/// Changes the modulus of the given polynomial to the new modulus.
/// It takes the representation of each coefficient in [0, q) as the new
/// coefficients and reduces them by the new [`Modulus`].
///
/// Parameters:
/// - `modulus`: the new modulus of the polynomial
///
/// # Examples
/// ```
/// use qfall_math::integer_mod_q::PolyOverZq;
/// use std::str::FromStr;
///
/// let mut poly = PolyOverZq::from_str("4 5 3 7 4 mod 13").unwrap();
///
/// poly.change_modulus(5);
///
/// assert_eq!(PolyOverZq::from_str("4 0 3 2 4 mod 5").unwrap(), poly);
/// ```
///
/// # Panics ...
/// - if `modulus` is smaller than `2`.
pub fn change_modulus(&mut self, modulus: impl Into<Modulus>) {
self.modulus = modulus.into();
let lnr = self.get_representative_least_nonnegative_residue();
unsafe {
fmpz_mod_poly_set_fmpz_poly(
&mut self.poly,
&lnr.poly,
self.modulus.get_fmpz_mod_ctx_struct(),
);
}
}
}
#[cfg(test)]
mod test_set_coeff_z {
use crate::{integer::Z, integer_mod_q::PolyOverZq, traits::SetCoefficient};
use std::str::FromStr;
/// Ensure that the negative indices return an error.
#[test]
fn set_min_negative_coeff() {
let mut poly = PolyOverZq::from_str("2 1 1 mod 11").unwrap();
assert!(poly.set_coeff(i64::MIN, 2).is_err());
assert!(poly.set_coeff(i32::MIN, 2).is_err());
assert!(poly.set_coeff(i16::MIN, 2).is_err());
assert!(poly.set_coeff(i8::MIN, 2).is_err());
}
/// Ensure that large coefficients work.
#[test]
fn set_coeff_large() {
let mut poly = PolyOverZq::from_str("2 1 1 mod 11").unwrap();
assert!(poly.set_coeff(2, i32::MAX).is_ok());
assert!(poly.set_coeff(2, i64::MAX).is_ok());
}
/// Ensure that the max of [`u8`] and [`u16`] works as an index.
#[test]
fn set_index_large() {
let mut poly = PolyOverZq::from_str("2 1 1 mod 11").unwrap();
assert!(poly.set_coeff(u8::MAX, 2).is_ok());
assert!(poly.set_coeff(u16::MAX, 2).is_ok());
}
/// Ensure that a general case is working.
#[test]
fn set_coeff_working() {
let mut poly = PolyOverZq::from_str("4 0 1 2 3 mod 11").unwrap();
let value = 10000;
poly.set_coeff(0, value).unwrap();
poly.set_coeff(5, value).unwrap();
assert_eq!(
PolyOverZq::from_str("6 1 1 2 3 0 1 mod 11").unwrap(),
poly
);
}
/// Ensure that the correct coefficient is set and all others are set to `0`.
#[test]
fn set_coeff_rest_zero() {
let mut poly = PolyOverZq::from_str("0 mod 11").unwrap();
poly.set_coeff(4, -10).unwrap();
assert_eq!(PolyOverZq::from_str("5 0 0 0 0 1 mod 11").unwrap(), poly);
}
/// Ensure that setting with a z works.
#[test]
fn set_coeff_z() {
let mut poly = PolyOverZq::from_str("0 mod 11").unwrap();
poly.set_coeff(4, Z::from(123)).unwrap();
assert_eq!(PolyOverZq::from_str("5 0 0 0 0 2 mod 11").unwrap(), poly);
}
}
/// we omit most of the tests for the correct values, since
/// the values itself are set with set_coeff taking in a [`Z`] value
#[cfg(test)]
mod test_set_coeff_zq {
use crate::{
integer_mod_q::{PolyOverZq, Zq},
traits::SetCoefficient,
};
use std::str::FromStr;
/// Ensure that an error is returned if the moduli do not match
#[test]
fn mismatching_moduli() {
let mut poly = PolyOverZq::from_str(&format!("4 0 1 2 3 mod {}", u64::MAX - 58)).unwrap();
let value = Zq::from((u64::MAX, u64::MAX - 57));
assert!(poly.set_coeff(4, &value).is_err());
assert_eq!(
PolyOverZq::from_str(&format!("4 0 1 2 3 mod {}", u64::MAX - 58)).unwrap(),
poly
)
}
/// Ensure that an ok is returned if the moduli not match
#[test]
fn matching_moduli() {
let mut poly = PolyOverZq::from_str(&format!("4 0 1 2 3 mod {}", u64::MAX - 58)).unwrap();
let value = Zq::from((u64::MAX, u64::MAX - 58));
assert!(poly.set_coeff(4, &value).is_ok());
assert_eq!(
PolyOverZq::from_str(&format!("5 0 1 2 3 58 mod {}", u64::MAX - 58)).unwrap(),
poly
)
}
}
#[cfg(test)]
mod test_change_modulus {
use super::PolyOverZq;
use crate::integer_mod_q::Modulus;
use std::str::FromStr;
/// Ensures that the modulus is changed correctly.
#[test]
fn modulus_correct() {
let mut poly = PolyOverZq::from_str("6 1 2 3 4 5 6 mod 7").unwrap();
let modulus = Modulus::from(8);
poly.change_modulus(&modulus);
assert_eq!("6 1 2 3 4 5 6 mod 8", poly.to_string());
}
/// Ensures that the modulus is changed correctly, if the modulus is big.
#[test]
fn big_modulus_correct() {
let mut poly = PolyOverZq::from_str(&format!("6 1 2 3 4 5 6 mod {}", i64::MAX)).unwrap();
let modulus = Modulus::from(u64::MAX);
poly.change_modulus(&modulus);
assert_eq!(format!("6 1 2 3 4 5 6 mod {}", u64::MAX), poly.to_string());
}
/// Ensures that the poly is reduced correctly.
#[test]
fn reduced_correct() {
let mut poly = PolyOverZq::from_str("6 1 2 3 4 5 6 mod 7").unwrap();
let modulus = Modulus::from(2);
poly.change_modulus(&modulus);
assert_eq!("5 1 0 1 0 1 mod 2", poly.to_string());
}
}