Skip to content

Commit

Permalink
fix ConstrainBuilderCommon
Browse files Browse the repository at this point in the history
  • Loading branch information
lispc committed Apr 26, 2023
1 parent 273df35 commit b55fa7d
Show file tree
Hide file tree
Showing 23 changed files with 61 additions and 42 deletions.
2 changes: 1 addition & 1 deletion circuit-benchmarks/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod tests {
// Bench setup generation
let setup_message = format!("{} {} with degree = {}", BENCHMARK_ID, setup_prfx, degree);
let start1 = start_timer!(|| setup_message);
let general_params = ParamsKZG::<Bn256>::setup(degree, &mut rng);
let general_params = ParamsKZG::<Bn256>::setup(degree as u32, &mut rng);
let verifier_params: ParamsVerifierKZG<Bn256> = general_params.verifier_params().clone();
end_timer!(start1);

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2023-04-24
nightly-2022-12-10
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{
evm_circuit::util::{and, constraint_builder::BaseConstraintBuilder, not, or, select},
evm_circuit::util::{
and,
constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon},
not, or, select,
},
table::{BytecodeFieldTag, KeccakTable, PoseidonTable},
util::{Challenges, Expr, SubCircuitConfig},
};
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
util::{
common_gadget::TransferGadget,
constraint_builder::{
ConstraintBuilder, ReversionInfo, StepStateTransition,
ConstrainBuilderCommon, EVMConstraintBuilder, ReversionInfo, StepStateTransition,
Transition::{Delta, To},
},
math_gadget::{
Expand Down Expand Up @@ -70,7 +70,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<

const EXECUTION_STATE: ExecutionState = S;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
// Use rw_counter of the step which triggers next call as its call_id.
let callee_call_id = cb.curr.state.rw_counter.clone();
let code_hash_previous = cb.query_cell();
Expand Down
6 changes: 4 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/end_inner_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::{
execution::ExecutionGadget,
step::ExecutionState,
util::{
constraint_builder::ConstraintBuilder, math_gadget::IsZeroGadget, CachedRegion, Cell,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::IsZeroGadget,
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -36,7 +38,7 @@ impl<F: Field> ExecutionGadget<F> for EndInnerBlockGadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::EndInnerBlock;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
// The number of txs in the inner block is also the ID of the last tx in the
// block.
let last_tx_id = cb.query_cell();
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/error_code_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget,
constraint_builder::ConstraintBuilder,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::LtGadget,
memory_gadget::{CommonMemoryAddressGadget, MemoryAddressGadget},
CachedRegion, Cell,
Expand Down Expand Up @@ -38,7 +38,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorCodeStoreGadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorCodeStore;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let offset = cb.query_cell_phase2();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget,
constraint_builder::ConstraintBuilder,
constraint_builder::EVMConstraintBuilder,
math_gadget::IsEqualGadget,
memory_gadget::{CommonMemoryAddressGadget, MemoryAddressGadget},
CachedRegion, Cell,
Expand Down Expand Up @@ -32,7 +32,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorInvalidCreationCodeGadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorInvalidCreationCode;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
let first_byte = cb.query_cell();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use crate::{
param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_GAS},
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget, constraint_builder::ConstraintBuilder, from_bytes,
math_gadget::LtGadget, select, CachedRegion, Cell, Word,
common_gadget::CommonErrorGadget,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
from_bytes,
math_gadget::LtGadget,
select, CachedRegion, Cell, Word,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -35,7 +38,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGAccountAccessGadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorOutOfGasAccountAccess;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
cb.require_in_set(
"ErrorOutOfGasAccountAccess happens for BALANCE | EXTCODESIZE | EXTCODEHASH ",
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/error_oog_create2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget,
constraint_builder::ConstraintBuilder,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::LtGadget,
memory_gadget::{
CommonMemoryAddressGadget, MemoryExpandedAddressGadget, MemoryExpansionGadget,
Expand Down Expand Up @@ -37,7 +37,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGCreate2Gadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorOutOfGasCREATE2;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

cb.require_equal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
util::{
common_gadget::RestoreContextGadget,
constraint_builder::{
ConstraintBuilder, StepStateTransition,
ConstrainBuilderCommon, EVMConstraintBuilder, StepStateTransition,
Transition::{Delta, Same},
},
math_gadget::{IsEqualGadget, LtGadget},
Expand Down Expand Up @@ -41,7 +41,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGDynamicMemoryGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorOutOfGasDynamicMemoryExpansion;

// Support other OOG due to pure memory including CREATE, RETURN and REVERT
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
cb.opcode_lookup(opcode.expr(), 1.expr());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget,
constraint_builder::ConstraintBuilder,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
from_bytes,
math_gadget::{IsZeroGadget, LtGadget},
memory_gadget::{
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorOutOfGasMemoryCopy;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
cb.require_in_set(
"ErrorOutOfGasMemoryCopy opcode must be CALLDATACOPY, CODECOPY, EXTCODECOPY or RETURNDATACOPY",
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/error_oog_sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget,
constraint_builder::ConstraintBuilder,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::LtGadget,
memory_gadget::{
CommonMemoryAddressGadget, MemoryCopierGasGadget, MemoryExpandedAddressGadget,
Expand Down Expand Up @@ -40,7 +40,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGSha3Gadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorOutOfGasSHA3;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
cb.require_equal(
"ErrorOutOfGasSHA3 opcode must be SHA3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
util::{
common_gadget::RestoreContextGadget,
constraint_builder::{
ConstraintBuilder, EVMConstraintBuilder, StepStateTransition,
ConstrainBuilderCommon, EVMConstraintBuilder, StepStateTransition,
Transition::{Delta, Same},
},
math_gadget::{IsEqualGadget, LtGadget},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
execution::ExecutionGadget,
step::ExecutionState,
util::{
constraint_builder::ConstraintBuilder,
constraint_builder::EVMConstraintBuilder,
math_gadget::IsZeroGadget,
memory_gadget::{CommonMemoryAddressGadget, MemoryAddressGadget},
CachedRegion, Cell, Word,
Expand Down Expand Up @@ -35,7 +35,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorPrecompileFailedGadget<F> {

const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorPrecompileFailed;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
cb.opcode_lookup(opcode.expr(), 1.expr());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
common_gadget::CommonErrorGadget,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::IsZeroGadget,
sum, CachedRegion, Cell, Word as RLCWord,
CachedRegion, Cell, Word as RLCWord,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/jumpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
util::{
common_gadget::{SameContextGadget, WordByteRangeGadget},
constraint_builder::{
EVMConstraintBuilder, StepStateTransition,
ConstrainBuilderCommon, EVMConstraintBuilder, StepStateTransition,
Transition::{Delta, To},
},
math_gadget::IsZeroGadget,
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ impl<F: Field> CommonErrorGadget<F> {
}

pub(crate) fn construct_with_lastcallee_return_data(
cb: &mut ConstraintBuilder<F>,
cb: &mut EVMConstraintBuilder<F>,
opcode: Expression<F>,
rw_counter_delta: Expression<F>,
return_data_offset: Expression<F>,
Expand Down Expand Up @@ -1114,7 +1114,7 @@ pub(crate) struct WordByteCapGadget<F, const VALID_BYTES: usize> {
}

impl<F: Field, const VALID_BYTES: usize> WordByteCapGadget<F, VALID_BYTES> {
pub(crate) fn construct(cb: &mut ConstraintBuilder<F>, cap: Expression<F>) -> Self {
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>, cap: Expression<F>) -> Self {
let word = WordByteRangeGadget::construct(cb);
let value = select::expr(word.within_range(), word.valid_value(), cap.expr());
let lt_cap = LtGadget::construct(cb, value, cap);
Expand Down Expand Up @@ -1175,7 +1175,7 @@ pub(crate) struct WordByteRangeGadget<F, const VALID_BYTES: usize> {
}

impl<F: Field, const VALID_BYTES: usize> WordByteRangeGadget<F, VALID_BYTES> {
pub(crate) fn construct(cb: &mut ConstraintBuilder<F>) -> Self {
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>) -> Self {
debug_assert!(VALID_BYTES < 32);

let original = cb.query_word_rlc();
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub(crate) struct ReversionInfo<F> {

impl<F: Field> ReversionInfo<F> {
pub(crate) fn from_caller(
cb: &mut ConstraintBuilder<F>,
cb: &mut EVMConstraintBuilder<F>,
caller: &mut ReversionInfo<F>,
callee_is_success: Expression<F>,
) -> Self {
Expand Down
11 changes: 7 additions & 4 deletions zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
}

/// Init Code's keccak hash word RLC.
pub(crate) fn keccak_code_hash_word_rlc(&self, cb: &ConstraintBuilder<F>) -> Expression<F> {
pub(crate) fn keccak_code_hash_word_rlc(&self, cb: &EVMConstraintBuilder<F>) -> Expression<F> {
cb.word_rlc::<N_BYTES_WORD>(
self.keccak_code_hash
.iter()
Expand All @@ -313,7 +313,10 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
}

/// Init Code's keccak hash keccak RLC.
pub(crate) fn keccak_code_hash_keccak_rlc(&self, cb: &ConstraintBuilder<F>) -> Expression<F> {
pub(crate) fn keccak_code_hash_keccak_rlc(
&self,
cb: &EVMConstraintBuilder<F>,
) -> Expression<F> {
cb.keccak_rlc::<N_BYTES_WORD>(
self.keccak_code_hash
.iter()
Expand All @@ -325,7 +328,7 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
}

/// Salt EVM word RLC.
pub(crate) fn salt_word_rlc(&self, cb: &ConstraintBuilder<F>) -> Expression<F> {
pub(crate) fn salt_word_rlc(&self, cb: &EVMConstraintBuilder<F>) -> Expression<F> {
cb.word_rlc::<N_BYTES_WORD>(
self.salt
.iter()
Expand All @@ -337,7 +340,7 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
}

/// Salt keccak RLC.
pub(crate) fn salt_keccak_rlc(&self, cb: &ConstraintBuilder<F>) -> Expression<F> {
pub(crate) fn salt_keccak_rlc(&self, cb: &EVMConstraintBuilder<F>) -> Expression<F> {
cb.keccak_rlc::<N_BYTES_WORD>(
self.salt
.iter()
Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/evm_circuit/util/memory_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) mod address_high {

/// Memory address trait to adapt for right and Uint overflow cases.
pub(crate) trait CommonMemoryAddressGadget<F: FieldExt> {
fn construct_self(cb: &mut ConstraintBuilder<F>) -> Self;
fn construct_self(cb: &mut EVMConstraintBuilder<F>) -> Self;

/// Return the memory address (offset + length).
fn assign(
Expand Down Expand Up @@ -104,7 +104,7 @@ pub(crate) struct MemoryAddressGadget<F> {
}

impl<F: Field> CommonMemoryAddressGadget<F> for MemoryAddressGadget<F> {
fn construct_self(cb: &mut ConstraintBuilder<F>) -> Self {
fn construct_self(cb: &mut EVMConstraintBuilder<F>) -> Self {
let offset = cb.query_cell_phase2();
let length = cb.query_word_rlc();
Self::construct(cb, offset, length)
Expand Down Expand Up @@ -173,7 +173,7 @@ impl<F: Field> CommonMemoryAddressGadget<F> for MemoryAddressGadget<F> {

impl<F: Field> MemoryAddressGadget<F> {
pub(crate) fn construct(
cb: &mut ConstraintBuilder<F>,
cb: &mut EVMConstraintBuilder<F>,
memory_offset: Cell<F>,
memory_length: MemoryAddress<F>,
) -> Self {
Expand Down Expand Up @@ -226,7 +226,7 @@ pub(crate) struct MemoryExpandedAddressGadget<F> {
}

impl<F: Field> CommonMemoryAddressGadget<F> for MemoryExpandedAddressGadget<F> {
fn construct_self(cb: &mut ConstraintBuilder<F>) -> Self {
fn construct_self(cb: &mut EVMConstraintBuilder<F>) -> Self {
let offset = cb.query_word_rlc();
let length = cb.query_word_rlc();
let sum = cb.query_word_rlc();
Expand Down
5 changes: 4 additions & 1 deletion zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use std::{iter, marker::PhantomData};

use crate::table::{BlockContextFieldTag, KeccakTable};
use crate::{
evm_circuit::util::constraint_builder::ConstrainBuilderCommon,
table::{BlockContextFieldTag, KeccakTable},
};
use bus_mapping::circuit_input_builder::get_dummy_tx_hash;
use eth_types::{Address, Field, Hash, ToBigEndian, Word, H256};
use ethers_core::utils::keccak256;
Expand Down
6 changes: 5 additions & 1 deletion zkevm-circuits/src/rlp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ use halo2_proofs::{
// use crate::evm_circuit::table::FixedTableTag;
use crate::{
evm_circuit::{
util::{and, constraint_builder::BaseConstraintBuilder, not, or},
util::{
and,
constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon},
not, or,
},
witness::{RlpTxTag, RlpWitnessGen},
},
table::RlpTable,
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod test;
pub use dev::TxCircuit as TestTxCircuit;

use crate::{
evm_circuit::util::constraint_builder::BaseConstraintBuilder,
evm_circuit::util::constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon},
table::{BlockTable, KeccakTable, LookupTable, RlpTable, TxFieldTag, TxTable},
util::{keccak, random_linear_combine_word as rlc, SubCircuit, SubCircuitConfig},
witness,
Expand Down

0 comments on commit b55fa7d

Please sign in to comment.