poulpy/base2k/src/vmp.rs
base2k/src/vmp.rsRaw
1use crate::ffi::vmp;
2use crate::{Infos, Module, VecZnx, VecZnxDft};
3use std::cmp::min;
4
5/// Vector Matrix Product Prepared Matrix: a vector of [VecZnx],
6/// stored as a 3D matrix in the DFT domain in a single contiguous array.
7/// Each row of the [VmpPMat] can be seen as a [VecZnxDft].
8///
9/// The backend array of [VmpPMat] is allocate in C,
10/// and thus must be manually freed.
11///
12/// [VmpPMat] is used to permform a vector matrix product between a [VecZnx] and a [VmpPMat].
13/// See the trait [VmpPMatOps] for additional information.
14pub struct VmpPMat {
15 /// The pointer to the C memory.
16 pub data: *mut vmp::vmp_pmat_t,
17 /// The number of [VecZnxDft].
18 pub rows: usize,
19 /// The number of limbs in each [VecZnxDft].
20 pub cols: usize,
21 /// The ring degree of each [VecZnxDft].
22 pub n: usize,
23}
24
25impl VmpPMat {
26 /// Returns the pointer to the [vmp_pmat_t].
27 pub fn data(&self) -> *mut vmp::vmp_pmat_t {
28 self.data
29 }
30
31 /// Returns a copy of the backend array at index (i, j) of the [VmpPMat].
32 /// When using [`crate::FFT64`] as backend, `T` should be [f64].
33 /// When using [`crate::NTT120`] as backend, `T` should be [i64].
34 ///
35 /// # Arguments
36 ///
37 /// * `row`: row index (i).
38 /// * `col`: col index (j).
39 pub fn at<T: Default + Copy>(&self, row: usize, col: usize) -> Vec<T> {
40 let mut res: Vec<T> = vec![T::default(); self.n];
41
42 if self.n < 8 {
43 res.copy_from_slice(
44 &self.get_backend_array::<T>()[(row + col * self.rows()) * self.n()
45 ..(row + col * self.rows()) * (self.n() + 1)],
46 );
47 } else {
48 (0..self.n >> 3).for_each(|blk| {
49 res[blk * 8..(blk + 1) * 8].copy_from_slice(&self.get_array(row, col, blk)[..8]);
50 });
51 }
52
53 res
54 }
55
56 /// When using [`crate::FFT64`] as backend, `T` should be [f64].
57 /// When using [`crate::NTT120`] as backend, `T` should be [i64].
58 fn get_array<T>(&self, row: usize, col: usize, blk: usize) -> &[T] {
59 let nrows: usize = self.rows();
60 let ncols: usize = self.cols();
61 if col == (ncols - 1) && (ncols & 1 == 1) {
62 &self.get_backend_array::<T>()[blk * nrows * ncols * 8 + col * nrows * 8 + row * 8..]
63 } else {
64 &self.get_backend_array::<T>()[blk * nrows * ncols * 8
65 + (col / 2) * (2 * nrows) * 8
66 + row * 2 * 8
67 + (col % 2) * 8..]
68 }
69 }
70
71 /// Returns a non-mutable reference of `T` of the entire contiguous array of the [VmpPMat].
72 /// When using [`crate::FFT64`] as backend, `T` should be [f64].
73 /// When using [`crate::NTT120`] as backend, `T` should be [i64].
74 /// The length of the returned array is rows * cols * n.
75 pub fn get_backend_array<T>(&self) -> &[T] {
76 let ptr: *const T = self.data as *const T;
77 let len: usize = (self.rows() * self.cols() * self.n() * 8) / std::mem::size_of::<T>();
78 unsafe { &std::slice::from_raw_parts(ptr, len) }
79 }
80}
81
82/// This trait implements methods for vector matrix product,
83/// that is, multiplying a [VecZnx] with a [VmpPMat].
84pub trait VmpPMatOps {
85 /// Allocates a new [VmpPMat] with the given number of rows and columns.
86 ///
87 /// # Arguments
88 ///
89 /// * `rows`: number of rows (number of [VecZnxDft]).
90 /// * `cols`: number of cols (number of limbs of each [VecZnxDft]).
91 fn new_vmp_pmat(&self, rows: usize, cols: usize) -> VmpPMat;
92
93 /// Returns the number of bytes needed as scratch space for [VmpPMatOps::vmp_prepare_contiguous].
94 ///
95 /// # Arguments
96 ///
97 /// * `rows`: number of rows of the [VmpPMat] used in [VmpPMatOps::vmp_prepare_contiguous].
98 /// * `cols`: number of cols of the [VmpPMat] used in [VmpPMatOps::vmp_prepare_contiguous].
99 fn vmp_prepare_tmp_bytes(&self, rows: usize, cols: usize) -> usize;
100
101 /// Prepares a [VmpPMat] from a contiguous array of [i64].
102 /// The helper struct [Matrix3D] can be used to contruct and populate
103 /// the appropriate contiguous array.
104 ///
105 /// # Arguments
106 ///
107 /// * `b`: [VmpPMat] on which the values are encoded.
108 /// * `a`: the contiguous array of [i64] of the 3D matrix to encode on the [VmpPMat].
109 /// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
110 ///
111 /// # Example
112 /// ```
113 /// use base2k::{Module, Matrix3D, VmpPMat, VmpPMatOps, FFT64, Free};
114 /// use std::cmp::min;
115 ///
116 /// let n: usize = 1024;
117 /// let module = Module::new::<FFT64>(n);
118 /// let rows = 5;
119 /// let cols = 6;
120 ///
121 /// let mut b_mat: Matrix3D<i64> = Matrix3D::new(rows, cols, n);
122 ///
123 /// // Populates the i-th row of b_math with X^1 * 2^(i * log_w) (here log_w is undefined)
124 /// (0..min(rows, cols)).for_each(|i| {
125 /// b_mat.at_mut(i, i)[1] = 1 as i64;
126 /// });
127 ///
128 /// let mut buf: Vec<u8> = vec![u8::default(); module.vmp_prepare_tmp_bytes(rows, cols)];
129 ///
130 /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols);
131 /// module.vmp_prepare_contiguous(&mut vmp_pmat, &b_mat.data, &mut buf);
132 ///
133 /// vmp_pmat.free() // don't forget to free the memory once vmp_pmat is not needed anymore.
134 /// ```
135 fn vmp_prepare_contiguous(&self, b: &mut VmpPMat, a: &[i64], buf: &mut [u8]);
136
137 /// Prepares a [VmpPMat] from a vector of [VecZnx].
138 ///
139 /// # Arguments
140 ///
141 /// * `b`: [VmpPMat] on which the values are encoded.
142 /// * `a`: the vector of [VecZnx] to encode on the [VmpPMat].
143 /// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
144 ///
145 /// The size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
146 ///
147 /// # Example
148 /// ```
149 /// use base2k::{Module, FFT64, Matrix3D, VmpPMat, VmpPMatOps, VecZnx, VecZnxOps, Free};
150 /// use std::cmp::min;
151 ///
152 /// let n: usize = 1024;
153 /// let module: Module = Module::new::<FFT64>(n);
154 /// let rows: usize = 5;
155 /// let cols: usize = 6;
156 ///
157 /// let mut vecznx: Vec<VecZnx>= Vec::new();
158 /// (0..rows).for_each(|_|{
159 /// vecznx.push(module.new_vec_znx(cols));
160 /// });
161 ///
162 /// let mut buf: Vec<u8> = vec![u8::default(); module.vmp_prepare_tmp_bytes(rows, cols)];
163 ///
164 /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols);
165 /// module.vmp_prepare_dblptr(&mut vmp_pmat, &vecznx, &mut buf);
166 ///
167 /// vmp_pmat.free();
168 /// module.free();
169 /// ```
170 fn vmp_prepare_dblptr(&self, b: &mut VmpPMat, a: &Vec<VecZnx>, buf: &mut [u8]);
171
172 /// Prepares the ith-row of [VmpPMat] from a vector of [VecZnx].
173 ///
174 /// # Arguments
175 ///
176 /// * `b`: [VmpPMat] on which the values are encoded.
177 /// * `a`: the vector of [VecZnx] to encode on the [VmpPMat].
178 /// * `row_i`: the index of the row to prepare.
179 /// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
180 ///
181 /// The size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes].
182 /// /// # Example
183 /// ```
184 /// use base2k::{Module, FFT64, Matrix3D, VmpPMat, VmpPMatOps, VecZnx, VecZnxOps, Free};
185 /// use std::cmp::min;
186 ///
187 /// let n: usize = 1024;
188 /// let module: Module = Module::new::<FFT64>(n);
189 /// let rows: usize = 5;
190 /// let cols: usize = 6;
191 ///
192 /// let vecznx = module.new_vec_znx(cols);
193 ///
194 /// let mut buf: Vec<u8> = vec![u8::default(); module.vmp_prepare_tmp_bytes(rows, cols)];
195 ///
196 /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols);
197 /// module.vmp_prepare_row(&mut vmp_pmat, &vecznx, 0, &mut buf);
198 ///
199 /// vmp_pmat.free();
200 /// module.free();
201 /// ```
202 fn vmp_prepare_row(&self, b: &mut VmpPMat, a: &VecZnx, row_i: usize, tmp_bytes: &mut [u8]);
203
204 /// Returns the size of the stratch space necessary for [VmpPMatOps::vmp_apply_dft].
205 ///
206 /// # Arguments
207 ///
208 /// * `c_limbs`: number of limbs of the output [VecZnxDft].
209 /// * `a_limbs`: number of limbs of the input [VecZnx].
210 /// * `rows`: number of rows of the input [VmpPMat].
211 /// * `cols`: number of cols of the input [VmpPMat].
212 fn vmp_apply_dft_tmp_bytes(
213 &self,
214 c_limbs: usize,
215 a_limbs: usize,
216 rows: usize,
217 cols: usize,
218 ) -> usize;
219
220 /// Applies the vector matrix product [VecZnxDft] x [VmpPMat].
221 ///
222 /// A vector matrix product is equivalent to a sum of [crate::SvpPPolOps::svp_apply_dft]
223 /// where each [crate::Scalar] is a limb of the input [VecZnxDft] (equivalent to an [crate::SvpPPol])
224 /// and each vector a [VecZnxDft] (row) of the [VmpPMat].
225 ///
226 /// As such, given an input [VecZnx] of `i` limbs and a [VmpPMat] of `i` rows and
227 /// `j` cols, the output is a [VecZnx] of `j` limbs.
228 ///
229 /// If there is a mismatch between the dimensions the largest valid ones are used.
230 ///
231 /// ```text
232 /// |a b c d| x |e f g| = (a * |e f g| + b * |h i j| + c * |k l m|) = |n o p|
233 /// |h i j|
234 /// |k l m|
235 /// ```
236 /// where each element is a [VecZnxDft].
237 ///
238 /// # Arguments
239 ///
240 /// * `c`: the output of the vector matrix product, as a [VecZnxDft].
241 /// * `a`: the left operand [VecZnx] of the vector matrix product.
242 /// * `b`: the right operand [VmpPMat] of the vector matrix product.
243 /// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_tmp_bytes].
244 ///
245 /// # Example
246 /// ```
247 /// use base2k::{Module, VecZnx, VecZnxOps, VecZnxDft, VmpPMat, VmpPMatOps, FFT64, Free};
248 ///
249 /// let n = 1024;
250 ///
251 /// let module: Module = Module::new::<FFT64>(n);
252 /// let limbs: usize = 5;
253 ///
254 /// let rows: usize = limbs;
255 /// let cols: usize = limbs + 1;
256 /// let c_limbs: usize = cols;
257 /// let a_limbs: usize = limbs;
258 /// let tmp_bytes: usize = module.vmp_apply_dft_tmp_bytes(c_limbs, a_limbs, rows, cols);
259 ///
260 /// let mut buf: Vec<u8> = vec![0; tmp_bytes];
261 /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols);
262 ///
263 /// let a: VecZnx = module.new_vec_znx(limbs);
264 /// let mut c_dft: VecZnxDft = module.new_vec_znx_dft(cols);
265 /// module.vmp_apply_dft(&mut c_dft, &a, &vmp_pmat, &mut buf);
266 ///
267 /// c_dft.free();
268 /// vmp_pmat.free();
269 /// module.free();
270 /// ```
271 fn vmp_apply_dft(&self, c: &mut VecZnxDft, a: &VecZnx, b: &VmpPMat, buf: &mut [u8]);
272
273 /// Returns the size of the stratch space necessary for [VmpPMatOps::vmp_apply_dft_to_dft].
274 ///
275 /// # Arguments
276 ///
277 /// * `c_limbs`: number of limbs of the output [VecZnxDft].
278 /// * `a_limbs`: number of limbs of the input [VecZnxDft].
279 /// * `rows`: number of rows of the input [VmpPMat].
280 /// * `cols`: number of cols of the input [VmpPMat].
281 fn vmp_apply_dft_to_dft_tmp_bytes(
282 &self,
283 c_limbs: usize,
284 a_limbs: usize,
285 rows: usize,
286 cols: usize,
287 ) -> usize;
288
289 /// Applies the vector matrix product [VecZnxDft] x [VmpPMat].
290 /// The size of `buf` is given by [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
291 ///
292 /// A vector matrix product is equivalent to a sum of [crate::SvpPPolOps::svp_apply_dft]
293 /// where each [crate::Scalar] is a limb of the input [VecZnxDft] (equivalent to an [crate::SvpPPol])
294 /// and each vector a [VecZnxDft] (row) of the [VmpPMat].
295 ///
296 /// As such, given an input [VecZnx] of `i` limbs and a [VmpPMat] of `i` rows and
297 /// `j` cols, the output is a [VecZnx] of `j` limbs.
298 ///
299 /// If there is a mismatch between the dimensions the largest valid ones are used.
300 ///
301 /// ```text
302 /// |a b c d| x |e f g| = (a * |e f g| + b * |h i j| + c * |k l m|) = |n o p|
303 /// |h i j|
304 /// |k l m|
305 /// ```
306 /// where each element is a [VecZnxDft].
307 ///
308 /// # Arguments
309 ///
310 /// * `c`: the output of the vector matrix product, as a [VecZnxDft].
311 /// * `a`: the left operand [VecZnxDft] of the vector matrix product.
312 /// * `b`: the right operand [VmpPMat] of the vector matrix product.
313 /// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
314 ///
315 /// # Example
316 /// ```
317 /// use base2k::{Module, VecZnx, VecZnxDft, VmpPMat, VmpPMatOps, FFT64, Free};
318 ///
319 /// let n = 1024;
320 ///
321 /// let module: Module = Module::new::<FFT64>(n);
322 /// let limbs: usize = 5;
323 ///
324 /// let rows: usize = limbs;
325 /// let cols: usize = limbs + 1;
326 /// let c_limbs: usize = cols;
327 /// let a_limbs: usize = limbs;
328 /// let tmp_bytes: usize = module.vmp_apply_dft_to_dft_tmp_bytes(c_limbs, a_limbs, rows, cols);
329 ///
330 /// let mut buf: Vec<u8> = vec![0; tmp_bytes];
331 /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols);
332 ///
333 /// let a_dft: VecZnxDft = module.new_vec_znx_dft(limbs);
334 /// let mut c_dft: VecZnxDft = module.new_vec_znx_dft(cols);
335 /// module.vmp_apply_dft_to_dft(&mut c_dft, &a_dft, &vmp_pmat, &mut buf);
336 ///
337 /// a_dft.free();
338 /// c_dft.free();
339 /// vmp_pmat.free();
340 /// module.free();
341 /// ```
342 fn vmp_apply_dft_to_dft(&self, c: &mut VecZnxDft, a: &VecZnxDft, b: &VmpPMat, buf: &mut [u8]);
343
344 /// Applies the vector matrix product [VecZnxDft] x [VmpPMat] in place.
345 /// The size of `buf` is given by [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
346 ///
347 /// A vector matrix product is equivalent to a sum of [crate::SvpPPolOps::svp_apply_dft]
348 /// where each [crate::Scalar] is a limb of the input [VecZnxDft] (equivalent to an [crate::SvpPPol])
349 /// and each vector a [VecZnxDft] (row) of the [VmpPMat].
350 ///
351 /// As such, given an input [VecZnx] of `i` limbs and a [VmpPMat] of `i` rows and
352 /// `j` cols, the output is a [VecZnx] of `j` limbs.
353 ///
354 /// If there is a mismatch between the dimensions the largest valid ones are used.
355 ///
356 /// ```text
357 /// |a b c d| x |e f g| = (a * |e f g| + b * |h i j| + c * |k l m|) = |n o p|
358 /// |h i j|
359 /// |k l m|
360 /// ```
361 /// where each element is a [VecZnxDft].
362 ///
363 /// # Arguments
364 ///
365 /// * `b`: the input and output of the vector matrix product, as a [VecZnxDft].
366 /// * `a`: the right operand [VmpPMat] of the vector matrix product.
367 /// * `buf`: scratch space, the size can be obtained with [VmpPMatOps::vmp_apply_dft_to_dft_tmp_bytes].
368 ///
369 /// # Example
370 /// ```
371 /// use base2k::{Module, VecZnx, VecZnxOps, VecZnxDft, VmpPMat, VmpPMatOps, FFT64, Free};
372 ///
373 /// let n = 1024;
374 ///
375 /// let module: Module = Module::new::<FFT64>(n);
376 /// let limbs: usize = 5;
377 ///
378 /// let rows: usize = limbs;
379 /// let cols: usize = limbs + 1;
380 /// let tmp_bytes: usize = module.vmp_apply_dft_to_dft_tmp_bytes(limbs, limbs, rows, cols);
381 ///
382 /// let mut buf: Vec<u8> = vec![0; tmp_bytes];
383 /// let a: VecZnx = module.new_vec_znx(limbs);
384 /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols);
385 ///
386 /// let mut c_dft: VecZnxDft = module.new_vec_znx_dft(limbs);
387 /// module.vmp_apply_dft_to_dft_inplace(&mut c_dft, &vmp_pmat, &mut buf);
388 ///
389 /// c_dft.free();
390 /// vmp_pmat.free();
391 /// module.free();
392 /// ```
393 fn vmp_apply_dft_to_dft_inplace(&self, b: &mut VecZnxDft, a: &VmpPMat, buf: &mut [u8]);
394}
395
396impl VmpPMatOps for Module {
397 fn new_vmp_pmat(&self, rows: usize, cols: usize) -> VmpPMat {
398 unsafe {
399 VmpPMat {
400 data: vmp::new_vmp_pmat(self.0, rows as u64, cols as u64),
401 rows,
402 cols,
403 n: self.n(),
404 }
405 }
406 }
407
408 fn vmp_prepare_tmp_bytes(&self, rows: usize, cols: usize) -> usize {
409 unsafe { vmp::vmp_prepare_tmp_bytes(self.0, rows as u64, cols as u64) as usize }
410 }
411
412 fn vmp_prepare_contiguous(&self, b: &mut VmpPMat, a: &[i64], buf: &mut [u8]) {
413 unsafe {
414 vmp::vmp_prepare_contiguous(
415 self.0,
416 b.data(),
417 a.as_ptr(),
418 b.rows() as u64,
419 b.cols() as u64,
420 buf.as_mut_ptr(),
421 );
422 }
423 }
424
425 fn vmp_prepare_dblptr(&self, b: &mut VmpPMat, a: &Vec<VecZnx>, buf: &mut [u8]) {
426 let ptrs: Vec<*const i64> = a.iter().map(|v| v.data.as_ptr()).collect();
427 unsafe {
428 vmp::vmp_prepare_dblptr(
429 self.0,
430 b.data(),
431 ptrs.as_ptr(),
432 b.rows() as u64,
433 b.cols() as u64,
434 buf.as_mut_ptr(),
435 );
436 }
437 }
438
439 fn vmp_prepare_row(&self, b: &mut VmpPMat, a: &VecZnx, row_i: usize, buf: &mut [u8]) {
440 unsafe {
441 vmp::vmp_prepare_row(
442 self.0,
443 b.data(),
444 a.data.as_ptr(),
445 row_i as u64,
446 b.rows() as u64,
447 b.cols() as u64,
448 buf.as_mut_ptr(),
449 );
450 }
451 }
452
453 fn vmp_apply_dft_tmp_bytes(
454 &self,
455 c_limbs: usize,
456 a_limbs: usize,
457 rows: usize,
458 cols: usize,
459 ) -> usize {
460 unsafe {
461 vmp::vmp_apply_dft_tmp_bytes(
462 self.0,
463 c_limbs as u64,
464 a_limbs as u64,
465 rows as u64,
466 cols as u64,
467 ) as usize
468 }
469 }
470
471 fn vmp_apply_dft(&self, c: &mut VecZnxDft, a: &VecZnx, b: &VmpPMat, buf: &mut [u8]) {
472 unsafe {
473 vmp::vmp_apply_dft(
474 self.0,
475 c.0,
476 c.limbs() as u64,
477 a.as_ptr(),
478 a.limbs() as u64,
479 a.n() as u64,
480 b.data(),
481 b.rows() as u64,
482 b.cols() as u64,
483 buf.as_mut_ptr(),
484 )
485 }
486 }
487
488 fn vmp_apply_dft_to_dft_tmp_bytes(
489 &self,
490 c_limbs: usize,
491 a_limbs: usize,
492 rows: usize,
493 cols: usize,
494 ) -> usize {
495 unsafe {
496 vmp::vmp_apply_dft_to_dft_tmp_bytes(
497 self.0,
498 c_limbs as u64,
499 a_limbs as u64,
500 rows as u64,
501 cols as u64,
502 ) as usize
503 }
504 }
505
506 fn vmp_apply_dft_to_dft(&self, c: &mut VecZnxDft, a: &VecZnxDft, b: &VmpPMat, buf: &mut [u8]) {
507 unsafe {
508 vmp::vmp_apply_dft_to_dft(
509 self.0,
510 c.0,
511 c.limbs() as u64,
512 a.0,
513 a.limbs() as u64,
514 b.data(),
515 b.rows() as u64,
516 b.cols() as u64,
517 buf.as_mut_ptr(),
518 )
519 }
520 }
521
522 fn vmp_apply_dft_to_dft_inplace(&self, b: &mut VecZnxDft, a: &VmpPMat, buf: &mut [u8]) {
523 unsafe {
524 vmp::vmp_apply_dft_to_dft(
525 self.0,
526 b.0,
527 b.limbs() as u64,
528 b.0,
529 b.limbs() as u64,
530 a.data(),
531 a.rows() as u64,
532 a.cols() as u64,
533 buf.as_mut_ptr(),
534 )
535 }
536 }
537}
538
539/// A helper struture that stores a 3D matrix as a contiguous array.
540/// To be passed to [VmpPMatOps::vmp_prepare_contiguous].
541///
542/// rows: index of the i-th base2K power.
543/// cols: index of the j-th limb of the i-th row.
544/// n : polynomial degree.
545///
546/// A [Matrix3D] can be seen as a vector of [VecZnx].
547pub struct Matrix3D<T> {
548 pub data: Vec<T>,
549 pub rows: usize,
550 pub cols: usize,
551 pub n: usize,
552}
553
554impl<T: Default + Clone + std::marker::Copy> Matrix3D<T> {
555 /// Allocates a new [Matrix3D] with the respective dimensions.
556 ///
557 /// # Arguments
558 ///
559 /// * `rows`: the number of rows of the matrix.
560 /// * `cols`: the number of cols of the matrix.
561 /// # `n`: the size of each entry of the matrix.
562 ///
563 /// # Example
564 /// ```
565 /// use base2k::Matrix3D;
566 ///
567 /// let rows = 5; // #decomp
568 /// let cols = 5; // #limbs
569 /// let n = 1024; // #coeffs
570 ///
571 /// let mut mat = Matrix3D::<i64>::new(rows, cols, n);
572 /// ```
573 pub fn new(rows: usize, cols: usize, n: usize) -> Self {
574 let size = rows * cols * n;
575 Self {
576 data: vec![T::default(); size],
577 rows,
578 cols,
579 n,
580 }
581 }
582
583 /// Returns a non-mutable reference to the entry (row, col) of the [Matrix3D].
584 /// The returned array is of size n.
585 ///
586 /// # Arguments
587 ///
588 /// * `row`: the index of the row.
589 /// * `col`: the index of the col.
590 ///
591 /// # Example
592 /// ```
593 /// use base2k::Matrix3D;
594 ///
595 /// let rows = 5; // #decomp
596 /// let cols = 5; // #limbs
597 /// let n = 1024; // #coeffs
598 ///
599 /// let mut mat = Matrix3D::<i64>::new(rows, cols, n);
600 ///
601 /// let elem: &[i64] = mat.at(4, 4); // size n
602 /// ```
603 pub fn at(&self, row: usize, col: usize) -> &[T] {
604 assert!(row < self.rows && col < self.cols);
605 let idx: usize = row * (self.n * self.cols) + col * self.n;
606 &self.data[idx..idx + self.n]
607 }
608
609 /// Returns a mutable reference of the array at the (row, col) entry of the [Matrix3D].
610 /// The returned array is of size n.
611 ///
612 /// # Arguments
613 ///
614 /// * `row`: the index of the row.
615 /// * `col`: the index of the col.
616 ///
617 /// # Example
618 /// ```
619 /// use base2k::Matrix3D;
620 ///
621 /// let rows = 5; // #decomp
622 /// let cols = 5; // #limbs
623 /// let n = 1024; // #coeffs
624 ///
625 /// let mut mat = Matrix3D::<i64>::new(rows, cols, n);
626 ///
627 /// let elem: &mut [i64] = mat.at_mut(4, 4); // size n
628 /// ```
629 pub fn at_mut(&mut self, row: usize, col: usize) -> &mut [T] {
630 assert!(row < self.rows && col < self.cols);
631 let idx: usize = row * (self.n * self.cols) + col * self.n;
632 &mut self.data[idx..idx + self.n]
633 }
634
635 /// Sets the entry \[row\] of the [Matrix3D].
636 /// Typicall this is used to assign a [VecZnx] to the i-th row
637 /// of the [Matrix3D].
638 ///
639 /// # Arguments
640 ///
641 /// * `row`: the index of the row.
642 /// * `a`: the data to encode onthe row.
643 ///
644 /// # Example
645 /// ```
646 /// use base2k::{Matrix3D, VecZnx};
647 ///
648 /// let rows = 5; // #decomp
649 /// let cols = 5; // #limbs
650 /// let n = 1024; // #coeffs
651 ///
652 /// let mut mat = Matrix3D::<i64>::new(rows, cols, n);
653 ///
654 /// let a: VecZnx = VecZnx::new(n, cols);
655 ///
656 /// mat.set_row(1, &a.data);
657 /// ```
658 pub fn set_row(&mut self, row: usize, a: &[T]) {
659 assert!(
660 row < self.rows,
661 "invalid argument row: row={} > self.rows={}",
662 row,
663 self.rows
664 );
665 let idx: usize = row * (self.n * self.cols);
666 let size: usize = min(a.len(), self.cols * self.n);
667 self.data[idx..idx + size].copy_from_slice(&a[..size]);
668 }
669}