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

Refactor: use StepRws in evm circuits to replace rw indices #1786

Merged
34 changes: 19 additions & 15 deletions zkevm-circuits/src/evm_circuit/execution/error_oog_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::{IsZeroGadget, LtGadget},
memory_gadget::MemoryExpandedAddressGadget,
or, CachedRegion, Cell,
or, CachedRegion, Cell, StepRws,
},
},
table::CallContextFieldTag,
Expand Down Expand Up @@ -127,26 +127,30 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGCallGadget<F> {
let opcode = step.opcode().unwrap();
let is_call_or_callcode =
usize::from([OpcodeId::CALL, OpcodeId::CALLCODE].contains(&opcode));
let [tx_id, is_static] =
[0, 1].map(|index| block.get_rws(step, index).call_context_value());
let [gas, callee_address] = [2, 3].map(|index| block.get_rws(step, index).stack_value());

let mut rws = StepRws::new(block, step);

let tx_id = rws.next().call_context_value();
let is_static = rws.next().call_context_value();
let gas = rws.next().stack_value();
let callee_address = rws.next().stack_value();

let value = if is_call_or_callcode == 1 {
block.get_rws(step, 4).stack_value()
rws.next().stack_value()
} else {
U256::zero()
};
let [cd_offset, cd_length, rd_offset, rd_length] =
[4, 5, 6, 7].map(|i| block.get_rws(step, is_call_or_callcode + i).stack_value());

let callee_code_hash = block
.get_rws(step, 9 + is_call_or_callcode)
.account_codehash_pair()
.0;
let callee_exists = !callee_code_hash.is_zero();
let cd_offset = rws.next().stack_value();
let cd_length = rws.next().stack_value();
let rd_offset = rws.next().stack_value();
let rd_length = rws.next().stack_value();

rws.offset_add(1);

let (is_warm, is_warm_prev) = block
.get_rws(step, 10 + is_call_or_callcode)
.tx_access_list_value_pair();
let callee_code_hash = rws.next().account_codehash_pair().0;
let callee_exists = !callee_code_hash.is_zero();
let (is_warm, is_warm_prev) = rws.next().tx_access_list_value_pair();

let memory_expansion_gas_cost = self.call.assign(
region,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
CommonMemoryAddressGadget, MemoryCopierGasGadget, MemoryExpandedAddressGadget,
MemoryExpansionGadget,
},
or, select, AccountAddress, CachedRegion, Cell,
or, select, AccountAddress, CachedRegion, Cell, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -157,6 +157,8 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
let opcode = step.opcode().unwrap();
let is_extcodecopy = opcode == OpcodeId::EXTCODECOPY;

let mut rws = StepRws::new(block, step);

log::debug!(
"ErrorOutOfGasMemoryCopy: opcode = {}, gas_left = {}, gas_cost = {}",
opcode,
Expand All @@ -165,17 +167,18 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
);

let (is_warm, external_address) = if is_extcodecopy {
rws.next();
(
block.get_rws(step, 1).tx_access_list_value_pair().0,
block.get_rws(step, 2).stack_value(),
rws.next().tx_access_list_value_pair().0,
rws.next().stack_value(),
)
} else {
(false, U256::zero())
};

let rw_offset = if is_extcodecopy { 3 } else { 0 };
let [dst_offset, src_offset, copy_size] = [rw_offset, rw_offset + 1, rw_offset + 2]
.map(|index| block.get_rws(step, index).stack_value());
let dst_offset = rws.next().stack_value();
let src_offset = rws.next().stack_value();
let copy_size = rws.next().stack_value();

self.opcode
.assign(region, offset, Value::known(F::from(opcode.as_u64())))?;
Expand Down
15 changes: 10 additions & 5 deletions zkevm-circuits/src/evm_circuit/execution/error_oog_sload_sstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::{LtGadget, PairSelectGadget},
or, select, CachedRegion, Cell,
or, select, CachedRegion, Cell, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -162,12 +162,17 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGSloadSstoreGadget<F> {
) -> Result<(), Error> {
let opcode = step.opcode().unwrap();
let is_sstore = opcode == OpcodeId::SSTORE;
let key = block.get_rws(step, 3).stack_value();
let (is_warm, _) = block.get_rws(step, 4).tx_access_list_value_pair();

let mut rws = StepRws::new(block, step);

rws.offset_add(3);

let key = rws.next().stack_value();
let (is_warm, _) = rws.next().tx_access_list_value_pair();

let (value, value_prev, original_value, gas_cost) = if is_sstore {
let value = block.get_rws(step, 5).stack_value();
let (_, value_prev, _, original_value) = block.get_rws(step, 6).storage_value_aux();
let value = rws.next().stack_value();
let (_, value_prev, _, original_value) = rws.next().storage_value_aux();
let gas_cost =
cal_sstore_gas_cost_for_assignment(value, value_prev, original_value, is_warm);
(value, value_prev, original_value, gas_cost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
from_bytes,
math_gadget::{AddWordsGadget, IsZeroGadget, LtGadget},
not, or, sum, CachedRegion, Cell, U64Cell,
not, or, sum, CachedRegion, Cell, StepRws, U64Cell,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -131,8 +131,11 @@ impl<F: Field> ExecutionGadget<F> for ErrorReturnDataOutOfBoundGadget<F> {
self.opcode
.assign(region, offset, Value::known(F::from(opcode.as_u64())))?;

let [dest_offset, data_offset, size] =
[0, 1, 2].map(|index| block.get_rws(step, index).stack_value());
let mut rws = StepRws::new(block, step);

let dest_offset = rws.next().stack_value();
let data_offset = rws.next().stack_value();
let size = rws.next().stack_value();

self.memory_offset
.assign(region, offset, Some(dest_offset.as_u64().to_le_bytes()))?;
Expand All @@ -141,7 +144,8 @@ impl<F: Field> ExecutionGadget<F> for ErrorReturnDataOutOfBoundGadget<F> {
self.sum
.assign(region, offset, [data_offset, size], remainder_end)?;

let return_data_length = block.get_rws(step, 3).call_context_value();
let return_data_length = rws.next().call_context_value();

self.return_data_length.assign(
region,
offset,
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::{IsEqualGadget, IsZeroWordGadget},
AccountAddress, CachedRegion, Cell,
AccountAddress, CachedRegion, Cell, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -110,9 +110,10 @@ impl<F: Field> ExecutionGadget<F> for ErrorWriteProtectionGadget<F> {
.assign(region, offset, Value::known(F::from(opcode.as_u64())))?;
let [mut gas, mut code_address, mut value] = [U256::zero(), U256::zero(), U256::zero()];

let mut rws = StepRws::new(block, step);

if is_call {
[gas, code_address, value] =
[0, 1, 2].map(|index| block.get_rws(step, index).stack_value());
[gas, code_address, value] = [0, 1, 2].map(|_| rws.next().stack_value());
}

self.gas.assign_u256(region, offset, gas)?;
Expand Down
17 changes: 12 additions & 5 deletions zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryCopierGasGadget,
MemoryExpansionGadget,
},
not, select, AccountAddress, CachedRegion, Cell,
not, select, AccountAddress, CachedRegion, Cell, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -183,8 +183,13 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

let [external_address, memory_offset, code_offset, memory_length] =
[0, 1, 2, 3].map(|idx| block.get_rws(step, idx).stack_value());
let mut rws = StepRws::new(block, step);

let external_address = rws.next().stack_value();
let memory_offset = rws.next().stack_value();
let code_offset = rws.next().stack_value();
let memory_length = rws.next().stack_value();

self.external_address_word
.assign_u256(region, offset, external_address)?;
let memory_address =
Expand All @@ -200,11 +205,13 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
call.is_persistent,
)?;

let (_, is_warm) = block.get_rws(step, 7).tx_access_list_value_pair();
rws.offset_add(3);

let (_, is_warm) = rws.next().tx_access_list_value_pair();
self.is_warm
.assign(region, offset, Value::known(F::from(is_warm as u64)))?;

let code_hash = block.get_rws(step, 8).account_codehash_pair().0;
let code_hash = rws.next().account_codehash_pair().0;
self.code_hash.assign_u256(region, offset, code_hash)?;
self.not_exists.assign_u256(region, offset, code_hash)?;

Expand Down
12 changes: 8 additions & 4 deletions zkevm-circuits/src/evm_circuit/execution/extcodehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
constraint_builder::{
EVMConstraintBuilder, ReversionInfo, StepStateTransition, Transition::Delta,
},
select, AccountAddress, CachedRegion, Cell,
select, AccountAddress, CachedRegion, Cell, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -107,7 +107,9 @@ impl<F: Field> ExecutionGadget<F> for ExtcodehashGadget<F> {
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

let address = block.get_rws(step, 0).stack_value();
let mut rws = StepRws::new(block, step);

let address = rws.next().stack_value();
self.address_word.assign_u256(region, offset, address)?;

self.tx_id
Expand All @@ -119,11 +121,13 @@ impl<F: Field> ExecutionGadget<F> for ExtcodehashGadget<F> {
call.is_persistent,
)?;

let (_, is_warm) = block.get_rws(step, 4).tx_access_list_value_pair();
rws.offset_add(3);

let (_, is_warm) = rws.next().tx_access_list_value_pair();
self.is_warm
.assign(region, offset, Value::known(F::from(is_warm as u64)))?;

let code_hash = block.get_rws(step, 5).account_codehash_pair().0;
let code_hash = rws.next().account_codehash_pair().0;
self.code_hash.assign_u256(region, offset, code_hash)?;

Ok(())
Expand Down
14 changes: 9 additions & 5 deletions zkevm-circuits/src/evm_circuit/execution/extcodesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Transition::Delta,
},
math_gadget::IsZeroWordGadget,
not, select, AccountAddress, CachedRegion, Cell, U64Cell,
not, select, AccountAddress, CachedRegion, Cell, StepRws, U64Cell,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -124,7 +124,9 @@ impl<F: Field> ExecutionGadget<F> for ExtcodesizeGadget<F> {
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

let address = block.get_rws(step, 0).stack_value();
let mut rws = StepRws::new(block, step);

let address = rws.next().stack_value();
self.address_word.assign_u256(region, offset, address)?;

self.tx_id
Expand All @@ -137,16 +139,18 @@ impl<F: Field> ExecutionGadget<F> for ExtcodesizeGadget<F> {
call.is_persistent,
)?;

let (_, is_warm) = block.get_rws(step, 4).tx_access_list_value_pair();
rws.offset_add(3);

let (_, is_warm) = rws.next().tx_access_list_value_pair();
self.is_warm
.assign(region, offset, Value::known(F::from(is_warm as u64)))?;

let code_hash = block.get_rws(step, 5).account_codehash_pair().0;
let code_hash = rws.next().account_codehash_pair().0;
self.code_hash.assign_u256(region, offset, code_hash)?;
self.not_exists
.assign(region, offset, WordLoHi::from(code_hash))?;

let code_size = block.get_rws(step, 6).stack_value().as_u64();
let code_size = rws.next().stack_value().as_u64();
self.code_size
.assign(region, offset, Some(code_size.to_le_bytes()))?;

Expand Down
8 changes: 6 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
memory_gadget::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryExpansionGadget,
},
not, sum, CachedRegion, Cell,
not, sum, CachedRegion, Cell, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -202,7 +202,11 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

let [memory_start, msize] = [0, 1].map(|index| block.get_rws(step, index).stack_value());
let mut rws = StepRws::new(block, step);

let memory_start = rws.next().stack_value();
let msize = rws.next().stack_value();

let memory_address = self
.memory_address
.assign(region, offset, memory_start, msize)?;
Expand Down
12 changes: 9 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
memory_gadget::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryExpansionGadget,
},
not, CachedRegion, Cell,
not, CachedRegion, Cell, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -293,7 +293,11 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
Value::known(F::from(step.opcode().unwrap().as_u64())),
)?;

let [memory_offset, length] = [0, 1].map(|index| block.get_rws(step, index).stack_value());
let mut rws = StepRws::new(block, step);

let memory_offset = rws.next().stack_value();
let length = rws.next().stack_value();

let range = self.range.assign(region, offset, memory_offset, length)?;
self.memory_expansion
.assign(region, offset, step.memory_word_size(), [range])?;
Expand Down Expand Up @@ -349,8 +353,10 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {

let is_contract_deployment = call.is_create() && call.is_success && !length.is_zero();

rws.next();

let init_code_first_byte = if is_contract_deployment {
block.get_rws(step, 3).memory_value()
rws.next().memory_value()
} else {
0
}
Expand Down
21 changes: 8 additions & 13 deletions zkevm-circuits/src/evm_circuit/execution/returndatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryCopierGasGadget,
MemoryExpansionGadget,
},
CachedRegion, Cell, MemoryAddress,
CachedRegion, Cell, MemoryAddress, StepRws,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
},
Expand Down Expand Up @@ -177,21 +177,16 @@ impl<F: Field> ExecutionGadget<F> for ReturnDataCopyGadget<F> {
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

let [dest_offset, data_offset, size] =
[0, 1, 2].map(|index| block.get_rws(step, index).stack_value());
let mut rws = StepRws::new(block, step);

let [dest_offset, data_offset, size] = [0, 1, 2].map(|_| rws.next().stack_value());

self.data_offset.assign_u256(region, offset, data_offset)?;

let [last_callee_id, return_data_offset, return_data_size] = [
(3, CallContextFieldTag::LastCalleeId),
(4, CallContextFieldTag::LastCalleeReturnDataOffset),
(5, CallContextFieldTag::LastCalleeReturnDataLength),
]
.map(|(i, tag)| {
let rw = block.get_rws(step, i);
assert_eq!(rw.field_tag(), Some(tag as u64));
rw.call_context_value()
});
let last_callee_id = rws.next().call_context_value();
let return_data_offset = rws.next().call_context_value();
let return_data_size = rws.next().call_context_value();

self.last_callee_id.assign(
region,
offset,
Expand Down
Loading
Loading