Skip to content

Commit 28a868b

Browse files
committed
refacto: proper error handling
1 parent 1e335fb commit 28a868b

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

crates/math/benches/polynomials/polynomial.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn polynomial_benchmarks(c: &mut Criterion) {
5151
group.bench_function("fast_mul big poly", |bench| {
5252
bench.iter(|| {
5353
black_box(&x_poly)
54-
.fast_multiplication::<Degree2ExtensionField>(black_box(&y_poly))
54+
.fast_fft_multiplication::<Degree2ExtensionField>(black_box(&y_poly))
5555
.unwrap()
5656
});
5757
});
@@ -62,8 +62,6 @@ pub fn polynomial_benchmarks(c: &mut Criterion) {
6262
let y_poly = rand_complex_mersenne_poly(big_order - 2);
6363

6464
group.bench_function("fast div big poly", |bench| {
65-
let x_poly = rand_complex_mersenne_poly(order as u32);
66-
let y_poly = rand_complex_mersenne_poly(order as u32);
6765
bench
6866
.iter(|| black_box(&x_poly).fast_division::<Degree2ExtensionField>(black_box(&y_poly)));
6967
});

crates/math/src/fft/polynomial.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::fft::errors::FFTError;
22

3+
use crate::field::errors::FieldError;
34
use crate::field::traits::{IsField, IsSubFieldOf};
45
use crate::{
56
field::{
@@ -125,8 +126,13 @@ impl<E: IsField> Polynomial<FieldElement<E>> {
125126
) -> Result<(Self, Self), FFTError> {
126127
let n = self.degree();
127128
let m = divisor.degree();
128-
if divisor.coefficients.is_empty() {
129-
panic!("Division by zero polynomial");
129+
if divisor.coefficients.is_empty()
130+
|| divisor
131+
.coefficients
132+
.iter()
133+
.all(|c| c == &FieldElement::zero())
134+
{
135+
return Err(FieldError::DivisionByZero.into());
130136
}
131137
if n < m {
132138
return Ok((Self::zero(), self.clone()));
@@ -136,11 +142,11 @@ impl<E: IsField> Polynomial<FieldElement<E>> {
136142
let b_rev = divisor.reverse(m);
137143
let inv_b_rev = Self::invert_polynomial::<F>(&b_rev, d + 1)?;
138144
let q = a_rev
139-
.fast_multiplication::<F>(&inv_b_rev)?
145+
.fast_fft_multiplication::<F>(&inv_b_rev)?
140146
.truncate(d + 1)
141147
.reverse(d);
142148

143-
let r = self - q.fast_multiplication::<F>(divisor)?;
149+
let r = self - q.fast_fft_multiplication::<F>(divisor)?;
144150
Ok((q, r))
145151
}
146152

@@ -151,22 +157,21 @@ impl<E: IsField> Polynomial<FieldElement<E>> {
151157
k: usize,
152158
) -> Result<Self, FFTError> {
153159
if p.coefficients.is_empty() || p.coefficients.iter().all(|c| c == &FieldElement::zero()) {
154-
panic!("Cannot invert polynomial with zero constant term");
160+
return Err(FieldError::DivisionByZero.into());
155161
}
156-
let mut q = Self::new(&[p.coefficients[0].inv().unwrap()]);
162+
let mut q = Self::new(&[p.coefficients[0].inv()?]);
157163
let mut current_precision = 1;
158164

165+
let two = Self::new(&[FieldElement::<F>::one() + FieldElement::one()]);
159166
while current_precision < k {
167+
current_precision *= 2;
160168
let temp = p
161-
.fast_multiplication::<F>(&q)?
162-
.truncate(2 * current_precision);
163-
let two = Self::new(&[FieldElement::<F>::one() + FieldElement::one()]);
164-
let correction = two - temp;
169+
.fast_fft_multiplication::<F>(&q)?
170+
.truncate(current_precision);
171+
let correction = &two - temp;
165172
q = q
166-
.fast_multiplication::<F>(&correction)?
167-
.truncate(2 * current_precision);
168-
169-
current_precision *= 2;
173+
.fast_fft_multiplication::<F>(&correction)?
174+
.truncate(current_precision);
170175
}
171176

172177
// Final truncation to desired degree k

0 commit comments

Comments
 (0)