From 8642a10fde28af930c80f174d724499034c6cc94 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Wed, 21 Jul 2021 15:58:15 +0200 Subject: [PATCH] Start of optimization --- src/gadgets/arithmetic_extension.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/gadgets/arithmetic_extension.rs b/src/gadgets/arithmetic_extension.rs index 10b60dcd..0cf57ed6 100644 --- a/src/gadgets/arithmetic_extension.rs +++ b/src/gadgets/arithmetic_extension.rs @@ -80,6 +80,7 @@ impl, const D: usize> CircuitBuilder { self.arithmetic_extension(F::ONE, F::ONE, one, a, b) } + /// Returns `(a0+b0, a1+b1)`. pub fn add_two_extension( &mut self, a0: ExtensionTarget, @@ -196,6 +197,17 @@ impl, const D: usize> CircuitBuilder { self.mul_extension_with_const(F::ONE, multiplicand_0, multiplicand_1) } + /// Returns `(a0*b0, a1*b1)`. + pub fn mul_two_extension( + &mut self, + a0: ExtensionTarget, + b0: ExtensionTarget, + a1: ExtensionTarget, + b1: ExtensionTarget, + ) -> (ExtensionTarget, ExtensionTarget) { + todo!() + } + /// Computes `x^2`. pub fn square_extension(&mut self, x: ExtensionTarget) -> ExtensionTarget { self.mul_extension(x, x) @@ -222,6 +234,19 @@ impl, const D: usize> CircuitBuilder { } pub fn mul_many_extension(&mut self, terms: &[ExtensionTarget]) -> ExtensionTarget { + let one = self.one_extension(); + let mut terms = terms.to_vec(); + if terms.len().is_odd() { + terms.push(one); + } + // We maintain two accumulators, one for the sum of even elements, and one for odd elements. + let mut acc0 = one; + let mut acc1 = one; + for chunk in terms.chunks_exact(2) { + (acc0, acc1) = self.mul_two_extension(acc0, chunk[0], acc1, chunk[1]); + } + // We sum both accumulators to get the final result. + self.add_extension(acc0, acc1) let mut product = self.one_extension(); for term in terms { product = self.mul_extension(product, *term);