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
impl ModPoly
Sourcepub fn from_int(modulus: Integer, constant: Integer) -> Self
pub fn from_int(modulus: Integer, constant: Integer) -> Self
A new polynomial, equal to constant.
Sourcepub fn with_capacity(modulus: Integer, n: usize) -> Self
pub fn with_capacity(modulus: Integer, n: usize) -> Self
A new polynomial, equal to zero, with room for n coefficients.
Sourcepub fn interpolate_from_mul_subgroup(
ys: Vec<Integer>,
m: Integer,
w: &Integer,
) -> Self
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));Sourcepub fn evaluate_over_mul_subgroup(&self, w: &Integer, n: usize) -> Vec<Integer>
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]);Sourcepub fn with_roots(xs: impl IntoIterator<Item = Integer>, m: &Integer) -> Self
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));Sourcepub fn reserve(&mut self, n: usize)
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.
Sourcepub fn evaluate(&self, i: &Integer) -> Integer
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));Sourcepub fn get_coefficient(&self, i: usize) -> Integer
pub fn get_coefficient(&self, i: usize) -> Integer
Get the ith coefficient
Sourcepub fn set_coefficient(&mut self, i: usize, c: &Integer)
pub fn set_coefficient(&mut self, i: usize, c: &Integer)
Set the ith coefficient to be c
Sourcepub fn set_coefficient_ui(&mut self, i: usize, c: usize)
pub fn set_coefficient_ui(&mut self, i: usize, c: usize)
Set the ith coefficient to be c
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of coefficients in the polynomial. One more than the degree.
Sourcepub fn xgcd(&self, other: &Self) -> (Self, Self, Self)
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.
Sourcepub fn derivative(&self) -> Self
pub fn derivative(&self) -> Self
Give the formal derivative of self.
Trait Implementations§
Source§impl AddAssign<&ModPoly> for ModPoly
impl AddAssign<&ModPoly> for ModPoly
Source§fn add_assign(&mut self, rhs: &ModPoly)
fn add_assign(&mut self, rhs: &ModPoly)
+= operation. Read moreSource§impl AddAssign<Integer> for ModPoly
impl AddAssign<Integer> for ModPoly
Source§fn add_assign(&mut self, rhs: Integer)
fn add_assign(&mut self, rhs: Integer)
+= operation. Read moreSource§impl AddAssign for ModPoly
impl AddAssign for ModPoly
Source§fn add_assign(&mut self, rhs: ModPoly)
fn add_assign(&mut self, rhs: ModPoly)
+= operation. Read moreSource§impl<'de> Deserialize<'de> for ModPoly
impl<'de> Deserialize<'de> for ModPoly
Source§fn deserialize<D>(deserializer: D) -> Result<ModPoly, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<ModPoly, D::Error>where
D: Deserializer<'de>,
Source§impl DivAssign<&ModPoly> for ModPoly
impl DivAssign<&ModPoly> for ModPoly
Source§fn div_assign(&mut self, rhs: &ModPoly)
fn div_assign(&mut self, rhs: &ModPoly)
/= operation. Read moreSource§impl DivAssign<Integer> for ModPoly
impl DivAssign<Integer> for ModPoly
Source§fn div_assign(&mut self, rhs: Integer)
fn div_assign(&mut self, rhs: Integer)
/= operation. Read moreSource§impl DivAssign for ModPoly
impl DivAssign for ModPoly
Source§fn div_assign(&mut self, rhs: ModPoly)
fn div_assign(&mut self, rhs: ModPoly)
/= operation. Read moreSource§impl From<&ModPoly> for ModPolySer
impl From<&ModPoly> for ModPolySer
Source§fn from(other: &ModPoly) -> ModPolySer
fn from(other: &ModPoly) -> ModPolySer
Source§impl From<ModPolySer> for ModPoly
impl From<ModPolySer> for ModPoly
Source§fn from(other: ModPolySer) -> ModPoly
fn from(other: ModPolySer) -> ModPoly
Source§impl MulAssign<&ModPoly> for ModPoly
impl MulAssign<&ModPoly> for ModPoly
Source§fn mul_assign(&mut self, rhs: &ModPoly)
fn mul_assign(&mut self, rhs: &ModPoly)
*= operation. Read moreSource§impl MulAssign<Integer> for ModPoly
impl MulAssign<Integer> for ModPoly
Source§fn mul_assign(&mut self, rhs: Integer)
fn mul_assign(&mut self, rhs: Integer)
*= operation. Read moreSource§impl MulAssign for ModPoly
impl MulAssign for ModPoly
Source§fn mul_assign(&mut self, rhs: ModPoly)
fn mul_assign(&mut self, rhs: ModPoly)
*= operation. Read moreSource§impl RemAssign<&ModPoly> for ModPoly
impl RemAssign<&ModPoly> for ModPoly
Source§fn rem_assign(&mut self, rhs: &ModPoly)
fn rem_assign(&mut self, rhs: &ModPoly)
%= operation. Read moreSource§impl RemAssign<Integer> for ModPoly
impl RemAssign<Integer> for ModPoly
Source§fn rem_assign(&mut self, rhs: Integer)
fn rem_assign(&mut self, rhs: Integer)
%= operation. Read moreSource§impl RemAssign for ModPoly
impl RemAssign for ModPoly
Source§fn rem_assign(&mut self, rhs: ModPoly)
fn rem_assign(&mut self, rhs: ModPoly)
%= operation. Read moreSource§impl SubAssign<&ModPoly> for ModPoly
impl SubAssign<&ModPoly> for ModPoly
Source§fn sub_assign(&mut self, rhs: &ModPoly)
fn sub_assign(&mut self, rhs: &ModPoly)
-= operation. Read moreSource§impl SubAssign<Integer> for ModPoly
impl SubAssign<Integer> for ModPoly
Source§fn sub_assign(&mut self, rhs: Integer)
fn sub_assign(&mut self, rhs: Integer)
-= operation. Read moreSource§impl SubAssign for ModPoly
impl SubAssign for ModPoly
Source§fn sub_assign(&mut self, rhs: ModPoly)
fn sub_assign(&mut self, rhs: ModPoly)
-= operation. Read more