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

Circuit for opcode calldatacopy #297

Merged
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
2 changes: 2 additions & 0 deletions eth-types/src/evm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ impl GasCost {
pub const CREATE: Self = Self(32000);
/// Constant cost for every additional word when expanding memory
pub const MEMORY: Self = Self(3);
/// Constant cost for copying every word
pub const COPY: Self = Self(3);
/// Constant cost for a cold SLOAD
pub const COLD_SLOAD_COST: Self = Self(2100);
/// Constant cost for a cold account access
Expand Down
28 changes: 26 additions & 2 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub mod test {
},
util::Expr,
};
use eth_types::Word;
use eth_types::{evm_types::GasCost, Word};
use halo2::{
arithmetic::{BaseExt, FieldExt},
circuit::{Layouter, SimpleFloorPlanner},
Expand All @@ -132,7 +132,7 @@ pub mod test {
}

pub(crate) fn rand_bytes(n: usize) -> Vec<u8> {
vec![random(); n]
(0..n).map(|_| random()).collect()
}

pub(crate) fn rand_bytes_array<const N: usize>() -> [u8; N] {
Expand Down Expand Up @@ -441,4 +441,28 @@ pub mod test {
) -> Result<(), Vec<VerifyFailure>> {
run_test_circuit(block, FixedTableTag::iterator().collect())
}

pub(crate) fn calc_memory_expension_gas_cost(
curr_memory_word_size: u64,
next_memory_word_size: u64,
) -> u64 {
if next_memory_word_size <= curr_memory_word_size {
0
} else {
let total_cost = |mem_word_size| {
mem_word_size * GasCost::MEMORY.as_u64() + mem_word_size * mem_word_size / 512
};
total_cost(next_memory_word_size) - total_cost(curr_memory_word_size)
}
}

pub(crate) fn calc_memory_copier_gas_cost(
curr_memory_word_size: u64,
next_memory_word_size: u64,
num_copy_bytes: u64,
) -> u64 {
let num_words = (num_copy_bytes + 31) / 32;
num_words * GasCost::COPY.as_u64()
+ calc_memory_expension_gas_cost(curr_memory_word_size, next_memory_word_size)
}
}
14 changes: 14 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod add;
mod begin_tx;
mod bitwise;
mod byte;
mod calldatacopy;
mod caller;
mod callvalue;
mod coinbase;
Expand All @@ -31,6 +32,7 @@ mod jump;
mod jumpdest;
mod jumpi;
mod memory;
mod memory_copy;
mod msize;
mod mul;
mod pc;
Expand All @@ -46,6 +48,7 @@ use add::AddGadget;
use begin_tx::BeginTxGadget;
use bitwise::BitwiseGadget;
use byte::ByteGadget;
use calldatacopy::CallDataCopyGadget;
use caller::CallerGadget;
use callvalue::CallValueGadget;
use coinbase::CoinbaseGadget;
Expand All @@ -57,6 +60,7 @@ use jump::JumpGadget;
use jumpdest::JumpdestGadget;
use jumpi::JumpiGadget;
use memory::MemoryGadget;
use memory_copy::CopyToMemoryGadget;
use msize::MsizeGadget;
use mul::MulGadget;
use pc::PcGadget;
Expand Down Expand Up @@ -97,6 +101,7 @@ pub(crate) struct ExecutionConfig<F> {
bitwise_gadget: BitwiseGadget<F>,
begin_tx_gadget: BeginTxGadget<F>,
byte_gadget: ByteGadget<F>,
calldatacopy_gadget: CallDataCopyGadget<F>,
caller_gadget: CallerGadget<F>,
call_value_gadget: CallValueGadget<F>,
comparator_gadget: ComparatorGadget<F>,
Expand All @@ -107,6 +112,7 @@ pub(crate) struct ExecutionConfig<F> {
jumpi_gadget: JumpiGadget<F>,
gas_gadget: GasGadget<F>,
memory_gadget: MemoryGadget<F>,
copy_to_memory_gadget: CopyToMemoryGadget<F>,
pc_gadget: PcGadget<F>,
pop_gadget: PopGadget<F>,
push_gadget: PushGadget<F>,
Expand Down Expand Up @@ -227,6 +233,7 @@ impl<F: FieldExt> ExecutionConfig<F> {
bitwise_gadget: configure_gadget!(),
begin_tx_gadget: configure_gadget!(),
byte_gadget: configure_gadget!(),
calldatacopy_gadget: configure_gadget!(),
caller_gadget: configure_gadget!(),
call_value_gadget: configure_gadget!(),
comparator_gadget: configure_gadget!(),
Expand All @@ -237,6 +244,7 @@ impl<F: FieldExt> ExecutionConfig<F> {
jumpi_gadget: configure_gadget!(),
gas_gadget: configure_gadget!(),
memory_gadget: configure_gadget!(),
copy_to_memory_gadget: configure_gadget!(),
pc_gadget: configure_gadget!(),
pop_gadget: configure_gadget!(),
push_gadget: configure_gadget!(),
Expand Down Expand Up @@ -501,6 +509,12 @@ impl<F: FieldExt> ExecutionConfig<F> {
ExecutionState::TIMESTAMP => {
assign_exec_step!(self.timestamp_gadget)
}
ExecutionState::CALLDATACOPY => {
assign_exec_step!(self.calldatacopy_gadget)
}
ExecutionState::CopyToMemory => {
assign_exec_step!(self.copy_to_memory_gadget)
}
ExecutionState::ErrorOutOfGasPureMemory => {
assign_exec_step!(self.error_oog_pure_memory_gadget)
}
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ impl<F: FieldExt> ExecutionGadget<F> for BeginTxGadget<F> {
TxContextFieldTag::CallDataLength,
TxContextFieldTag::CallDataGasCost,
]
.map(|field_tag| cb.tx_context(tx_id.expr(), field_tag));
.map(|field_tag| cb.tx_context(tx_id.expr(), field_tag, None));
let [tx_gas_price, tx_value] = [TxContextFieldTag::GasPrice, TxContextFieldTag::Value]
.map(|field_tag| cb.tx_context_as_word(tx_id.expr(), field_tag));
.map(|field_tag| cb.tx_context_as_word(tx_id.expr(), field_tag, None));

// Add first step constraint to have both rw_counter and tx_id to be 1
cb.add_constraint_first_step(
Expand Down Expand Up @@ -293,7 +293,7 @@ mod test {
GasCost::TX.as_u64()
} + call_data_gas_cost;

let from_balance_prev = Word::from(10).pow(20.into());
let from_balance_prev = Word::from(10_i32).pow(20_i32.into());
let to_balance_prev = Word::zero();
let from_balance = from_balance_prev - tx.value - gas_fee;
let to_balance = to_balance_prev + tx.value;
Expand Down
Loading