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

Commit

Permalink
refactor: use StepRws in evm circuits to replace rw indices (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Mar 1, 2024
1 parent 3bbc757 commit 86adab3
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 86 deletions.
25 changes: 11 additions & 14 deletions zkevm-circuits/src/evm_circuit/execution/end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
MulWordByU64Gadget,
},
tx::EndTxHelperGadget,
CachedRegion, Cell,
CachedRegion, Cell, StepRws,
},
witness::{Block, Call, ExecStep, Transaction},
},
Expand Down Expand Up @@ -152,9 +152,12 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
step: &ExecStep,
) -> Result<(), Error> {
let gas_used = tx.gas() - step.gas_left;
let (refund, _) = block.get_rws(step, 2).tx_refund_value_pair();
let (caller_balance, caller_balance_prev) = block.get_rws(step, 3).account_balance_pair();
let (coinbase_code_hash_prev, _) = block.get_rws(step, 4).account_codehash_pair();
let mut rws = StepRws::new(block, step);
rws.offset_add(2);

let (refund, _) = rws.next().tx_refund_value_pair();
let (caller_balance, caller_balance_prev) = rws.next().account_balance_pair();
let (coinbase_code_hash_prev, _) = rws.next().account_codehash_pair();

self.tx_id
.assign(region, offset, Value::known(F::from(tx.id)))?;
Expand Down Expand Up @@ -209,16 +212,10 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
self.coinbase_code_hash_is_zero
.assign_u256(region, offset, coinbase_code_hash_prev)?;
if !coinbase_reward.is_zero() {
let coinbase_balance_pair = block
.get_rws(
step,
if coinbase_code_hash_prev.is_zero() {
6
} else {
5
},
)
.account_balance_pair();
if coinbase_code_hash_prev.is_zero() {
rws.next();
}
let coinbase_balance_pair = rws.next().account_balance_pair();
self.coinbase_reward.assign(
region,
offset,
Expand Down
34 changes: 20 additions & 14 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 @@ -126,26 +126,32 @@ 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 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();

let callee_code_hash = rws.next().account_codehash_pair().0;

let callee_exists = !callee_code_hash.is_zero();

let (is_warm, is_warm_prev) = block
.get_rws(step, 10 + is_call_or_callcode)
.tx_access_list_value_pair();
let (is_warm, is_warm_prev) = rws.next().tx_access_list_value_pair();

let memory_expansion_gas_cost = self.call.assign(
region,
Expand Down
18 changes: 12 additions & 6 deletions zkevm-circuits/src/evm_circuit/execution/error_oog_memory_copy.rs
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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -156,6 +156,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,16 +167,20 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {

let (is_warm, external_address) = if is_extcodecopy {
(
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());
if is_extcodecopy {
rws.next(); // Skip external address
}

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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -161,12 +161,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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -130,8 +130,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 @@ -140,7 +143,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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -109,9 +109,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
12 changes: 8 additions & 4 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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -182,8 +182,10 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

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

let [external_address, memory_offset, code_offset, memory_length] =
[0, 1, 2, 3].map(|idx| block.get_rws(step, idx).stack_value());
[0, 1, 2, 3].map(|_| rws.next().stack_value());
self.external_address_word
.assign_u256(region, offset, external_address)?;
let memory_address =
Expand All @@ -199,11 +201,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
10 changes: 6 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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -106,7 +106,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 @@ -118,11 +120,11 @@ impl<F: Field> ExecutionGadget<F> for ExtcodehashGadget<F> {
call.is_persistent,
)?;

let (_, is_warm) = block.get_rws(step, 4).tx_access_list_value_pair();
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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -123,7 +123,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 @@ -136,16 +138,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
11 changes: 6 additions & 5 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, ExecStep, Transaction},
},
Expand Down Expand Up @@ -201,7 +201,9 @@ 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, msize] = [0, 1].map(|_| rws.next().stack_value());
let memory_address = self
.memory_address
.assign(region, offset, memory_start, msize)?;
Expand All @@ -221,9 +223,8 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
// It takes 6 + is_persistent reads or writes to reach the topic stack write section.
// Each topic takes at least 1 stack read. They take an additional tx log write if the
// call is persistent.
block
.get_rws(step, 6 + is_persistent + topic * (1 + is_persistent))
.stack_value()
rws.offset_add(6 + is_persistent + topic * (1 + is_persistent));
rws.next().stack_value()
});
for i in 0..4 {
let topic = topics.next();
Expand Down
Loading

0 comments on commit 86adab3

Please sign in to comment.