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>

source

pub fn scale<S, XF>(&self, alpha: S) -> Polynomial<XF>
where S: Clone + One, FF: Mul<S, Output = XF>, XF: FiniteField,

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).

source

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.

source

pub fn square(&self) -> Self

source

pub fn fast_mod_pow(&self, pow: BigInt) -> Self

source

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

Multiply self by other.

Prefer this over self * other since it chooses the fastest multiplication strategy.

source

pub fn zerofier(roots: &[FF]) -> Self

Compute the lowest degree polynomial with the provided roots.

source

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.
source

pub fn batch_fast_interpolate( domain: &[FF], values_matrix: &Vec<Vec<FF>>, primitive_root: BFieldElement, root_order: usize ) -> Vec<Self>

source

pub fn batch_evaluate(&self, domain: &[FF]) -> Vec<FF>

source

pub fn fast_coset_evaluate<S>( &self, offset: S, generator: BFieldElement, order: usize ) -> Vec<FF>
where S: Clone + One, FF: Mul<S, Output = 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.

source

pub fn fast_coset_interpolate<S>( offset: S, generator: BFieldElement, values: &[FF] ) -> Self
where S: Clone + One + Inverse, FF: Mul<S, Output = FF>,

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.

source

pub fn divide(&self, divisor: &Self) -> Self

Divide self by some divisor.

§Panics

Panics if the divisor is zero.

source

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);
source

pub fn mod_x_to_the_n(&self, n: usize) -> Self

self % x^n

A special case of Self::rem, and faster.

§Examples
let f = Polynomial::new(bfe_vec![0, 1, 2, 3, 4]); // 4x⁴ + 3x³ + 2x² + 1x¹ + 0
let g = f.mod_x_to_the_n(2);                      // 1x¹ + 0
assert_eq!(Polynomial::new(bfe_vec![0, 1]), g);
source§

impl Polynomial<BFieldElement>

source

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>

source

pub const fn new(coefficients: Vec<FF>) -> Self

source

pub fn normalize(&mut self)

source

pub fn from_constant(constant: FF) -> Self

source

pub fn is_x(&self) -> bool

source

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

source

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());
source

pub fn are_colinear_3(p0: (FF, FF), p1: (FF, FF), p2: (FF, FF)) -> bool

source

pub fn get_colinear_y(p0: (FF, FF), p1: (FF, FF), p2_x: FF) -> FF

source

pub fn slow_square(&self) -> Self

Slow square implementation that does not use NTT

source§

impl<FF: FiniteField> Polynomial<FF>

source

pub fn are_colinear(points: &[(FF, FF)]) -> bool

source§

impl<FF: FiniteField> Polynomial<FF>

source

pub fn mod_pow(&self, pow: BigInt) -> Self

Multiply a polynomial with itself pow times

source

pub fn shift_coefficients_mut(&mut self, power: usize, zero: FF)

source

pub fn shift_coefficients(&self, power: usize) -> Self

Multiply a polynomial with x^power

source

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);
source

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>

source

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>

source

pub fn degree(&self) -> isize

source

pub fn formal_derivative(&self) -> Self

Trait Implementations§

source§

impl<FF: FiniteField> Add for Polynomial<FF>

§

type Output = Polynomial<FF>

The resulting type after applying the + operator.
source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
source§

impl<FF: FiniteField> AddAssign for Polynomial<FF>

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl<'arbitrary, FF: FiniteField + Arbitrary<'arbitrary>> Arbitrary<'arbitrary> for Polynomial<FF>

source§

fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

impl<FF: Clone + FiniteField> Clone for Polynomial<FF>

source§

fn clone(&self) -> Polynomial<FF>

Returns a copy 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<FF: FiniteField> Debug for Polynomial<FF>

source§

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

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

impl<FF: FiniteField> Display for Polynomial<FF>

source§

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

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

impl<FF: FiniteField> Div for Polynomial<FF>

§

type Output = Polynomial<FF>

The resulting type after applying the / operator.
source§

fn div(self, other: Self) -> Self

Performs the / operation. Read more
source§

impl<FF, E> From<&[E]> for Polynomial<FF>
where FF: FiniteField, E: Into<FF> + Clone,

source§

fn from(coefficients: &[E]) -> Self

Converts to this type from the input type.
source§

impl<FF, E> From<&Vec<E>> for Polynomial<FF>
where FF: FiniteField, E: Into<FF> + Clone,

source§

fn from(coefficients: &Vec<E>) -> Self

Converts to this type from the input type.
source§

impl<const N: usize, FF, E> From<[E; N]> for Polynomial<FF>
where FF: FiniteField, E: Into<FF>,

source§

fn from(coefficients: [E; N]) -> Self

Converts to this type from the input type.
source§

impl From<Polynomial<BFieldElement>> for XFieldElement

source§

fn from(poly: Polynomial<BFieldElement>) -> Self

Converts to this type from the input type.
source§

impl<FF, E> From<Vec<E>> for Polynomial<FF>
where FF: FiniteField, E: Into<FF>,

source§

fn from(coefficients: Vec<E>) -> Self

Converts to this type from the input type.
source§

impl From<XFieldElement> for Polynomial<BFieldElement>

source§

fn from(item: XFieldElement) -> Self

Converts to this type from the input type.
source§

impl<FF: FiniteField> Hash for Polynomial<FF>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<FF: FiniteField> Mul for Polynomial<FF>

§

type Output = Polynomial<FF>

The resulting type after applying the * operator.
source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
source§

impl<FF: FiniteField> Neg for Polynomial<FF>

§

type Output = Polynomial<FF>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<FF: FiniteField> One for Polynomial<FF>

source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
source§

fn is_one(&self) -> bool

Returns true if self is equal to the multiplicative identity. Read more
source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
source§

impl<FF: FiniteField> PartialEq for Polynomial<FF>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<FF: FiniteField> Rem for Polynomial<FF>

§

type Output = Polynomial<FF>

The resulting type after applying the % operator.
source§

fn rem(self, other: Self) -> Self

Performs the % operation. Read more
source§

impl<FF: FiniteField> Sub for Polynomial<FF>

§

type Output = Polynomial<FF>

The resulting type after applying the - operator.
source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
source§

impl<FF: FiniteField> Zero for Polynomial<FF>

source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
source§

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

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

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§

default 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>,

§

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>,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,