Skip to main content

Polynomial

Struct Polynomial 

Source
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

Source

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:

Source

pub fn zero(max_degree: usize, modulus: u64) -> Result<Self, PolynomialError>

Returns the additive identity polynomial 0.

Source

pub fn one(max_degree: usize, modulus: u64) -> Result<Self, PolynomialError>

Returns the multiplicative identity polynomial 1.

Source

pub fn max_degree(&self) -> usize

Returns the configured maximum degree n.

Source

pub fn modulus(&self) -> u64

Returns the coefficient modulus q.

Source

pub fn coefficients(&self) -> &[u64]

Returns the internal dense coefficient slice of length max_degree + 1.

Source

pub fn trimmed_coefficients(&self) -> Vec<u64>

Returns coefficients trimmed to the actual degree.

Zero polynomial is returned as [0].

Source

pub fn coeff(&self, degree: usize) -> Option<u64>

Returns coefficient of x^degree, or None when out of bounds.

Source

pub fn degree(&self) -> Option<usize>

Returns the true polynomial degree, or None for zero polynomial.

Source

pub fn is_zero(&self) -> bool

Returns true when all coefficients are zero.

Source

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.

Source

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.

Source

pub fn neg(&self) -> Self

Returns additive inverse -self in Z_q[x].

Source

pub fn scalar_mul(&self, scalar: u64) -> Self

Multiplies all coefficients by scalar in Z_q.

Source

pub fn mul(&self, rhs: &Self) -> Result<Self, PolynomialError>

Schoolbook polynomial multiplication with strict degree bound enforcement.

§Errors

Returns:

Source

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:

Source

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.

Source

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:

Source

pub fn add_mod_poly( &self, rhs: &Self, modulus_poly: &Self, ) -> Result<Self, PolynomialError>

Adds two polynomials and reduces modulo modulus_poly.

Source

pub fn sub_mod_poly( &self, rhs: &Self, modulus_poly: &Self, ) -> Result<Self, PolynomialError>

Subtracts two polynomials and reduces modulo modulus_poly.

Source

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.

Source

pub fn evaluate(&self, x: u64) -> u64

Evaluates polynomial at x using Horner’s method in Z_q.

Source

pub fn derivative(&self) -> Self

Returns formal derivative d/dx(self) in Z_q[x].

Source

pub fn div_rem(&self, divisor: &Self) -> Result<(Self, Self), PolynomialError>

Polynomial long division in Z_q[x]: returns (quotient, remainder).

§Errors

Returns:

Trait Implementations§

Source§

impl Clone for Polynomial

Source§

fn clone(&self) -> Polynomial

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Polynomial

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Polynomial

Source§

fn eq(&self, other: &Polynomial) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Polynomial

Source§

impl StructuralPartialEq for Polynomial

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.