From 857b74bac538e5bb5d28d69a5c3f396c1a2fcf05 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Fri, 12 Nov 2021 09:48:27 -0800 Subject: [PATCH] Bring back the base field arithmetic gate (#343) * Bring back the base field arithmetic gate * fix --- src/bin/bench_recursion.rs | 1 + src/gadgets/arithmetic.rs | 136 +++++++++-- src/gadgets/arithmetic_extension.rs | 10 +- src/gates/arithmetic_base.rs | 212 ++++++++++++++++++ ...{arithmetic.rs => arithmetic_extension.rs} | 5 +- src/gates/gate_tree.rs | 2 +- src/gates/mod.rs | 3 +- src/iop/generator.rs | 2 +- src/plonk/circuit_builder.rs | 97 +++++--- src/plonk/circuit_data.rs | 4 + 10 files changed, 420 insertions(+), 52 deletions(-) create mode 100644 src/gates/arithmetic_base.rs rename src/gates/{arithmetic.rs => arithmetic_extension.rs} (96%) diff --git a/src/bin/bench_recursion.rs b/src/bin/bench_recursion.rs index e9fc25a4..fc1ad37e 100644 --- a/src/bin/bench_recursion.rs +++ b/src/bin/bench_recursion.rs @@ -26,6 +26,7 @@ fn bench_prove, const D: usize>() -> Result<()> { num_wires: 126, num_routed_wires: 33, constant_gate_size: 6, + use_base_arithmetic_gate: false, security_bits: 128, rate_bits: 3, num_challenges: 3, diff --git a/src/gadgets/arithmetic.rs b/src/gadgets/arithmetic.rs index 103857c5..3fe90019 100644 --- a/src/gadgets/arithmetic.rs +++ b/src/gadgets/arithmetic.rs @@ -1,8 +1,8 @@ use std::borrow::Borrow; use crate::field::extension_field::Extendable; -use crate::field::field_types::RichField; -use crate::gates::arithmetic::ArithmeticExtensionGate; +use crate::field::field_types::{PrimeField, RichField}; +use crate::gates::arithmetic_base::ArithmeticGate; use crate::gates::exponentiation::ExponentiationGate; use crate::iop::target::{BoolTarget, Target}; use crate::plonk::circuit_builder::CircuitBuilder; @@ -33,18 +33,117 @@ impl, const D: usize> CircuitBuilder { multiplicand_1: Target, addend: Target, ) -> Target { - let multiplicand_0_ext = self.convert_to_ext(multiplicand_0); - let multiplicand_1_ext = self.convert_to_ext(multiplicand_1); - let addend_ext = self.convert_to_ext(addend); + // If we're not configured to use the base arithmetic gate, just call arithmetic_extension. + if !self.config.use_base_arithmetic_gate { + let multiplicand_0_ext = self.convert_to_ext(multiplicand_0); + let multiplicand_1_ext = self.convert_to_ext(multiplicand_1); + let addend_ext = self.convert_to_ext(addend); - self.arithmetic_extension( + return self + .arithmetic_extension( + const_0, + const_1, + multiplicand_0_ext, + multiplicand_1_ext, + addend_ext, + ) + .0[0]; + } + + // See if we can determine the result without adding an `ArithmeticGate`. + if let Some(result) = + self.arithmetic_special_cases(const_0, const_1, multiplicand_0, multiplicand_1, addend) + { + return result; + } + + // See if we've already computed the same operation. + let operation = BaseArithmeticOperation { const_0, const_1, - multiplicand_0_ext, - multiplicand_1_ext, - addend_ext, - ) - .0[0] + multiplicand_0, + multiplicand_1, + addend, + }; + if let Some(&result) = self.base_arithmetic_results.get(&operation) { + return result; + } + + // Otherwise, we must actually perform the operation using an ArithmeticExtensionGate slot. + let result = self.add_base_arithmetic_operation(operation); + self.base_arithmetic_results.insert(operation, result); + result + } + + fn add_base_arithmetic_operation(&mut self, operation: BaseArithmeticOperation) -> Target { + let (gate, i) = self.find_base_arithmetic_gate(operation.const_0, operation.const_1); + let wires_multiplicand_0 = Target::wire(gate, ArithmeticGate::wire_ith_multiplicand_0(i)); + let wires_multiplicand_1 = Target::wire(gate, ArithmeticGate::wire_ith_multiplicand_1(i)); + let wires_addend = Target::wire(gate, ArithmeticGate::wire_ith_addend(i)); + + self.connect(operation.multiplicand_0, wires_multiplicand_0); + self.connect(operation.multiplicand_1, wires_multiplicand_1); + self.connect(operation.addend, wires_addend); + + Target::wire(gate, ArithmeticGate::wire_ith_output(i)) + } + + /// Checks for special cases where the value of + /// `const_0 * multiplicand_0 * multiplicand_1 + const_1 * addend` + /// can be determined without adding an `ArithmeticGate`. + fn arithmetic_special_cases( + &mut self, + const_0: F, + const_1: F, + multiplicand_0: Target, + multiplicand_1: Target, + addend: Target, + ) -> Option { + let zero = self.zero(); + + let mul_0_const = self.target_as_constant(multiplicand_0); + let mul_1_const = self.target_as_constant(multiplicand_1); + let addend_const = self.target_as_constant(addend); + + let first_term_zero = + const_0 == F::ZERO || multiplicand_0 == zero || multiplicand_1 == zero; + let second_term_zero = const_1 == F::ZERO || addend == zero; + + // If both terms are constant, return their (constant) sum. + let first_term_const = if first_term_zero { + Some(F::ZERO) + } else if let (Some(x), Some(y)) = (mul_0_const, mul_1_const) { + Some(x * y * const_0) + } else { + None + }; + let second_term_const = if second_term_zero { + Some(F::ZERO) + } else { + addend_const.map(|x| x * const_1) + }; + if let (Some(x), Some(y)) = (first_term_const, second_term_const) { + return Some(self.constant(x + y)); + } + + if first_term_zero && const_1.is_one() { + return Some(addend); + } + + if second_term_zero { + if let Some(x) = mul_0_const { + if (x * const_0).is_one() { + return Some(multiplicand_1); + } + } + if let Some(x) = mul_1_const { + if (x * const_0).is_one() { + return Some(multiplicand_0); + } + } + } + + None } /// Computes `x * y + z`. @@ -116,7 +215,7 @@ impl, const D: usize> CircuitBuilder { /// Exponentiate `base` to the power of `2^power_log`. pub fn exp_power_of_2(&mut self, base: Target, power_log: usize) -> Target { - if power_log > ArithmeticExtensionGate::::new_from_config(&self.config).num_ops { + if power_log > ArithmeticGate::new_from_config(&self.config).num_ops { // Cheaper to just use `ExponentiateGate`. return self.exp_u64(base, 1 << power_log); } @@ -170,8 +269,7 @@ impl, const D: usize> CircuitBuilder { let base_t = self.constant(base); let exponent_bits: Vec<_> = exponent_bits.into_iter().map(|b| *b.borrow()).collect(); - if exponent_bits.len() > ArithmeticExtensionGate::::new_from_config(&self.config).num_ops - { + if exponent_bits.len() > ArithmeticGate::new_from_config(&self.config).num_ops { // Cheaper to just use `ExponentiateGate`. return self.exp_from_bits(base_t, exponent_bits); } @@ -221,3 +319,13 @@ impl, const D: usize> CircuitBuilder { self.inverse_extension(x_ext).0[0] } } + +/// Represents a base arithmetic operation in the circuit. Used to memoize results. +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub(crate) struct BaseArithmeticOperation { + const_0: F, + const_1: F, + multiplicand_0: Target, + multiplicand_1: Target, + addend: Target, +} diff --git a/src/gadgets/arithmetic_extension.rs b/src/gadgets/arithmetic_extension.rs index e2654dcc..9fbffad3 100644 --- a/src/gadgets/arithmetic_extension.rs +++ b/src/gadgets/arithmetic_extension.rs @@ -4,7 +4,7 @@ use crate::field::extension_field::target::{ExtensionAlgebraTarget, ExtensionTar use crate::field::extension_field::FieldExtension; use crate::field::extension_field::{Extendable, OEF}; use crate::field::field_types::{Field, PrimeField, RichField}; -use crate::gates::arithmetic::ArithmeticExtensionGate; +use crate::gates::arithmetic_extension::ArithmeticExtensionGate; use crate::iop::generator::{GeneratedValues, SimpleGenerator}; use crate::iop::target::Target; use crate::iop::witness::{PartitionWitness, Witness}; @@ -32,7 +32,7 @@ impl, const D: usize> CircuitBuilder { } // See if we've already computed the same operation. - let operation = ArithmeticOperation { + let operation = ExtensionArithmeticOperation { const_0, const_1, multiplicand_0, @@ -51,7 +51,7 @@ impl, const D: usize> CircuitBuilder { fn add_arithmetic_extension_operation( &mut self, - operation: ArithmeticOperation, + operation: ExtensionArithmeticOperation, ) -> ExtensionTarget { let (gate, i) = self.find_arithmetic_gate(operation.const_0, operation.const_1); let wires_multiplicand_0 = ExtensionTarget::from_range( @@ -519,9 +519,9 @@ impl, const D: usize> CircuitBuilder { } } -/// Represents an arithmetic operation in the circuit. Used to memoize results. +/// Represents an extension arithmetic operation in the circuit. Used to memoize results. #[derive(Copy, Clone, Eq, PartialEq, Hash)] -pub(crate) struct ArithmeticOperation, const D: usize> { +pub(crate) struct ExtensionArithmeticOperation, const D: usize> { const_0: F, const_1: F, multiplicand_0: ExtensionTarget, diff --git a/src/gates/arithmetic_base.rs b/src/gates/arithmetic_base.rs new file mode 100644 index 00000000..d5c131a5 --- /dev/null +++ b/src/gates/arithmetic_base.rs @@ -0,0 +1,212 @@ +use crate::field::extension_field::target::ExtensionTarget; +use crate::field::extension_field::Extendable; +use crate::field::field_types::RichField; +use crate::gates::gate::Gate; +use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator}; +use crate::iop::target::Target; +use crate::iop::witness::{PartitionWitness, Witness}; +use crate::plonk::circuit_builder::CircuitBuilder; +use crate::plonk::circuit_data::CircuitConfig; +use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase}; + +/// A gate which can perform a weighted multiply-add, i.e. `result = c0 x y + c1 z`. If the config +/// supports enough routed wires, it can support several such operations in one gate. +#[derive(Debug)] +pub struct ArithmeticGate { + /// Number of arithmetic operations performed by an arithmetic gate. + pub num_ops: usize, +} + +impl ArithmeticGate { + pub fn new_from_config(config: &CircuitConfig) -> Self { + Self { + num_ops: Self::num_ops(config), + } + } + + /// Determine the maximum number of operations that can fit in one gate for the given config. + pub(crate) fn num_ops(config: &CircuitConfig) -> usize { + let wires_per_op = 4; + config.num_routed_wires / wires_per_op + } + + pub fn wire_ith_multiplicand_0(i: usize) -> usize { + 4 * i + } + pub fn wire_ith_multiplicand_1(i: usize) -> usize { + 4 * i + 1 + } + pub fn wire_ith_addend(i: usize) -> usize { + 4 * i + 2 + } + pub fn wire_ith_output(i: usize) -> usize { + 4 * i + 3 + } +} + +impl, const D: usize> Gate for ArithmeticGate { + fn id(&self) -> String { + format!("{:?}", self) + } + + fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { + let const_0 = vars.local_constants[0]; + let const_1 = vars.local_constants[1]; + + let mut constraints = Vec::new(); + for i in 0..self.num_ops { + let multiplicand_0 = vars.local_wires[Self::wire_ith_multiplicand_0(i)]; + let multiplicand_1 = vars.local_wires[Self::wire_ith_multiplicand_1(i)]; + let addend = vars.local_wires[Self::wire_ith_addend(i)]; + let output = vars.local_wires[Self::wire_ith_output(i)]; + let computed_output = multiplicand_0 * multiplicand_1 * const_0 + addend * const_1; + + constraints.push(output - computed_output); + } + + constraints + } + + fn eval_unfiltered_base(&self, vars: EvaluationVarsBase) -> Vec { + let const_0 = vars.local_constants[0]; + let const_1 = vars.local_constants[1]; + + let mut constraints = Vec::new(); + for i in 0..self.num_ops { + let multiplicand_0 = vars.local_wires[Self::wire_ith_multiplicand_0(i)]; + let multiplicand_1 = vars.local_wires[Self::wire_ith_multiplicand_1(i)]; + let addend = vars.local_wires[Self::wire_ith_addend(i)]; + let output = vars.local_wires[Self::wire_ith_output(i)]; + let computed_output = multiplicand_0 * multiplicand_1 * const_0 + addend * const_1; + + constraints.push(output - computed_output); + } + + constraints + } + + fn eval_unfiltered_recursively( + &self, + builder: &mut CircuitBuilder, + vars: EvaluationTargets, + ) -> Vec> { + let const_0 = vars.local_constants[0]; + let const_1 = vars.local_constants[1]; + + let mut constraints = Vec::new(); + for i in 0..self.num_ops { + let multiplicand_0 = vars.local_wires[Self::wire_ith_multiplicand_0(i)]; + let multiplicand_1 = vars.local_wires[Self::wire_ith_multiplicand_1(i)]; + let addend = vars.local_wires[Self::wire_ith_addend(i)]; + let output = vars.local_wires[Self::wire_ith_output(i)]; + let computed_output = { + let scaled_mul = + builder.mul_many_extension(&[const_0, multiplicand_0, multiplicand_1]); + builder.mul_add_extension(const_1, addend, scaled_mul) + }; + + let diff = builder.sub_extension(output, computed_output); + constraints.push(diff); + } + + constraints + } + + fn generators( + &self, + gate_index: usize, + local_constants: &[F], + ) -> Vec>> { + (0..self.num_ops) + .map(|i| { + let g: Box> = Box::new( + ArithmeticBaseGenerator { + gate_index, + const_0: local_constants[0], + const_1: local_constants[1], + i, + } + .adapter(), + ); + g + }) + .collect::>() + } + + fn num_wires(&self) -> usize { + self.num_ops * 4 + } + + fn num_constants(&self) -> usize { + 2 + } + + fn degree(&self) -> usize { + 3 + } + + fn num_constraints(&self) -> usize { + self.num_ops + } +} + +#[derive(Clone, Debug)] +struct ArithmeticBaseGenerator, const D: usize> { + gate_index: usize, + const_0: F, + const_1: F, + i: usize, +} + +impl, const D: usize> SimpleGenerator + for ArithmeticBaseGenerator +{ + fn dependencies(&self) -> Vec { + [ + ArithmeticGate::wire_ith_multiplicand_0(self.i), + ArithmeticGate::wire_ith_multiplicand_1(self.i), + ArithmeticGate::wire_ith_addend(self.i), + ] + .iter() + .map(|&i| Target::wire(self.gate_index, i)) + .collect() + } + + fn run_once(&self, witness: &PartitionWitness, out_buffer: &mut GeneratedValues) { + let get_wire = + |wire: usize| -> F { witness.get_target(Target::wire(self.gate_index, wire)) }; + + let multiplicand_0 = get_wire(ArithmeticGate::wire_ith_multiplicand_0(self.i)); + let multiplicand_1 = get_wire(ArithmeticGate::wire_ith_multiplicand_1(self.i)); + let addend = get_wire(ArithmeticGate::wire_ith_addend(self.i)); + + let output_target = Target::wire(self.gate_index, ArithmeticGate::wire_ith_output(self.i)); + + let computed_output = + multiplicand_0 * multiplicand_1 * self.const_0 + addend * self.const_1; + + out_buffer.set_target(output_target, computed_output) + } +} + +#[cfg(test)] +mod tests { + use anyhow::Result; + + use crate::field::goldilocks_field::GoldilocksField; + use crate::gates::arithmetic_base::ArithmeticGate; + use crate::gates::gate_testing::{test_eval_fns, test_low_degree}; + use crate::plonk::circuit_data::CircuitConfig; + + #[test] + fn low_degree() { + let gate = ArithmeticGate::new_from_config(&CircuitConfig::standard_recursion_config()); + test_low_degree::(gate); + } + + #[test] + fn eval_fns() -> Result<()> { + let gate = ArithmeticGate::new_from_config(&CircuitConfig::standard_recursion_config()); + test_eval_fns::(gate) + } +} diff --git a/src/gates/arithmetic.rs b/src/gates/arithmetic_extension.rs similarity index 96% rename from src/gates/arithmetic.rs rename to src/gates/arithmetic_extension.rs index 95b48e2f..dbde7535 100644 --- a/src/gates/arithmetic.rs +++ b/src/gates/arithmetic_extension.rs @@ -12,7 +12,8 @@ use crate::plonk::circuit_builder::CircuitBuilder; use crate::plonk::circuit_data::CircuitConfig; use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase}; -/// A gate which can a linear combination `c0*x*y+c1*z` twice with the same `x`. +/// A gate which can perform a weighted multiply-add, i.e. `result = c0 x y + c1 z`. If the config +/// supports enough routed wires, it can support several such operations in one gate. #[derive(Debug)] pub struct ArithmeticExtensionGate { /// Number of arithmetic operations performed by an arithmetic gate. @@ -206,7 +207,7 @@ mod tests { use anyhow::Result; use crate::field::goldilocks_field::GoldilocksField; - use crate::gates::arithmetic::ArithmeticExtensionGate; + use crate::gates::arithmetic_extension::ArithmeticExtensionGate; use crate::gates::gate_testing::{test_eval_fns, test_low_degree}; use crate::plonk::circuit_data::CircuitConfig; diff --git a/src/gates/gate_tree.rs b/src/gates/gate_tree.rs index ed9a73ac..aaba41c7 100644 --- a/src/gates/gate_tree.rs +++ b/src/gates/gate_tree.rs @@ -223,7 +223,7 @@ impl, const D: usize> Tree> { mod tests { use super::*; use crate::field::goldilocks_field::GoldilocksField; - use crate::gates::arithmetic::ArithmeticExtensionGate; + use crate::gates::arithmetic_extension::ArithmeticExtensionGate; use crate::gates::base_sum::BaseSumGate; use crate::gates::constant::ConstantGate; use crate::gates::gmimc::GMiMCGate; diff --git a/src/gates/mod.rs b/src/gates/mod.rs index b1a6028e..7f7ee32b 100644 --- a/src/gates/mod.rs +++ b/src/gates/mod.rs @@ -1,7 +1,8 @@ // Gates have `new` methods that return `GateRef`s. #![allow(clippy::new_ret_no_self)] -pub mod arithmetic; +pub mod arithmetic_base; +pub mod arithmetic_extension; pub mod arithmetic_u32; pub mod assert_le; pub mod base_sum; diff --git a/src/iop/generator.rs b/src/iop/generator.rs index 8c6cb294..ae973d7c 100644 --- a/src/iop/generator.rs +++ b/src/iop/generator.rs @@ -87,7 +87,7 @@ pub(crate) fn generate_partial_witness<'a, F: RichField + Extendable, const D assert_eq!( remaining_generators, 0, "{} generators weren't run", - remaining_generators + remaining_generators, ); witness diff --git a/src/plonk/circuit_builder.rs b/src/plonk/circuit_builder.rs index c6c49826..aac9d42e 100644 --- a/src/plonk/circuit_builder.rs +++ b/src/plonk/circuit_builder.rs @@ -12,9 +12,11 @@ use crate::field::fft::fft_root_table; use crate::field::field_types::{Field, RichField}; use crate::fri::commitment::PolynomialBatchCommitment; use crate::fri::{FriConfig, FriParams}; -use crate::gadgets::arithmetic_extension::ArithmeticOperation; +use crate::gadgets::arithmetic::BaseArithmeticOperation; +use crate::gadgets::arithmetic_extension::ExtensionArithmeticOperation; use crate::gadgets::arithmetic_u32::U32Target; -use crate::gates::arithmetic::ArithmeticExtensionGate; +use crate::gates::arithmetic_base::ArithmeticGate; +use crate::gates::arithmetic_extension::ArithmeticExtensionGate; use crate::gates::arithmetic_u32::{U32ArithmeticGate, NUM_U32_ARITHMETIC_OPS}; use crate::gates::constant::ConstantGate; use crate::gates::gate::{Gate, GateInstance, GateRef, PrefixedGate}; @@ -74,8 +76,11 @@ pub struct CircuitBuilder, const D: usize> { constants_to_targets: HashMap, targets_to_constants: HashMap, + /// Memoized results of `arithmetic` calls. + pub(crate) base_arithmetic_results: HashMap, Target>, + /// Memoized results of `arithmetic_extension` calls. - pub(crate) arithmetic_results: HashMap, ExtensionTarget>, + pub(crate) arithmetic_results: HashMap, ExtensionTarget>, batched_gates: BatchedGates, } @@ -93,6 +98,7 @@ impl, const D: usize> CircuitBuilder { marked_targets: Vec::new(), generators: Vec::new(), constants_to_targets: HashMap::new(), + base_arithmetic_results: HashMap::new(), arithmetic_results: HashMap::new(), targets_to_constants: HashMap::new(), batched_gates: BatchedGates::new(), @@ -742,11 +748,13 @@ impl, const D: usize> CircuitBuilder { } } -/// Various gate types can contain multiple copies in a single Gate. This helper struct lets a CircuitBuilder track such gates that are currently being "filled up." +/// Various gate types can contain multiple copies in a single Gate. This helper struct lets a +/// CircuitBuilder track such gates that are currently being "filled up." pub struct BatchedGates, const D: usize> { /// A map `(c0, c1) -> (g, i)` from constants `(c0,c1)` to an available arithmetic gate using /// these constants with gate index `g` and already using `i` arithmetic operations. pub(crate) free_arithmetic: HashMap<(F, F), (usize, usize)>, + pub(crate) free_base_arithmetic: HashMap<(F, F), (usize, usize)>, /// A map `(c0, c1) -> (g, i)` from constants `vec_size` to an available arithmetic gate using /// these constants with gate index `g` and already using `i` random accesses. @@ -771,6 +779,7 @@ impl, const D: usize> BatchedGates { pub fn new() -> Self { Self { free_arithmetic: HashMap::new(), + free_base_arithmetic: HashMap::new(), free_random_access: HashMap::new(), current_switch_gates: Vec::new(), current_u32_arithmetic_gate: None, @@ -781,6 +790,37 @@ impl, const D: usize> BatchedGates { } impl, const D: usize> CircuitBuilder { + /// Finds the last available arithmetic gate with the given constants or add one if there aren't any. + /// Returns `(g,i)` such that there is an arithmetic gate with the given constants at index + /// `g` and the gate's `i`-th operation is available. + pub(crate) fn find_base_arithmetic_gate(&mut self, const_0: F, const_1: F) -> (usize, usize) { + let (gate, i) = self + .batched_gates + .free_base_arithmetic + .get(&(const_0, const_1)) + .copied() + .unwrap_or_else(|| { + let gate = self.add_gate( + ArithmeticGate::new_from_config(&self.config), + vec![const_0, const_1], + ); + (gate, 0) + }); + + // Update `free_arithmetic` with new values. + if i < ArithmeticGate::num_ops(&self.config) - 1 { + self.batched_gates + .free_base_arithmetic + .insert((const_0, const_1), (gate, i + 1)); + } else { + self.batched_gates + .free_base_arithmetic + .remove(&(const_0, const_1)); + } + + (gate, i) + } + /// Finds the last available arithmetic gate with the given constants or add one if there aren't any. /// Returns `(g,i)` such that there is an arithmetic gate with the given constants at index /// `g` and the gate's `i`-th operation is available. @@ -941,36 +981,36 @@ impl, const D: usize> CircuitBuilder { (gate, instance) } + /// Fill the remaining unused arithmetic operations with zeros, so that all + /// `ArithmeticGate` are run. + fn fill_base_arithmetic_gates(&mut self) { + let zero = self.zero(); + for ((c0, c1), (_gate, i)) in self.batched_gates.free_base_arithmetic.clone() { + for _ in i..ArithmeticGate::num_ops(&self.config) { + // If we directly wire in zero, an optimization will skip doing anything and return + // zero. So we pass in a virtual target and connect it to zero afterward. + let dummy = self.add_virtual_target(); + self.arithmetic(c0, c1, dummy, dummy, dummy); + self.connect(dummy, zero); + } + } + assert!(self.batched_gates.free_base_arithmetic.is_empty()); + } + /// Fill the remaining unused arithmetic operations with zeros, so that all /// `ArithmeticExtensionGenerator`s are run. fn fill_arithmetic_gates(&mut self) { let zero = self.zero_extension(); - let remaining_arithmetic_gates = self - .batched_gates - .free_arithmetic - .values() - .copied() - .collect::>(); - for (gate, i) in remaining_arithmetic_gates { - for j in i..ArithmeticExtensionGate::::num_ops(&self.config) { - let wires_multiplicand_0 = ExtensionTarget::from_range( - gate, - ArithmeticExtensionGate::::wires_ith_multiplicand_0(j), - ); - let wires_multiplicand_1 = ExtensionTarget::from_range( - gate, - ArithmeticExtensionGate::::wires_ith_multiplicand_1(j), - ); - let wires_addend = ExtensionTarget::from_range( - gate, - ArithmeticExtensionGate::::wires_ith_addend(j), - ); - - self.connect_extension(zero, wires_multiplicand_0); - self.connect_extension(zero, wires_multiplicand_1); - self.connect_extension(zero, wires_addend); + for ((c0, c1), (_gate, i)) in self.batched_gates.free_arithmetic.clone() { + for _ in i..ArithmeticExtensionGate::::num_ops(&self.config) { + // If we directly wire in zero, an optimization will skip doing anything and return + // zero. So we pass in a virtual target and connect it to zero afterward. + let dummy = self.add_virtual_extension_target(); + self.arithmetic_extension(c0, c1, dummy, dummy, dummy); + self.connect_extension(dummy, zero); } } + assert!(self.batched_gates.free_arithmetic.is_empty()); } /// Fill the remaining unused random access operations with zeros, so that all @@ -1064,6 +1104,7 @@ impl, const D: usize> CircuitBuilder { fn fill_batched_gates(&mut self) { self.fill_arithmetic_gates(); + self.fill_base_arithmetic_gates(); self.fill_random_access_gates(); self.fill_switch_gates(); self.fill_u32_arithmetic_gates(); diff --git a/src/plonk/circuit_data.rs b/src/plonk/circuit_data.rs index 869543af..d54d327d 100644 --- a/src/plonk/circuit_data.rs +++ b/src/plonk/circuit_data.rs @@ -26,6 +26,9 @@ pub struct CircuitConfig { pub num_wires: usize, pub num_routed_wires: usize, pub constant_gate_size: usize, + /// Whether to use a dedicated gate for base field arithmetic, rather than using a single gate + /// for both base field and extension field arithmetic. + pub use_base_arithmetic_gate: bool, pub security_bits: usize, pub rate_bits: usize, /// The number of challenge points to generate, for IOPs that have soundness errors of (roughly) @@ -55,6 +58,7 @@ impl CircuitConfig { num_wires: 143, num_routed_wires: 25, constant_gate_size: 6, + use_base_arithmetic_gate: true, security_bits: 100, rate_bits: 3, num_challenges: 2,