diff --git a/field/src/polynomial/mod.rs b/field/src/polynomial/mod.rs index 4d6d55c2..ac3beb8e 100644 --- a/field/src/polynomial/mod.rs +++ b/field/src/polynomial/mod.rs @@ -197,12 +197,21 @@ impl PolynomialCoeffs { poly } - /// Removes leading zero coefficients. + /// Removes any leading zero coefficients. pub fn trim(&mut self) { self.coeffs.truncate(self.degree_plus_one()); } - /// Removes leading zero coefficients. + /// Removes some leading zero coefficients, such that a desired length is reached. Fails if a + /// nonzero coefficient is encountered before then. + pub fn trim_to_len(&mut self, len: usize) -> Result<()> { + ensure!(self.len() >= len); + ensure!(self.coeffs[len..].iter().all(F::is_zero)); + self.coeffs.truncate(len); + Ok(()) + } + + /// Removes any leading zero coefficients. pub fn trimmed(&self) -> Self { let coeffs = self.coeffs[..self.degree_plus_one()].to_vec(); Self { coeffs } diff --git a/plonky2/src/plonk/prover.rs b/plonky2/src/plonk/prover.rs index 7a172aff..d49014f0 100644 --- a/plonky2/src/plonk/prover.rs +++ b/plonky2/src/plonk/prover.rs @@ -148,11 +148,10 @@ pub(crate) fn prove, C: GenericConfig, co quotient_polys .into_par_iter() .flat_map(|mut quotient_poly| { - quotient_poly.trim(); - quotient_poly.pad(quotient_degree).expect( - "Quotient has failed, the vanishing polynomial is not divisible by `Z_H", + quotient_poly.trim_to_len(quotient_degree).expect( + "Quotient has failed, the vanishing polynomial is not divisible by Z_H", ); - // Split t into degree-n chunks. + // Split quotient into degree-n chunks. quotient_poly.chunks(degree) }) .collect() diff --git a/starky/src/prover.rs b/starky/src/prover.rs index 35c30cbf..de97ecce 100644 --- a/starky/src/prover.rs +++ b/starky/src/prover.rs @@ -80,10 +80,9 @@ where let all_quotient_chunks = quotient_polys .into_par_iter() .flat_map(|mut quotient_poly| { - quotient_poly.trim(); quotient_poly - .pad(degree * stark.quotient_degree_factor()) - .expect("Quotient has failed, the vanishing polynomial is not divisible by `Z_H"); + .trim_to_len(degree * stark.quotient_degree_factor()) + .expect("Quotient has failed, the vanishing polynomial is not divisible by Z_H"); // Split quotient into degree-n chunks. quotient_poly.chunks(degree) })