Skip to content

Commit

Permalink
Keccak integration tests and more (scroll-tech#1224)
Browse files Browse the repository at this point in the history
### Description

Add keccak to integration tests, and solve some issues that also fix
variadic problem for Super Circuit integration test.

### Issue Link

Closes
privacy-scaling-explorations#1128
Closes
privacy-scaling-explorations#1208
Closes
privacy-scaling-explorations#1209
Closes
privacy-scaling-explorations#1212

### Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Contents

- Keccak integration tests should not have keccak_padding: None. 
- SuperCircuit integration tests: make it non variadic.
- Add keccak circuit to integration tests. 
- Unify circuits params dynamic / static encodings. 

### Rationale

In the issues.

### How Has This Been Tested?

Integration test mock prover passes. The integration test actual prover
does not pass because of unrelated bug
privacy-scaling-explorations#1016

---------

Co-authored-by: Eduard S. <[email protected]>
  • Loading branch information
leolara and ed255 authored Feb 23, 2023
1 parent 0cf673b commit 3cf53b4
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 26 deletions.
14 changes: 8 additions & 6 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ pub struct CircuitsParams {
/// Maximum number of bytes supported in the Bytecode Circuit
pub max_bytecode: usize,
/// Pad evm circuit number of rows.
/// When 0, the EVM circuit number of row will be dynamically calculated, so
/// the same circuit will not be able to proof different witnesses. In this
/// case it will contain as many rows for all steps + 1 row
/// When 0, the EVM circuit number of rows will be dynamically calculated,
/// so the same circuit will not be able to proof different witnesses.
/// In this case it will contain as many rows for all steps + 1 row
/// for EndBlock.
pub max_evm_rows: usize,
// TODO: Rename for consistency
/// Pad the keccak circuit with this number of invocations to a static
/// capacity. Number of keccak_f that the Keccak circuit will support.
pub keccak_padding: Option<usize>,
/// When 0, the Keccak circuit number of rows will be dynamically
/// calculated, so the same circuit will not be able to prove different
/// witnesses.
pub max_keccak_rows: usize,
}

impl Default for CircuitsParams {
Expand All @@ -77,7 +79,7 @@ impl Default for CircuitsParams {
max_exp_steps: 1000,
max_bytecode: 512,
max_evm_rows: 0,
keccak_padding: None,
max_keccak_rows: 0,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion circuit-benchmarks/src/packed_multi_keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
let inputs = vec![(0u8..135).collect::<Vec<_>>(); 3];

// Create the circuit. Leave last dozens of rows for blinding.
let circuit = KeccakCircuit::new(Some(2usize.pow(degree) - 64), inputs);
let circuit = KeccakCircuit::new(2usize.pow(degree) - 64, inputs);

// Initialize the polynomial commitment parameters
let mut rng = XorShiftRng::from_seed([
Expand Down
2 changes: 1 addition & 1 deletion circuit-benchmarks/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mod tests {
max_exp_steps: 256,
max_bytecode: 512,
max_evm_rows: 0,
keccak_padding: None,
max_keccak_rows: 0,
};
let (_, circuit, instance, _) =
SuperCircuit::<_, MAX_TXS, MAX_CALLDATA, 0x100>::build(block, circuits_params).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

ARG_DEFAULT_SUDO=
ARG_DEFAULT_STEPS="setup gendata tests cleanup"
ARG_DEFAULT_TESTS="rpc circuit_input_builder circuits:mock_prover"
ARG_DEFAULT_TESTS="rpc circuit_input_builder circuits::mock_prover"

usage() {
cat >&2 << EOF
Expand Down
13 changes: 11 additions & 2 deletions integration-tests/src/integration_test_circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use tokio::sync::Mutex as TokioMutex;
use zkevm_circuits::bytecode_circuit::circuit::BytecodeCircuit;
use zkevm_circuits::copy_circuit::CopyCircuit;
use zkevm_circuits::evm_circuit::EvmCircuit;
use zkevm_circuits::keccak_circuit::KeccakCircuit;
use zkevm_circuits::state_circuit::StateCircuit;
use zkevm_circuits::super_circuit::SuperCircuit;
use zkevm_circuits::tx_circuit::TxCircuit;
Expand All @@ -51,7 +52,9 @@ const MAX_COPY_ROWS: usize = 5888;
/// MAX_EVM_ROWS
const MAX_EVM_ROWS: usize = 10000;
/// MAX_EXP_STEPS
pub const MAX_EXP_STEPS: usize = 1000;
const MAX_EXP_STEPS: usize = 1000;

const MAX_KECCAK_ROWS: usize = 10000;

const CIRCUITS_PARAMS: CircuitsParams = CircuitsParams {
max_rws: MAX_RWS,
Expand All @@ -61,7 +64,7 @@ const CIRCUITS_PARAMS: CircuitsParams = CircuitsParams {
max_copy_rows: MAX_COPY_ROWS,
max_evm_rows: MAX_EVM_ROWS,
max_exp_steps: MAX_EXP_STEPS,
keccak_padding: None,
max_keccak_rows: MAX_KECCAK_ROWS,
};

/// EVM Circuit degree
Expand All @@ -74,6 +77,8 @@ const TX_CIRCUIT_DEGREE: u32 = 20;
const BYTECODE_CIRCUIT_DEGREE: u32 = 16;
/// Copy Circuit degree
const COPY_CIRCUIT_DEGREE: u32 = 16;

const KECCAK_CIRCUIT_DEGREE: u32 = 16;
/// Super Circuit degree
const SUPER_CIRCUIT_DEGREE: u32 = 20;

Expand Down Expand Up @@ -111,6 +116,10 @@ lazy_static! {
pub static ref COPY_CIRCUIT_TEST: TokioMutex<IntegrationTest<CopyCircuit<Fr>>> =
TokioMutex::new(IntegrationTest::new("Copy", COPY_CIRCUIT_DEGREE));

/// Integration test for Keccak circuit
pub static ref KECCAK_CIRCUIT_TEST: TokioMutex<IntegrationTest<KeccakCircuit<Fr>>> =
TokioMutex::new(IntegrationTest::new("Keccak", KECCAK_CIRCUIT_DEGREE));

/// Integration test for Copy circuit
pub static ref SUPER_CIRCUIT_TEST: TokioMutex<IntegrationTest<SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>>> =
TokioMutex::new(IntegrationTest::new("Super", SUPER_CIRCUIT_DEGREE));
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/tests/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn test_circuit_input_builder_block(block_num: u64) {
max_copy_rows: 16384,
max_evm_rows: 0,
max_exp_steps: 1000,
keccak_padding: None,
max_keccak_rows: 0,
},
)
.await
Expand Down
6 changes: 6 additions & 0 deletions integration-tests/tests/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ macro_rules! declare_tests {
run_test! (COPY_CIRCUIT_TEST, $block_tag, $real_prover);
}

#[tokio::test]
async fn [<serial_test_keccak_ $name>]() {
run_test! (KECCAK_CIRCUIT_TEST, $block_tag, $real_prover);
}

#[tokio::test]
async fn [<serial_test_super_ $name>]() {
run_test! (SUPER_CIRCUIT_TEST, $block_tag, $real_prover);
Expand All @@ -52,6 +57,7 @@ macro_rules! unroll_tests {
TX_CIRCUIT_TEST,
BYTECODE_CIRCUIT_TEST,
COPY_CIRCUIT_TEST,
KECCAK_CIRCUIT_TEST,
SUPER_CIRCUIT_TEST
};
use integration_tests::log_init;
Expand Down
4 changes: 2 additions & 2 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub fn run_test(
max_copy_rows: 55000,
max_evm_rows: 0,
max_exp_steps: 5000,
keccak_padding: None,
max_keccak_rows: 0,
};
let block_data = BlockData::new_from_geth_data_with_params(geth_data, circuits_params);

Expand All @@ -293,7 +293,7 @@ pub fn run_test(
max_exp_steps: 256,
max_bytecode: 512,
max_evm_rows: 0,
keccak_padding: None,
max_keccak_rows: 0,
};
let (k, circuit, instance, _builder) =
SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA, 0x100>::build(geth_data, circuits_params)
Expand Down
17 changes: 10 additions & 7 deletions zkevm-circuits/src/keccak_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ impl<F: Field> KeccakCircuitConfig<F> {
#[derive(Default, Clone, Debug)]
pub struct KeccakCircuit<F: Field> {
inputs: Vec<Vec<u8>>,
num_rows: Option<usize>,
num_rows: usize,
_marker: PhantomData<F>,
}

Expand All @@ -960,7 +960,7 @@ impl<F: Field> SubCircuit<F> for KeccakCircuit<F> {
/// independently of the permutations required by `inputs`.
fn new_from_block(block: &witness::Block<F>) -> Self {
Self::new(
block.circuits_params.keccak_padding,
block.circuits_params.max_keccak_rows,
block.keccak_inputs.clone(),
)
}
Expand All @@ -974,7 +974,7 @@ impl<F: Field> SubCircuit<F> for KeccakCircuit<F> {
.iter()
.map(|bytes| (bytes.len() as f64 / 136.0).ceil() as usize * rows_per_chunk)
.sum(),
block.circuits_params.keccak_padding.unwrap_or_default(),
block.circuits_params.max_keccak_rows,
)
}

Expand Down Expand Up @@ -1029,7 +1029,7 @@ impl<F: Field> Circuit<F> for KeccakCircuit<F> {

impl<F: Field> KeccakCircuit<F> {
/// Creates a new circuit instance
pub fn new(num_rows: Option<usize>, inputs: Vec<Vec<u8>>) -> Self {
pub fn new(num_rows: usize, inputs: Vec<Vec<u8>>) -> Self {
KeccakCircuit {
inputs,
num_rows,
Expand All @@ -1039,9 +1039,12 @@ impl<F: Field> KeccakCircuit<F> {

/// The number of keccak_f's that can be done in this circuit
pub fn capacity(&self) -> Option<usize> {
// Subtract two for unusable rows
self.num_rows
.map(|num_rows| num_rows / ((NUM_ROUNDS + 1) * get_num_rows_per_round()) - 2)
if self.num_rows > 0 {
// Subtract two for unusable rows
Some(self.num_rows / ((NUM_ROUNDS + 1) * get_num_rows_per_round()) - 2)
} else {
None
}
}

/// Sets the witness using the data to be hashed
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/keccak_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
use log::error;

fn verify<F: Field>(k: u32, inputs: Vec<Vec<u8>>, success: bool) {
let circuit = KeccakCircuit::new(Some(2usize.pow(k)), inputs);
let circuit = KeccakCircuit::new(2usize.pow(k), inputs);

let prover = MockProver::<F>::run(k, &circuit, vec![]).unwrap();
let verify_result = prover.verify();
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/root_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ mod test {
max_exp_steps: 256,
max_bytecode: 512,
max_evm_rows: 0,
keccak_padding: None,
max_keccak_rows: 0,
};
let (k, circuit, instance, _) =
SuperCircuit::<_, MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>::build(
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ pub(crate) mod super_circuit_tests {
max_exp_steps: 256,
max_bytecode: 512,
max_evm_rows: 0,
keccak_padding: None,
max_keccak_rows: 0,
};
test_super_circuit::<MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>(block, circuits_params);
}
Expand All @@ -616,7 +616,7 @@ pub(crate) mod super_circuit_tests {
max_exp_steps: 256,
max_bytecode: 512,
max_evm_rows: 0,
keccak_padding: None,
max_keccak_rows: 0,
};
test_super_circuit::<MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>(block, circuits_params);
}
Expand All @@ -634,7 +634,7 @@ pub(crate) mod super_circuit_tests {
max_exp_steps: 256,
max_bytecode: 512,
max_evm_rows: 0,
keccak_padding: None,
max_keccak_rows: 0,
};
test_super_circuit::<MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>(block, circuits_params);
}
Expand Down

0 comments on commit 3cf53b4

Please sign in to comment.