From 475491bb596bbb76336170a7b49650f86a8a4759 Mon Sep 17 00:00:00 2001 From: diegokingston Date: Fri, 22 May 2026 18:13:59 -0300 Subject: [PATCH] refactor(math): drop Polynomial operator impls unused by production MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes 6 operator families from polynomial.rs that no production code reaches — proven by a clean `cargo build --workspace`: - Sub Polynomial - Polynomial - Mul Polynomial * FieldElement (and FieldElement * Polynomial) - Add Polynomial +/- FieldElement (and FieldElement +/- Polynomial) - Sub FieldElement - Polynomial Production uses exactly two operator entry points, both when building the product-of-linear-factors polynomial prod(x - r_i): - boundary.rs:149 zerofier * binomial -> Mul Poly x Poly - transition.rs:127 acc * (x_poly - current) -> Mul Poly x Poly, Sub Poly - FieldElement Sub for Polynomial is implemented as `-monomial + self`, so Neg and Add Poly+/-Poly remain as transitive dependencies; those four families (Mul Poly x Poly, Sub Poly - FieldElement, Neg, Add Poly+/-Poly) are kept. Also removes the 2 self-referential Sub Poly-Poly tests and the now-orphaned polynomial_b_minus_a test helper. --- crypto/math/src/polynomial.rs | 302 ---------------------- crypto/math/src/tests/polynomial_tests.rs | 17 -- 2 files changed, 319 deletions(-) diff --git a/crypto/math/src/polynomial.rs b/crypto/math/src/polynomial.rs index b7aeba73..ac220408 100644 --- a/crypto/math/src/polynomial.rs +++ b/crypto/math/src/polynomial.rs @@ -250,55 +250,6 @@ impl ops::Neg for Polynomial> { } } -// impl Sub -impl ops::Sub<&Polynomial>> for &Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, substrahend: &Polynomial>) -> Polynomial> { - self + (-substrahend) - } -} - -impl ops::Sub>> for Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, substrahend: Polynomial>) -> Polynomial> { - &self - &substrahend - } -} - -impl ops::Sub<&Polynomial>> for Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, substrahend: &Polynomial>) -> Polynomial> { - &self - substrahend - } -} - -impl ops::Sub>> for &Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, substrahend: Polynomial>) -> Polynomial> { - self - &substrahend - } -} - impl ops::Mul<&Polynomial>> for &Polynomial> { type Output = Polynomial>; fn mul(self, factor: &Polynomial>) -> Polynomial> { @@ -327,210 +278,6 @@ impl ops::Mul<&Polynomial>> for Polynomial ops::Mul> for Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: FieldElement) -> Polynomial> { - let new_coefficients = self - .coefficients - .iter() - .map(|value| &multiplicand * value) - .collect(); - Polynomial { - coefficients: new_coefficients, - } - } -} - -impl ops::Mul<&FieldElement> for &Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: &FieldElement) -> Polynomial> { - self.clone() * multiplicand.clone() - } -} - -impl ops::Mul> for &Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: FieldElement) -> Polynomial> { - self * &multiplicand - } -} - -impl ops::Mul<&FieldElement> for Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: &FieldElement) -> Polynomial> { - &self * multiplicand - } -} - -/* Multiplication field element at right */ -impl ops::Mul<&Polynomial>> for &FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: &Polynomial>) -> Polynomial> { - multiplicand * self - } -} - -impl ops::Mul>> for &FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: Polynomial>) -> Polynomial> { - &multiplicand * self - } -} - -impl ops::Mul<&Polynomial>> for FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: &Polynomial>) -> Polynomial> { - multiplicand * self - } -} - -impl ops::Mul>> for FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn mul(self, multiplicand: Polynomial>) -> Polynomial> { - &multiplicand * &self - } -} - -/* Addition field element at left */ -impl ops::Add<&FieldElement> for &Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: &FieldElement) -> Polynomial> { - Polynomial::new_monomial(other.clone(), 0) + self - } -} - -impl ops::Add> for Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: FieldElement) -> Polynomial> { - &self + &other - } -} - -impl ops::Add> for &Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: FieldElement) -> Polynomial> { - self + &other - } -} - -impl ops::Add<&FieldElement> for Polynomial> -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: &FieldElement) -> Polynomial> { - &self + other - } -} - -/* Addition field element at right */ -impl ops::Add<&Polynomial>> for &FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: &Polynomial>) -> Polynomial> { - Polynomial::new_monomial(self.clone(), 0) + other - } -} - -impl ops::Add>> for FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: Polynomial>) -> Polynomial> { - &self + &other - } -} - -impl ops::Add>> for &FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: Polynomial>) -> Polynomial> { - self + &other - } -} - -impl ops::Add<&Polynomial>> for FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn add(self, other: &Polynomial>) -> Polynomial> { - &self + other - } -} - /* Substraction field element at left */ impl ops::Sub<&FieldElement> for &Polynomial> where @@ -580,55 +327,6 @@ where } } -/* Substraction field element at right */ -impl ops::Sub<&Polynomial>> for &FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, other: &Polynomial>) -> Polynomial> { - Polynomial::new_monomial(self.clone(), 0) - other - } -} - -impl ops::Sub>> for FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, other: Polynomial>) -> Polynomial> { - &self - &other - } -} - -impl ops::Sub>> for &FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, other: Polynomial>) -> Polynomial> { - self - &other - } -} - -impl ops::Sub<&Polynomial>> for FieldElement -where - L: IsField, - F: IsSubFieldOf, -{ - type Output = Polynomial>; - - fn sub(self, other: &Polynomial>) -> Polynomial> { - &self - other - } -} - #[derive(Debug)] pub enum InterpolateError { UnequalLengths(usize, usize), diff --git a/crypto/math/src/tests/polynomial_tests.rs b/crypto/math/src/tests/polynomial_tests.rs index ca1bfde2..06e667b9 100644 --- a/crypto/math/src/tests/polynomial_tests.rs +++ b/crypto/math/src/tests/polynomial_tests.rs @@ -116,10 +116,6 @@ mod tests { Polynomial::new(&[FE::new(4), FE::new(6), FE::new(8)]) } - fn polynomial_b_minus_a() -> Polynomial { - Polynomial::new(&[FE::new(2), FE::new(2), FE::new(2)]) - } - #[test] fn adding_a_and_b_equals_a_plus_b() { assert_eq!(polynomial_a() + polynomial_b(), polynomial_a_plus_b()); @@ -160,19 +156,6 @@ mod tests { assert_ne!(-polynomial_a(), polynomial_a()); } - #[test] - fn substracting_5_5_gives_0() { - let p1 = Polynomial::new(&[FE::new(5)]); - let p2 = Polynomial::new(&[FE::new(5)]); - let p3 = Polynomial::new(&[FE::new(0)]); - assert_eq!(p1 - p2, p3); - } - - #[test] - fn substracting_b_and_a_equals_b_minus_a() { - assert_eq!(polynomial_b() - polynomial_a(), polynomial_b_minus_a()); - } - #[test] fn constructor_removes_zeros_at_the_end_of_polynomial() { let p1 = Polynomial::new(&[FE::new(3), FE::new(4), FE::new(0)]);