Struct twenty_first::math::polynomial::Polynomial
source · pub struct Polynomial<FF: FiniteField> {
pub coefficients: Vec<FF>,
}Fields§
§coefficients: Vec<FF>Implementations§
source§impl<FF> Polynomial<FF>
impl<FF> Polynomial<FF>
sourcepub fn scale<S, XF>(&self, alpha: S) -> Polynomial<XF>
pub fn scale<S, XF>(&self, alpha: S) -> Polynomial<XF>
Return the polynomial which corresponds to the transformation x → α·x.
Given a polynomial P(x), produce P’(x) := P(α·x). Evaluating P’(x) then corresponds to evaluating P(α·x).
sourcepub fn fast_square(&self) -> Self
pub fn fast_square(&self) -> Self
It is the caller’s responsibility that this function is called with sufficiently large input
to be safe and to be faster than square.
pub fn square(&self) -> Self
pub fn fast_mod_pow(&self, pow: BigInt) -> Self
sourcepub fn multiply(&self, other: &Self) -> Self
pub fn multiply(&self, other: &Self) -> Self
Multiply self by other.
Prefer this over self * other since it chooses the fastest multiplication
strategy.
sourcepub fn zerofier(roots: &[FF]) -> Self
pub fn zerofier(roots: &[FF]) -> Self
Compute the lowest degree polynomial with the provided roots.
sourcepub fn interpolate(domain: &[FF], values: &[FF]) -> Self
pub fn interpolate(domain: &[FF], values: &[FF]) -> Self
Construct the lowest-degree polynomial interpolating the given points.
let domain = bfe_vec![0, 1, 2, 3];
let values = bfe_vec![1, 3, 5, 7];
let polynomial = Polynomial::interpolate(&domain, &values);
assert_eq!(1, polynomial.degree());
assert_eq!(bfe!(9), polynomial.evaluate(bfe!(4)));§Panics
- Panics if the provided domain is empty.
- Panics if the provided domain and values are not of the same length.
pub fn batch_fast_interpolate( domain: &[FF], values_matrix: &Vec<Vec<FF>>, primitive_root: BFieldElement, root_order: usize ) -> Vec<Self>
pub fn batch_evaluate(&self, domain: &[FF]) -> Vec<FF>
sourcepub fn fast_coset_evaluate<S>(
&self,
offset: S,
generator: BFieldElement,
order: usize
) -> Vec<FF>
pub fn fast_coset_evaluate<S>( &self, offset: S, generator: BFieldElement, order: usize ) -> Vec<FF>
Fast evaluate on a coset domain, which is the group generated by generator^i * offset.
§Performance
If possible, use a base field element as the offset.
§Panics
Panics if the order of the domain generated by the generator is smaller than or equal to
the degree of self.
sourcepub fn fast_coset_interpolate<S>(
offset: S,
generator: BFieldElement,
values: &[FF]
) -> Self
pub fn fast_coset_interpolate<S>( offset: S, generator: BFieldElement, values: &[FF] ) -> Self
The inverse of Self::fast_coset_evaluate.
§Performance
If possible, use a base field element as the offset.
§Panics
Panics if the length of values does not equal the order of the domain generated by the
generator.
sourcepub fn truncate(&self, k: usize) -> Self
pub fn truncate(&self, k: usize) -> Self
The degree-k polynomial with the same k + 1 leading coefficients as self. To be more
precise: The degree of the result will be the minimum of k and Self::degree(). This
implies, among other things, that if self is zero, the result will also
be zero, independent of k.
§Examples
let f = Polynomial::new(bfe_vec![0, 1, 2, 3, 4]); // 4x⁴ + 3x³ + 2x² + 1x¹ + 0
let g = f.truncate(2); // 4x² + 3x¹ + 2
assert_eq!(Polynomial::new(bfe_vec![2, 3, 4]), g);sourcepub fn mod_x_to_the_n(&self, n: usize) -> Self
pub fn mod_x_to_the_n(&self, n: usize) -> Self
source§impl Polynomial<BFieldElement>
impl Polynomial<BFieldElement>
sourcepub fn clean_divide(self, divisor: Self) -> Self
pub fn clean_divide(self, divisor: Self) -> Self
A fast way of dividing two polynomials. Only works if division is clean, i.e., if the
remainder of polynomial long division is zero. This must be known ahead of time. If
division is unclean, this method might panic or produce a wrong result.
Use Polynomial::divide for more generality.
§Panics
Panics if
- the divisor is zero, or
- division is not clean, i.e., if polynomial long division leaves some non-zero remainder.
source§impl<FF: FiniteField> Polynomial<FF>
impl<FF: FiniteField> Polynomial<FF>
pub const fn new(coefficients: Vec<FF>) -> Self
pub fn normalize(&mut self)
pub fn from_constant(constant: FF) -> Self
pub fn is_x(&self) -> bool
pub fn evaluate(&self, x: FF) -> FF
sourcepub fn leading_coefficient(&self) -> Option<FF>
pub fn leading_coefficient(&self) -> Option<FF>
The coefficient of the polynomial’s term of highest power. None if (and only if) self
is zero.
Furthermore, is never Some(FF::zero()).
§Examples
let f = Polynomial::new(bfe_vec![1, 2, 3]);
assert_eq!(Some(bfe!(3)), f.leading_coefficient());
assert_eq!(None, Polynomial::<XFieldElement>::zero().leading_coefficient());pub fn are_colinear_3(p0: (FF, FF), p1: (FF, FF), p2: (FF, FF)) -> bool
pub fn get_colinear_y(p0: (FF, FF), p1: (FF, FF), p2_x: FF) -> FF
sourcepub fn slow_square(&self) -> Self
pub fn slow_square(&self) -> Self
Slow square implementation that does not use NTT
source§impl<FF: FiniteField> Polynomial<FF>
impl<FF: FiniteField> Polynomial<FF>
pub fn are_colinear(points: &[(FF, FF)]) -> bool
source§impl<FF: FiniteField> Polynomial<FF>
impl<FF: FiniteField> Polynomial<FF>
pub fn shift_coefficients_mut(&mut self, power: usize, zero: FF)
sourcepub fn shift_coefficients(&self, power: usize) -> Self
pub fn shift_coefficients(&self, power: usize) -> Self
Multiply a polynomial with x^power
sourcepub fn scalar_mul_mut(&mut self, scalar: FF)
pub fn scalar_mul_mut(&mut self, scalar: FF)
Multiply a polynomial with a scalar, i.e., compute scalar · self(x).
Slightly faster than Self::scalar_mul.
§Examples
let mut f = Polynomial::new(bfe_vec![1, 2, 3]);
f.scalar_mul_mut(bfe!(2));
assert_eq!(Polynomial::new(bfe_vec![2, 4, 6]), f);sourcepub fn scalar_mul(&self, scalar: FF) -> Self
pub fn scalar_mul(&self, scalar: FF) -> Self
Multiply a polynomial with a scalar, i.e., compute scalar · self(x).
Slightly slower than Self::scalar_mul_mut.
§Examples
let f = Polynomial::new(bfe_vec![1, 2, 3]);
let g = f.scalar_mul(bfe!(2));
assert_eq!(Polynomial::new(bfe_vec![2, 4, 6]), g);source§impl<FF: FiniteField> Polynomial<FF>
impl<FF: FiniteField> Polynomial<FF>
sourcepub fn xgcd(x: Self, y: Self) -> (Self, Self, Self)
pub fn xgcd(x: Self, y: Self) -> (Self, Self, Self)
Extended Euclidean algorithm with polynomials. Computes the greatest
common divisor gcd as a monic polynomial, as well as the corresponding
Bézout coefficients a and b, satisfying gcd = a·x + b·y
§Example
let x = Polynomial::<BFieldElement>::from([1, 0, 1]);
let y = Polynomial::<BFieldElement>::from([1, 1]);
let (gcd, a, b) = Polynomial::xgcd(x.clone(), y.clone());
assert_eq!(gcd, a * x + b * y);source§impl<FF: FiniteField> Polynomial<FF>
impl<FF: FiniteField> Polynomial<FF>
pub fn degree(&self) -> isize
pub fn formal_derivative(&self) -> Self
Trait Implementations§
source§impl<FF: FiniteField> Add for Polynomial<FF>
impl<FF: FiniteField> Add for Polynomial<FF>
source§impl<FF: FiniteField> AddAssign for Polynomial<FF>
impl<FF: FiniteField> AddAssign for Polynomial<FF>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moresource§impl<'arbitrary, FF: FiniteField + Arbitrary<'arbitrary>> Arbitrary<'arbitrary> for Polynomial<FF>
impl<'arbitrary, FF: FiniteField + Arbitrary<'arbitrary>> Arbitrary<'arbitrary> for Polynomial<FF>
source§fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>
Self from the given unstructured data. Read moresource§fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>
Self from the entirety of the given
unstructured data. Read moresource§impl<FF: Clone + FiniteField> Clone for Polynomial<FF>
impl<FF: Clone + FiniteField> Clone for Polynomial<FF>
source§fn clone(&self) -> Polynomial<FF>
fn clone(&self) -> Polynomial<FF>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<FF: FiniteField> Debug for Polynomial<FF>
impl<FF: FiniteField> Debug for Polynomial<FF>
source§impl<FF: FiniteField> Display for Polynomial<FF>
impl<FF: FiniteField> Display for Polynomial<FF>
source§impl<FF: FiniteField> Div for Polynomial<FF>
impl<FF: FiniteField> Div for Polynomial<FF>
source§impl<FF, E> From<&[E]> for Polynomial<FF>
impl<FF, E> From<&[E]> for Polynomial<FF>
source§impl<FF, E> From<&Vec<E>> for Polynomial<FF>
impl<FF, E> From<&Vec<E>> for Polynomial<FF>
source§impl<const N: usize, FF, E> From<[E; N]> for Polynomial<FF>where
FF: FiniteField,
E: Into<FF>,
impl<const N: usize, FF, E> From<[E; N]> for Polynomial<FF>where
FF: FiniteField,
E: Into<FF>,
source§impl From<Polynomial<BFieldElement>> for XFieldElement
impl From<Polynomial<BFieldElement>> for XFieldElement
source§fn from(poly: Polynomial<BFieldElement>) -> Self
fn from(poly: Polynomial<BFieldElement>) -> Self
source§impl<FF, E> From<Vec<E>> for Polynomial<FF>where
FF: FiniteField,
E: Into<FF>,
impl<FF, E> From<Vec<E>> for Polynomial<FF>where
FF: FiniteField,
E: Into<FF>,
source§impl From<XFieldElement> for Polynomial<BFieldElement>
impl From<XFieldElement> for Polynomial<BFieldElement>
source§fn from(item: XFieldElement) -> Self
fn from(item: XFieldElement) -> Self
source§impl<FF: FiniteField> Hash for Polynomial<FF>
impl<FF: FiniteField> Hash for Polynomial<FF>
source§impl<FF: FiniteField> Mul for Polynomial<FF>
impl<FF: FiniteField> Mul for Polynomial<FF>
source§impl<FF: FiniteField> Neg for Polynomial<FF>
impl<FF: FiniteField> Neg for Polynomial<FF>
source§impl<FF: FiniteField> One for Polynomial<FF>
impl<FF: FiniteField> One for Polynomial<FF>
source§impl<FF: FiniteField> PartialEq for Polynomial<FF>
impl<FF: FiniteField> PartialEq for Polynomial<FF>
source§impl<FF: FiniteField> Rem for Polynomial<FF>
impl<FF: FiniteField> Rem for Polynomial<FF>
source§impl<FF: FiniteField> Sub for Polynomial<FF>
impl<FF: FiniteField> Sub for Polynomial<FF>
source§impl<FF: FiniteField> Zero for Polynomial<FF>
impl<FF: FiniteField> Zero for Polynomial<FF>
impl<FF: FiniteField> Eq for Polynomial<FF>
Auto Trait Implementations§
impl<FF> Freeze for Polynomial<FF>
impl<FF> RefUnwindSafe for Polynomial<FF>where
FF: RefUnwindSafe,
impl<FF> Send for Polynomial<FF>
impl<FF> Sync for Polynomial<FF>
impl<FF> Unpin for Polynomial<FF>where
FF: Unpin,
impl<FF> UnwindSafe for Polynomial<FF>where
FF: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more