qfall_math/integer_mod_q/mat_polynomial_ring_zq.rs
1// Copyright © 2023 Marcel Luca Schmidt
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! [`MatPolynomialRingZq`] is a type of matrix with entries of [`PolynomialRingZq`](crate::integer_mod_q::PolynomialRingZq).
10//! This implementation uses the [FLINT](https://flintlib.org/) library.
11
12use super::ModulusPolynomialRingZq;
13use crate::{integer::MatPolyOverZ, utils::parse::partial_string};
14use derive_more::Display;
15use serde::{Deserialize, Serialize};
16use std::fmt;
17
18mod arithmetic;
19mod cmp;
20mod coefficient_embedding;
21mod concat;
22mod default;
23mod from;
24mod get;
25mod norm;
26mod properties;
27mod reduce;
28mod sample;
29mod set;
30mod sort;
31mod tensor;
32mod to_string;
33mod transpose;
34mod unsafe_functions;
35mod vector;
36
37/// [`MatPolynomialRingZq`] is a matrix with entries of type [`PolynomialRingZq`](crate::integer_mod_q::PolynomialRingZq).
38///
39/// Attributes:
40/// - `matrix`: holds the [`MatPolyOverZ`] matrix
41/// - `modulus`: holds the [`ModulusPolynomialRingZq`] modulus of the matrix
42///
43/// # Examples
44/// ## Matrix usage
45/// ```
46/// use qfall_math::{
47/// integer::{PolyOverZ, MatPolyOverZ},
48/// integer_mod_q::{MatPolynomialRingZq, PolyOverZq},
49/// traits::{MatrixGetEntry, MatrixSetEntry},
50/// };
51/// use std::str::FromStr;
52///
53/// // instantiate new matrix
54/// let id_mat = MatPolyOverZ::identity(2, 2);
55/// // instantiate modulus_object
56/// let modulus = PolyOverZq::from_str("5 1 0 0 0 1 mod 17").unwrap();
57///
58/// let poly_mat = MatPolynomialRingZq::from((id_mat, modulus));
59///
60/// // clone object, set and get entry
61/// let mut clone = poly_mat.clone();
62/// clone.set_entry(0, 0, PolyOverZ::from(-16));
63///
64/// let entry: PolyOverZ = clone.get_entry(0,0).unwrap();
65/// assert_eq!(
66/// entry,
67/// PolyOverZ::from(1),
68/// );
69///
70/// // to_string
71/// assert_eq!("[[1 1, 0],[0, 1 1]] / 5 1 0 0 0 1 mod 17", &poly_mat.to_string());
72/// ```
73///
74/// ## Vector usage
75/// ```
76/// use qfall_math::{
77/// integer::{PolyOverZ, MatPolyOverZ},
78/// integer_mod_q::{MatPolynomialRingZq, PolyOverZq},
79/// };
80/// use std::str::FromStr;
81///
82/// let row_vec = MatPolyOverZ::from_str("[[1 1, 0, 1 1]]").unwrap();
83/// let col_vec = MatPolyOverZ::from_str("[[1 -5],[1 -1],[0]]").unwrap();
84///
85/// let modulus = PolyOverZq::from_str("5 1 0 0 0 1 mod 17").unwrap();
86///
87/// let row_vec = MatPolynomialRingZq::from((row_vec, modulus));
88/// let col_vec = MatPolynomialRingZq::from((col_vec, row_vec.get_mod()));
89///
90/// // check if matrix instance is vector
91/// assert!(row_vec.is_row_vector());
92/// assert!(col_vec.is_column_vector());
93/// ```
94#[derive(PartialEq, Eq, Serialize, Deserialize, Display, Clone)]
95#[display("{matrix} / {modulus}")]
96pub struct MatPolynomialRingZq {
97 pub(crate) matrix: MatPolyOverZ,
98 pub(crate) modulus: ModulusPolynomialRingZq,
99}
100
101impl fmt::Debug for MatPolynomialRingZq {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 write!(
104 f,
105 "MatPolynomialRingZq: {{matrix: {}, modulus: {} storage: {{matrix: {:?}, modulus: {:?}}}}}",
106 // printing the entire matrix is not meaningful for large matrices
107 partial_string(&self.get_representative_least_nonnegative_residue(), 3, 3),
108 self.modulus,
109 self.matrix,
110 self.modulus
111 )
112 }
113}