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 21, 2024
1 parent 5602d3a commit 37a01a3
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,14 @@ impl<F: Field> TransferToGadget<F> {
/// setting it's code_hash = EMPTY_HASH. The receiver account is also created
/// unconditionally if must_create is true. This gadget is used in BeginTx.
#[derive(Clone, Debug)]
pub(crate) struct TransferWithGasFeeGadget<F> {
sender_sub_fee: UpdateBalanceGadget<F, 2, false>,
pub(crate) struct TransferWithGasFeeGadget<F, const WITH_FEE: bool> {
sender_sub_fee: Option<UpdateBalanceGadget<F, 2, false>>,
sender_sub_value: UpdateBalanceGadget<F, 2, false>,
receiver: TransferToGadget<F>,
value_is_zero: IsZeroWordGadget<F, Word32Cell<F>>,
}

impl<F: Field> TransferWithGasFeeGadget<F> {
impl<F: Field, const WITH_FEE: bool> TransferWithGasFeeGadget<F, WITH_FEE> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn construct(
cb: &mut EVMConstraintBuilder<F>,
Expand All @@ -468,10 +468,14 @@ impl<F: Field> TransferWithGasFeeGadget<F> {
receiver_exists: Expression<F>,
must_create: Expression<F>,
value: Word32Cell<F>,
gas_fee: Word32Cell<F>,
reversion_info: &mut ReversionInfo<F>,
gas_fee: Option<Word32Cell<F>>,
) -> Self {
let sender_sub_fee = cb.decrease_balance(sender_address.to_word(), gas_fee, None);
let sender_sub_fee = if WITH_FEE {
Some(cb.decrease_balance(sender_address.to_word(), gas_fee.expect("fee exists"), None))
} else {
None
};
let value_is_zero = cb.is_zero_word(&value);
// Skip transfer if value == 0
let sender_sub_value = cb.condition(not::expr(value_is_zero.expr()), |cb| {
Expand Down Expand Up @@ -517,6 +521,19 @@ impl<F: Field> TransferWithGasFeeGadget<F> {
// +1 Write Account (sender) Balance
// +1 Write Account (receiver) Balance
not::expr(self.value_is_zero.expr()) * 2.expr()


/// without gas
// +1 Write Account (receiver) CodeHash (account creation via code_hash update)
or::expr([
not::expr(self.value_is_zero.expr()) * not::expr(self.receiver.receiver_exists.clone()),
self.receiver.must_create.clone()]
) * 1.expr() +
// +1 Write Account (sender) Balance
// +1 Write Account (receiver) Balance
not::expr(self.value_is_zero.expr()) * 2.expr()

}

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

0 comments on commit 37a01a3

Please sign in to comment.