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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihChengLiang committed Feb 22, 2024
1 parent 8d4d596 commit 45a7ba3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
6 changes: 1 addition & 5 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,13 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
AccountFieldTag::CodeHash,
code_hash.to_word(),
);
cb.require_equal(
"is create: callee_not_exists",
tx.is_create.expr(),
callee_not_exists.expr(),
);
// Transfer value from caller to callee, creating account if necessary.
let transfer_with_gas_fee = TransferGadget::construct(
cb,
tx.caller_address.to_word(),
tx.callee_address.to_word(),
not::expr(callee_not_exists.expr()),
tx.is_create.expr(),
tx.value.clone(),
&mut reversion_info,
Some(tx.mul_gas_fee_by_gas.product().clone()),
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/execution/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
caller_address.to_word(),
callee_address.to_word(),
not::expr(call_gadget.callee_not_exists.expr()),
false.expr(),
call_gadget.value.clone(),
&mut callee_reversion_info,
None,
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
create.caller_address(),
contract_addr.to_word(),
0.expr(),
true.expr(),
value.clone(),
&mut callee_reversion_info,
None,
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/execution/end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl<F: Field> ExecutionGadget<F> for EndTxGadget<F> {
cb,
coinbase.to_word(),
1.expr() - coinbase_code_hash_is_zero.expr(),
false.expr(),
mul_effective_tip_by_gas_used.product().clone(),
None,
);
Expand Down
28 changes: 16 additions & 12 deletions zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use bus_mapping::state_db::CodeDB;
use eth_types::{
evm_types::GasCost, Field, OpsIdentity, ToAddress, ToLittleEndian, ToScalar, ToWord, U256,
};
use gadgets::util::{select, sum};
use gadgets::util::{or, select, sum};
use halo2_proofs::{
circuit::Value,
plonk::{Error, Expression},
Expand Down Expand Up @@ -370,6 +370,7 @@ impl<F: Field, const N_ADDENDS: usize, const INCREASE: bool>
pub(crate) struct TransferToGadget<F> {
receiver: UpdateBalanceGadget<F, 2, true>,
receiver_exists: Expression<F>,
opcode_is_create: Expression<F>,
value_is_zero: IsZeroWordGadget<F, Word32Cell<F>>,
}

Expand All @@ -379,6 +380,7 @@ impl<F: Field> TransferToGadget<F> {
cb: &mut EVMConstraintBuilder<F>,
receiver_address: WordLoHi<Expression<F>>,
receiver_exists: Expression<F>,
opcode_is_create: Expression<F>,
value: Word32Cell<F>,
mut reversion_info: Option<&mut ReversionInfo<F>>,
) -> Self {
Expand All @@ -388,7 +390,7 @@ impl<F: Field> TransferToGadget<F> {
cb.condition(
and::expr([
not::expr(receiver_exists.expr()),
not::expr(value_is_zero.expr()),
or::expr([not::expr(value_is_zero.expr()), opcode_is_create.expr()]),
]),
|cb| {
cb.account_write(
Expand All @@ -408,6 +410,7 @@ impl<F: Field> TransferToGadget<F> {
Self {
receiver,
receiver_exists,
opcode_is_create,
value_is_zero,
}
}
Expand All @@ -434,8 +437,9 @@ impl<F: Field> TransferToGadget<F> {
pub(crate) fn rw_delta(&self) -> Expression<F> {
// +1 Write Account (receiver) CodeHash (account creation via code_hash update)
and::expr([
not::expr(self.receiver_exists.expr()),not::expr(self.value_is_zero.expr())
]) +
not::expr(self.receiver_exists.expr()),
or::expr([not::expr(self.value_is_zero.expr()), self.opcode_is_create.expr()])
])+
// +1 Write Account (receiver) Balance
not::expr(self.value_is_zero.expr())
}
Expand Down Expand Up @@ -463,6 +467,7 @@ impl<F: Field, const WITH_FEE: bool> TransferGadget<F, WITH_FEE> {
sender_address: WordLoHi<Expression<F>>,
receiver_address: WordLoHi<Expression<F>>,
receiver_exists: Expression<F>,
opcode_is_create: Expression<F>,
value: Word32Cell<F>,
reversion_info: &mut ReversionInfo<F>,
gas_fee: Option<Word32Cell<F>>,
Expand All @@ -481,6 +486,7 @@ impl<F: Field, const WITH_FEE: bool> TransferGadget<F, WITH_FEE> {
cb,
receiver_address,
receiver_exists,
opcode_is_create,
value,
Some(reversion_info),
);
Expand All @@ -496,20 +502,18 @@ impl<F: Field, const WITH_FEE: bool> TransferGadget<F, WITH_FEE> {
pub(crate) fn rw_delta(&self) -> Expression<F> {
// +1 Write Account (sender) Balance (Not Reversible tx fee)
WITH_FEE.expr() +
// +1 Write Account (receiver) CodeHash (account creation via code_hash update)
self.receiver.rw_delta()+
// +1 Write Account (sender) Balance
// +1 Write Account (receiver) Balance
not::expr(self.value_is_zero.expr()) * 2.expr()
not::expr(self.value_is_zero.expr()) +
// +1 Write Account (receiver) CodeHash (account creation via code_hash update)
self.receiver.rw_delta()
}

pub(crate) fn reversible_w_delta(&self) -> Expression<F> {
// NOTE: Write Account (sender) Balance (Not Reversible tx fee)
// +1 Write Account (receiver) CodeHash (account creation via code_hash update)
self.receiver.rw_delta()+
// +1 Write Account (sender) Balance
// +1 Write Account (receiver) Balance
not::expr(self.value_is_zero.expr()) * 2.expr()
not::expr(self.value_is_zero.expr()) +
// +1 Write Account (receiver) CodeHash (account creation via code_hash update)
self.receiver.rw_delta()
}

#[allow(clippy::too_many_arguments)]
Expand Down

0 comments on commit 45a7ba3

Please sign in to comment.