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

Commit

Permalink
refactor mul_word_by_u64 gadget
Browse files Browse the repository at this point in the history
* add zero equality constraint for `quotient_hi`

* return with `product_bytes` which was unused
  • Loading branch information
TomTaehoonKim committed Jun 29, 2023
1 parent e00ffa2 commit 90875e7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/zkevm_specs/evm_circuit/execution/begin_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def begin_tx(instruction: Instruction):
# Calculate gas fee
tx_gas = instruction.tx_context_lookup(tx_id, TxContextFieldTag.Gas)
tx_gas_price = instruction.tx_gas_price(tx_id)
gas_fee, carry = instruction.mul_word_by_u64(tx_gas_price, tx_gas)
instruction.constrain_zero(carry)
gas_fee = instruction.mul_word_by_u64(tx_gas_price, tx_gas)

# intrinsic gas
# G_0 = sum([G_txdatazero if CallData[i] == 0 else G_txdatanonzero for i in len(CallData)]) +
Expand Down
8 changes: 2 additions & 6 deletions src/zkevm_specs/evm_circuit/execution/end_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ def end_tx(instruction: Instruction):

# Add effective_refund * gas_price back to caller's balance
tx_gas_price = instruction.tx_gas_price(tx_id)
value, carry = instruction.mul_word_by_u64(
tx_gas_price, instruction.curr.gas_left + effective_refund
)
instruction.constrain_zero(carry)
value = instruction.mul_word_by_u64(tx_gas_price, instruction.curr.gas_left + effective_refund)
tx_caller_address_word = instruction.tx_context_lookup_word(
tx_id, TxContextFieldTag.CallerAddress
)
Expand All @@ -37,8 +34,7 @@ def end_tx(instruction: Instruction):
# Add gas_used * effective_tip to coinbase's balance
base_fee = instruction.block_context_lookup_word(BlockContextFieldTag.BaseFee)
effective_tip, _ = instruction.sub_word(tx_gas_price, base_fee)
reward, carry = instruction.mul_word_by_u64(effective_tip, gas_used)
instruction.constrain_zero(carry)
reward = instruction.mul_word_by_u64(effective_tip, gas_used)
coinbase_word = instruction.block_context_lookup_word(BlockContextFieldTag.Coinbase)
coinbase = instruction.word_to_address(coinbase_word)
instruction.add_balance(coinbase, [reward])
Expand Down
4 changes: 3 additions & 1 deletion src/zkevm_specs/evm_circuit/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,11 @@ def mul_word_by_u64(self, multiplicand: Word, multiplier: Expression) -> Tuple[W
(multiplicand_hi * multiplier.expr() + quotient_lo).n, 1 << 128
)

self.constrain_zero(FQ(quotient_hi))

product_bytes = product_lo.to_bytes(16, "little") + product_hi.to_bytes(16, "little")

return Word((FQ(product_lo), FQ(product_hi))), FQ(quotient_hi)
return Word(product_bytes)

def mul_add_words(self, a: Word, b: Word, c: Word, d: Word) -> FQ:
"""
Expand Down

0 comments on commit 90875e7

Please sign in to comment.