Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
feat(super): remove unnecessary const generics
Browse files Browse the repository at this point in the history
- Remove MAX_TXS, MAX_CALLDATA and MAX_RWS from SuperCircuitConfig
- Remove MAX_RWS and MAX_CALLDATA from SuperCircuit
  • Loading branch information
ed255 committed Jan 25, 2023
1 parent 96fb753 commit 8d40c64
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 105 deletions.
15 changes: 13 additions & 2 deletions circuit-benchmarks/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -97,7 +108,7 @@ mod tests {
Challenge255<G1Affine>,
ChaChaRng,
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<G1Affine>>,
SuperCircuit<Fr, 1, 32, 512, 512, 0x100>,
SuperCircuit<Fr, MAX_TXS, MAX_CALLDATA, 0x100>,
>(
&general_params,
&pk,
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, TEST_MOCK_RANDOMNESS>>
test_circuit_at_block::<SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>>
("super", SUPER_CIRCUIT_DEGREE, *block_num, $real_prover, pk).await;
}
}
Expand Down
13 changes: 12 additions & 1 deletion testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Fr, 1, 32, 255, 32, 0x100>::build(geth_data).unwrap();
SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA, 0x100>::build(geth_data, circuits_params)
.unwrap();
builder = _builder;

let prover = MockProver::run(k, &circuit, instance).unwrap();
Expand Down
173 changes: 72 additions & 101 deletions zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Field> {
block_table: BlockTable,
mpt_table: MptTable,
evm_circuit: EvmCircuitConfig<F>,
Expand All @@ -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<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: usize>
SubCircuitConfig<F> for SuperCircuitConfig<F, MAX_TXS, MAX_CALLDATA, MAX_RWS>
{
impl<F: Field> SubCircuitConfig<F> for SuperCircuitConfig<F> {
type ConfigArgs = SuperCircuitConfigArgs;

/// Configure SuperCircuitConfig
fn new(
meta: &mut ConstraintSystem<F>,
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);
Expand All @@ -128,6 +126,8 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
let exp_table = ExpTable::construct(meta);
let keccak_table = KeccakTable::construct(meta);

// Use a mock randomness instead of the randomness derived from the challange
// (either from mock or real prover) to help debugging assignments.
let power_of_randomness: [Expression<F>; 31] = array::from_fn(|i| {
Expression::Constant(F::from(mock_randomness).pow(&[1 + i as u64, 0, 0, 0]))
});
Expand All @@ -149,8 +149,8 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
let pi_circuit = PiCircuitConfig::new(
meta,
PiCircuitConfigArgs {
max_txs: MAX_TXS,
max_calldata: MAX_CALLDATA,
max_txs,
max_calldata,
block_table: block_table.clone(),
tx_table: tx_table.clone(),
},
Expand Down Expand Up @@ -226,8 +226,6 @@ pub struct SuperCircuit<
F: Field,
const MAX_TXS: usize,
const MAX_CALLDATA: usize,
const MAX_RWS: usize,
const MAX_COPY_ROWS: usize,
const MOCK_RANDOMNESS: u64,
> {
/// EVM Circuit
Expand All @@ -248,14 +246,8 @@ pub struct SuperCircuit<
pub keccak_circuit: KeccakCircuit<F>,
}

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<F, MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, MOCK_RANDOMNESS>
impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MOCK_RANDOMNESS: u64>
SuperCircuit<F, MAX_TXS, MAX_CALLDATA, MOCK_RANDOMNESS>
{
/// Return the number of rows required to verify a given block
pub fn get_num_rows_required(block: &Block<F>) -> usize {
Expand All @@ -264,25 +256,19 @@ impl<
let config = Self::configure(&mut cs);
config.evm_circuit.get_num_rows_required(block)
};
let num_rows_tx_circuit = TxCircuitConfig::<F>::get_num_rows_required(MAX_TXS);
let num_rows_tx_circuit =
TxCircuitConfig::<F>::get_num_rows_required(block.circuits_params.max_txs);
num_rows_evm_circuit.max(num_rows_tx_circuit)
}
}

// 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<F>
for SuperCircuit<F, MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, MOCK_RANDOMNESS>
impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MOCK_RANDOMNESS: u64>
SubCircuit<F> for SuperCircuit<F, MAX_TXS, MAX_CALLDATA, MOCK_RANDOMNESS>
{
type Config = SuperCircuitConfig<F, MAX_TXS, MAX_CALLDATA, MAX_RWS>;
type Config = SuperCircuitConfig<F>;

fn new_from_block(block: &Block<F>) -> Self {
let evm_circuit = EvmCircuit::new_from_block(block);
Expand All @@ -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,
Expand Down Expand Up @@ -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<F>
for SuperCircuit<F, MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, MOCK_RANDOMNESS>
impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MOCK_RANDOMNESS: u64>
Circuit<F> for SuperCircuit<F, MAX_TXS, MAX_CALLDATA, MOCK_RANDOMNESS>
{
type Config = SuperCircuitConfig<F, MAX_TXS, MAX_CALLDATA, MAX_RWS>;
type Config = SuperCircuitConfig<F>;
type FloorPlanner = SimpleFloorPlanner;

fn without_witnesses(&self) -> Self {
Expand All @@ -389,6 +368,8 @@ impl<
Self::Config::new(
meta,
SuperCircuitConfigArgs {
max_txs: MAX_TXS,
max_calldata: MAX_CALLDATA,
mock_randomness: MOCK_RANDOMNESS,
},
)
Expand Down Expand Up @@ -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<F, MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, MOCK_RANDOMNESS>
impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MOCK_RANDOMNESS: u64>
SuperCircuit<F, MAX_TXS, MAX_CALLDATA, MOCK_RANDOMNESS>
{
/// From the witness data, generate a SuperCircuit instance with all of the
/// sub-circuits filled with their corresponding witnesses.
Expand All @@ -440,18 +415,10 @@ impl<
#[allow(clippy::type_complexity)]
pub fn build(
geth_data: GethData,
circuits_params: CircuitsParams,
) -> Result<(u32, Self, Vec<Vec<F>>, 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)
Expand All @@ -471,20 +438,16 @@ impl<
) -> Result<(u32, Self, Vec<Vec<F>>), 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))
Expand All @@ -508,7 +471,7 @@ mod super_circuit_tests {
#[test]
fn super_circuit_degree() {
let mut cs = ConstraintSystem::<Fr>::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);
Expand All @@ -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::<Fr, MAX_TXS, MAX_CALLDATA, MOCK_RANDOMNESS>::build(
block,
circuits_params,
)
.unwrap();
let prover = MockProver::run(k, &circuit, instance).unwrap();
let res = prover.verify_par();
if let Err(err) = res {
Expand Down Expand Up @@ -636,34 +595,46 @@ 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::<MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, TEST_MOCK_RANDOMNESS>(
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::<MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>(block, circuits_params);
}
#[ignore]
#[test]
fn serial_test_super_circuit_1tx_2max_tx() {
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::<MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, TEST_MOCK_RANDOMNESS>(
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::<MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>(block, circuits_params);
}
#[ignore]
#[test]
fn serial_test_super_circuit_2tx_2max_tx() {
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::<MAX_TXS, MAX_CALLDATA, MAX_RWS, MAX_COPY_ROWS, TEST_MOCK_RANDOMNESS>(
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::<MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>(block, circuits_params);
}
}

0 comments on commit 8d40c64

Please sign in to comment.