Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions crypto/math/src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ impl<F: IsField> Polynomial<FieldElement<F>> {
}
}

/// Creates a new monomial term coefficient*x^degree
pub fn new_monomial(coefficient: FieldElement<F>, degree: usize) -> Self {
let mut coefficients = vec![FieldElement::zero(); degree];
coefficients.push(coefficient);
Self::new(&coefficients)
}

/// Creates the null polynomial
pub fn zero() -> Self {
Self::new(&[])
Expand Down Expand Up @@ -80,26 +73,6 @@ impl<F: IsField> Polynomial<FieldElement<F>> {
self.coefficients().len()
}

pub fn mul_with_ref(&self, factor: &Self) -> Self {
let degree = self.degree() + factor.degree();
let mut coefficients = vec![FieldElement::zero(); degree + 1];

if self.coefficients.is_empty() || factor.coefficients.is_empty() {
Polynomial::new(&[FieldElement::zero()])
} else {
for i in 0..=factor.degree() {
if factor.coefficients[i] != FieldElement::zero() {
for j in 0..=self.degree() {
if self.coefficients[j] != FieldElement::zero() {
coefficients[i + j] += &factor.coefficients[i] * &self.coefficients[j];
}
}
}
}
Polynomial::new(&coefficients)
}
}

/// Scales the coefficients of a polynomial P by a factor
/// Returns P(factor * x)
pub fn scale<S: IsSubFieldOf<F>>(&self, factor: &FieldElement<S>) -> Self {
Expand Down Expand Up @@ -149,30 +122,6 @@ impl<F: IsField> Polynomial<FieldElement<F>> {
}
}

/// Pads a polynomial with zeros until the desired length
/// This function can be useful when evaluating polynomials with the FFT
pub fn pad_with_zero_coefficients_to_length<F: IsField>(
pa: &mut Polynomial<FieldElement<F>>,
n: usize,
) {
pa.coefficients.resize(n, FieldElement::zero());
}

/// Pads polynomial representations with minimum number of zeros to match lengths.
pub fn pad_with_zero_coefficients<L: IsField, F: IsSubFieldOf<L>>(
pa: &Polynomial<FieldElement<F>>,
pb: &Polynomial<FieldElement<L>>,
) -> (Polynomial<FieldElement<F>>, Polynomial<FieldElement<L>>) {
let mut pa = pa.clone();
let mut pb = pb.clone();

if pa.coefficients.len() > pb.coefficients.len() {
pad_with_zero_coefficients_to_length(&mut pb, pa.coefficients.len());
} else {
pad_with_zero_coefficients_to_length(&mut pa, pb.coefficients.len());
}
(pa, pb)
}
// ── Barycentric coset interpolation ──────────────────────────────────────
// Four evaluation variants along two axes:
// - eval field: base field (F) or extension field (E)
Expand Down Expand Up @@ -504,25 +453,6 @@ impl<E: IsField> Polynomial<FieldElement<E>> {
}
}

#[cfg(test)]
pub fn compose_fft<F, E>(
poly_1: &Polynomial<FieldElement<E>>,
poly_2: &Polynomial<FieldElement<E>>,
) -> Polynomial<FieldElement<E>>
where
F: IsFFTField + IsSubFieldOf<E>,
E: IsField + Send + Sync,
{
let poly_2_evaluations = Polynomial::evaluate_fft::<F>(poly_2, 1, None).unwrap();

let values: Vec<_> = poly_2_evaluations
.iter()
.map(|value| poly_1.evaluate(value))
.collect();

Polynomial::interpolate_fft::<F>(values.as_slice()).unwrap()
}

fn evaluate_fft_cpu_raw<F, E>(
coeffs: &[FieldElement<E>],
permute_to_natural: bool,
Expand Down
11 changes: 0 additions & 11 deletions crypto/math/src/tests/fft_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ mod fft_polynomial_tests {
use crate::field::extensions_goldilocks::Degree2GoldilocksExtensionField;
use crate::field::traits::{IsFFTField, RootsConfig};
use crate::polynomial::Polynomial;
use crate::polynomial::compose_fft;
use proptest::{collection, prelude::*};

/// Evaluates a polynomial at a slice of points
Expand Down Expand Up @@ -232,16 +231,6 @@ mod fft_polynomial_tests {
}

}

#[test]
fn composition_fft_works() {
let p = Polynomial::new(&[FE::new(0), FE::new(2)]);
let q = Polynomial::new(&[FE::new(0), FE::new(0), FE::new(0), FE::new(1)]);
assert_eq!(
compose_fft::<F, F>(&p, &q),
Polynomial::new(&[FE::new(0), FE::new(0), FE::new(0), FE::new(2)])
);
}
}

#[test]
Expand Down
46 changes: 8 additions & 38 deletions crypto/math/src/tests/polynomial_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use crate::field::element::FieldElement;
use crate::field::goldilocks::GoldilocksField;
use crate::field::traits::{IsField, IsPrimeField};
use crate::polynomial::{Polynomial, pad_with_zero_coefficients};
use crate::polynomial::Polynomial;
use alloc::string::{String, ToString};
use alloc::{format, vec::Vec};

Expand Down Expand Up @@ -82,17 +82,6 @@ mod tests {
assert_eq!(p1.coefficients, &[FE::new(3), FE::new(4)]);
}

#[test]
fn pad_with_zero_coefficients_returns_polynomials_with_zeros_until_matching_size() {
let p1 = Polynomial::new(&[FE::new(3), FE::new(4)]);
let p2 = Polynomial::new(&[FE::new(3)]);

assert_eq!(p2.coefficients, &[FE::new(3)]);
let (pp1, pp2) = pad_with_zero_coefficients(&p1, &p2);
assert_eq!(pp1, p1);
assert_eq!(pp2.coefficients, &[FE::new(3), FE::new(0)]);
}

#[test]
fn evaluate_constant_polynomial_returns_constant() {
let three = FE::new(3);
Expand All @@ -108,35 +97,29 @@ mod tests {
assert_eq!(ret, [three, three]);
}

#[test]
fn create_degree_0_new_monomial() {
assert_eq!(
Polynomial::new_monomial(FE::new(3), 0),
Polynomial::new(&[FE::new(3)])
);
}

#[test]
fn zero_poly_evals_0_in_3() {
assert_eq!(
Polynomial::new_monomial(FE::new(0), 0).evaluate(&FE::new(3)),
Polynomial::new(&[FE::new(0)]).evaluate(&FE::new(3)),
FE::new(0)
);
}

#[test]
fn evaluate_degree_1_new_monomial() {
fn evaluate_degree_1_polynomial() {
let two = FE::new(2);
let four = FE::new(4);
let p = Polynomial::new_monomial(two, 1);
// 2X
let p = Polynomial::new(&[FE::new(0), two]);
assert_eq!(p.evaluate(&two), four);
}

#[test]
fn evaluate_degree_2_monomyal() {
fn evaluate_degree_2_polynomial() {
let two = FE::new(2);
let eight = FE::new(8);
let p = Polynomial::new_monomial(two, 2);
// 2X^2
let p = Polynomial::new(&[FE::new(0), FE::new(0), two]);
assert_eq!(p.evaluate(&two), eight);
}

Expand All @@ -146,19 +129,6 @@ mod tests {
assert_eq!(p.evaluate(&FE::new(2)), FE::new(15));
}

#[test]
fn simple_interpolating_polynomial_by_hand_works() {
let denominator = Polynomial::new(&[FE::new(1) * (FE::new(2) - FE::new(4)).inv().unwrap()]);
let numerator = Polynomial::new(&[-FE::new(4), FE::new(1)]);
let interpolating = numerator.mul_with_ref(&denominator);
assert_eq!(
(FE::new(2) - FE::new(4)) * (FE::new(1) * (FE::new(2) - FE::new(4)).inv().unwrap()),
FE::new(1)
);
assert_eq!(interpolating.evaluate(&FE::new(2)), FE::new(1));
assert_eq!(interpolating.evaluate(&FE::new(4)), FE::new(0));
}

#[test]
fn break_in_parts() {
// p = 3 X^3 + X^2 + 2X + 1
Expand Down
13 changes: 12 additions & 1 deletion crypto/stark/src/tests/prover_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ fn test_evaluate_polynomial_on_lde_domain_on_trace_polys() {

#[test]
fn test_evaluate_polynomial_on_lde_domain_edge_case() {
let poly = Polynomial::new_monomial(Felt::one(), 8);
// X^8
let poly = Polynomial::new(&[
Felt::zero(),
Felt::zero(),
Felt::zero(),
Felt::zero(),
Felt::zero(),
Felt::zero(),
Felt::zero(),
Felt::zero(),
Felt::one(),
]);
let blowup_factor: usize = 4;
let domain_size: usize = 8;
let offset = Felt::from(3);
Expand Down
Loading