From e8f5f529e12fda76df170029320ed86fee2e9438 Mon Sep 17 00:00:00 2001 From: Eduard S Date: Wed, 22 Feb 2023 16:58:03 +0100 Subject: [PATCH 1/3] feat: cache EVM circuit configuration in unit tests --- zkevm-circuits/src/evm_circuit.rs | 60 +++++++++++++++++++++++++++++++ zkevm-circuits/src/test_util.rs | 4 +-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit.rs b/zkevm-circuits/src/evm_circuit.rs index 642a883328..1775ca0a8e 100644 --- a/zkevm-circuits/src/evm_circuit.rs +++ b/zkevm-circuits/src/evm_circuit.rs @@ -285,6 +285,66 @@ pub(crate) fn detect_fixed_table_tags(block: &Block) -> Vec, + config: (EvmCircuitConfig, Challenges), + } + + lazy_static! { + static ref CACHE: Mutex> = Mutex::new(None); + } + + pub struct EvmCircuitCached(EvmCircuit); + + impl Circuit for EvmCircuitCached { + type Config = (EvmCircuitConfig, Challenges); + type FloorPlanner = SimpleFloorPlanner; + + fn without_witnesses(&self) -> Self { + Self(self.0.without_witnesses()) + } + + fn configure(meta: &mut ConstraintSystem) -> Self::Config { + let mut cache = CACHE.lock().unwrap(); + match &*cache { + Some(Cache { cs, config }) => { + *meta = cs.clone(); + config.clone() + } + None => { + let config = EvmCircuit::::configure(meta); + *cache = Some(Cache { + cs: meta.clone(), + config: config.clone(), + }); + config + } + } + } + + fn synthesize( + &self, + config: Self::Config, + layouter: impl Layouter, + ) -> Result<(), Error> { + self.0.synthesize(config, layouter) + } + } + + impl EvmCircuitCached { + pub fn get_test_cicuit_from_block(block: Block) -> Self { + Self(EvmCircuit::::get_test_cicuit_from_block(block)) + } + } +} + // Always exported because of `EXECUTION_STATE_HEIGHT_MAP` impl Circuit for EvmCircuit { type Config = (EvmCircuitConfig, Challenges); diff --git a/zkevm-circuits/src/test_util.rs b/zkevm-circuits/src/test_util.rs index ee01046c3b..e994f8a3b1 100644 --- a/zkevm-circuits/src/test_util.rs +++ b/zkevm-circuits/src/test_util.rs @@ -1,7 +1,7 @@ //! Testing utilities use crate::{ - evm_circuit::EvmCircuit, + evm_circuit::{cached::EvmCircuitCached, EvmCircuit}, state_circuit::StateCircuit, util::SubCircuit, witness::{Block, Rw}, @@ -211,7 +211,7 @@ impl CircuitTestBuilder { let (active_gate_rows, active_lookup_rows) = EvmCircuit::::get_active_rows(&block); - let circuit = EvmCircuit::::get_test_cicuit_from_block(block.clone()); + let circuit = EvmCircuitCached::get_test_cicuit_from_block(block.clone()); let prover = MockProver::::run(k, &circuit, vec![]).unwrap(); self.evm_checks.as_ref()(prover, &active_gate_rows, &active_lookup_rows) From 853cc9ccc37585db3ec4a2ac362a225bac329a1b Mon Sep 17 00:00:00 2001 From: Eduard S Date: Wed, 22 Feb 2023 17:23:07 +0100 Subject: [PATCH 2/3] Remove mutex --- zkevm-circuits/src/evm_circuit.rs | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit.rs b/zkevm-circuits/src/evm_circuit.rs index 1775ca0a8e..c6feb4abd7 100644 --- a/zkevm-circuits/src/evm_circuit.rs +++ b/zkevm-circuits/src/evm_circuit.rs @@ -290,7 +290,6 @@ pub(crate) mod cached { use super::*; use halo2_proofs::halo2curves::bn256::Fr; use lazy_static::lazy_static; - use std::sync::Mutex; struct Cache { cs: ConstraintSystem, @@ -298,7 +297,14 @@ pub(crate) mod cached { } lazy_static! { - static ref CACHE: Mutex> = Mutex::new(None); + static ref CACHE: Cache = { + let mut meta = ConstraintSystem::::default(); + let config = EvmCircuit::::configure(&mut meta); + Cache { + cs: meta, + config: config, + } + }; } pub struct EvmCircuitCached(EvmCircuit); @@ -312,21 +318,8 @@ pub(crate) mod cached { } fn configure(meta: &mut ConstraintSystem) -> Self::Config { - let mut cache = CACHE.lock().unwrap(); - match &*cache { - Some(Cache { cs, config }) => { - *meta = cs.clone(); - config.clone() - } - None => { - let config = EvmCircuit::::configure(meta); - *cache = Some(Cache { - cs: meta.clone(), - config: config.clone(), - }); - config - } - } + *meta = CACHE.cs.clone(); + CACHE.config.clone() } fn synthesize( From 990d252c493f19f9e5680d4535942c0a57db986b Mon Sep 17 00:00:00 2001 From: Eduard S Date: Wed, 22 Feb 2023 17:42:33 +0100 Subject: [PATCH 3/3] Enable incremental compilation --- Cargo.toml | 2 +- zkevm-circuits/src/evm_circuit.rs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ae60130c36..100836d5ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,4 +34,4 @@ debug-assertions = true overflow-checks = true rpath = false lto = "thin" -incremental = false +incremental = true diff --git a/zkevm-circuits/src/evm_circuit.rs b/zkevm-circuits/src/evm_circuit.rs index c6feb4abd7..0c19a6f59d 100644 --- a/zkevm-circuits/src/evm_circuit.rs +++ b/zkevm-circuits/src/evm_circuit.rs @@ -291,6 +291,7 @@ pub(crate) mod cached { use halo2_proofs::halo2curves::bn256::Fr; use lazy_static::lazy_static; + /// Cache struct Cache { cs: ConstraintSystem, config: (EvmCircuitConfig, Challenges), @@ -300,10 +301,7 @@ pub(crate) mod cached { static ref CACHE: Cache = { let mut meta = ConstraintSystem::::default(); let config = EvmCircuit::::configure(&mut meta); - Cache { - cs: meta, - config: config, - } + Cache { cs: meta, config } }; }