Skip to content

Commit d21d834

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

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

crates/math/benches/polynomials/polynomial.rs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,58 @@ pub fn polynomial_benchmarks(c: &mut Criterion) {
1010
let mut group = c.benchmark_group("Polynomial");
1111
let order = const_random!(u64) % 8;
1212

13-
group.bench_function("evaluate", |bench| {
14-
let poly = rand_poly(order);
15-
let x = FE::new(rand::random::<u64>());
16-
bench.iter(|| poly.evaluate(black_box(&x)));
17-
});
13+
// group.bench_function("evaluate", |bench| {
14+
// let poly = rand_poly(order);
15+
// let x = FE::new(rand::random::<u64>());
16+
// bench.iter(|| poly.evaluate(black_box(&x)));
17+
// });
1818

19-
group.bench_function("evaluate_slice", |bench| {
20-
let poly = rand_poly(order);
21-
let inputs = rand_field_elements(order);
22-
bench.iter(|| poly.evaluate_slice(black_box(&inputs)));
23-
});
19+
// group.bench_function("evaluate_slice", |bench| {
20+
// let poly = rand_poly(order);
21+
// let inputs = rand_field_elements(order);
22+
// bench.iter(|| poly.evaluate_slice(black_box(&inputs)));
23+
// });
2424

25-
group.bench_function("add", |bench| {
26-
let x_poly = rand_poly(order);
27-
let y_poly = rand_poly(order);
28-
bench.iter(|| black_box(&x_poly) + black_box(&y_poly));
29-
});
25+
// group.bench_function("add", |bench| {
26+
// let x_poly = rand_poly(order);
27+
// let y_poly = rand_poly(order);
28+
// bench.iter(|| black_box(&x_poly) + black_box(&y_poly));
29+
// });
3030

31-
group.bench_function("neg", |bench| {
32-
let x_poly = rand_poly(order);
33-
bench.iter(|| -black_box(&x_poly));
34-
});
31+
// group.bench_function("neg", |bench| {
32+
// let x_poly = rand_poly(order);
33+
// bench.iter(|| -black_box(&x_poly));
34+
// });
3535

36-
group.bench_function("sub", |bench| {
37-
let x_poly = rand_poly(order);
38-
let y_poly = rand_poly(order);
39-
bench.iter(|| black_box(&x_poly) - black_box(&y_poly));
40-
});
36+
// group.bench_function("sub", |bench| {
37+
// let x_poly = rand_poly(order);
38+
// let y_poly = rand_poly(order);
39+
// bench.iter(|| black_box(&x_poly) - black_box(&y_poly));
40+
// });
4141

42-
group.bench_function("mul", |bench| {
43-
let x_poly = rand_poly(order);
44-
let y_poly = rand_poly(order);
45-
bench.iter(|| black_box(&x_poly) * black_box(&y_poly));
46-
});
42+
// group.bench_function("mul", |bench| {
43+
// let x_poly = rand_poly(order);
44+
// let y_poly = rand_poly(order);
45+
// bench.iter(|| black_box(&x_poly) * black_box(&y_poly));
46+
// });
4747

4848
let big_order = 9;
4949
let x_poly = rand_complex_mersenne_poly(big_order);
50-
let y_poly = rand_complex_mersenne_poly(big_order);
51-
group.bench_function("fast_mul big poly", |bench| {
52-
bench.iter(|| {
53-
black_box(&x_poly)
54-
.fast_multiplication::<Degree2ExtensionField>(black_box(&y_poly))
55-
.unwrap()
56-
});
57-
});
58-
group.bench_function("slow mul big poly", |bench| {
59-
bench.iter(|| black_box(&x_poly) * black_box(&y_poly));
60-
});
50+
// let y_poly = rand_complex_mersenne_poly(big_order);
51+
// group.bench_function("fast_mul big poly", |bench| {
52+
// bench.iter(|| {
53+
// black_box(&x_poly)
54+
// .fast_fft_multiplication::<Degree2ExtensionField>(black_box(&y_poly))
55+
// .unwrap()
56+
// });
57+
// });
58+
// group.bench_function("slow mul big poly", |bench| {
59+
// bench.iter(|| black_box(&x_poly) * black_box(&y_poly));
60+
// });
6161

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)