From 8d40c6497332789fb45ac583782838ffaa1eb141 Mon Sep 17 00:00:00 2001 From: Eduard S Date: Wed, 25 Jan 2023 11:04:22 +0100 Subject: [PATCH] feat(super): remove unnecessary const generics - Remove MAX_TXS, MAX_CALLDATA and MAX_RWS from SuperCircuitConfig - Remove MAX_RWS and MAX_CALLDATA from SuperCircuit --- circuit-benchmarks/src/super_circuit.rs | 15 +- integration-tests/tests/circuits.rs | 2 +- testool/src/statetest/executor.rs | 13 +- zkevm-circuits/src/super_circuit.rs | 173 ++++++++++-------------- 4 files changed, 98 insertions(+), 105 deletions(-) diff --git a/circuit-benchmarks/src/super_circuit.rs b/circuit-benchmarks/src/super_circuit.rs index e450ed1a527..ac9cddfcd5d 100644 --- a/circuit-benchmarks/src/super_circuit.rs +++ b/circuit-benchmarks/src/super_circuit.rs @@ -3,6 +3,7 @@ #[cfg(test)] mod tests { use ark_std::{end_timer, start_timer}; + use bus_mapping::circuit_input_builder::CircuitsParams; use eth_types::geth_types::GethData; use eth_types::{address, bytecode, Word}; use ethers_signers::LocalWallet; @@ -71,8 +72,18 @@ mod tests { block.sign(&wallets); + const MAX_TXS: usize = 1; + const MAX_CALLDATA: usize = 32; + let circuits_params = CircuitsParams { + max_txs: MAX_TXS, + max_calldata: MAX_CALLDATA, + max_rws: 256, + max_copy_rows: 256, + max_bytecode: 512, + keccak_padding: None, + }; let (_, circuit, instance, _) = - SuperCircuit::<_, 1, 32, 512, 512, 0x100>::build(block).unwrap(); + SuperCircuit::<_, MAX_TXS, MAX_CALLDATA, 0x100>::build(block, circuits_params).unwrap(); let instance_refs: Vec<&[Fr]> = instance.iter().map(|v| &v[..]).collect(); // Bench setup generation @@ -97,7 +108,7 @@ mod tests { Challenge255, ChaChaRng, Blake2bWrite, G1Affine, Challenge255>, - SuperCircuit, + SuperCircuit, >( &general_params, &pk, diff --git a/integration-tests/tests/circuits.rs b/integration-tests/tests/circuits.rs index e014981f6de..7ffd1a78018 100644 --- a/integration-tests/tests/circuits.rs +++ b/integration-tests/tests/circuits.rs @@ -51,7 +51,7 @@ macro_rules! declare_tests { log_init(); let block_num = GEN_DATA.blocks.get($block_tag).unwrap(); let pk = None; - test_circuit_at_block::> + test_circuit_at_block::> ("super", SUPER_CIRCUIT_DEGREE, *block_num, $real_prover, pk).await; } } diff --git a/testool/src/statetest/executor.rs b/testool/src/statetest/executor.rs index ce211f08505..b9143311eb2 100644 --- a/testool/src/statetest/executor.rs +++ b/testool/src/statetest/executor.rs @@ -275,8 +275,19 @@ pub fn run_test( } else { geth_data.sign(&wallets); + const MAX_TXS: usize = 1; + const MAX_CALLDATA: usize = 32; + let circuits_params = CircuitsParams { + max_txs: MAX_TXS, + max_calldata: MAX_CALLDATA, + max_rws: 256, + max_copy_rows: 256, + max_bytecode: 512, + keccak_padding: None, + }; let (k, circuit, instance, _builder) = - SuperCircuit::::build(geth_data).unwrap(); + SuperCircuit::::build(geth_data, circuits_params) + .unwrap(); builder = _builder; let prover = MockProver::run(k, &circuit, instance).unwrap(); diff --git a/zkevm-circuits/src/super_circuit.rs b/zkevm-circuits/src/super_circuit.rs index c40e8a0c590..a2a549736c0 100644 --- a/zkevm-circuits/src/super_circuit.rs +++ b/zkevm-circuits/src/super_circuit.rs @@ -79,17 +79,9 @@ use halo2_proofs::{ use std::array; -// TODO: Figure out if we can remove MAX_TXS, MAX_CALLDATA and MAX_RWS from the -// struct. - /// Configuration of the Super Circuit #[derive(Clone)] -pub struct SuperCircuitConfig< - F: Field, - const MAX_TXS: usize, - const MAX_CALLDATA: usize, - const MAX_RWS: usize, -> { +pub struct SuperCircuitConfig { block_table: BlockTable, mpt_table: MptTable, evm_circuit: EvmCircuitConfig, @@ -104,19 +96,25 @@ pub struct SuperCircuitConfig< /// Circuit configuration arguments pub struct SuperCircuitConfigArgs { + /// Max txs + pub max_txs: usize, + /// Max calldata + pub max_calldata: usize, /// Mock randomness pub mock_randomness: u64, } -impl - SubCircuitConfig for SuperCircuitConfig -{ +impl SubCircuitConfig for SuperCircuitConfig { type ConfigArgs = SuperCircuitConfigArgs; /// Configure SuperCircuitConfig fn new( meta: &mut ConstraintSystem, - Self::ConfigArgs { mock_randomness }: Self::ConfigArgs, + Self::ConfigArgs { + max_txs, + max_calldata, + mock_randomness, + }: Self::ConfigArgs, ) -> Self { let tx_table = TxTable::construct(meta); let rw_table = RwTable::construct(meta); @@ -128,6 +126,8 @@ impl; 31] = array::from_fn(|i| { Expression::Constant(F::from(mock_randomness).pow(&[1 + i as u64, 0, 0, 0])) }); @@ -149,8 +149,8 @@ impl { /// EVM Circuit @@ -248,14 +246,8 @@ pub struct SuperCircuit< pub keccak_circuit: KeccakCircuit, } -impl< - F: Field, - const MAX_TXS: usize, - const MAX_CALLDATA: usize, - const MAX_RWS: usize, - const MAX_COPY_ROWS: usize, - const MOCK_RANDOMNESS: u64, - > SuperCircuit +impl + SuperCircuit { /// Return the number of rows required to verify a given block pub fn get_num_rows_required(block: &Block) -> usize { @@ -264,7 +256,8 @@ impl< let config = Self::configure(&mut cs); config.evm_circuit.get_num_rows_required(block) }; - let num_rows_tx_circuit = TxCircuitConfig::::get_num_rows_required(MAX_TXS); + let num_rows_tx_circuit = + TxCircuitConfig::::get_num_rows_required(block.circuits_params.max_txs); num_rows_evm_circuit.max(num_rows_tx_circuit) } } @@ -272,17 +265,10 @@ impl< // Eventhough the SuperCircuit is not a subcircuit we implement the SubCircuit // trait for it in order to get the `new_from_block` and `instance` methods that // allow us to generalize integration tests. -impl< - F: Field, - const MAX_TXS: usize, - const MAX_CALLDATA: usize, - const MAX_RWS: usize, - const MAX_COPY_ROWS: usize, - const MOCK_RANDOMNESS: u64, - > SubCircuit - for SuperCircuit +impl + SubCircuit for SuperCircuit { - type Config = SuperCircuitConfig; + type Config = SuperCircuitConfig; fn new_from_block(block: &Block) -> Self { let evm_circuit = EvmCircuit::new_from_block(block); @@ -294,7 +280,7 @@ impl< let exp_circuit = ExpCircuit::new_from_block(block); let keccak_circuit = KeccakCircuit::new_from_block(block); - SuperCircuit::<_, MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, MOCK_RANDOMNESS> { + SuperCircuit::<_, MAX_TXS, MAX_CALLDATA, MOCK_RANDOMNESS> { evm_circuit, state_circuit, tx_circuit, @@ -368,17 +354,10 @@ impl< } } -impl< - F: Field, - const MAX_TXS: usize, - const MAX_CALLDATA: usize, - const MAX_RWS: usize, - const MAX_COPY_ROWS: usize, - const MOCK_RANDOMNESS: u64, - > Circuit - for SuperCircuit +impl + Circuit for SuperCircuit { - type Config = SuperCircuitConfig; + type Config = SuperCircuitConfig; type FloorPlanner = SimpleFloorPlanner; fn without_witnesses(&self) -> Self { @@ -389,6 +368,8 @@ impl< Self::Config::new( meta, SuperCircuitConfigArgs { + max_txs: MAX_TXS, + max_calldata: MAX_CALLDATA, mock_randomness: MOCK_RANDOMNESS, }, ) @@ -423,14 +404,8 @@ impl< } } -impl< - F: Field, - const MAX_TXS: usize, - const MAX_CALLDATA: usize, - const MAX_RWS: usize, - const MAX_COPY_ROWS: usize, - const MOCK_RANDOMNESS: u64, - > SuperCircuit +impl + SuperCircuit { /// From the witness data, generate a SuperCircuit instance with all of the /// sub-circuits filled with their corresponding witnesses. @@ -440,18 +415,10 @@ impl< #[allow(clippy::type_complexity)] pub fn build( geth_data: GethData, + circuits_params: CircuitsParams, ) -> Result<(u32, Self, Vec>, CircuitInputBuilder), bus_mapping::Error> { - let block_data = BlockData::new_from_geth_data_with_params( - geth_data.clone(), - CircuitsParams { - max_txs: MAX_TXS, - max_calldata: MAX_CALLDATA, - max_rws: MAX_RWS, - max_copy_rows: MAX_COPY_ROWS, - max_bytecode: 512, - keccak_padding: None, - }, - ); + let block_data = + BlockData::new_from_geth_data_with_params(geth_data.clone(), circuits_params); let mut builder = block_data.new_circuit_input_builder(); builder .handle_block(&geth_data.eth_block, &geth_data.geth_traces) @@ -471,20 +438,16 @@ impl< ) -> Result<(u32, Self, Vec>), bus_mapping::Error> { let mut block = block_convert(&builder.block, &builder.code_db).unwrap(); block.randomness = F::from(MOCK_RANDOMNESS); + assert_eq!(block.circuits_params.max_txs, MAX_TXS); + assert_eq!(block.circuits_params.max_calldata, MAX_CALLDATA); const NUM_BLINDING_ROWS: usize = 64; let (_, rows_needed) = Self::min_num_rows_block(&block); let k = log2_ceil(NUM_BLINDING_ROWS + rows_needed); log::debug!("super circuit uses k = {}", k); - let circuit = SuperCircuit::< - _, - MAX_TXS, - MAX_CALLDATA, - MAX_RWS, - MAX_COPY_ROWS, - MOCK_RANDOMNESS, - >::new_from_block(&block); + let circuit = + SuperCircuit::<_, MAX_TXS, MAX_CALLDATA, MOCK_RANDOMNESS>::new_from_block(&block); let instance = circuit.instance(); Ok((k, circuit, instance)) @@ -508,7 +471,7 @@ mod super_circuit_tests { #[test] fn super_circuit_degree() { let mut cs = ConstraintSystem::::default(); - SuperCircuit::<_, 1, 32, 256, 32, 0x100>::configure(&mut cs); + SuperCircuit::<_, 1, 32, 0x100>::configure(&mut cs); log::info!("super circuit degree: {}", cs.degree()); log::info!("super circuit minimum_rows: {}", cs.minimum_rows()); assert!(cs.degree() <= 9); @@ -517,21 +480,17 @@ mod super_circuit_tests { fn test_super_circuit< const MAX_TXS: usize, const MAX_CALLDATA: usize, - const MAX_RWS: usize, - const MAX_COPY_ROWS: usize, const MOCK_RANDOMNESS: u64, >( block: GethData, + circuits_params: CircuitsParams, ) { - let (k, circuit, instance, _) = SuperCircuit::< - Fr, - MAX_TXS, - MAX_CALLDATA, - MAX_RWS, - MAX_COPY_ROWS, - MOCK_RANDOMNESS, - >::build(block) - .unwrap(); + let (k, circuit, instance, _) = + SuperCircuit::::build( + block, + circuits_params, + ) + .unwrap(); let prover = MockProver::run(k, &circuit, instance).unwrap(); let res = prover.verify_par(); if let Err(err) = res { @@ -636,11 +595,15 @@ mod super_circuit_tests { let block = block_1tx(); const MAX_TXS: usize = 1; const MAX_CALLDATA: usize = 32; - const MAX_RWS: usize = 256; - const MAX_COPY_ROWS: usize = 256; - test_super_circuit::( - block, - ); + let circuits_params = CircuitsParams { + max_txs: MAX_TXS, + max_calldata: MAX_CALLDATA, + max_rws: 256, + max_copy_rows: 256, + max_bytecode: 512, + keccak_padding: None, + }; + test_super_circuit::(block, circuits_params); } #[ignore] #[test] @@ -648,11 +611,15 @@ mod super_circuit_tests { let block = block_1tx(); const MAX_TXS: usize = 2; const MAX_CALLDATA: usize = 32; - const MAX_RWS: usize = 256; - const MAX_COPY_ROWS: usize = 256; - test_super_circuit::( - block, - ); + let circuits_params = CircuitsParams { + max_txs: MAX_TXS, + max_calldata: MAX_CALLDATA, + max_rws: 256, + max_copy_rows: 256, + max_bytecode: 512, + keccak_padding: None, + }; + test_super_circuit::(block, circuits_params); } #[ignore] #[test] @@ -660,10 +627,14 @@ mod super_circuit_tests { let block = block_2tx(); const MAX_TXS: usize = 2; const MAX_CALLDATA: usize = 32; - const MAX_RWS: usize = 256; - const MAX_COPY_ROWS: usize = 256; - test_super_circuit::( - block, - ); + let circuits_params = CircuitsParams { + max_txs: MAX_TXS, + max_calldata: MAX_CALLDATA, + max_rws: 256, + max_copy_rows: 256, + max_bytecode: 512, + keccak_padding: None, + }; + test_super_circuit::(block, circuits_params); } }