Skip to content

Commit

Permalink
Use ethereum-types for fixed integers (london)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Sep 30, 2024
1 parent 4328312 commit d76940b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
27 changes: 17 additions & 10 deletions src/ethereum/london/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from ethereum.exceptions import InvalidBlock

from .. import rlp
from ..base_types import U64, U256, U256_CEIL_VALUE, Bytes, Uint
from ..base_types import U64, U256, Bytes, Uint
from . import FORK_CRITERIA, vm
from .blocks import Block, Header, Log, Receipt
from .bloom import logs_bloom
Expand Down Expand Up @@ -389,7 +389,9 @@ def validate_proof_of_work(header: Header) -> None:
)
if mix_digest != header.mix_digest:
raise InvalidBlock
if Uint.from_be_bytes(result) > (U256_CEIL_VALUE // header.difficulty):

limit = Uint(U256.MAX_VALUE) + Uint(1)
if Uint.from_be_bytes(result) > (limit // header.difficulty):
raise InvalidBlock


Expand Down Expand Up @@ -743,13 +745,14 @@ def pay_rewards(
ommers :
List of ommers mentioned in the current block.
"""
miner_reward = BLOCK_REWARD + (len(ommers) * (BLOCK_REWARD // 32))
ommer_count = U256(len(ommers))
miner_reward = BLOCK_REWARD + (ommer_count * (BLOCK_REWARD // U256(32)))
create_ether(state, coinbase, miner_reward)

for ommer in ommers:
# Ommer age with respect to the current block.
ommer_age = U256(block_number - ommer.number)
ommer_miner_reward = ((8 - ommer_age) * BLOCK_REWARD) // 8
ommer_miner_reward = ((U256(8) - ommer_age) * BLOCK_REWARD) // U256(8)
create_ether(state, ommer.coinbase, ommer_miner_reward)


Expand Down Expand Up @@ -897,7 +900,7 @@ def validate_transaction(tx: Transaction) -> bool:
"""
if calculate_intrinsic_cost(tx) > Uint(tx.gas):
return False
if tx.nonce >= 2**64 - 1:
if tx.nonce >= U256(U64.MAX_VALUE):
return False
return True

Expand Down Expand Up @@ -970,22 +973,26 @@ def recover_sender(chain_id: U64, tx: Transaction) -> Address:
The address of the account that signed the transaction.
"""
r, s = tx.r, tx.s
if 0 >= r or r >= SECP256K1N:
if U256(0) >= r or r >= SECP256K1N:
raise InvalidBlock
if 0 >= s or s > SECP256K1N // 2:
if U256(0) >= s or s > SECP256K1N // U256(2):
raise InvalidBlock

if isinstance(tx, LegacyTransaction):
v = tx.v
if v == 27 or v == 28:
public_key = secp256k1_recover(
r, s, v - 27, signing_hash_pre155(tx)
r, s, v - U256(27), signing_hash_pre155(tx)
)
else:
if v != 35 + chain_id * 2 and v != 36 + chain_id * 2:
chain_id_x2 = U256(chain_id) * U256(2)
if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2:
raise InvalidBlock
public_key = secp256k1_recover(
r, s, v - 35 - chain_id * 2, signing_hash_155(tx, chain_id)
r,
s,
v - U256(35) - chain_id_x2,
signing_hash_155(tx, chain_id),
)
elif isinstance(tx, AccessListTransaction):
public_key = secp256k1_recover(
Expand Down
11 changes: 7 additions & 4 deletions src/ethereum/london/vm/instructions/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Implementations of the EVM Arithmetic instructions.
"""

from ethereum.base_types import U255_CEIL_VALUE, U256, U256_CEIL_VALUE, Uint
from ethereum.base_types import U256, Uint
from ethereum.utils.numeric import get_sign

from .. import Evm
Expand Down Expand Up @@ -138,6 +138,9 @@ def div(evm: Evm) -> None:
evm.pc += Uint(1)


U255_CEIL_VALUE = 2**255


def sdiv(evm: Evm) -> None:
"""
Signed integer division of the top two elements of the stack. Pushes the
Expand Down Expand Up @@ -318,7 +321,7 @@ def exp(evm: Evm) -> None:
)

# OPERATION
result = U256(pow(base, exponent, U256_CEIL_VALUE))
result = U256(pow(base, exponent, Uint(U256.MAX_VALUE) + Uint(1)))

push(evm.stack, result)

Expand All @@ -345,7 +348,7 @@ def signextend(evm: Evm) -> None:
charge_gas(evm, GAS_LOW)

# OPERATION
if byte_num > 31:
if byte_num > U256(31):
# Can't extend any further
result = value
else:
Expand All @@ -358,7 +361,7 @@ def signextend(evm: Evm) -> None:
if sign_bit == 0:
result = U256.from_be_bytes(value_bytes)
else:
num_bytes_prepend = 32 - (byte_num + 1)
num_bytes_prepend = U256(32) - (byte_num + U256(1))
result = U256.from_be_bytes(
bytearray([0xFF] * num_bytes_prepend) + value_bytes
)
Expand Down
22 changes: 11 additions & 11 deletions src/ethereum/london/vm/instructions/bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ def get_byte(evm: Evm) -> None:
charge_gas(evm, GAS_VERY_LOW)

# OPERATION
if byte_index >= 32:
if byte_index >= U256(32):
result = U256(0)
else:
extra_bytes_to_right = 31 - byte_index
extra_bytes_to_right = U256(31) - byte_index
# Remove the extra bytes in the right
word = word >> (extra_bytes_to_right * 8)
word = word >> (extra_bytes_to_right * U256(8))
# Remove the extra bytes in the left
word = word & 0xFF
result = U256(word)
word = word & U256(0xFF)
result = word

push(evm.stack, result)

Expand All @@ -164,15 +164,15 @@ def bitwise_shl(evm: Evm) -> None:
The current EVM frame.
"""
# STACK
shift = pop(evm.stack)
value = pop(evm.stack)
shift = Uint(pop(evm.stack))
value = Uint(pop(evm.stack))

# GAS
charge_gas(evm, GAS_VERY_LOW)

# OPERATION
if shift < 256:
result = U256((value << shift) & U256.MAX_VALUE)
if shift < Uint(256):
result = U256((value << shift) & Uint(U256.MAX_VALUE))
else:
result = U256(0)

Expand All @@ -199,7 +199,7 @@ def bitwise_shr(evm: Evm) -> None:
charge_gas(evm, GAS_VERY_LOW)

# OPERATION
if shift < 256:
if shift < U256(256):
result = value >> shift
else:
result = U256(0)
Expand All @@ -220,7 +220,7 @@ def bitwise_sar(evm: Evm) -> None:
The current EVM frame.
"""
# STACK
shift = pop(evm.stack)
shift = int(pop(evm.stack))
signed_value = pop(evm.stack).to_signed()

# GAS
Expand Down
2 changes: 1 addition & 1 deletion src/ethereum/london/vm/instructions/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def mstore8(evm: Evm) -> None:

# OPERATION
evm.memory += b"\x00" * extend_memory.expand_by
normalized_bytes_value = Bytes([value & 0xFF])
normalized_bytes_value = Bytes([value & U256(0xFF)])
memory_write(evm.memory, start_position, normalized_bytes_value)

# PROGRAM COUNTER
Expand Down
22 changes: 12 additions & 10 deletions src/ethereum/london/vm/precompiled_contracts/alt_bn128.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def alt_bn128_add(evm: Evm) -> None:

# OPERATION
x0_bytes = buffer_read(data, U256(0), U256(32))
x0_value = U256.from_be_bytes(x0_bytes)
x0_value = int(U256.from_be_bytes(x0_bytes))
y0_bytes = buffer_read(data, U256(32), U256(32))
y0_value = U256.from_be_bytes(y0_bytes)
y0_value = int(U256.from_be_bytes(y0_bytes))
x1_bytes = buffer_read(data, U256(64), U256(32))
x1_value = U256.from_be_bytes(x1_bytes)
x1_value = int(U256.from_be_bytes(x1_bytes))
y1_bytes = buffer_read(data, U256(96), U256(32))
y1_value = U256.from_be_bytes(y1_bytes)
y1_value = int(U256.from_be_bytes(y1_bytes))

for i in (x0_value, y0_value, x1_value, y1_value):
if i >= ALT_BN128_PRIME:
Expand Down Expand Up @@ -84,10 +84,10 @@ def alt_bn128_mul(evm: Evm) -> None:

# OPERATION
x0_bytes = buffer_read(data, U256(0), U256(32))
x0_value = U256.from_be_bytes(x0_bytes)
x0_value = int(U256.from_be_bytes(x0_bytes))
y0_bytes = buffer_read(data, U256(32), U256(32))
y0_value = U256.from_be_bytes(y0_bytes)
n = U256.from_be_bytes(buffer_read(data, U256(64), U256(32)))
y0_value = int(U256.from_be_bytes(y0_bytes))
n = int(U256.from_be_bytes(buffer_read(data, U256(64), U256(32))))

for i in (x0_value, y0_value):
if i >= ALT_BN128_PRIME:
Expand Down Expand Up @@ -124,12 +124,14 @@ def alt_bn128_pairing_check(evm: Evm) -> None:
for i in range(len(data) // 192):
values = []
for j in range(6):
value = U256.from_be_bytes(
data[i * 192 + 32 * j : i * 192 + 32 * (j + 1)]
value = int(
U256.from_be_bytes(
data[i * 192 + 32 * j : i * 192 + 32 * (j + 1)]
)
)
if value >= ALT_BN128_PRIME:
raise OutOfGasError
values.append(int(value))
values.append(value)

try:
p = BNP(BNF(values[0]), BNF(values[1]))
Expand Down
8 changes: 4 additions & 4 deletions src/ethereum/london/vm/precompiled_contracts/ecrecover.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def ecrecover(evm: Evm) -> None:
r = U256.from_be_bytes(buffer_read(data, U256(64), U256(32)))
s = U256.from_be_bytes(buffer_read(data, U256(96), U256(32)))

if v != 27 and v != 28:
if v != U256(27) and v != U256(28):
return
if 0 >= r or r >= SECP256K1N:
if U256(0) >= r or r >= SECP256K1N:
return
if 0 >= s or s >= SECP256K1N:
if U256(0) >= s or s >= SECP256K1N:
return

try:
public_key = secp256k1_recover(r, s, v - 27, message_hash)
public_key = secp256k1_recover(r, s, v - U256(27), message_hash)
except ValueError:
# unable to extract public key
return
Expand Down
4 changes: 2 additions & 2 deletions src/ethereum/london/vm/precompiled_contracts/modexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ def iterations(exponent_length: U256, exponent_head: Uint) -> Uint:
iterations : `Uint`
Number of iterations.
"""
if exponent_length <= 32 and exponent_head == 0:
if exponent_length <= U256(32) and exponent_head == U256(0):
count = Uint(0)
elif exponent_length <= 32:
elif exponent_length <= U256(32):
bit_length = Uint(exponent_head.bit_length())

if bit_length > Uint(0):
Expand Down

0 comments on commit d76940b

Please sign in to comment.