trim_to_len helper function (#472)

* trim_to_len helper function

Seems a little nicer IMO to only remove a certain number of zeros, vs removing all trailing zeros then re-adding some.

* PR feedback
This commit is contained in:
Daniel Lubarov 2022-02-06 23:35:46 -08:00 committed by GitHub
parent a43e138f57
commit b40827e655
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 9 deletions

View File

@ -197,12 +197,21 @@ impl<F: Field> PolynomialCoeffs<F> {
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 }

View File

@ -148,11 +148,10 @@ pub(crate) fn prove<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, 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()

View File

@ -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)
})