Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number subtractions in get_lagrange_coefficients by half #657

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions fastcrypto-tbls/src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,32 @@ impl<C: GroupElement> Poly<C> {
.iter()
.fold(C::ScalarType::generator(), |acc, i| acc * i);
let mut coeffs = Vec::new();
for i in &indices {
let denominator = indices
.iter()
.filter(|j| *j != i)
.fold(*i, |acc, j| acc * (*j - i));

// Keep track of differences that are already computed: deltas[i][j] = indices[j] - indices[n - 1 - i]
let n = indices.len();
let mut deltas: Vec<Vec<<C as GroupElement>::ScalarType>> = vec![Vec::new(); n];
for (i_idx, i) in indices.iter().enumerate() {
// Compute product with cached deltas
let mut denominator = (0..i_idx)
.map(|k| {
deltas[k]
.pop()
.expect("deltas[k] is popped k-1 times and contains exactly k-1 elements")
})
.fold(*i, |acc, delta| acc * delta);

// Adjust sign since the cached values are negations of the factors we need.
if i_idx % 2 == 1 {
denominator = -denominator;
}

// Compute remaining deltas
for j in indices[i_idx + 1..].iter().rev() {
let delta = *j - i;
denominator = denominator * delta;
deltas[i_idx].push(delta);
}

let coeff = full_numerator / denominator;
coeffs.push(coeff.expect("safe since i != j"));
}
Expand Down
Loading