Skip to main content

qfall_math/integer_mod_q/
polynomial_ring_zq.rs

1// Copyright 2023 Marvin Beckmann
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//! [`PolynomialRingZq`] is a type of ring over PolyOverZq/f(X).
10//! Where f(X) is a [`PolyOverZq`](crate::integer_mod_q::PolyOverZq).
11//! This implementation uses the [FLINT](https://flintlib.org/) library.
12
13// For **DEVELOPERS**: Many functions assume that the [`PolynomialRingZq`] instances are reduced.
14// To avoid unnecessary checks and reductions, always return canonical/reduced
15// values. The end-user should be unable to obtain a non-reduced value.
16// Therefore, the DEVELOPER has to call the [`PolynomialRingZq::reduce`], whenever
17// a computation may exceed the modulus, because it is not reduced automatically
18
19use super::ModulusPolynomialRingZq;
20use crate::integer::PolyOverZ;
21use derive_more::Display;
22use serde::{Deserialize, Serialize};
23
24mod arithmetic;
25mod cmp;
26mod coefficient_embedding;
27mod from;
28mod get;
29mod norm;
30mod properties;
31mod reduce;
32mod sample;
33mod set;
34mod to_string;
35mod unsafe_functions;
36
37/// [`PolynomialRingZq`] represents polynomials over the finite field
38/// [`PolyOverZq`](crate::integer_mod_q::PolyOverZq)/f(X) where f(X) is a polynomial over [`Zq`](super::Zq).
39///
40/// Attributes
41/// - `poly`: holds the value
42/// - `modulus`: holds the modulus q and f(X)
43///
44/// # Examples
45/// ```
46/// # use qfall_math::error::MathError;
47/// use qfall_math::integer::PolyOverZ;
48/// use qfall_math::integer_mod_q::ModulusPolynomialRingZq;
49/// use qfall_math::integer_mod_q::PolyOverZq;
50/// use qfall_math::integer_mod_q::PolynomialRingZq;
51/// use std::str::FromStr;
52///
53/// let poly_mod = PolyOverZq::from_str("3  1 0 1 mod 17").unwrap();
54/// let modulus = ModulusPolynomialRingZq::from(poly_mod);
55///
56/// // instantiation
57/// let a = PolynomialRingZq::from((PolyOverZ::from(5), &modulus));
58/// let b = PolynomialRingZq::from((PolyOverZ::from_str("2  1 5").unwrap(), &modulus));
59/// let _ = a.clone();
60///
61/// // arithmetics
62/// let _ = &a + &b;
63/// let _ = &a * &b;
64///
65/// // to_string incl. (de-)serialization
66/// assert_eq!("1  5 / 3  1 0 1 mod 17", &a.to_string());
67/// let _ = serde_json::to_string(&a).unwrap();
68///
69/// # Ok::<(), MathError>(())
70/// ```
71#[derive(PartialEq, Eq, Serialize, Deserialize, Display, Clone, Debug)]
72#[display("{poly} / {modulus}")]
73pub struct PolynomialRingZq {
74    pub(crate) poly: PolyOverZ,
75    pub(crate) modulus: ModulusPolynomialRingZq,
76}