ModPoly

Struct ModPoly 

Source
pub struct ModPoly { /* private fields */ }
Expand description

A polynomial with modular integer coefficients.

That is, a member of Z/nZ[X].

§Examples

See constructors:

Implementations§

Source§

impl ModPoly

Source

pub fn new(modulus: Integer) -> Self

A new polynomial, equal to zero.

Source

pub fn from_int(modulus: Integer, constant: Integer) -> Self

A new polynomial, equal to constant.

Source

pub fn with_capacity(modulus: Integer, n: usize) -> Self

A new polynomial, equal to zero, with room for n coefficients.

Source

pub fn interpolate_from_mul_subgroup( ys: Vec<Integer>, m: Integer, w: &Integer, ) -> Self

Interpolate a polynomial which agrees with the given values over a multiplicative subgroup of the prime field with modulus m.

Let n be a power of two and the order of the multiplicative subgroup generated by w modulo m. Let ys be a vector of values at 1, w, w^2, …

Returns a polynomial f, such that for i in 0..n, f(w^i) = ys[i] mod m.

§Panics

If n is not a power of two, or if w does not generate a subgroup of order n.

§Examples
use rug_polynomial::*;
use rug::Integer;

let m = Integer::from(5);
let w = Integer::from(2);
let ys: Vec<Integer> = vec![2, 3, 0, 4].into_iter().map(Integer::from).collect();
let p = ModPoly::interpolate_from_mul_subgroup(ys, m, &w);
debug_assert_eq!(p.len(), 2);
debug_assert_eq!(p.get_coefficient(0), Integer::from(1));
debug_assert_eq!(p.get_coefficient(1), Integer::from(1));
Source

pub fn evaluate_over_mul_subgroup(&self, w: &Integer, n: usize) -> Vec<Integer>

Evaluate this polynomial over the multiplicative subgroup generated by w, of size n.

Returns list of evaluations, over {1, w, w^2, ... w^(2^n-1)}.

§Panics

If n is not a power of two, or if w does not generate a subgroup of order n.

§Examples
use rug_polynomial::*;
use rug::Integer;

let m = Integer::from(5);
let w = Integer::from(2);
let ys: Vec<Integer> = vec![2, 3, 0, 4].into_iter().map(Integer::from).collect();
let mut p = ModPoly::new(m);
p.set_coefficient_ui(0, 1);
p.set_coefficient_ui(1, 1);
let vs = p.evaluate_over_mul_subgroup(&Integer::from(2), 4);
let vs: Vec<usize> = vs.into_iter().map(|i| i.to_usize().unwrap()).collect();
debug_assert_eq!(vs, vec![2, 3, 0, 4]);
Source

pub fn with_roots(xs: impl IntoIterator<Item = Integer>, m: &Integer) -> Self

Returns the minimal-degree monic polynomial with the given roots.

§Example
use rug_polynomial::*;
use rug::Integer;

let p = ModPoly::with_roots(vec![0, 1].into_iter().map(Integer::from), &Integer::from(5));
debug_assert_eq!(p.len(), 3);
debug_assert_eq!(p.get_coefficient(0), Integer::from(0));
debug_assert_eq!(p.get_coefficient(1), Integer::from(4));
debug_assert_eq!(p.get_coefficient(2), Integer::from(1));
Source

pub fn reserve(&mut self, n: usize)

Reallocates the polynomial to have room for n coefficients. Truncates the polynomial if it has more than n coefficients.

Source

pub fn evaluate(&self, i: &Integer) -> Integer

Evaluate the polynomial at the given input.

§Example
use rug_polynomial::*;
use rug::Integer;

let p = ModPoly::with_roots(vec![0, 1].into_iter().map(Integer::from), &Integer::from(5));
let y = p.evaluate(&Integer::from(3));
debug_assert_eq!(y, Integer::from(1));
Source

pub fn modulus(&self) -> &Integer

Get the modulus of this polynomial.

Source

pub fn get_coefficient(&self, i: usize) -> Integer

Get the ith coefficient

Source

pub fn set_coefficient(&mut self, i: usize, c: &Integer)

Set the ith coefficient to be c

Source

pub fn set_coefficient_ui(&mut self, i: usize, c: usize)

Set the ith coefficient to be c

Source

pub fn len(&self) -> usize

The number of coefficients in the polynomial. One more than the degree.

Source

pub fn neg(&mut self)

self = -self

Source

pub fn add(&mut self, other: &Self)

self = self + other

Source

pub fn sub(&mut self, other: &Self)

self = self - other

Source

pub fn sub_from(&mut self, other: &Self)

self = other - self

Source

pub fn mul(&mut self, other: &Self)

self = self * other

Source

pub fn divrem(&self, other: &Self) -> (ModPoly, ModPoly)

Find q and r such that self = other * q + r and r has degree less than other.

§Returns

(q, r)

Source

pub fn div(&mut self, other: &Self)

self = self / other

Source

pub fn div_from(&mut self, other: &Self)

self = other / self

Source

pub fn rem(&mut self, other: &Self)

self = self % other

Source

pub fn rem_from(&mut self, other: &Self)

self = other % self

Source

pub fn sqr(&mut self)

self = self * self

Source

pub fn xgcd(&self, other: &Self) -> (Self, Self, Self)

From (a, b), returns (g, s, t) such that g | a, g | b and g = a*s + b*t.

Source

pub fn derivative(&self) -> Self

Give the formal derivative of self.

Trait Implementations§

Source§

impl Add<&ModPoly> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &ModPoly) -> ModPoly

Performs the + operation. Read more
Source§

impl Add<Integer> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Integer) -> ModPoly

Performs the + operation. Read more
Source§

impl Add<ModPoly> for &ModPoly

Source§

type Output = ModPoly

The resulting type after applying the + operator.
Source§

fn add(self, rhs: ModPoly) -> ModPoly

Performs the + operation. Read more
Source§

impl Add<ModPoly> for Integer

Source§

type Output = ModPoly

The resulting type after applying the + operator.
Source§

fn add(self, rhs: ModPoly) -> ModPoly

Performs the + operation. Read more
Source§

impl Add for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the + operator.
Source§

fn add(self, rhs: ModPoly) -> ModPoly

Performs the + operation. Read more
Source§

impl AddAssign<&ModPoly> for ModPoly

Source§

fn add_assign(&mut self, rhs: &ModPoly)

Performs the += operation. Read more
Source§

impl AddAssign<Integer> for ModPoly

Source§

fn add_assign(&mut self, rhs: Integer)

Performs the += operation. Read more
Source§

impl AddAssign for ModPoly

Source§

fn add_assign(&mut self, rhs: ModPoly)

Performs the += operation. Read more
Source§

impl Clone for ModPoly

Source§

fn clone(&self) -> Self

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 ModPoly

Source§

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

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

impl<'de> Deserialize<'de> for ModPoly

Source§

fn deserialize<D>(deserializer: D) -> Result<ModPoly, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for ModPoly

Source§

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

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

impl Div<&ModPoly> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &ModPoly) -> ModPoly

Performs the / operation. Read more
Source§

impl Div<Integer> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Integer) -> ModPoly

Performs the / operation. Read more
Source§

impl Div<ModPoly> for &ModPoly

Source§

type Output = ModPoly

The resulting type after applying the / operator.
Source§

fn div(self, rhs: ModPoly) -> ModPoly

Performs the / operation. Read more
Source§

impl Div<ModPoly> for Integer

Source§

type Output = ModPoly

The resulting type after applying the / operator.
Source§

fn div(self, rhs: ModPoly) -> ModPoly

Performs the / operation. Read more
Source§

impl Div for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the / operator.
Source§

fn div(self, rhs: ModPoly) -> ModPoly

Performs the / operation. Read more
Source§

impl DivAssign<&ModPoly> for ModPoly

Source§

fn div_assign(&mut self, rhs: &ModPoly)

Performs the /= operation. Read more
Source§

impl DivAssign<Integer> for ModPoly

Source§

fn div_assign(&mut self, rhs: Integer)

Performs the /= operation. Read more
Source§

impl DivAssign for ModPoly

Source§

fn div_assign(&mut self, rhs: ModPoly)

Performs the /= operation. Read more
Source§

impl Drop for ModPoly

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<&ModPoly> for ModPolySer

Source§

fn from(other: &ModPoly) -> ModPolySer

Converts to this type from the input type.
Source§

impl From<ModPolySer> for ModPoly

Source§

fn from(other: ModPolySer) -> ModPoly

Converts to this type from the input type.
Source§

impl Mul<&ModPoly> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &ModPoly) -> ModPoly

Performs the * operation. Read more
Source§

impl Mul<Integer> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Integer) -> ModPoly

Performs the * operation. Read more
Source§

impl Mul<ModPoly> for &ModPoly

Source§

type Output = ModPoly

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: ModPoly) -> ModPoly

Performs the * operation. Read more
Source§

impl Mul<ModPoly> for Integer

Source§

type Output = ModPoly

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: ModPoly) -> ModPoly

Performs the * operation. Read more
Source§

impl Mul for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: ModPoly) -> ModPoly

Performs the * operation. Read more
Source§

impl MulAssign<&ModPoly> for ModPoly

Source§

fn mul_assign(&mut self, rhs: &ModPoly)

Performs the *= operation. Read more
Source§

impl MulAssign<Integer> for ModPoly

Source§

fn mul_assign(&mut self, rhs: Integer)

Performs the *= operation. Read more
Source§

impl MulAssign for ModPoly

Source§

fn mul_assign(&mut self, rhs: ModPoly)

Performs the *= operation. Read more
Source§

impl PartialEq for ModPoly

Source§

fn eq(&self, other: &ModPoly) -> 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 Rem<&ModPoly> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &ModPoly) -> ModPoly

Performs the % operation. Read more
Source§

impl Rem<Integer> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Integer) -> ModPoly

Performs the % operation. Read more
Source§

impl Rem<ModPoly> for &ModPoly

Source§

type Output = ModPoly

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: ModPoly) -> ModPoly

Performs the % operation. Read more
Source§

impl Rem<ModPoly> for Integer

Source§

type Output = ModPoly

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: ModPoly) -> ModPoly

Performs the % operation. Read more
Source§

impl Rem for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: ModPoly) -> ModPoly

Performs the % operation. Read more
Source§

impl RemAssign<&ModPoly> for ModPoly

Source§

fn rem_assign(&mut self, rhs: &ModPoly)

Performs the %= operation. Read more
Source§

impl RemAssign<Integer> for ModPoly

Source§

fn rem_assign(&mut self, rhs: Integer)

Performs the %= operation. Read more
Source§

impl RemAssign for ModPoly

Source§

fn rem_assign(&mut self, rhs: ModPoly)

Performs the %= operation. Read more
Source§

impl Serialize for ModPoly

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub<&ModPoly> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &ModPoly) -> ModPoly

Performs the - operation. Read more
Source§

impl Sub<Integer> for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Integer) -> ModPoly

Performs the - operation. Read more
Source§

impl Sub<ModPoly> for &ModPoly

Source§

type Output = ModPoly

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: ModPoly) -> ModPoly

Performs the - operation. Read more
Source§

impl Sub<ModPoly> for Integer

Source§

type Output = ModPoly

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: ModPoly) -> ModPoly

Performs the - operation. Read more
Source§

impl Sub for ModPoly

Source§

type Output = ModPoly

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: ModPoly) -> ModPoly

Performs the - operation. Read more
Source§

impl SubAssign<&ModPoly> for ModPoly

Source§

fn sub_assign(&mut self, rhs: &ModPoly)

Performs the -= operation. Read more
Source§

impl SubAssign<Integer> for ModPoly

Source§

fn sub_assign(&mut self, rhs: Integer)

Performs the -= operation. Read more
Source§

impl SubAssign for ModPoly

Source§

fn sub_assign(&mut self, rhs: ModPoly)

Performs the -= operation. Read more
Source§

impl Eq for ModPoly

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,