pub struct Polynomial { /* private fields */ }Expand description
A polynomial over GF(256) with at most 255 coefficients.
Coefficients are stored from degree 0 upward in a fixed stack-allocated
array, so the type is Copy and never allocates.
Implementations§
Source§impl Polynomial
impl Polynomial
Sourcepub const MAX_DEGREE: u8 = POLYNOMIAL_MAX_DEGREE_U8
pub const MAX_DEGREE: u8 = POLYNOMIAL_MAX_DEGREE_U8
Maximum degree of a polynomial over GF(256).
Sourcepub const MAX_COEFFICIENTS: u8 = POLYNOMIAL_MAX_COEFFICIENTS_U8
pub const MAX_COEFFICIENTS: u8 = POLYNOMIAL_MAX_COEFFICIENTS_U8
Maximum number of coefficients in a polynomial.
Source§impl Polynomial
impl Polynomial
Sourcepub fn coefficients(&self) -> &[u8] ⓘ
pub fn coefficients(&self) -> &[u8] ⓘ
Returns the coefficients of this polynomial, from degree 0 to the leading term.
§Panics
Panics if the stored degree is 255, which exceeds
Polynomial::MAX_DEGREE and cannot occur for a valid polynomial.
Source§impl Polynomial
impl Polynomial
Source§impl Polynomial
impl Polynomial
Sourcepub fn div_rem(
&self,
divisor: impl AsRef<[u8]>,
) -> Result<(Self, Self), PolynomialDivError>
pub fn div_rem( &self, divisor: impl AsRef<[u8]>, ) -> Result<(Self, Self), PolynomialDivError>
Divides this polynomial by a divisor, returning (quotient, remainder).
The divisor is interpreted as coefficients in ascending degree order. Trailing zeros are ignored when determining the divisor’s degree.
§Errors
Returns ZeroDivisor if the divisor is the zero polynomial.
Source§impl Polynomial
impl Polynomial
Sourcepub fn div_rem_inplace(
&mut self,
divisor: &Self,
quotient: &mut Self,
) -> Result<(), PolynomialDivError>
pub fn div_rem_inplace( &mut self, divisor: &Self, quotient: &mut Self, ) -> Result<(), PolynomialDivError>
In-place polynomial division: self becomes the remainder, quotient is output.
After this method, self contains the remainder and quotient contains
the quotient of dividing the original self by divisor.
§Errors
Returns PolynomialDivError::ZeroDivisor if the divisor is the zero polynomial.
Returns PolynomialDivError::GFError if field inversion fails.
Source§impl Polynomial
impl Polynomial
Source§impl Polynomial
impl Polynomial
Sourcepub fn eval_coefficient_iter_at<I>(coefficients: I, x: u8) -> u8where
I: DoubleEndedIterator<Item = u8>,
pub fn eval_coefficient_iter_at<I>(coefficients: I, x: u8) -> u8where
I: DoubleEndedIterator<Item = u8>,
Evaluates a polynomial from an iterator of coefficients at the given point.
Coefficients are yielded from degree 0 (constant term) to the leading term.
Requires DoubleEndedIterator for Horner’s method.
Source§impl Polynomial
impl Polynomial
Sourcepub fn eval_coefficient_slices_at(slices: &[&[u8]], x: u8) -> u8
pub fn eval_coefficient_slices_at(slices: &[&[u8]], x: u8) -> u8
Evaluates a polynomial from multiple coefficient slices at the given point.
Slices are concatenated in order, with the first slice containing the lowest degree terms.
Source§impl Polynomial
impl Polynomial
Sourcepub fn eval_coefficients_at(coefficients: &[u8], x: u8) -> u8
pub fn eval_coefficients_at(coefficients: &[u8], x: u8) -> u8
Evaluates a polynomial given its coefficients at the given point using Horner’s method.
Coefficients are ordered from degree 0 (constant term) to the leading term.
Source§impl Polynomial
impl Polynomial
Sourcepub fn eval_coefficients_derivative_at(coefficients: &[u8], x: u8) -> u8
pub fn eval_coefficients_derivative_at(coefficients: &[u8], x: u8) -> u8
Evaluates the formal derivative of a polynomial at the given point.
In characteristic 2, only odd-degree terms contribute to the derivative: p’(x) = a₁ + a₃x² + a₅x⁴ + …
Coefficients are ordered from degree 0 (constant term) to the leading term.
Source§impl Polynomial
impl Polynomial
Sourcepub fn eval_derivative_at(&self, x: u8) -> u8
pub fn eval_derivative_at(&self, x: u8) -> u8
Evaluates the formal derivative of this polynomial at the given point.
In characteristic 2, only odd-degree terms contribute to the derivative.
Source§impl Polynomial
impl Polynomial
Sourcepub fn first_n_coefficients(&self, n: usize) -> &[u8] ⓘ
pub fn first_n_coefficients(&self, n: usize) -> &[u8] ⓘ
Returns the first n coefficients.
The returned slice has length min(n, 255).
Source§impl Polynomial
impl Polynomial
Sourcepub fn from_slice(coeffs: &[u8]) -> Result<Self, PolynomialFromSliceError>
pub fn from_slice(coeffs: &[u8]) -> Result<Self, PolynomialFromSliceError>
Creates a polynomial from a slice of coefficients.
Coefficients are ordered from degree 0 (constant term) to the leading term.
§Errors
Returns PolynomialFromSliceError::TooLong if the slice exceeds 255 elements.
Source§impl Polynomial
impl Polynomial
Source§impl Polynomial
impl Polynomial
Source§impl Polynomial
impl Polynomial
Sourcepub fn mul_xor_assign(
&mut self,
a: &Self,
b: &Self,
) -> Result<(), PolynomialMulError>
pub fn mul_xor_assign( &mut self, a: &Self, b: &Self, ) -> Result<(), PolynomialMulError>
Fused multiply-XOR: self ^= a * b
Computes the product of polynomials a and b, XORing the result into self.
This avoids allocating an intermediate polynomial for the product.
In GF(2^8), XOR is equivalent to both addition and subtraction, so this
computes self = self + a * b or equivalently self = self - a * b.
§Errors
Returns PolynomialMulError::DegreeOverflow if the product degree,
a.degree() + b.degree(), exceeds Polynomial::MAX_DEGREE.
self is unmodified on error.
Source§impl Polynomial
impl Polynomial
Sourcepub fn set(&mut self, idx: u8, value: u8)
pub fn set(&mut self, idx: u8, value: u8)
Sets the coefficient of degree idx to value.
If value is non-zero and idx exceeds the current degree, the degree
increases to idx. If value is zero and idx equals the current degree,
the degree decreases to the next non-zero coefficient.
§Panics
Panics if idx exceeds 254, the maximum degree of a polynomial over GF(256).
Source§impl Polynomial
impl Polynomial
Sourcepub fn set_coefficients(
&mut self,
offset: u8,
values: &[u8],
) -> Result<(), PolynomialSetCoefficientsError>
pub fn set_coefficients( &mut self, offset: u8, values: &[u8], ) -> Result<(), PolynomialSetCoefficientsError>
Sets coefficients starting at degree offset from a slice.
Copies values into the coefficient array at offset..offset + values.len(),
then adjusts the degree accordingly.
Empty slices are a no-op.
§Errors
Returns OutOfBounds if offset + values.len() exceeds 255.
Trait Implementations§
Source§impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Add<I> for Polynomial
impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Add<I> for Polynomial
Source§fn add(self, rhs: I) -> Self::Output
fn add(self, rhs: I) -> Self::Output
Adds coefficients from an iterator to this polynomial.
In GF(256), addition is XOR. This delegates to BitXor.
§Errors
Returns TooManyCoefficients if the iterator yields more than 255 elements.
Source§type Output = Result<Polynomial, PolynomialXorError>
type Output = Result<Polynomial, PolynomialXorError>
+ operator.Source§impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Add<I> for &Polynomial
impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Add<I> for &Polynomial
Source§impl AddAssign for Polynomial
impl AddAssign for Polynomial
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl AddAssign<&Polynomial> for Polynomial
impl AddAssign<&Polynomial> for Polynomial
Source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
+= operation. Read moreSource§impl AsRef<[u8]> for Polynomial
impl AsRef<[u8]> for Polynomial
Source§impl<B: Borrow<u8>, I: IntoIterator<Item = B>> BitXor<I> for Polynomial
impl<B: Borrow<u8>, I: IntoIterator<Item = B>> BitXor<I> for Polynomial
Source§impl<B: Borrow<u8>, I: IntoIterator<Item = B>> BitXor<I> for &Polynomial
impl<B: Borrow<u8>, I: IntoIterator<Item = B>> BitXor<I> for &Polynomial
Source§impl BitXorAssign for Polynomial
impl BitXorAssign for Polynomial
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^= operation. Read moreSource§impl BitXorAssign<&Polynomial> for Polynomial
impl BitXorAssign<&Polynomial> for Polynomial
Source§fn bitxor_assign(&mut self, rhs: &Self)
fn bitxor_assign(&mut self, rhs: &Self)
^= operation. Read moreSource§impl Clone for Polynomial
impl Clone for Polynomial
Source§fn clone(&self) -> Polynomial
fn clone(&self) -> Polynomial
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for Polynomial
Source§impl Debug for Polynomial
impl Debug for Polynomial
Source§impl Default for Polynomial
impl Default for Polynomial
impl Eq for Polynomial
Source§impl FromIterator<u8> for Polynomial
impl FromIterator<u8> for Polynomial
Source§fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self
Collects coefficients from degree 0 (constant term) to the leading term.
Consumes at most 255 elements; excess elements are ignored. Leading zeros are trimmed. Safe for infinite iterators.
Source§impl Hash for Polynomial
impl Hash for Polynomial
Source§impl Index<u8> for Polynomial
impl Index<u8> for Polynomial
Source§impl Index<usize> for Polynomial
impl Index<usize> for Polynomial
Source§impl IntoIterator for Polynomial
impl IntoIterator for Polynomial
Source§impl<'a> IntoIterator for &'a Polynomial
impl<'a> IntoIterator for &'a Polynomial
Source§impl Mul for Polynomial
impl Mul for Polynomial
Source§impl Mul<&Polynomial> for &Polynomial
impl Mul<&Polynomial> for &Polynomial
Source§fn mul(self, rhs: &Polynomial) -> Self::Output
fn mul(self, rhs: &Polynomial) -> Self::Output
Multiplies two polynomials over GF(256).
§Errors
Returns DegreeOverflow if the result degree exceeds 254.
Source§type Output = Result<Polynomial, PolynomialMulError>
type Output = Result<Polynomial, PolynomialMulError>
* operator.Source§impl Mul<&Polynomial> for Polynomial
impl Mul<&Polynomial> for Polynomial
Source§impl Mul<&[u8]> for &Polynomial
impl Mul<&[u8]> for &Polynomial
Source§impl Mul<&[u8]> for Polynomial
impl Mul<&[u8]> for Polynomial
Source§impl Mul<Polynomial> for &Polynomial
impl Mul<Polynomial> for &Polynomial
Source§type Output = Result<Polynomial, PolynomialMulError>
type Output = Result<Polynomial, PolynomialMulError>
* operator.Source§impl MulAssign<u8> for Polynomial
impl MulAssign<u8> for Polynomial
Source§fn mul_assign(&mut self, scalar: u8)
fn mul_assign(&mut self, scalar: u8)
Multiplies all coefficients by a scalar in GF(2^8).
If scalar is zero, the result is the zero polynomial.
Otherwise, the degree is preserved since GF(2^8) has no zero divisors.
Source§impl Ord for Polynomial
impl Ord for Polynomial
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Polynomial
impl PartialEq for Polynomial
Source§impl PartialOrd for Polynomial
impl PartialOrd for Polynomial
Source§impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Sub<I> for Polynomial
impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Sub<I> for Polynomial
Source§fn sub(self, rhs: I) -> Self::Output
fn sub(self, rhs: I) -> Self::Output
Subtracts coefficients from an iterator from this polynomial.
In GF(256), subtraction is equivalent to addition, which is XOR.
This delegates to BitXor.
§Errors
Returns TooManyCoefficients if the iterator yields more than 255 elements.
Source§type Output = Result<Polynomial, PolynomialXorError>
type Output = Result<Polynomial, PolynomialXorError>
- operator.Source§impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Sub<I> for &Polynomial
impl<B: Borrow<u8>, I: IntoIterator<Item = B>> Sub<I> for &Polynomial
Source§impl SubAssign for Polynomial
impl SubAssign for Polynomial
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl SubAssign<&Polynomial> for Polynomial
impl SubAssign<&Polynomial> for Polynomial
Source§fn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
-= operation. Read moreSource§impl TryFrom<&[u8]> for Polynomial
impl TryFrom<&[u8]> for Polynomial
Auto Trait Implementations§
impl Freeze for Polynomial
impl RefUnwindSafe for Polynomial
impl Send for Polynomial
impl Sync for Polynomial
impl Unpin for Polynomial
impl UnsafeUnpin for Polynomial
impl UnwindSafe for Polynomial
Blanket Implementations§
Source§impl<A, T> Array<T> for A
impl<A, T> Array<T> for A
Source§fn at(&self, index: usize) -> Option<&T>
fn at(&self, index: usize) -> Option<&T>
None if the index is out of bounds. Read moreSource§fn concat(&self, other: impl AsRef<[T]>) -> Vec<T>where
T: Clone,
fn concat(&self, other: impl AsRef<[T]>) -> Vec<T>where
T: Clone,
Source§fn entries<'a>(&'a self) -> impl Iterator<Item = (usize, &'a T)>where
T: 'a,
fn entries<'a>(&'a self) -> impl Iterator<Item = (usize, &'a T)>where
T: 'a,
Source§fn every(&self, predicate: impl FnMut(&T) -> bool) -> bool
fn every(&self, predicate: impl FnMut(&T) -> bool) -> bool
Source§fn every_equal_in_sorted_by(
&self,
comparator: impl FnMut(&T) -> Ordering,
) -> bool
fn every_equal_in_sorted_by( &self, comparator: impl FnMut(&T) -> Ordering, ) -> bool
Source§fn filter(&self, predicate: impl FnMut(&T) -> bool) -> Vec<T>where
T: Clone,
fn filter(&self, predicate: impl FnMut(&T) -> bool) -> Vec<T>where
T: Clone,
Source§fn filter_equal_in_sorted_by(
&self,
comparator: impl FnMut(&T) -> Ordering,
) -> Vec<T>where
T: Clone,
fn filter_equal_in_sorted_by(
&self,
comparator: impl FnMut(&T) -> Ordering,
) -> Vec<T>where
T: Clone,
Source§fn find(&self, predicate: impl FnMut(&T) -> bool) -> Option<&T>
fn find(&self, predicate: impl FnMut(&T) -> bool) -> Option<&T>
Source§fn find_equal_in_sorted_by(
&self,
comparator: impl FnMut(&T) -> Ordering,
) -> Option<&T>
fn find_equal_in_sorted_by( &self, comparator: impl FnMut(&T) -> Ordering, ) -> Option<&T>
Source§fn find_index(&self, predicate: impl FnMut(&T) -> bool) -> Option<usize>
fn find_index(&self, predicate: impl FnMut(&T) -> bool) -> Option<usize>
Source§fn find_index_equal_in_sorted_by(
&self,
comparator: impl FnMut(&T) -> Ordering,
) -> Option<usize>
fn find_index_equal_in_sorted_by( &self, comparator: impl FnMut(&T) -> Ordering, ) -> Option<usize>
Source§fn find_last(&self, predicate: impl FnMut(&T) -> bool) -> Option<&T>
fn find_last(&self, predicate: impl FnMut(&T) -> bool) -> Option<&T>
Source§fn find_last_equal_in_sorted_by(
&self,
comparator: impl FnMut(&T) -> Ordering,
) -> Option<&T>
fn find_last_equal_in_sorted_by( &self, comparator: impl FnMut(&T) -> Ordering, ) -> Option<&T>
Source§fn find_last_index(&self, predicate: impl FnMut(&T) -> bool) -> Option<usize>
fn find_last_index(&self, predicate: impl FnMut(&T) -> bool) -> Option<usize>
Source§fn find_last_index_equal_in_sorted_by(
&self,
comparator: impl FnMut(&T) -> Ordering,
) -> Option<usize>
fn find_last_index_equal_in_sorted_by( &self, comparator: impl FnMut(&T) -> Ordering, ) -> Option<usize>
Source§fn flat<'a, O>(&'a self) -> Vec<O>
fn flat<'a, O>(&'a self) -> Vec<O>
Source§fn flat_map<O, I>(&self, mapper: impl FnMut(&T) -> I) -> Vec<O>where
I: IntoIterator<Item = O>,
fn flat_map<O, I>(&self, mapper: impl FnMut(&T) -> I) -> Vec<O>where
I: IntoIterator<Item = O>,
Source§fn for_each(&self, cb: impl FnMut(&T))
fn for_each(&self, cb: impl FnMut(&T))
Source§fn includes(&self, value: &T) -> boolwhere
T: PartialEq,
fn includes(&self, value: &T) -> boolwhere
T: PartialEq,
Source§fn index_of(&self, value: &T) -> Option<usize>where
T: PartialEq,
fn index_of(&self, value: &T) -> Option<usize>where
T: PartialEq,
None if not found. Read moreSource§fn join<S>(&self, separator: &S) -> String
fn join<S>(&self, separator: &S) -> String
Source§fn keys(&self) -> impl Iterator<Item = usize>
fn keys(&self) -> impl Iterator<Item = usize>
Source§fn last_index_of(&self, value: &T) -> Option<usize>where
T: PartialEq,
fn last_index_of(&self, value: &T) -> Option<usize>where
T: PartialEq,
None if not found. Read more