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

Commit

Permalink
Merge branch 'main' into integration-tests-benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
AronisAt79 authored Oct 5, 2023
2 parents 481b533 + ed866f0 commit 5e3dbf9
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 177 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ pub struct FixedCParams {
pub max_keccak_rows: usize,
}

/// Unset Circuits Parameters, computed dynamically together with circuit witness generation.
/// Unset Circuits Parameters
///
/// To reduce the testing overhead, we determine the parameters by the testing inputs.
/// A new [`FixedCParams`] will be computed from the generated circuit witness.
#[derive(Debug, Clone, Copy)]
pub struct DynamicCParams {}

Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ impl<'a> CircuitInputStateRef<'a> {
sender_balance
);
// If receiver doesn't exist, create it
if (!receiver_exists && !value.is_zero()) || must_create {
if !receiver_exists && (!value.is_zero() || must_create) {
self.push_op_reversible(
step,
AccountOp {
Expand Down
17 changes: 15 additions & 2 deletions bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Error,
};
use eth_types::{
evm_types::{GasCost, MAX_REFUND_QUOTIENT_OF_GAS_USED},
evm_types::{GasCost, MAX_REFUND_QUOTIENT_OF_GAS_USED, PRECOMPILE_COUNT},
evm_unimplemented, ToWord, Word,
};
use ethers_core::utils::get_contract_address;
Expand Down Expand Up @@ -60,6 +60,19 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
false,
)?;

// Add precompile contract address to access list
for address in 1..=PRECOMPILE_COUNT {
let address = eth_types::Address::from_low_u64_be(address);
let is_warm_prev = !state.sdb.add_account_to_access_list(address);
state.tx_accesslist_account_write(
&mut exec_step,
state.tx_ctx.id(),
address,
true,
is_warm_prev,
)?;
}

// Add caller, callee and coinbase (for EIP-3651) to access list.
for address in [call.caller_address, call.address, state.block.coinbase] {
let is_warm_prev = !state.sdb.add_account_to_access_list(address);
Expand Down Expand Up @@ -98,7 +111,7 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
} else {
(Word::zero(), true)
};
if !state.is_precompiled(&call.address) && !call.is_create() {
if !state.is_precompiled(&call.address) {
state.account_read(
&mut exec_step,
call.address,
Expand Down
3 changes: 3 additions & 0 deletions eth-types/src/evm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ impl GasCost {
/// it from 10 to 50.
pub const EXP_BYTE_TIMES: u64 = 50;
}

/// This constant is used to iterate through precompile contract addresses 0x01 to 0x09
pub const PRECOMPILE_COUNT: u64 = 9;
21 changes: 19 additions & 2 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use external_tracer::TraceConfig;
use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
use std::{collections::HashMap, str::FromStr};
use thiserror::Error;
use zkevm_circuits::{super_circuit::SuperCircuit, test_util::CircuitTestBuilder, witness::Block};
use zkevm_circuits::{
super_circuit::SuperCircuit,
test_util::{CircuitTestBuilder, CircuitTestError},
witness::Block,
};

#[derive(PartialEq, Eq, Error, Debug)]
pub enum StateTestError {
Expand Down Expand Up @@ -329,7 +333,20 @@ pub fn run_test(
let block: Block<Fr> =
zkevm_circuits::evm_circuit::witness::block_convert(&builder).unwrap();

CircuitTestBuilder::<1, 1>::new_from_block(block).run();
CircuitTestBuilder::<1, 1>::new_from_block(block)
.run_with_result()
.map_err(|err| match err {
CircuitTestError::VerificationFailed { reasons, .. } => {
StateTestError::CircuitUnsatisfied {
num_failure: reasons.len(),
first_failure: reasons[0].to_string(),
}
}
err => StateTestError::Exception {
expected: false,
found: err.to_string(),
},
})?;
} else {
geth_data.sign(&wallets);

Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cli-table = { version = "0.4", optional = true }
num_enum = "0.5.7"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.78"
thiserror = "1.0"

[dev-dependencies]
bus-mapping = { path = "../bus-mapping", features = ["test"] }
Expand Down
29 changes: 10 additions & 19 deletions zkevm-circuits/src/bytecode_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use halo2_proofs::{
poly::Rotation,
};
use itertools::Itertools;
use log::trace;
use std::{iter, ops::Deref, vec};

const PUSH_TABLE_WIDTH: usize = 2;
Expand Down Expand Up @@ -162,7 +161,6 @@ impl<F: Field> Deref for BytecodeCircuitAssignment<F> {
#[derive(Clone, Debug)]
/// Bytecode circuit configuration
pub struct BytecodeCircuitConfig<F> {
minimum_rows: usize,
q_enable: Column<Fixed>,
q_first: Column<Fixed>,
q_last: Column<Fixed>,
Expand Down Expand Up @@ -555,7 +553,6 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
let index_length_diff_is_zero = IsZeroChip::construct(index_length_diff_is_zero);

BytecodeCircuitConfig {
minimum_rows: meta.minimum_rows(),
q_enable,
q_first,
q_last,
Expand All @@ -576,21 +573,12 @@ impl<F: Field> BytecodeCircuitConfig<F> {
pub(crate) fn assign_internal(
&self,
layouter: &mut impl Layouter<F>,
size: usize,
max_rows: usize,
witness: &BytecodeCircuitAssignment<F>,
challenges: &Challenges<Value<F>>,
) -> Result<(), Error> {
// Subtract the unusable rows from the size
assert!(size > self.minimum_rows);
let last_row_offset = max_rows - 1;

let last_row_offset = size - self.minimum_rows + 1;

trace!(
"size: {}, minimum_rows: {}, last_row_offset:{}",
size,
self.minimum_rows,
last_row_offset
);
if witness.len() > last_row_offset {
// The last_row_offset-th row must be reserved for padding.
// so we have "last_row_offset rows" usable
Expand Down Expand Up @@ -778,18 +766,21 @@ pub struct BytecodeCircuit<F: Field> {
pub(crate) bytecodes: CodeDB,
/// Unrolled bytecodes
pub(crate) rows: BytecodeCircuitAssignment<F>,
/// Circuit size
pub size: usize,
/// The number of Halo2 rows that can be utilized
///
/// In tests, we set the number as small as possible to reduce MockProver's overhead.
/// In production, this number must be the power of degree minus unusable rows.
pub max_rows: usize,
}

impl<F: Field> BytecodeCircuit<F> {
/// new BytecodeCircuitTester
pub fn new(bytecodes: CodeDB, size: usize) -> Self {
pub fn new(bytecodes: CodeDB, max_rows: usize) -> Self {
let rows: BytecodeCircuitAssignment<F> = bytecodes.clone().into();
Self {
bytecodes,
rows,
size,
max_rows,
}
}
}
Expand Down Expand Up @@ -823,6 +814,6 @@ impl<F: Field> SubCircuit<F> for BytecodeCircuit<F> {
layouter: &mut impl Layouter<F>,
) -> Result<(), Error> {
config.load_aux_tables(layouter)?;
config.assign_internal(layouter, self.size, &self.rows, challenges)
config.assign_internal(layouter, self.max_rows, &self.rows, challenges)
}
}
4 changes: 2 additions & 2 deletions zkevm-circuits/src/bytecode_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ impl<F: Field> BytecodeCircuit<F> {
}

fn from_bytes(bytecodes: impl Into<CodeDB>, k: u32) -> Self {
Self::new(bytecodes.into(), 2usize.pow(k))
Self::new(bytecodes.into(), 2usize.pow(k) - Self::unusable_rows())
}

fn verify(&self, success: bool) {
let prover = MockProver::<F>::run(log2_ceil(self.size), self, Vec::new()).unwrap();
let prover = MockProver::<F>::run(log2_ceil(self.max_rows), self, Vec::new()).unwrap();
let result = prover.verify_par();
if success {
if let Err(failures) = &result {
Expand Down
13 changes: 5 additions & 8 deletions zkevm-circuits/src/evm_circuit/execution/addmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,12 @@ mod test {
.unwrap();
last.stack = Stack::from_vec(vec![r]);
}
let mut ctb = CircuitTestBuilder::new_from_test_ctx(ctx);
if !ok {
ctb = ctb.evm_checks(Box::new(|prover, gate_rows, lookup_rows| {
assert!(prover
.verify_at_rows_par(gate_rows.iter().cloned(), lookup_rows.iter().cloned())
.is_err())
}));
let result = CircuitTestBuilder::new_from_test_ctx(ctx).run_with_result();
if ok {
assert!(result.is_ok());
} else {
result.unwrap_err().assert_evm_failure()
};
ctb.run()
}

fn test_ok_u32(a: u32, b: u32, c: u32, r: Option<u32>) {
Expand Down
Loading

0 comments on commit 5e3dbf9

Please sign in to comment.