Skip to content

Commit

Permalink
fix CI by update min_num_rows_block to return both row num without/wi…
Browse files Browse the repository at this point in the history
…th padding (privacy-scaling-explorations#1054)

* feat: min_num_rows_block returns both row num without/with padding

* fix
  • Loading branch information
lispc authored Jan 11, 2023
1 parent 5bc40eb commit 9007367
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 41 deletions.
15 changes: 9 additions & 6 deletions zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,15 @@ impl<F: Field> SubCircuit<F> for BytecodeCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
block
.bytecodes
.values()
.map(|bytecode| bytecode.bytes.len() + 1)
.sum()
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
(
block
.bytecodes
.values()
.map(|bytecode| bytecode.bytes.len() + 1)
.sum(),
block.circuits_params.max_bytecode,
)
}

/// Make the assignments to the TxCircuit
Expand Down
7 changes: 5 additions & 2 deletions zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,11 @@ impl<F: Field> SubCircuit<F> for CopyCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
block.copy_events.iter().map(|c| c.bytes.len() * 2).sum()
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
(
block.copy_events.iter().map(|c| c.bytes.len() * 2).sum(),
block.copy_circuit_pad_to,
)
}

/// Make the assignments to the CopyCircuit
Expand Down
11 changes: 7 additions & 4 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,19 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
let num_rows_required_for_execution_steps: usize =
EvmCircuit::<F>::get_num_rows_required(block);
let num_rows_required_for_fixed_table: usize = detect_fixed_table_tags(block)
.iter()
.map(|tag| tag.build::<F>().count())
.sum();
std::cmp::max(
num_rows_required_for_execution_steps,
num_rows_required_for_fixed_table,
(
std::cmp::max(
num_rows_required_for_execution_steps,
num_rows_required_for_fixed_table,
),
block.evm_circuit_pad_to,
)
}

Expand Down
15 changes: 9 additions & 6 deletions zkevm-circuits/src/exp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,15 @@ impl<F: Field> SubCircuit<F> for ExpCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
block
.exp_events
.iter()
.map(|e| e.steps.len() * OFFSET_INCREMENT)
.sum()
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
(
block
.exp_events
.iter()
.map(|e| e.steps.len() * OFFSET_INCREMENT)
.sum(),
block.exp_circuit_pad_to,
)
}

/// Make the assignments to the ExpCircuit
Expand Down
15 changes: 9 additions & 6 deletions zkevm-circuits/src/keccak_circuit/keccak_packed_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,16 @@ impl<F: Field> SubCircuit<F> for KeccakCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
let rows_per_chunk = (NUM_ROUNDS + 1) * get_num_rows_per_round();
block
.keccak_inputs
.iter()
.map(|bytes| (bytes.len() as f64 / 136.0).ceil() as usize * rows_per_chunk)
.sum()
(
block
.keccak_inputs
.iter()
.map(|bytes| (bytes.len() as f64 / 136.0).ceil() as usize * rows_per_chunk)
.sum(),
block.circuits_params.keccak_padding.unwrap_or_default(),
)
}

/// Make the assignments to the KeccakCircuit
Expand Down
18 changes: 12 additions & 6 deletions zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,12 +1144,18 @@ impl<F: Field> SubCircuit<F> for PiCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
BLOCK_LEN
+ 1
+ EXTRA_LEN
+ 3 * (TX_LEN * block.circuits_params.max_txs + 1)
+ block.circuits_params.max_calldata
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
let row_num = |tx_num, calldata_len| {
BLOCK_LEN + 1 + EXTRA_LEN + 3 * (TX_LEN * tx_num + 1) + calldata_len
};
let calldata_len = block.txs.iter().map(|tx| tx.call_data.len()).sum();
(
row_num(block.txs.len(), calldata_len),
row_num(
block.circuits_params.max_txs,
block.circuits_params.max_calldata,
),
)
}

/// Compute the public inputs for this circuit.
Expand Down
7 changes: 5 additions & 2 deletions zkevm-circuits/src/state_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,11 @@ impl<F: Field> SubCircuit<F> for StateCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
block.circuits_params.max_rws
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
(
block.rws.0.values().flatten().count() + 1,
block.circuits_params.max_rws,
)
}

/// Make the assignments to the StateCircuit
Expand Down
12 changes: 9 additions & 3 deletions zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
block.randomness = F::from(MOCK_RANDOMNESS);

const NUM_BLINDING_ROWS: usize = 64;
let rows_needed = Self::min_num_rows_block(&block);
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);

Expand Down Expand Up @@ -382,7 +382,7 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
}

/// Return the minimum number of rows required to prove the block
pub fn min_num_rows_block(block: &Block<F>) -> usize {
pub fn min_num_rows_block(block: &Block<F>) -> (usize, usize) {
let evm = EvmCircuit::min_num_rows_block(block);
let state = StateCircuit::min_num_rows_block(block);
let bytecode = BytecodeCircuit::min_num_rows_block(block);
Expand All @@ -392,7 +392,13 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
let exp = ExpCircuit::min_num_rows_block(block);
let pi = PiCircuit::min_num_rows_block(block);

itertools::max([evm, state, bytecode, copy, keccak, tx, exp, pi]).unwrap()
let rows: Vec<(usize, usize)> = vec![evm, state, bytecode, copy, keccak, tx, exp, pi];
let (rows_without_padding, rows_with_padding): (Vec<usize>, Vec<usize>) =
rows.into_iter().unzip();
(
itertools::max(rows_without_padding).unwrap(),
itertools::max(rows_with_padding).unwrap(),
)
}
}

Expand Down
14 changes: 10 additions & 4 deletions zkevm-circuits/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,16 @@ impl<F: Field> SubCircuit<F> for TxCircuit<F> {
}

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize {
Self::min_num_rows(
block.txs.len(),
block.txs.iter().map(|tx| tx.call_data.len()).sum(),
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize) {
(
Self::min_num_rows(
block.txs.len(),
block.txs.iter().map(|tx| tx.call_data.len()).sum(),
),
Self::min_num_rows(
block.circuits_params.max_txs,
block.circuits_params.max_calldata,
),
)
}

Expand Down
5 changes: 3 additions & 2 deletions zkevm-circuits/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ pub trait SubCircuit<F: Field> {
layouter: &mut impl Layouter<F>,
) -> Result<(), Error>;

/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(block: &witness::Block<F>) -> usize;
/// Return the minimum number of rows required to prove the block.
/// Row numbers without/with padding are both returned.
fn min_num_rows_block(block: &witness::Block<F>) -> (usize, usize);
}

/// SubCircuit configuration
Expand Down
3 changes: 3 additions & 0 deletions zkevm-circuits/src/witness/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct Block<F> {
pub evm_circuit_pad_to: usize,
/// Pad exponentiation circuit to make selectors fixed.
pub exp_circuit_pad_to: usize,
/// Pad copy circuit to make selectors fixed.
pub copy_circuit_pad_to: usize,
/// Circuit Setup Parameters
pub circuits_params: CircuitsParams,
/// Inputs to the SHA3 opcode
Expand Down Expand Up @@ -192,6 +194,7 @@ pub fn block_convert<F: Field>(
circuits_params: block.circuits_params.clone(),
evm_circuit_pad_to: <usize>::default(),
exp_circuit_pad_to: <usize>::default(),
copy_circuit_pad_to: <usize>::default(),
prev_state_root: block.prev_state_root,
keccak_inputs: circuit_input_builder::keccak_inputs(block, code_db)?,
eth_block: block.eth_block.clone(),
Expand Down

0 comments on commit 9007367

Please sign in to comment.