Skip to content

Commit b501f58

Browse files
committed
Integrate modulus in NTT structs
1 parent dd59b1b commit b501f58

File tree

17 files changed

+597
-698
lines changed

17 files changed

+597
-698
lines changed

benches/ntt_multiplication.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use criterion::*;
1313
use qfall_math::{
1414
integer_mod_q::{
15-
MatNTTPolynomialRingZq, MatPolynomialRingZq, Modulus, ModulusPolynomialRingZq,
16-
NTTPolynomialRingZq, PolyOverZq, PolynomialRingZq,
15+
MatNTTPolynomialRingZq, MatPolynomialRingZq, ModulusPolynomialRingZq, NTTPolynomialRingZq,
16+
PolyOverZq, PolynomialRingZq,
1717
},
1818
traits::*,
1919
};
@@ -52,7 +52,6 @@ pub fn get_hawk1024_setup() -> ModulusPolynomialRingZq {
5252
/// `n=256`, `q = 2^23 - 2^13 + 1` and `zeta = 1753`
5353
pub fn bench_ntt_dilithium_params_with_ntt(c: &mut Criterion) {
5454
let modulus = get_dilithium_setup();
55-
let mod_q = Modulus::from(modulus.get_q());
5655

5756
let p1 = PolynomialRingZq::sample_uniform(&modulus);
5857
let p2 = PolynomialRingZq::sample_uniform(&modulus);
@@ -62,15 +61,14 @@ pub fn bench_ntt_dilithium_params_with_ntt(c: &mut Criterion) {
6261

6362
c.bench_function(
6463
"PolynomialRingZq Multiplication with NTT (Dilithium)",
65-
|b| b.iter(|| ntt1.mul(&ntt2, &mod_q)),
64+
|b| b.iter(|| &ntt1 * &ntt2),
6665
);
6766
}
6867

6968
/// benchmark multiplication in typical dilithium parameter set with NTT & Transforms
7069
/// `n=256`, `q = 2^23 - 2^13 + 1` and `zeta = 1753`
7170
pub fn bench_ntt_dilithium_params_with_ntt_and_transforms(c: &mut Criterion) {
7271
let modulus = get_dilithium_setup();
73-
let mod_q = Modulus::from(modulus.get_q());
7472

7573
let p1 = PolynomialRingZq::sample_uniform(&modulus);
7674
let p2 = PolynomialRingZq::sample_uniform(&modulus);
@@ -82,9 +80,9 @@ pub fn bench_ntt_dilithium_params_with_ntt_and_transforms(c: &mut Criterion) {
8280
let ntt1 = NTTPolynomialRingZq::from(&p1);
8381
let ntt2 = NTTPolynomialRingZq::from(&p2);
8482

85-
let ntt_res = ntt1.mul(&ntt2, &mod_q);
83+
let ntt_res = &ntt1 * &ntt2;
8684

87-
let _ = PolynomialRingZq::from((ntt_res, &modulus));
85+
let _ = PolynomialRingZq::from(ntt_res);
8886
})
8987
},
9088
);
@@ -108,7 +106,6 @@ pub fn bench_ntt_dilithium_params_without_ntt(c: &mut Criterion) {
108106
/// `n=256`, `q = 12289` and `zeta = 1945`
109107
pub fn bench_ntt_hawk1024_params_with_ntt(c: &mut Criterion) {
110108
let modulus = get_hawk1024_setup();
111-
let mod_q = Modulus::from(modulus.get_q());
112109

113110
let p1 = PolynomialRingZq::sample_uniform(&modulus);
114111
let p2 = PolynomialRingZq::sample_uniform(&modulus);
@@ -117,15 +114,14 @@ pub fn bench_ntt_hawk1024_params_with_ntt(c: &mut Criterion) {
117114
let ntt2 = NTTPolynomialRingZq::from(&p2);
118115

119116
c.bench_function("PolynomialRingZq Multiplication with NTT (HAWK1024)", |b| {
120-
b.iter(|| ntt1.mul(&ntt2, &mod_q))
117+
b.iter(|| &ntt1 * &ntt2)
121118
});
122119
}
123120

124121
/// benchmark multiplication in typical HAWK1024 parameter set with NTT and Transforms
125122
/// `n=256`, `q = 12289` and `zeta = 1945`
126123
pub fn bench_ntt_hawk1024_params_with_ntt_and_transforms(c: &mut Criterion) {
127124
let modulus = get_hawk1024_setup();
128-
let mod_q = Modulus::from(modulus.get_q());
129125

130126
let p1 = PolynomialRingZq::sample_uniform(&modulus);
131127
let p2 = PolynomialRingZq::sample_uniform(&modulus);
@@ -137,9 +133,9 @@ pub fn bench_ntt_hawk1024_params_with_ntt_and_transforms(c: &mut Criterion) {
137133
let ntt1 = NTTPolynomialRingZq::from(&p1);
138134
let ntt2 = NTTPolynomialRingZq::from(&p2);
139135

140-
let ntt_res = ntt1.mul(&ntt2, &mod_q);
136+
let ntt_res = &ntt1 * &ntt2;
141137

142-
let _ = PolynomialRingZq::from((ntt_res, &modulus));
138+
let _ = PolynomialRingZq::from(ntt_res);
143139
})
144140
},
145141
);
@@ -163,7 +159,6 @@ pub fn bench_ntt_hawk1024_params_without_ntt(c: &mut Criterion) {
163159
/// `n=256`, `q = 2^23 - 2^13 + 1` and `zeta = 1753`
164160
pub fn bench_mat_ntt_dilithium_params_with_ntt(c: &mut Criterion) {
165161
let modulus = get_dilithium_setup();
166-
let mod_q = Modulus::from(modulus.get_q());
167162

168163
let p1 = MatPolynomialRingZq::sample_uniform(4, 4, &modulus);
169164
let p2 = MatPolynomialRingZq::sample_uniform(4, 1, &modulus);
@@ -173,15 +168,14 @@ pub fn bench_mat_ntt_dilithium_params_with_ntt(c: &mut Criterion) {
173168

174169
c.bench_function(
175170
"MatPolynomialRingZq Multiplication with NTT (Dilithium)",
176-
|b| b.iter(|| ntt1.mul(&ntt2, &mod_q)),
171+
|b| b.iter(|| &ntt1 * &ntt2),
177172
);
178173
}
179174

180175
/// benchmark multiplication in typical dilithium parameter set with NTT & Transforms
181176
/// `n=256`, `q = 2^23 - 2^13 + 1` and `zeta = 1753`
182177
pub fn bench_mat_ntt_dilithium_params_with_ntt_and_transforms(c: &mut Criterion) {
183178
let modulus = get_dilithium_setup();
184-
let mod_q = Modulus::from(modulus.get_q());
185179

186180
let p1 = MatPolynomialRingZq::sample_uniform(4, 4, &modulus);
187181
let p2 = MatPolynomialRingZq::sample_uniform(4, 1, &modulus);
@@ -193,9 +187,9 @@ pub fn bench_mat_ntt_dilithium_params_with_ntt_and_transforms(c: &mut Criterion)
193187
let ntt1 = MatNTTPolynomialRingZq::from(&p1);
194188
let ntt2 = MatNTTPolynomialRingZq::from(&p2);
195189

196-
let mut ntt_res = ntt1.mul(&ntt2, &mod_q);
190+
let mut ntt_res = &ntt1 * &ntt2;
197191

198-
let _ = MatPolynomialRingZq::from((&mut ntt_res, &modulus));
192+
let _ = MatPolynomialRingZq::from(&mut ntt_res);
199193
})
200194
},
201195
);
@@ -219,7 +213,6 @@ pub fn bench_mat_ntt_dilithium_params_without_ntt(c: &mut Criterion) {
219213
/// `n=256`, `q = 12289` and `zeta = 1945`
220214
pub fn bench_mat_ntt_hawk1024_params_with_ntt(c: &mut Criterion) {
221215
let modulus = get_hawk1024_setup();
222-
let mod_q = Modulus::from(modulus.get_q());
223216

224217
let p1 = MatPolynomialRingZq::sample_uniform(1, 2, &modulus);
225218
let p2 = MatPolynomialRingZq::sample_uniform(2, 2, &modulus);
@@ -229,15 +222,14 @@ pub fn bench_mat_ntt_hawk1024_params_with_ntt(c: &mut Criterion) {
229222

230223
c.bench_function(
231224
"MatPolynomialRingZq Multiplication with NTT (HAWK1024)",
232-
|b| b.iter(|| ntt1.mul(&ntt2, &mod_q)),
225+
|b| b.iter(|| &ntt1 * &ntt2),
233226
);
234227
}
235228

236229
/// benchmark multiplication in typical HAWK1024 parameter set with NTT and Transforms
237230
/// `n=256`, `q = 12289` and `zeta = 1945`
238231
pub fn bench_mat_ntt_hawk1024_params_with_ntt_and_transforms(c: &mut Criterion) {
239232
let modulus = get_hawk1024_setup();
240-
let mod_q = Modulus::from(modulus.get_q());
241233

242234
let p1 = MatPolynomialRingZq::sample_uniform(2, 2, &modulus);
243235
let p2 = MatPolynomialRingZq::sample_uniform(2, 1, &modulus);
@@ -249,9 +241,9 @@ pub fn bench_mat_ntt_hawk1024_params_with_ntt_and_transforms(c: &mut Criterion)
249241
let ntt1 = MatNTTPolynomialRingZq::from(&p1);
250242
let ntt2 = MatNTTPolynomialRingZq::from(&p2);
251243

252-
let mut ntt_res = ntt1.mul(&ntt2, &mod_q);
244+
let mut ntt_res = &ntt1 * &ntt2;
253245

254-
let _ = MatPolynomialRingZq::from((&mut ntt_res, &modulus));
246+
let _ = MatPolynomialRingZq::from(&mut ntt_res);
255247
})
256248
},
257249
);
@@ -291,7 +283,6 @@ pub fn get_rbe_setup() -> ModulusPolynomialRingZq {
291283
/// `n=256`, `q = 12289` and `zeta = 1945`
292284
pub fn bench_mat_ntt_rbe_params_with_ntt(c: &mut Criterion) {
293285
let modulus = get_rbe_setup();
294-
let mod_q = Modulus::from(modulus.get_q());
295286

296287
let p1 = MatPolynomialRingZq::sample_uniform(1, 2, &modulus);
297288
let p2 = MatPolynomialRingZq::sample_uniform(2, 12, &modulus);
@@ -300,15 +291,14 @@ pub fn bench_mat_ntt_rbe_params_with_ntt(c: &mut Criterion) {
300291
let ntt2 = MatNTTPolynomialRingZq::from(&p2);
301292

302293
c.bench_function("MatPolynomialRingZq Multiplication with NTT (RBE)", |b| {
303-
b.iter(|| ntt1.mul(&ntt2, &mod_q))
294+
b.iter(|| &ntt1 * &ntt2)
304295
});
305296
}
306297

307298
/// benchmark multiplication in typical RBE parameter set with NTT and Transforms
308299
/// `n=256`, `q = 12289` and `zeta = 1945`
309300
pub fn bench_mat_ntt_rbe_params_with_ntt_and_transforms(c: &mut Criterion) {
310301
let modulus = get_rbe_setup();
311-
let mod_q = Modulus::from(modulus.get_q());
312302

313303
let p1 = MatPolynomialRingZq::sample_uniform(1, 2, &modulus);
314304
let p2 = MatPolynomialRingZq::sample_uniform(2, 12, &modulus);
@@ -320,9 +310,9 @@ pub fn bench_mat_ntt_rbe_params_with_ntt_and_transforms(c: &mut Criterion) {
320310
let ntt1 = MatNTTPolynomialRingZq::from(&p1);
321311
let ntt2 = MatNTTPolynomialRingZq::from(&p2);
322312

323-
let mut ntt_res = ntt1.mul(&ntt2, &mod_q);
313+
let mut ntt_res = &ntt1 * &ntt2;
324314

325-
let _ = MatPolynomialRingZq::from((&mut ntt_res, &modulus));
315+
let _ = MatPolynomialRingZq::from(&mut ntt_res);
326316
})
327317
},
328318
);

src/integer_mod_q/mat_ntt_polynomial_ring_zq.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
//! [`MatNTTPolynomialRingZq`] containts the NTT representations of matrices over polynomials.
1010
11-
use crate::integer::Z;
11+
use crate::{integer::Z, integer_mod_q::ModulusPolynomialRingZq};
1212
use derive_more::Display;
1313
use serde::{Deserialize, Serialize};
1414
use std::fmt;
@@ -25,36 +25,41 @@ mod sample;
2525
///
2626
/// Attributes
2727
/// - `matrix`: holds the matrix entries with its coefficients
28+
/// - `nr_rows`: the number of rows of the matrix
29+
/// - `nr_columns`: the number of columns of the matrix
30+
/// - `modulus`: the [`ModulusPolynomialRingZq`] defining the modulus `q`, the ring `Z_q[X]/f(X)`, and
31+
/// the NTT transform [`NTTBasisPolynomialRingZq`](crate::integer_mod_q::NTTBasisPolynomialRingZq)
2832
///
2933
/// # Examples
3034
/// ```
3135
/// use qfall_math::integer_mod_q::{Modulus, MatPolynomialRingZq, MatNTTPolynomialRingZq, ModulusPolynomialRingZq};
3236
/// use std::str::FromStr;
3337
///
34-
/// // sample random matrix
35-
/// let mat_rnd = MatNTTPolynomialRingZq::sample_uniform(2, 2, 4, 257);
36-
/// // or instantiate matrix from MatPolynomialRingZq
38+
/// // setup modulus with ability to transform to NTT
3739
/// let mut modulus = ModulusPolynomialRingZq::from_str("5 1 0 0 0 1 mod 257").unwrap();
3840
/// modulus.set_ntt_unchecked(64);
41+
///
42+
/// // sample random matrix
43+
/// let mat_rnd = MatNTTPolynomialRingZq::sample_uniform(2, 2, &modulus);
44+
/// // or instantiate matrix from MatPolynomialRingZq
3945
/// let mat_poly_ring = MatPolynomialRingZq::identity(2, 2, &modulus);
4046
/// let mat_ntt_poly_ring = MatNTTPolynomialRingZq::from(&mat_poly_ring);
4147
///
4248
/// // multiply, add and subtract objects
43-
/// let mod_q = Modulus::from(modulus.get_q());
44-
/// let mut tmp_mat_ntt = mat_ntt_poly_ring.mul(&mat_rnd, &mod_q);
45-
/// tmp_mat_ntt.add_assign(&mat_rnd, &mod_q);
46-
/// tmp_mat_ntt.sub_assign(&mat_rnd, &mod_q);
49+
/// let mut tmp_mat_ntt = mat_ntt_poly_ring * &mat_rnd;
50+
/// tmp_mat_ntt += &mat_rnd;
51+
/// tmp_mat_ntt -= &mat_rnd;
4752
///
4853
/// // Return to MatPolynomialRingZq
49-
/// let res = MatPolynomialRingZq::from((&mut tmp_mat_ntt, &modulus));
54+
/// let res = tmp_mat_ntt.inv_ntt();
5055
/// ```
5156
#[derive(PartialEq, Eq, Serialize, Deserialize, Display, Clone)]
52-
#[display("{}", print_vec_z(&self.matrix))]
57+
#[display("{} / {}", print_vec_z(&self.matrix), self.modulus)]
5358
pub struct MatNTTPolynomialRingZq {
5459
pub matrix: Vec<Z>,
55-
pub d: usize, // modulus degree
5660
pub nr_rows: usize,
5761
pub nr_columns: usize,
62+
pub modulus: ModulusPolynomialRingZq,
5863
}
5964

6065
impl fmt::Debug for MatNTTPolynomialRingZq {
@@ -65,8 +70,8 @@ impl fmt::Debug for MatNTTPolynomialRingZq {
6570

6671
write!(
6772
f,
68-
"MatNTTPolynomialRingZq {{matrix: {}, d: {}, nr_rows: {}, nr_columns: {} storage: {{matrix: {:?}}}}}",
69-
short_print, self.d, self.nr_rows, self.nr_columns, self.matrix
73+
"MatNTTPolynomialRingZq {{matrix: {}, nr_rows: {}, nr_columns: {}, modulus: {}, storage: {{matrix: {:?}, modulus: {:?}}}}}",
74+
short_print, self.nr_rows, self.nr_columns, self.modulus, self.matrix, self.modulus
7075
)
7176
}
7277
}
@@ -75,7 +80,7 @@ impl fmt::Debug for MatNTTPolynomialRingZq {
7580
pub(crate) fn print_vec_z(vector: &Vec<Z>) -> String {
7681
let mut out = String::new();
7782
for v in vector {
78-
out.push_str(&format!("{}, ", v.to_string()));
83+
out.push_str(&format!("{}, ", v));
7984
}
8085
// Remove last whitespace and comma
8186
out.pop().unwrap();

0 commit comments

Comments
 (0)