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

Commit

Permalink
fix caller/callee/coinbase cell to word type
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed May 23, 2023
1 parent ab9551f commit ec8f365
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 116 deletions.
49 changes: 15 additions & 34 deletions zkevm-circuits/src/evm_circuit/execution/end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use crate::{
BlockContextFieldTag, CallContextFieldTag, RwTableTag, TxContextFieldTag, TxReceiptFieldTag,
},
util::{
word::{Word, WordExpr},
word::{Word, WordCell, WordExpr},
Expr,
},
};
use eth_types::{evm_types::MAX_REFUND_QUOTIENT_OF_GAS_USED, Field, ToScalar};
use eth_types::{evm_types::MAX_REFUND_QUOTIENT_OF_GAS_USED, Field};
use halo2_proofs::{circuit::Value, plonk::Error};
use strum::EnumCount;

Expand All @@ -37,11 +37,11 @@ pub(crate) struct EndTxGadget<F> {
refund: Cell<F>,
effective_refund: MinMaxGadget<F, N_BYTES_GAS>,
mul_gas_price_by_refund: MulWordByU64Gadget<F>,
tx_caller_address: Cell<F>,
tx_caller_address: WordCell<F>,
gas_fee_refund: UpdateBalanceGadget<F, 2, true>,
sub_gas_price_by_base_fee: AddWordsGadget<F, 2, true>,
mul_effective_tip_by_gas_used: MulWordByU64Gadget<F>,
coinbase: Cell<F>,
coinbase: WordCell<F>,
coinbase_reward: UpdateBalanceGadget<F, 2, true>,
current_cumulative_gas_used: Cell<F>,
is_first_tx: IsEqualGadget<F>,
Expand All @@ -57,9 +57,9 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
let is_persistent = cb.call_context(None, CallContextFieldTag::IsPersistent);

let [tx_gas, tx_caller_address] =
[TxContextFieldTag::Gas, TxContextFieldTag::CallerAddress]
.map(|field_tag| cb.tx_context(tx_id.expr(), field_tag, None));
let tx_gas = cb.tx_context(tx_id.expr(), TxContextFieldTag::Gas, None);
let tx_caller_address =
cb.tx_context_as_word(tx_id.expr(), TxContextFieldTag::CallerAddress, None);
let tx_gas_price = cb.tx_context_as_word32(tx_id.expr(), TxContextFieldTag::GasPrice, None);

// Calculate effective gas to refund
Expand All @@ -81,20 +81,17 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
);
let gas_fee_refund = UpdateBalanceGadget::construct(
cb,
tx_caller_address.expr(),
tx_caller_address.to_word().expr_unchecked(),
vec![mul_gas_price_by_refund.product().clone()],
None,
);

// Add gas_used * effective_tip to coinbase's balance
let coinbase = cb.query_cell();
let coinbase = cb.query_word_unchecked();
let base_fee = cb.query_word32();
// lookup && range check
for (tag, value) in [
(
BlockContextFieldTag::Coinbase,
Word::from_lo_unchecked(coinbase.expr()),
),
(BlockContextFieldTag::Coinbase, coinbase.to_word()),
(BlockContextFieldTag::BaseFee, base_fee.to_word()),
] {
cb.block_lookup(tag.expr(), None, value);
Expand All @@ -106,7 +103,7 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
MulWordByU64Gadget::construct(cb, effective_tip, gas_used.clone());
let coinbase_reward = UpdateBalanceGadget::construct(
cb,
coinbase.expr(),
coinbase.to_word().expr_unchecked(),
vec![mul_effective_tip_by_gas_used.product().clone()],
None,
);
Expand Down Expand Up @@ -236,15 +233,8 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
effective_refund + step.gas_left,
gas_fee_refund,
)?;
self.tx_caller_address.assign(
region,
offset,
Value::known(
tx.caller_address
.to_scalar()
.expect("unexpected Address -> Scalar conversion failure"),
),
)?;
self.tx_caller_address
.assign_h160(region, offset, tx.caller_address)?;
self.gas_fee_refund.assign(
region,
offset,
Expand All @@ -266,17 +256,8 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
gas_used,
effective_tip * gas_used,
)?;
self.coinbase.assign(
region,
offset,
Value::known(
block
.context
.coinbase
.to_scalar()
.expect("unexpected Address -> Scalar conversion failure"),
),
)?;
self.coinbase
.assign_h160(region, offset, block.context.coinbase)?;
self.coinbase_reward.assign(
region,
offset,
Expand Down
21 changes: 7 additions & 14 deletions zkevm-circuits/src/evm_circuit/execution/error_oog_sload_sstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
};
use eth_types::{
evm_types::{GasCost, OpcodeId},
Field, ToLittleEndian, ToScalar, U256,
Field, ToLittleEndian, U256,
};
use halo2_proofs::{circuit::Value, plonk::Error};

Expand All @@ -34,7 +34,7 @@ pub(crate) struct ErrorOOGSloadSstoreGadget<F> {
opcode: Cell<F>,
tx_id: Cell<F>,
is_static: Cell<F>,
callee_address: Cell<F>,
callee_address: WordCell<F>,
key: WordCell<F>,
value: WordCell<F>,
value_prev: WordCell<F>,
Expand Down Expand Up @@ -65,7 +65,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGSloadSstoreGadget<F> {

let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
let is_static = cb.call_context(None, CallContextFieldTag::IsStatic);
let callee_address = cb.call_context(None, CallContextFieldTag::CalleeAddress);
let callee_address = cb.call_context_read_as_word(None, CallContextFieldTag::CalleeAddress);

// Constrain `is_static` must be false for SSTORE.
cb.require_zero("is_static == false", is_static.expr() * is_sstore.expr().0);
Expand All @@ -79,7 +79,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGSloadSstoreGadget<F> {
cb.stack_pop(key.to_word());
cb.account_storage_access_list_read(
tx_id.expr(),
callee_address.expr(),
callee_address.to_word().expr_unchecked(),
key.to_word(),
Word::from_lo_unchecked(is_warm.expr()),
);
Expand All @@ -89,7 +89,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGSloadSstoreGadget<F> {
cb.stack_pop(value.to_word());

cb.account_storage_read(
callee_address.expr(),
callee_address.to_word().expr_unchecked(),
key.to_word(),
value_prev.to_word(),
tx_id.expr(),
Expand Down Expand Up @@ -193,15 +193,8 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGSloadSstoreGadget<F> {
.assign(region, offset, Value::known(F::from(tx.id as u64)))?;
self.is_static
.assign(region, offset, Value::known(F::from(call.is_static as u64)))?;
self.callee_address.assign(
region,
offset,
Value::known(
call.address
.to_scalar()
.expect("unexpected Address -> Scalar conversion failure"),
),
)?;
self.callee_address
.assign_h160(region, offset, call.address)?;
self.key.assign(region, offset, Some(key.to_le_bytes()))?;
self.value
.assign(region, offset, Some(value.to_le_bytes()))?;
Expand Down
22 changes: 8 additions & 14 deletions zkevm-circuits/src/evm_circuit/execution/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
table::{CallContextFieldTag, RwTableTag, TxLogFieldTag},
util::{
build_tx_log_expression,
word::{Word, WordExpr},
word::{Word, WordCell, WordExpr},
Expr,
},
};
Expand All @@ -37,7 +37,7 @@ pub(crate) struct LogGadget<F> {
phase2_topics: [Cell<F>; 4],
topic_selectors: [Cell<F>; 4],

contract_address: Cell<F>,
contract_address: WordCell<F>,
is_static_call: Cell<F>,
is_persistent: Cell<F>,
tx_id: Cell<F>,
Expand Down Expand Up @@ -65,17 +65,18 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {

// check contract_address in CallContext & TxLog
// use call context's callee address as contract address
let contract_address = cb.call_context(None, CallContextFieldTag::CalleeAddress);
let contract_address =
cb.call_context_read_as_word(None, CallContextFieldTag::CalleeAddress);
let is_persistent = cb.call_context(None, CallContextFieldTag::IsPersistent);
cb.require_boolean("is_persistent is bool", is_persistent.expr());

cb.condition(is_persistent.expr(), |cb| {
cb.tx_log_lookup(
cb.tx_log_lookup_word(
tx_id.expr(),
cb.curr.state.log_id.expr() + 1.expr(),
TxLogFieldTag::Address,
0.expr(),
contract_address.expr(),
contract_address.to_word(),
);
});

Expand Down Expand Up @@ -233,15 +234,8 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
self.phase2_topics[i].assign(region, offset, topic)?;
}

self.contract_address.assign(
region,
offset,
Value::known(
call.address
.to_scalar()
.expect("unexpected Address -> Scalar conversion failure"),
),
)?;
self.contract_address
.assign_h160(region, offset, call.address)?;

self.is_static_call
.assign(region, offset, Value::known(F::from(call.is_static as u64)))?;
Expand Down
21 changes: 8 additions & 13 deletions zkevm-circuits/src/evm_circuit/execution/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
},
table::{AccountFieldTag, CallContextFieldTag},
util::{
word::{Word, Word32Cell, WordExpr},
word::{Word, Word32Cell, WordCell, WordExpr},
Expr,
},
};
Expand Down Expand Up @@ -45,7 +45,7 @@ pub(crate) struct ReturnRevertGadget<F> {
code_hash: Word32Cell<F>,

caller_id: Cell<F>,
address: Cell<F>,
address: WordCell<F>,
reversion_info: ReversionInfo<F>,
}

Expand Down Expand Up @@ -116,15 +116,14 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
copy_rw_increase.expr(),
);

let [caller_id, address] = [
CallContextFieldTag::CallerId,
CallContextFieldTag::CalleeAddress,
]
.map(|tag| cb.call_context(None, tag));
let caller_id = cb.call_context(None, CallContextFieldTag::CallerId);
let address =
cb.call_context_read_as_word(None, CallContextFieldTag::CalleeAddress);

let mut reversion_info = cb.reversion_info_read(None);

cb.account_write(
address.expr(),
address.to_word().expr_unchecked(),
AccountFieldTag::CodeHash,
code_hash.to_word(),
cb.empty_code_hash_word(),
Expand Down Expand Up @@ -322,11 +321,7 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
Value::known(call.caller_id.to_scalar().unwrap()),
)?;

self.address.assign(
region,
offset,
Value::known(call.address.to_scalar().unwrap()),
)?;
self.address.assign_h160(region, offset, call.address)?;

self.reversion_info.assign(
region,
Expand Down
17 changes: 5 additions & 12 deletions zkevm-circuits/src/evm_circuit/execution/selfbalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use halo2_proofs::{circuit::Value, plonk::Error};
#[derive(Clone, Debug)]
pub(crate) struct SelfbalanceGadget<F> {
same_context: SameContextGadget<F>,
callee_address: Cell<F>,
callee_address: WordCell<F>,
self_balance: WordCell<F>,
}

Expand All @@ -32,11 +32,11 @@ impl<F: Field> ExecutionGadget<F> for SelfbalanceGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::SELFBALANCE;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let callee_address = cb.call_context(None, CallContextFieldTag::CalleeAddress);
let callee_address = cb.call_context_read_as_word(None, CallContextFieldTag::CalleeAddress);

let self_balance = cb.query_word_unchecked();
cb.account_read(
callee_address.expr(),
callee_address.to_word().expr_unchecked(),
AccountFieldTag::Balance,
self_balance.to_word(),
);
Expand Down Expand Up @@ -71,15 +71,8 @@ impl<F: Field> ExecutionGadget<F> for SelfbalanceGadget<F> {
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

self.callee_address.assign(
region,
offset,
Value::known(
call.address
.to_scalar()
.expect("unexpected Address -> Scalar conversion failure"),
),
)?;
self.callee_address
.assign_h160(region, offset, call.address)?;

let self_balance = block.rws[step.rw_indices[2]].stack_value();
self.self_balance
Expand Down
21 changes: 7 additions & 14 deletions zkevm-circuits/src/evm_circuit/execution/sload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ use crate::{
Expr,
},
};
use eth_types::{Field, ToLittleEndian, ToScalar};
use eth_types::{Field, ToLittleEndian};
use halo2_proofs::{circuit::Value, plonk::Error};

#[derive(Clone, Debug)]
pub(crate) struct SloadGadget<F> {
same_context: SameContextGadget<F>,
tx_id: Cell<F>,
reversion_info: ReversionInfo<F>,
callee_address: Cell<F>,
callee_address: WordCell<F>,
key: WordCell<F>,
value: WordCell<F>,
committed_value: WordCell<F>,
Expand All @@ -42,7 +42,7 @@ impl<F: Field> ExecutionGadget<F> for SloadGadget<F> {

let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
let mut reversion_info = cb.reversion_info_read(None);
let callee_address = cb.call_context(None, CallContextFieldTag::CalleeAddress);
let callee_address = cb.call_context_read_as_word(None, CallContextFieldTag::CalleeAddress);

let key = cb.query_word_unchecked();
// Pop the key from the stack
Expand All @@ -51,7 +51,7 @@ impl<F: Field> ExecutionGadget<F> for SloadGadget<F> {
let value = cb.query_word_unchecked();
let committed_value = cb.query_word_unchecked();
cb.account_storage_read(
callee_address.expr(),
callee_address.to_word().expr_unchecked(),
key.to_word(),
value.to_word(),
tx_id.expr(),
Expand All @@ -63,7 +63,7 @@ impl<F: Field> ExecutionGadget<F> for SloadGadget<F> {
let is_warm = cb.query_bool();
cb.account_storage_access_list_write(
tx_id.expr(),
callee_address.expr(),
callee_address.to_word().expr_unchecked(),
key.to_word(),
Word::from_lo_unchecked(true.expr()),
Word::from_lo_unchecked(is_warm.expr()),
Expand Down Expand Up @@ -111,15 +111,8 @@ impl<F: Field> ExecutionGadget<F> for SloadGadget<F> {
call.rw_counter_end_of_reversion,
call.is_persistent,
)?;
self.callee_address.assign(
region,
offset,
Value::known(
call.address
.to_scalar()
.expect("unexpected Address -> Scalar conversion failure"),
),
)?;
self.callee_address
.assign_h160(region, offset, call.address)?;

let [key, value] =
[step.rw_indices[4], step.rw_indices[6]].map(|idx| block.rws[idx].stack_value());
Expand Down
Loading

0 comments on commit ec8f365

Please sign in to comment.