Skip to content

Commit

Permalink
And of dot_product
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleiserson committed Nov 19, 2024
1 parent 158b68d commit a00d566
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,23 @@ where
/// Computes the dot product of two arrays of the same size.
/// It is isolated from Lagrange because there could be potential SIMD optimizations used
fn dot_product<F: PrimeField, const N: usize>(a: &[F; N], b: &[F; N]) -> F {
// Staying in integers allows rustc to optimize this code properly, but puts a restriction
// on how large the prime field can be
debug_assert!(
2 * F::BITS + N.next_power_of_two().ilog2() <= 128,
"The prime field {} is too large for this dot product implementation",
F::PRIME.into()
);

let mut sum = 0;

// I am cautious about using zip in hot code
// https://github.com/rust-lang/rust/issues/103555

let mut acc = <F as MultiplyAccumulate>::Accumulator::new();
for i in 0..N {
acc.multiply_accumulate(a[i], b[i]);
sum += a[i].as_u128() * b[i].as_u128();
}
acc.take()

F::truncate_from(sum)
}

#[cfg(all(test, unit_test))]
Expand Down

0 comments on commit a00d566

Please sign in to comment.