Skip to main content

qfall_math/integer_mod_q/mat_polynomial_ring_zq/arithmetic/
neg.rs

1// Copyright 2026 Jan Niklas Siemer
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//! Implementation of the [`Neg`] trait for [`MatPolynomialRingZq`] values.
10
11use super::super::MatPolynomialRingZq;
12use flint3_sys::fmpz_poly_mat_neg;
13use std::ops::Neg;
14
15impl Neg for MatPolynomialRingZq {
16    type Output = MatPolynomialRingZq;
17
18    /// Implements the [`Neg`] trait for [`MatPolynomialRingZq`] values.
19    /// [`Neg`] is implemented for both borrowed and owned [`MatPolynomialRingZq`].
20    ///
21    /// When called on owned [`MatPolynomialRingZq`] it will reuse the memory of `self`.
22    ///
23    /// Returns the additive inverse of `self` as a [`MatPolynomialRingZq`].
24    ///
25    /// # Examples
26    /// ```
27    /// use qfall_math::integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq};
28    /// use qfall_math::integer::MatPolyOverZ;
29    /// use std::str::FromStr;
30    ///
31    /// let modulus = ModulusPolynomialRingZq::from_str("4  1 0 0 1 mod 17").unwrap();
32    /// let poly_mat = MatPolyOverZ::from_str("[[2  1 2, 0],[1  42, 3  1 2 3]]").unwrap();
33    /// let a: MatPolynomialRingZq = MatPolynomialRingZq::from((&poly_mat, &modulus));
34    ///
35    /// let b: MatPolynomialRingZq = -&a;
36    /// let c: MatPolynomialRingZq = -a;
37    /// ```
38    fn neg(mut self) -> Self::Output {
39        unsafe { fmpz_poly_mat_neg(&mut self.matrix.matrix, &self.matrix.matrix) };
40        self.reduce();
41        self
42    }
43}
44
45impl Neg for &MatPolynomialRingZq {
46    type Output = MatPolynomialRingZq;
47
48    /// Documentation at [`MatPolynomialRingZq::neg`].
49    fn neg(self) -> Self::Output {
50        let mut out = MatPolynomialRingZq {
51            matrix: -&self.matrix,
52            modulus: self.modulus.clone(),
53        };
54        out.reduce();
55        out
56    }
57}
58
59#[cfg(test)]
60mod test_neg {
61    use super::MatPolynomialRingZq;
62    use crate::integer::MatPolyOverZ;
63    use crate::integer_mod_q::ModulusPolynomialRingZq;
64    use std::str::FromStr;
65
66    /// Ensure that `neg` works for small coefficients.
67    #[test]
68    fn correct_small() {
69        let modulus = ModulusPolynomialRingZq::from_str("4  1 0 0 1 mod 17").unwrap();
70        let mat_a = MatPolyOverZ::from_str("[[2  1 -2, 0],[1  -3, 3  1 0 4]]").unwrap();
71        let mat_b = MatPolyOverZ::from_str("[[2  -1 2, 0],[1  3, 3  -1 0 -4]]").unwrap();
72        let a = MatPolynomialRingZq::from((&mat_a, &modulus));
73        let b = MatPolynomialRingZq::from((&mat_b, &modulus));
74        let c = MatPolynomialRingZq::new(2, 2, &modulus);
75
76        assert_eq!(a, -(-&a));
77        assert_eq!(a, -&b);
78        assert_eq!(b, -&a);
79        assert_eq!(a, -b.clone());
80        assert_eq!(b, -a);
81        assert_eq!(c, -&c);
82        assert_eq!(c.clone(), -c);
83    }
84
85    /// Ensure that `neg` works for large coefficients and moduli.
86    #[test]
87    fn correct_large() {
88        let modulus =
89            ModulusPolynomialRingZq::from_str(&format!("4  1 0 0 1 mod {}", u64::MAX)).unwrap();
90        let mat_a = MatPolyOverZ::from_str(&format!("[[2  {} 1],[0]]", i64::MAX)).unwrap();
91        let mat_b = MatPolyOverZ::from_str(&format!("[[2  -{} -1],[0]]", i64::MAX)).unwrap();
92        let a = MatPolynomialRingZq::from((&mat_a, &modulus));
93        let b = MatPolynomialRingZq::from((&mat_b, &modulus));
94
95        assert_eq!(a, -(-&a));
96        assert_eq!(b, -&a);
97        assert_eq!(b, -a);
98    }
99
100    /// Ensure that the result is reduced to the least non-negative residues
101    /// and that the modulus is kept.
102    #[test]
103    fn reduced() {
104        let modulus = ModulusPolynomialRingZq::from_str("4  1 0 0 1 mod 17").unwrap();
105        let mat_a = MatPolyOverZ::from_str("[[2  1 16, 0],[1  8, 3  1 0 4]]").unwrap();
106        let a = MatPolynomialRingZq::from((&mat_a, &modulus));
107        let cmp = MatPolyOverZ::from_str("[[2  16 1, 0],[1  9, 3  16 0 13]]").unwrap();
108
109        let neg_a = -&a;
110
111        assert_eq!(cmp, neg_a.get_representative_least_nonnegative_residue());
112        assert_eq!(cmp, (-a).get_representative_least_nonnegative_residue());
113        assert_eq!(modulus, neg_a.get_mod());
114    }
115}