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

perf(spartan): Optimize SparsePolynomial evaluate to O(n) complexity #353

Open
wants to merge 1 commit 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
28 changes: 20 additions & 8 deletions spartan/src/sparse_mlpoly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1804,16 +1804,28 @@ impl<F: PrimeField> SparsePolynomial<F> {
chi_i
}

// Takes O(n log n). TODO: do this in O(n) where n is the number of entries in Z
// Optimized to O(n) from O(n log n)
pub fn evaluate(&self, r: &[F]) -> F {
assert_eq!(self.num_vars, r.len());

(0..self.Z.len())
.map(|i| {
let bits = self.Z[i].idx.get_bits(r.len());
SparsePolynomial::compute_chi(&bits, r) * self.Z[i].val
})
.sum()

// Pre-compute 1-r[i] for each variable to avoid repeated subtraction
let one_minus_r: Vec<F> = r.iter().map(|&ri| F::one() - ri).collect();

// For each entry in Z, compute its contribution
self.Z.iter().map(|entry| {
let bits = entry.idx.get_bits(r.len());

// Compute chi value directly using pre-computed values
let chi = bits.iter().enumerate().fold(F::one(), |acc, (j, &bit)| {
if bit {
acc * r[j]
} else {
acc * one_minus_r[j]
}
});

chi * entry.val
}).sum()
}
}

Expand Down