Skip to main content

qfall_math/integer_mod_q/
ntt_polynomial_ring_zq.rs

1// Copyright 2025 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//! [`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};
17
18mod arithmetic;
19mod cmp;
20mod from;
21mod get;
22mod sample;
23
24/// [`NTTPolynomialRingZq`] contains the NTT representation of some polynomial with respect to
25/// a [`NTTBasisPolynomialRingZq`](super::NTTBasisPolynomialRingZq) that itself isn't aware of.
26///
27/// Attributes
28/// - `poly`: holds the coefficients
29/// - `modulus`: the [`ModulusPolynomialRingZq`] defining the modulus `q`, the ring `Z_q[X]/f(X)`, and
30///   the NTT transform [`NTTBasisPolynomialRingZq`](crate::integer_mod_q::NTTBasisPolynomialRingZq)
31///
32/// # Examples
33/// ```
34/// use qfall_math::integer_mod_q::{Modulus, PolynomialRingZq, NTTPolynomialRingZq, ModulusPolynomialRingZq};
35/// use std::str::FromStr;
36/// // Setup modulus with capability to perform NTT transform
37/// let mut modulus = ModulusPolynomialRingZq::from_str("5  1 0 0 0 1 mod 257").unwrap();
38/// modulus.set_ntt_unchecked(64);
39///
40/// // sample random polynomial
41/// let rnd = NTTPolynomialRingZq::sample_uniform(&modulus);
42/// // or instantiate polynomial from PolynomialRingZq (or PolyOverZq)
43///
44/// let poly_ring = PolynomialRingZq::sample_uniform(&modulus);
45/// let ntt_poly_ring = NTTPolynomialRingZq::from(&poly_ring);
46///
47/// // multiply, add and subtract objects
48/// let mod_q = Modulus::from(modulus.get_q());
49/// let mut tmp_ntt = ntt_poly_ring * &rnd;
50/// tmp_ntt += &rnd;
51/// tmp_ntt -= &rnd;
52///
53/// // Return to PolynomialRingZq
54/// let res = tmp_ntt.inv_ntt();
55/// ```
56#[derive(PartialEq, Eq, Serialize, Deserialize, Display, Clone, Debug)]
57#[display("{} / {}", print_vec_z(&self.poly), self.modulus)]
58pub struct NTTPolynomialRingZq {
59    pub poly: Vec<Z>,
60    pub modulus: ModulusPolynomialRingZq,
61}