qfall_math/integer_mod_q/
ntt_polynomial_ring_zq.rs

1// Copyright © 2025 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//! [`NTTPolynomialRingZq`] containts the NTT representations of polynomials.
10
11use crate::{
12    integer::Z,
13    integer_mod_q::{ModulusPolynomialRingZq, mat_ntt_polynomial_ring_zq::print_vec_z},
14};
15use derive_more::Display;
16use serde::{Deserialize, Serialize};
17use std::fmt;
18
19mod arithmetic;
20mod cmp;
21mod from;
22mod get;
23mod sample;
24
25/// [`NTTPolynomialRingZq`] contains the NTT representation of some polynomial with respect to
26/// a [`NTTBasisPolynomialRingZq`](super::NTTBasisPolynomialRingZq) that itself isn't aware of.
27///
28/// Attributes
29/// - `poly`: holds the coefficients
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)
32///
33/// # Examples
34/// ```
35/// use qfall_math::integer_mod_q::{Modulus, PolynomialRingZq, NTTPolynomialRingZq, ModulusPolynomialRingZq};
36/// use std::str::FromStr;
37/// // Setup modulus with capability to perform NTT transform
38/// let mut modulus = ModulusPolynomialRingZq::from_str("5  1 0 0 0 1 mod 257").unwrap();
39/// modulus.set_ntt_unchecked(64);
40///
41/// // sample random polynomial
42/// let rnd = NTTPolynomialRingZq::sample_uniform(&modulus);
43/// // or instantiate polynomial from PolynomialRingZq (or PolyOverZq)
44///
45/// let poly_ring = PolynomialRingZq::sample_uniform(&modulus);
46/// let ntt_poly_ring = NTTPolynomialRingZq::from(&poly_ring);
47///
48/// // multiply, add and subtract objects
49/// let mod_q = Modulus::from(modulus.get_q());
50/// let mut tmp_ntt = ntt_poly_ring * &rnd;
51/// tmp_ntt += &rnd;
52/// tmp_ntt -= &rnd;
53///
54/// // Return to PolynomialRingZq
55/// let res = tmp_ntt.inv_ntt();
56/// ```
57#[derive(PartialEq, Eq, Serialize, Deserialize, Display, Clone)]
58#[display("{} / {}", print_vec_z(&self.poly), self.modulus)]
59pub struct NTTPolynomialRingZq {
60    pub poly: Vec<Z>,
61    pub modulus: ModulusPolynomialRingZq,
62}
63
64impl fmt::Debug for NTTPolynomialRingZq {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        let short_print = print_vec_z(&self.poly);
67        let a: Vec<&str> = short_print.split_whitespace().collect();
68        let short_print = format!("{}{} ..., {}{}", a[0], a[1], a[a.len() - 2], a[a.len() - 1]);
69
70        write!(
71            f,
72            "NTTPolynomialRingZq {{poly: {}, modulus: {}, storage: {{poly: {:?}, modulus: {:?}}}}}",
73            short_print, self.modulus, self.poly, self.modulus
74        )
75    }
76}