qfall_math/integer_mod_q/modulus_polynomial_ring_zq/
norm.rs

1// Copyright © 2024 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//! This module includes functionality to compute several norms
10//! defined on polynomials.
11
12use super::ModulusPolynomialRingZq;
13use crate::{integer::Z, integer_mod_q::PolyOverZq};
14
15impl ModulusPolynomialRingZq {
16    /// Returns the squared Euclidean norm or 2-norm of the given polynomial.
17    /// The squared Euclidean norm for a polynomial is obtained by treating the coefficients
18    /// of the polynomial as a vector and then applying the standard squared Euclidean norm.
19    ///
20    /// # Examples
21    /// ```
22    /// use qfall_math::{integer::Z, integer_mod_q::ModulusPolynomialRingZq};
23    /// use std::str::FromStr;
24    ///
25    /// let poly = ModulusPolynomialRingZq::from_str("3  1 2 3 mod 11").unwrap();
26    ///
27    /// let sqrd_2_norm = poly.norm_eucl_sqrd();
28    ///
29    /// // 1*1 + 2*2 + 3*3 = 14
30    /// assert_eq!(Z::from(14), sqrd_2_norm);
31    /// ```
32    pub fn norm_eucl_sqrd(&self) -> Z {
33        PolyOverZq::from(self).norm_eucl_sqrd()
34    }
35
36    /// Returns the infinity norm or the maximal absolute value of a
37    /// coefficient of the given polynomial.
38    /// The infinity norm for a polynomial is obtained by treating the coefficients
39    /// of the polynomial as a vector and then applying the standard infinity norm.
40    ///
41    /// # Examples
42    /// ```
43    /// use qfall_math::{integer::Z, integer_mod_q::ModulusPolynomialRingZq};
44    /// use std::str::FromStr;
45    ///
46    /// let poly = ModulusPolynomialRingZq::from_str("3  1 2 4 mod 7").unwrap();
47    ///
48    /// let infty_norm = poly.norm_infty();
49    ///
50    /// // max coefficient is 4 = -3
51    /// assert_eq!(Z::from(3), infty_norm);
52    /// ```
53    pub fn norm_infty(&self) -> Z {
54        PolyOverZq::from(self).norm_infty()
55    }
56}
57
58#[cfg(test)]
59mod test_norms {
60    use super::Z;
61    use crate::integer_mod_q::ModulusPolynomialRingZq;
62    use std::str::FromStr;
63
64    /// Check whether the norms can be computed for [`ModulusPolynomialRingZq`].
65    /// Correctness is already checked for [`PolyOverZq`](crate::integer_mod_q::PolyOverZq).
66    #[test]
67    fn availability() {
68        let poly = ModulusPolynomialRingZq::from_str("3  1 2 3 mod 11").unwrap();
69
70        let norm_es = poly.norm_eucl_sqrd();
71        let norm_i = poly.norm_infty();
72
73        assert_eq!(Z::from(14), norm_es);
74        assert_eq!(Z::from(3), norm_i);
75    }
76}