Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
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: 3 additions & 1 deletion bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod caller;
mod callvalue;
mod dup;
mod extcodehash;
mod gasprice;
mod mload;
mod mstore;
mod number;
Expand All @@ -39,6 +40,7 @@ use caller::Caller;
use callvalue::Callvalue;
use dup::Dup;
use extcodehash::Extcodehash;
use gasprice::GasPrice;
use mload::Mload;
use mstore::Mstore;
use selfbalance::Selfbalance;
Expand Down Expand Up @@ -113,7 +115,7 @@ fn fn_gen_associated_ops(opcode_id: &OpcodeId) -> FnGenAssociatedOps {
OpcodeId::CALLDATACOPY => Calldatacopy::gen_associated_ops,
// OpcodeId::CODESIZE => {},
// OpcodeId::CODECOPY => {},
// OpcodeId::GASPRICE => {},
OpcodeId::GASPRICE => GasPrice::gen_associated_ops,
// OpcodeId::EXTCODESIZE => {},
// OpcodeId::EXTCODECOPY => {},
// OpcodeId::RETURNDATASIZE => {},
Expand Down
124 changes: 124 additions & 0 deletions bus-mapping/src/evm/opcodes/gasprice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use super::Opcode;
use crate::circuit_input_builder::{CircuitInputStateRef, ExecStep};
use crate::operation::{CallContextField, CallContextOp, RW};
use crate::Error;
use eth_types::GethExecStep;

/// Placeholder structure used to implement [`Opcode`] trait over it
/// corresponding to the [`OpcodeId::PC`](crate::evm::OpcodeId::PC) `OpcodeId`.
#[derive(Debug, Copy, Clone)]
pub(crate) struct GasPrice;

impl Opcode for GasPrice {
fn gen_associated_ops(
state: &mut CircuitInputStateRef,
geth_steps: &[GethExecStep],
) -> Result<Vec<ExecStep>, Error> {
let geth_step = &geth_steps[0];
let mut exec_step = state.new_step(geth_step)?;
// Get gasprice result from next step
let value = geth_steps[1].stack.last()?;
let tx_id = state.tx_ctx.id();

// CallContext read of the TxId
state.push_op(
&mut exec_step,
RW::READ,
CallContextOp {
call_id: state.call()?.call_id,
field: CallContextField::TxId,
value: tx_id.into(),
},
);

// Stack write of the gasprice value
state.push_stack_op(
&mut exec_step,
RW::WRITE,
geth_step.stack.last_filled().map(|a| a - 1),
value,
)?;

Ok(vec![exec_step])
}
}

#[cfg(test)]
mod gasprice_tests {
use crate::{
circuit_input_builder::ExecState,
evm::OpcodeId,
mock::BlockData,
operation::{CallContextField, CallContextOp, StackOp, RW},
Error,
};
use eth_types::{bytecode, evm_types::StackAddress, geth_types::GethData, Word};
use mock::test_ctx::{helpers::*, TestContext};
use pretty_assertions::assert_eq;

#[test]
fn gasprice_opcode_impl() -> Result<(), Error> {
let code = bytecode! {
#[start]
GASPRICE
STOP
};

let two_gwei = Word::from(2_000_000_000u64);

// Get the execution steps from the external tracer
let block: GethData = TestContext::<2, 1>::new(
None,
account_0_code_account_1_no_code(code),
|mut txs, accs| {
txs[0]
.from(accs[1].address)
.to(accs[0].address)
.gas_price(two_gwei);
},
|block, _tx| block.number(0xcafeu64),
)
.unwrap()
.into();

let mut builder = BlockData::new_from_geth_data(block.clone()).new_circuit_input_builder();
builder
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();

let step = builder.block.txs()[0]
.steps()
.iter()
.find(|step| step.exec_state == ExecState::Op(OpcodeId::GASPRICE))
.unwrap();

let op_gasprice = &builder.block.container.stack[step.bus_mapping_instance[1].as_usize()];
assert_eq!(
(op_gasprice.rw(), op_gasprice.op()),
(
RW::WRITE,
&StackOp::new(1, StackAddress(1023usize), two_gwei)
)
);

let call_id = builder.block.txs()[0].calls()[0].call_id;

assert_eq!(
{
let operation =
&builder.block.container.call_context[step.bus_mapping_instance[0].as_usize()];
(operation.rw(), operation.op())
},
(
RW::READ,
&CallContextOp {
call_id,
field: CallContextField::TxId,
value: Word::one(),
}
)
);

Ok(())
}
}
19 changes: 17 additions & 2 deletions mock/src/test_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use eth_types::{
Block, Bytecode, Error, GethExecTrace, Transaction, Word,
};
use external_tracer::{trace, TraceConfig};
use helpers::*;
use itertools::Itertools;

/// TestContext is a type that contains all the information from a block
Expand Down Expand Up @@ -185,6 +186,20 @@ impl<const NACC: usize, const NTX: usize> TestContext<NACC, NTX> {
geth_traces,
})
}

/// Returns a simple TestContext setup with a single tx executing the
/// bytecode passed as parameters. The balances of the 2 accounts and
/// addresses are the ones used in [`TestContext::
/// account_0_code_account_1_no_code`]. Extra accounts, txs and/or block
/// configs are set as [`Default`].
pub fn simple_ctx_with_bytecode(bytecode: Bytecode) -> Result<TestContext<2, 1>, Error> {
TestContext::new(
None,
account_0_code_account_1_no_code(bytecode),
tx_from_1_to_0,
|block, _txs| block,
)
}
}

/// Generates execution traces for the transactions included in the provided
Expand Down Expand Up @@ -228,11 +243,11 @@ pub mod helpers {
|accs| {
accs[0]
.address(MOCK_ACCOUNTS[0])
.balance(Word::from(1u64 << 20))
.balance(Word::from(10u64.pow(19)))
.code(code);
accs[1]
.address(MOCK_ACCOUNTS[1])
.balance(Word::from(1u64 << 20));
.balance(Word::from(10u64.pow(19)));
}
}

Expand Down
5 changes: 5 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod end_tx;
mod error_oog_static_memory;
mod extcodehash;
mod gas;
mod gasprice;
mod is_zero;
mod jump;
mod jumpdest;
Expand Down Expand Up @@ -74,6 +75,7 @@ use end_tx::EndTxGadget;
use error_oog_static_memory::ErrorOOGStaticMemoryGadget;
use extcodehash::ExtcodehashGadget;
use gas::GasGadget;
use gasprice::GasPriceGadget;
use is_zero::IsZeroGadget;
use jump::JumpGadget;
use jumpdest::JumpdestGadget;
Expand Down Expand Up @@ -139,6 +141,7 @@ pub(crate) struct ExecutionConfig<F> {
jump_gadget: JumpGadget<F>,
jumpdest_gadget: JumpdestGadget<F>,
jumpi_gadget: JumpiGadget<F>,
gasprice_gadget: GasPriceGadget<F>,
gas_gadget: GasGadget<F>,
memory_gadget: MemoryGadget<F>,
copy_to_memory_gadget: CopyToMemoryGadget<F>,
Expand Down Expand Up @@ -358,6 +361,7 @@ impl<F: Field> ExecutionConfig<F> {
jumpdest_gadget: configure_gadget!(),
jumpi_gadget: configure_gadget!(),
gas_gadget: configure_gadget!(),
gasprice_gadget: configure_gadget!(),
memory_gadget: configure_gadget!(),
copy_to_memory_gadget: configure_gadget!(),
pc_gadget: configure_gadget!(),
Expand Down Expand Up @@ -630,6 +634,7 @@ impl<F: Field> ExecutionConfig<F> {
assign_exec_step!(self.jumpdest_gadget)
}
ExecutionState::GAS => assign_exec_step!(self.gas_gadget),
ExecutionState::GASPRICE => assign_exec_step!(self.gasprice_gadget),
ExecutionState::PUSH => assign_exec_step!(self.push_gadget),
ExecutionState::DUP => assign_exec_step!(self.dup_gadget),
ExecutionState::SWAP => assign_exec_step!(self.swap_gadget),
Expand Down
10 changes: 9 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ mod test {
use crate::test_util::run_test_circuits;
use eth_types::evm_types::OpcodeId;
use eth_types::{bytecode, Word};
use mock::TestContext;

fn test_ok(opcode: OpcodeId, a: Word, b: Word) {
let bytecode = bytecode! {
Expand All @@ -116,7 +117,14 @@ mod test {
.write_op(opcode)
STOP
};
assert_eq!(run_test_circuits(bytecode), Ok(()));

assert_eq!(
run_test_circuits(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
None
),
Ok(())
);
}

#[test]
Expand Down
11 changes: 7 additions & 4 deletions zkevm-circuits/src/evm_circuit/execution/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ impl<F: Field> ExecutionGadget<F> for BitwiseGadget<F> {
mod test {
use crate::{
evm_circuit::test::rand_word,
test_util::{
get_fixed_table, test_circuits_using_bytecode, BytecodeTestConfig, FixedTableConfig,
},
test_util::{get_fixed_table, run_test_circuits, BytecodeTestConfig, FixedTableConfig},
};
use eth_types::{bytecode, Word};
use mock::TestContext;

fn test_ok(a: Word, b: Word) {
let bytecode = bytecode! {
Expand All @@ -128,8 +127,12 @@ mod test {
evm_circuit_lookup_tags: get_fixed_table(FixedTableConfig::Complete),
..Default::default()
};

assert_eq!(
test_circuits_using_bytecode(bytecode, test_config, None),
run_test_circuits(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
Some(test_config)
),
Ok(())
);
}
Expand Down
10 changes: 9 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl<F: Field> ExecutionGadget<F> for ByteGadget<F> {
mod test {
use crate::{evm_circuit::test::rand_word, test_util::run_test_circuits};
use eth_types::{bytecode, Word};
use mock::TestContext;

fn test_ok(index: Word, value: Word) {
let bytecode = bytecode! {
Expand All @@ -137,7 +138,14 @@ mod test {
BYTE
STOP
};
assert_eq!(run_test_circuits(bytecode), Ok(()));

assert_eq!(
run_test_circuits(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
None
),
Ok(())
);
}

#[test]
Expand Down
24 changes: 18 additions & 6 deletions zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,14 @@ mod test {
test::{rand_bytes, run_test_circuit_incomplete_fixed_table},
witness::{Block, Bytecode, Call, CodeSource, ExecStep, Rw, RwMap, Transaction},
};
use crate::test_util::{test_circuits_using_bytecode, BytecodeTestConfig};
use crate::test_util::run_test_circuits;
use eth_types::{
bytecode,
evm_types::{gas_utils::memory_copier_gas_cost, GasCost, OpcodeId},
ToBigEndian, Word,
};
use halo2_proofs::arithmetic::BaseExt;
use mock::test_ctx::{helpers::*, TestContext};
use pairing::bn256::Fr as Fp;

fn test_ok_root(call_data_length: usize, memory_offset: Word, data_offset: Word, length: Word) {
Expand All @@ -253,11 +254,22 @@ mod test {
STOP
};
let call_data = rand_bytes(call_data_length);
let test_config = BytecodeTestConfig::default();
assert_eq!(
test_circuits_using_bytecode(bytecode, test_config, Some(call_data)),
Ok(())
);

// Get the execution steps from the external tracer
let ctx = TestContext::<2, 1>::new(
None,
account_0_code_account_1_no_code(bytecode),
|mut txs, accs| {
txs[0]
.from(accs[1].address)
.to(accs[0].address)
.input(call_data.into());
},
|block, _tx| block.number(0xcafeu64),
)
.unwrap();

assert_eq!(run_test_circuits(ctx, None), Ok(()));
}

fn test_ok_internal(
Expand Down
17 changes: 11 additions & 6 deletions zkevm-circuits/src/evm_circuit/execution/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,21 @@ impl<F: Field> ExecutionGadget<F> for CallerGadget<F> {
mod test {
use crate::test_util::run_test_circuits;
use eth_types::bytecode;
use mock::TestContext;

fn test_ok() {
#[test]
fn caller_gadget_test() {
let bytecode = bytecode! {
CALLER
STOP
};
assert_eq!(run_test_circuits(bytecode), Ok(()));
}
#[test]
fn caller_gadget_test() {
test_ok();

assert_eq!(
run_test_circuits(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
None
),
Ok(())
);
}
}
Loading