1
1
use crate :: fft:: errors:: FFTError ;
2
2
3
+ use crate :: field:: errors:: FieldError ;
3
4
use crate :: field:: traits:: { IsField , IsSubFieldOf } ;
4
5
use crate :: {
5
6
field:: {
@@ -125,8 +126,13 @@ impl<E: IsField> Polynomial<FieldElement<E>> {
125
126
) -> Result < ( Self , Self ) , FFTError > {
126
127
let n = self . degree ( ) ;
127
128
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 ( ) ) ;
130
136
}
131
137
if n < m {
132
138
return Ok ( ( Self :: zero ( ) , self . clone ( ) ) ) ;
@@ -136,11 +142,11 @@ impl<E: IsField> Polynomial<FieldElement<E>> {
136
142
let b_rev = divisor. reverse ( m) ;
137
143
let inv_b_rev = Self :: invert_polynomial :: < F > ( & b_rev, d + 1 ) ?;
138
144
let q = a_rev
139
- . fast_multiplication :: < F > ( & inv_b_rev) ?
145
+ . fast_fft_multiplication :: < F > ( & inv_b_rev) ?
140
146
. truncate ( d + 1 )
141
147
. reverse ( d) ;
142
148
143
- let r = self - q. fast_multiplication :: < F > ( divisor) ?;
149
+ let r = self - q. fast_fft_multiplication :: < F > ( divisor) ?;
144
150
Ok ( ( q, r) )
145
151
}
146
152
@@ -151,22 +157,21 @@ impl<E: IsField> Polynomial<FieldElement<E>> {
151
157
k : usize ,
152
158
) -> Result < Self , FFTError > {
153
159
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 ( ) ) ;
155
161
}
156
- let mut q = Self :: new ( & [ p. coefficients [ 0 ] . inv ( ) . unwrap ( ) ] ) ;
162
+ let mut q = Self :: new ( & [ p. coefficients [ 0 ] . inv ( ) ? ] ) ;
157
163
let mut current_precision = 1 ;
158
164
165
+ let two = Self :: new ( & [ FieldElement :: < F > :: one ( ) + FieldElement :: one ( ) ] ) ;
159
166
while current_precision < k {
167
+ current_precision *= 2 ;
160
168
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;
165
172
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) ;
170
175
}
171
176
172
177
// Final truncation to desired degree k
0 commit comments