Skip to content

Commit

Permalink
garaga-rs: Faster polynomial division. (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime authored Dec 5, 2024
1 parent 1837724 commit 8023ef6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions tools/garaga_rs/src/algebra/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ impl<F: IsPrimeField> Polynomial<F> {

Polynomial::new(result_coeffs)
}

pub fn divmod(self, denominator: &Self) -> (Self, Self) {
let den_deg = denominator.degree();
if den_deg == -1 {
Expand All @@ -127,10 +126,9 @@ impl<F: IsPrimeField> Polynomial<F> {
let shift = rem_deg - den_deg;
let coefficient = &remainder.coefficients[rem_deg as usize] * &denom_lead_inv;
quotient_coeffs[shift as usize] = coefficient.clone();

let mut subtractee_coeffs = vec![FieldElement::<F>::zero(); (shift) as usize];
subtractee_coeffs.push(coefficient);
let subtractee = Polynomial::new(subtractee_coeffs).mul_with_ref(denominator);
let subtractee = denominator
.scale_by_coeff(&coefficient)
.shift(shift as usize);
remainder = remainder - subtractee;
rem_deg = remainder.degree();
}
Expand All @@ -139,6 +137,12 @@ impl<F: IsPrimeField> Polynomial<F> {
(quotient, remainder)
}

pub fn shift(&self, shift: usize) -> Self {
let mut shifted_coeffs = vec![FieldElement::<F>::zero(); shift];
shifted_coeffs.extend(self.coefficients.clone());
Self::new(shifted_coeffs)
}

pub fn divfloor(&self, denominator: &Self) -> Self {
let (quotient, _remainder) = self.clone().divmod(denominator);
quotient
Expand Down
2 changes: 1 addition & 1 deletion tools/npm/garaga_ts/src/wasm/pkg/garaga_rs_bg.wasm.js

Large diffs are not rendered by default.

0 comments on commit 8023ef6

Please sign in to comment.