pub struct Polynomial { /* private fields */ }Expand description
Dense polynomial representation with fixed metadata (max_degree, modulus).
Internally, coeffs always has length max_degree + 1. Coefficients beyond the true degree
are stored as zeros.
Implementations§
Source§impl Polynomial
impl Polynomial
Sourcepub fn new(
max_degree: usize,
modulus: u64,
coeffs: &[u64],
) -> Result<Self, PolynomialError>
pub fn new( max_degree: usize, modulus: u64, coeffs: &[u64], ) -> Result<Self, PolynomialError>
Creates a polynomial with degree bound max_degree over modulus modulus.
Coefficients are normalized modulo modulus and then zero-padded to length
max_degree + 1.
§Errors
Returns:
PolynomialError::InvalidModuluswhenmodulus < 2,PolynomialError::DegreeOverflowwhencoeffs.len() > max_degree + 1.
Sourcepub fn zero(max_degree: usize, modulus: u64) -> Result<Self, PolynomialError>
pub fn zero(max_degree: usize, modulus: u64) -> Result<Self, PolynomialError>
Returns the additive identity polynomial 0.
Sourcepub fn one(max_degree: usize, modulus: u64) -> Result<Self, PolynomialError>
pub fn one(max_degree: usize, modulus: u64) -> Result<Self, PolynomialError>
Returns the multiplicative identity polynomial 1.
Sourcepub fn max_degree(&self) -> usize
pub fn max_degree(&self) -> usize
Returns the configured maximum degree n.
Sourcepub fn coefficients(&self) -> &[u64]
pub fn coefficients(&self) -> &[u64]
Returns the internal dense coefficient slice of length max_degree + 1.
Sourcepub fn trimmed_coefficients(&self) -> Vec<u64>
pub fn trimmed_coefficients(&self) -> Vec<u64>
Returns coefficients trimmed to the actual degree.
Zero polynomial is returned as [0].
Sourcepub fn coeff(&self, degree: usize) -> Option<u64>
pub fn coeff(&self, degree: usize) -> Option<u64>
Returns coefficient of x^degree, or None when out of bounds.
Sourcepub fn degree(&self) -> Option<usize>
pub fn degree(&self) -> Option<usize>
Returns the true polynomial degree, or None for zero polynomial.
Sourcepub fn add(&self, rhs: &Self) -> Result<Self, PolynomialError>
pub fn add(&self, rhs: &Self) -> Result<Self, PolynomialError>
Adds two polynomials coefficient-wise in Z_q[x].
§Errors
Returns PolynomialError::IncompatiblePolynomials when n or q differ.
Sourcepub fn sub(&self, rhs: &Self) -> Result<Self, PolynomialError>
pub fn sub(&self, rhs: &Self) -> Result<Self, PolynomialError>
Subtracts rhs from self coefficient-wise in Z_q[x].
§Errors
Returns PolynomialError::IncompatiblePolynomials when n or q differ.
Sourcepub fn scalar_mul(&self, scalar: u64) -> Self
pub fn scalar_mul(&self, scalar: u64) -> Self
Multiplies all coefficients by scalar in Z_q.
Sourcepub fn mul(&self, rhs: &Self) -> Result<Self, PolynomialError>
pub fn mul(&self, rhs: &Self) -> Result<Self, PolynomialError>
Schoolbook polynomial multiplication with strict degree bound enforcement.
§Errors
Returns:
PolynomialError::IncompatiblePolynomialswhennorqdiffer,PolynomialError::ProductDegreeOverflowif exact product degree exceedsmax_degree.
Sourcepub fn mul_ntt(
&self,
rhs: &Self,
primitive_root: u64,
) -> Result<Self, PolynomialError>
pub fn mul_ntt( &self, rhs: &Self, primitive_root: u64, ) -> Result<Self, PolynomialError>
NTT-based polynomial multiplication.
This computes exact convolution via NTT and then places the result in the dense output
buffer (length max_degree + 1).
§Errors
Returns:
PolynomialError::IncompatiblePolynomialswhennorqdiffer,PolynomialError::ProductDegreeOverflowif exact product degree exceedsmax_degree,- NTT-specific errors when modulus/root parameters are not valid.
Sourcepub fn mul_truncated(&self, rhs: &Self) -> Result<Self, PolynomialError>
pub fn mul_truncated(&self, rhs: &Self) -> Result<Self, PolynomialError>
Schoolbook multiplication truncated to degree max_degree.
Unlike Self::mul, this method never returns degree-overflow errors; terms above
max_degree are discarded.
§Errors
Returns PolynomialError::IncompatiblePolynomials when n or q differ.
Sourcepub fn rem_mod_poly(&self, modulus_poly: &Self) -> Result<Self, PolynomialError>
pub fn rem_mod_poly(&self, modulus_poly: &Self) -> Result<Self, PolynomialError>
Reduces self modulo modulus_poly, returning self mod modulus_poly in Z_q[x].
§Errors
Returns:
PolynomialError::IncompatiblePolynomialswhennorqdiffer,PolynomialError::DivisionByZeroPolynomialifmodulus_polyis zero,PolynomialError::NonInvertibleCoefficientif leading coefficient ofmodulus_polyis not invertible inZ_q.
Sourcepub fn add_mod_poly(
&self,
rhs: &Self,
modulus_poly: &Self,
) -> Result<Self, PolynomialError>
pub fn add_mod_poly( &self, rhs: &Self, modulus_poly: &Self, ) -> Result<Self, PolynomialError>
Adds two polynomials and reduces modulo modulus_poly.
Sourcepub fn sub_mod_poly(
&self,
rhs: &Self,
modulus_poly: &Self,
) -> Result<Self, PolynomialError>
pub fn sub_mod_poly( &self, rhs: &Self, modulus_poly: &Self, ) -> Result<Self, PolynomialError>
Subtracts two polynomials and reduces modulo modulus_poly.
Sourcepub fn mul_mod_poly(
&self,
rhs: &Self,
modulus_poly: &Self,
) -> Result<Self, PolynomialError>
pub fn mul_mod_poly( &self, rhs: &Self, modulus_poly: &Self, ) -> Result<Self, PolynomialError>
Multiplies two polynomials and reduces modulo modulus_poly.
This computes the exact product first (in a temporary buffer sized to exact degree), then applies polynomial reduction.
§Errors
Returns compatibility and reduction-related errors as described by Self::rem_mod_poly.
Sourcepub fn derivative(&self) -> Self
pub fn derivative(&self) -> Self
Returns formal derivative d/dx(self) in Z_q[x].
Sourcepub fn div_rem(&self, divisor: &Self) -> Result<(Self, Self), PolynomialError>
pub fn div_rem(&self, divisor: &Self) -> Result<(Self, Self), PolynomialError>
Polynomial long division in Z_q[x]: returns (quotient, remainder).
§Errors
Returns:
PolynomialError::IncompatiblePolynomialswhennorqdiffer,PolynomialError::DivisionByZeroPolynomialif divisor is zero,PolynomialError::NonInvertibleCoefficientif divisor leading coefficient has no inverse inZ_q.
Trait Implementations§
Source§impl Clone for Polynomial
impl Clone for Polynomial
Source§fn clone(&self) -> Polynomial
fn clone(&self) -> Polynomial
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more