From a00d566b4deee3c798f196a280a01d0b8f31e391 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Mon, 18 Nov 2024 16:56:56 -0800 Subject: [PATCH] And of dot_product --- .../ipa_prf/malicious_security/lagrange.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs b/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs index 851898c5e..0fdfd3c8e 100644 --- a/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs +++ b/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs @@ -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(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 = ::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))]