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

fix evm circuit phase2 cells #1087

Merged
merged 5 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
is_warm.expr(),
Some(&mut reversion_info),
);
let code_hash = cb.query_cell();
let code_hash = cb.query_cell_phase2();
// For non-existing accounts the code_hash must be 0 in the rw_table.
cb.account_read(address.expr(), AccountFieldTag::CodeHash, code_hash.expr());
let not_exists = IsZeroGadget::construct(cb, code_hash.expr());
let exists = not::expr(not_exists.expr());
let balance = cb.query_cell();
let balance = cb.query_cell_phase2();
cb.condition(exists.expr(), |cb| {
cb.account_read(address.expr(), AccountFieldTag::Balance, balance.expr());
});
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Transition::{Delta, To},
},
math_gadget::{IsEqualGadget, IsZeroGadget, MulWordByU64Gadget, RangeCheckGadget},
select, CachedRegion, Cell, CellType, Word,
select, CachedRegion, Cell, Word,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
// TODO: Handle precompiled

// Read code_hash of callee
let phase2_code_hash = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_code_hash = cb.query_cell_phase2();
cb.account_read(
tx_callee_address.expr(),
AccountFieldTag::CodeHash,
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<F: Field> ExecutionGadget<F> for CallDataCopyGadget<F> {
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let memory_offset = cb.query_cell();
let memory_offset = cb.query_cell_phase2();
let data_offset = cb.query_word_rlc();
let length = cb.query_word_rlc();

Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/callvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<F: Field> ExecutionGadget<F> for CallValueGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::CALLVALUE;

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

// Lookup rw_table -> call_context with call value
cb.call_context_lookup(
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/chainid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<F: Field> ExecutionGadget<F> for ChainIdGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::CHAINID;

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

// Push the value to the stack
cb.stack_push(chain_id.expr());
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/codecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<F: Field> ExecutionGadget<F> for CodeCopyGadget<F> {
let opcode = cb.query_cell();

// Query elements to be popped from the stack.
let dst_memory_offset = cb.query_cell();
let dst_memory_offset = cb.query_cell_phase2();
let code_offset = cb.query_word_rlc();
let size = cb.query_word_rlc();

Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/dup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
util::{
common_gadget::SameContextGadget,
constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta},
CachedRegion, Cell, CellType,
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand All @@ -28,7 +28,7 @@ impl<F: Field> ExecutionGadget<F> for DupGadget<F> {
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let value = cb.query_cell_with_type(CellType::StoragePhase2);
let value = cb.query_cell_phase2();

// The stack index we have to peek, deduced from the 'x' value of 'dupx'
// The offset starts at 0 for DUP1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
from_bytes,
math_gadget::{IsEqualGadget, IsZeroGadget, LtGadget},
CachedRegion, Cell, CellType, RandomLinearCombination,
CachedRegion, Cell, RandomLinearCombination,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorInvalidJumpGadget<F> {
let value = cb.query_cell();
let is_code = cb.query_cell();
let rw_counter_end_of_reversion = cb.query_cell();
let phase2_condition = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_condition = cb.query_cell_phase2();

cb.require_in_set(
"ErrorInvalidJump only happend in JUMP or JUMPI",
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
let opcode = cb.query_cell();

let external_address = cb.query_word_rlc();
let memory_offset = cb.query_cell();
let memory_offset = cb.query_cell_phase2();
let data_offset = cb.query_word_rlc();
let memory_length = cb.query_word_rlc();

Expand All @@ -69,7 +69,7 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
Some(&mut reversion_info),
);

let code_hash = cb.query_cell();
let code_hash = cb.query_cell_phase2();
cb.account_read(
from_bytes::expr(&external_address.cells),
AccountFieldTag::CodeHash,
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/extcodehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<F: Field> ExecutionGadget<F> for ExtcodehashGadget<F> {
Some(&mut reversion_info),
);

let code_hash = cb.query_cell();
let code_hash = cb.query_cell_phase2();
// For non-existing accounts the code_hash must be 0 in the rw_table.
cb.account_read(address, AccountFieldTag::CodeHash, code_hash.expr());
cb.stack_push(code_hash.expr());
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/extcodesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<F: Field> ExecutionGadget<F> for ExtcodesizeGadget<F> {
Some(&mut reversion_info),
);

let code_hash = cb.query_cell();
let code_hash = cb.query_cell_phase2();
// For non-existing accounts the code_hash must be 0 in the rw_table.
cb.account_read(address.expr(), AccountFieldTag::CodeHash, code_hash.expr());
let not_exists = IsZeroGadget::construct(cb, code_hash.expr());
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/gasprice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
// Query gasprice value
let gas_price = cb.query_cell();
let gas_price = cb.query_cell_phase2();

// Lookup in call_ctx the TxId
let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<F: Field> ExecutionGadget<F> for IsZeroGadget<F> {
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let value = cb.query_cell();
let value = cb.query_cell_phase2();
let is_zero = math_gadget::IsZeroGadget::construct(cb, value.expr());

cb.stack_pop(value.expr());
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/jumpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
from_bytes,
math_gadget::IsZeroGadget,
select, CachedRegion, Cell, CellType, RandomLinearCombination,
select, CachedRegion, Cell, RandomLinearCombination,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand All @@ -35,7 +35,7 @@ impl<F: Field> ExecutionGadget<F> for JumpiGadget<F> {

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let destination = cb.query_word_rlc();
let phase2_condition = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_condition = cb.query_cell_phase2();

// Pop the value from the stack
cb.stack_pop(destination.expr());
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Transition::{Delta, To},
},
memory_gadget::{MemoryAddressGadget, MemoryExpansionGadget},
not, sum, CachedRegion, Cell, CellType,
not, sum, CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::LOG;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let mstart = cb.query_cell();
let mstart = cb.query_cell_phase2();
let msize = cb.query_word_rlc();

// Pop mstart_address, msize from stack
Expand Down Expand Up @@ -74,7 +74,7 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
});

// constrain topics in logs
let phase2_topics = array_init(|_| cb.query_cell_with_type(CellType::StoragePhase2));
let phase2_topics = array_init(|_| cb.query_cell_phase2());
let topic_selectors: [Cell<F>; 4] = array_init(|_| cb.query_cell());
for (idx, topic) in phase2_topics.iter().enumerate() {
cb.condition(topic_selectors[idx].expr(), |cb| {
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/pop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
util::{
common_gadget::SameContextGadget,
constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta},
CachedRegion, Cell, CellType,
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand All @@ -27,7 +27,7 @@ impl<F: Field> ExecutionGadget<F> for PopGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::POP;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let phase2_value = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_value = cb.query_cell_phase2();

// Pop the value from the stack
cb.stack_pop(phase2_value.expr());
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
let opcode = cb.query_cell();
cb.opcode_lookup(opcode.expr(), 1.expr());

let offset = cb.query_cell();
let offset = cb.query_cell_phase2();
let length = cb.query_word_rlc();
cb.stack_pop(offset.expr());
cb.stack_pop(length.expr());
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
cb.condition(is_contract_deployment.clone(), |cb| {
// We don't need to place any additional constraints on code_hash because the
// copy circuit enforces that it is the hash of the bytes in the copy lookup.
let code_hash = cb.query_cell();
let code_hash = cb.query_cell_phase2();
cb.copy_table_lookup(
cb.curr.state.call_id.expr(),
CopyDataType::Memory.expr(),
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/returndatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<F: Field> ExecutionGadget<F> for ReturnDataCopyGadget<F> {
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let dest_offset = cb.query_cell();
let dest_offset = cb.query_cell_phase2();
let data_offset = cb.query_word_rlc();
let size = cb.query_word_rlc();

Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/selfbalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
util::{
common_gadget::SameContextGadget,
constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta},
CachedRegion, Cell, CellType,
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand All @@ -31,7 +31,7 @@ impl<F: Field> ExecutionGadget<F> for SelfbalanceGadget<F> {
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let callee_address = cb.call_context(None, CallContextFieldTag::CalleeAddress);

let phase2_self_balance = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_self_balance = cb.query_cell_phase2();
cb.account_read(
callee_address.expr(),
AccountFieldTag::Balance,
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<F: Field> ExecutionGadget<F> for Sha3Gadget<F> {
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let offset = cb.query_cell();
let offset = cb.query_cell_phase2();
let size = cb.query_word_rlc();
let sha3_rlc = cb.query_word_rlc();

Expand All @@ -47,7 +47,7 @@ impl<F: Field> ExecutionGadget<F> for Sha3Gadget<F> {
let memory_address = MemoryAddressGadget::construct(cb, offset, size);

let copy_rwc_inc = cb.query_cell();
let rlc_acc = cb.query_cell();
let rlc_acc = cb.query_cell_phase2();

cb.condition(memory_address.has_length(), |cb| {
cb.copy_table_lookup(
Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/evm_circuit/execution/sload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
constraint_builder::{
ConstraintBuilder, ReversionInfo, StepStateTransition, Transition::Delta,
},
select, CachedRegion, Cell, CellType,
select, CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -44,12 +44,12 @@ impl<F: Field> ExecutionGadget<F> for SloadGadget<F> {
let mut reversion_info = cb.reversion_info_read(None);
let callee_address = cb.call_context(None, CallContextFieldTag::CalleeAddress);

let phase2_key = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_key = cb.query_cell_phase2();
// Pop the key from the stack
cb.stack_pop(phase2_key.expr());

let phase2_value = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_committed_value = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_value = cb.query_cell_phase2();
let phase2_committed_value = cb.query_cell_phase2();
cb.account_storage_read(
callee_address.expr(),
phase2_key.expr(),
Expand Down
10 changes: 5 additions & 5 deletions zkevm-circuits/src/evm_circuit/execution/sstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
ConstraintBuilder, ReversionInfo, StepStateTransition, Transition::Delta,
},
math_gadget::{IsEqualGadget, IsZeroGadget},
not, select, CachedRegion, Cell, CellType,
not, select, CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -56,16 +56,16 @@ impl<F: Field> ExecutionGadget<F> for SstoreGadget<F> {
let mut reversion_info = cb.reversion_info_read(None);
let callee_address = cb.call_context(None, CallContextFieldTag::CalleeAddress);

let phase2_key = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_key = cb.query_cell_phase2();
// Pop the key from the stack
cb.stack_pop(phase2_key.expr());

let phase2_value = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_value = cb.query_cell_phase2();
// Pop the value from the stack
cb.stack_pop(phase2_value.expr());

let phase2_value_prev = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_original_value = cb.query_cell_with_type(CellType::StoragePhase2);
let phase2_value_prev = cb.query_cell_phase2();
let phase2_original_value = cb.query_cell_phase2();
cb.account_storage_write(
callee_address.expr(),
phase2_key.expr(),
Expand Down
7 changes: 2 additions & 5 deletions zkevm-circuits/src/evm_circuit/execution/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
util::{
common_gadget::SameContextGadget,
constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta},
CachedRegion, Cell, CellType,
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand All @@ -28,10 +28,7 @@ impl<F: Field> ExecutionGadget<F> for SwapGadget<F> {
fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let phase2_values = [
cb.query_cell_with_type(CellType::StoragePhase2),
cb.query_cell_with_type(CellType::StoragePhase2),
];
let phase2_values = [cb.query_cell_phase2(), cb.query_cell_phase2()];

// The stack index we have to peek, deduced from the 'x' value of
// 'swapx' The offset starts at 1 for SWAP1
Expand Down
11 changes: 9 additions & 2 deletions zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ pub(crate) struct Cell<F> {
column: Column<Advice>,
// relative position to selector for synthesis
rotation: usize,
cell_column_index: usize,
}

impl<F: FieldExt> Cell<F> {
pub(crate) fn new(meta: &mut VirtualCells<F>, column: Column<Advice>, rotation: usize) -> Self {
pub(crate) fn new(
meta: &mut VirtualCells<F>,
column: Column<Advice>,
rotation: usize,
cell_column_index: usize,
) -> Self {
Self {
expression: meta.query_advice(column, Rotation(rotation as i32)),
column,
rotation,
cell_column_index,
}
}

Expand Down Expand Up @@ -325,7 +332,7 @@ impl<F: FieldExt> CellManager<F> {
query_expression(meta, |meta| {
for c in 0..width {
for r in 0..height {
cells.push(Cell::new(meta, advices[c], height_offset + r));
cells.push(Cell::new(meta, advices[c], height_offset + r, c));
}
columns.push(CellColumn {
index: c,
Expand Down
Loading