Skip to content

feat(tests): enhance eip7883 test coverage #1929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 161 additions & 68 deletions tests/osaka/eip7883_modexp_gas_increase/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,209 @@
import pytest

from ethereum_test_forks import Fork, Osaka
from ethereum_test_tools import Account, Address, Alloc, Storage, Transaction
from ethereum_test_tools import Account, Address, Alloc, Bytes, Storage, Transaction, keccak256
from ethereum_test_tools.vm.opcode import Opcodes as Op

from .helpers import Vector
from ...byzantium.eip198_modexp_precompile.helpers import ModExpInput
from .spec import Spec, Spec7883


@pytest.fixture
def gas_old() -> int | None:
"""Get old gas cost from the test vector if any."""
return None


@pytest.fixture
def gas_new() -> int | None:
"""Get new gas cost from the test vector if any."""
return None


@pytest.fixture
def call_opcode() -> Op:
"""Return default call used to call the precompile."""
"""Return call operation used to call the precompile."""
return Op.CALL


@pytest.fixture
def gas_measure_contract(pre: Alloc, call_opcode: Op, fork: Fork, vector: Vector) -> Address:
"""Deploys a contract that measures ModExp gas consumption."""
def call_contract_post_storage() -> Storage:
"""
Storage of the test contract after the transaction is executed.
Note: Fixture `call_contract_code` fills the actual expected storage values.
"""
return Storage()


@pytest.fixture
def call_succeeds() -> bool:
"""
By default, depending on the expected output, we can deduce if the call is expected to succeed
or fail.
"""
return True


@pytest.fixture
def gas_measure_contract(
pre: Alloc,
call_opcode: Op,
fork: Fork,
modexp_expected: bytes,
precompile_gas: int,
precompile_gas_modifier: int,
call_contract_post_storage: Storage,
call_succeeds: bool,
) -> Address:
"""
Deploys a contract that measures ModExp gas consumption and execution result.

Always stored:
storage[0]: precompile call success
storage[1]: return data length from precompile
Only if the precompile call succeeds:
storage[2]: gas consumed by precompile
storage[3]: hash of return data from precompile
"""
assert call_opcode in [Op.CALL, Op.CALLCODE, Op.DELEGATECALL, Op.STATICCALL]
value = [0] if call_opcode in [Op.CALL, Op.CALLCODE] else []

call_code = call_opcode(
address=Spec.MODEXP_ADDRESS,
value=0,
args_offset=0,
args_size=Op.CALLDATASIZE,
precompile_gas + precompile_gas_modifier,
Spec.MODEXP_ADDRESS,
*value,
0,
Op.CALLDATASIZE(),
0,
0,
)

gas_costs = fork.gas_costs()
extra_gas = (
gas_costs.G_WARM_ACCOUNT_ACCESS
+ (gas_costs.G_VERY_LOW * (len(call_opcode.kwargs) - 2)) # type: ignore
+ (gas_costs.G_BASE * 3)
+ (gas_costs.G_VERY_LOW * (len(call_opcode.kwargs) - 1)) # type: ignore
+ gas_costs.G_BASE # CALLDATASIZE
+ gas_costs.G_BASE # GAS
)
measure_code = (

# Build the gas measurement contract code
# Stack operations:
# [gas_start]
# [gas_start, call_result]
# [gas_start, call_result, gas_end]
# [gas_start, gas_end, call_result]
call_result_measurement = Op.GAS + call_code + Op.GAS + Op.SWAP1

# Calculate gas consumed: gas_start - (gas_end + extra_gas)
# Stack Operation:
# [gas_start, gas_end]
# [gas_start, gas_end, extra_gas]
# [gas_start, gas_end + extra_gas]
# [gas_end + extra_gas, gas_start]
# [gas_consumed]
gas_calculation = Op.PUSH2[extra_gas] + Op.ADD + Op.SWAP1 + Op.SUB

code = (
Op.CALLDATACOPY(dest_offset=0, offset=0, size=Op.CALLDATASIZE)
+ Op.GAS # [gas_start]
+ call_code # [gas_start, call_result]
+ Op.GAS # [gas_start, call_result, gas_end]
+ Op.SWAP1 # [gas_start, gas_end, call_result]
+ Op.PUSH1[0] # [gas_start, gas_end, call_result, 0]
+ Op.SSTORE # [gas_start, gas_end]
+ Op.PUSH2[extra_gas] # [gas_start, gas_end, extra_gas]
+ Op.ADD # [gas_start, gas_end + extra_gas]
+ Op.SWAP1 # [gas_end + extra_gas, gas_start]
+ Op.SUB # [gas_start - (gas_end + extra_gas)]
+ Op.PUSH1[1] # [gas_start - (gas_end + extra_gas), 1]
+ Op.SSTORE # []
+ Op.SSTORE(call_contract_post_storage.store_next(call_succeeds), call_result_measurement)
+ Op.SSTORE(
call_contract_post_storage.store_next(len(modexp_expected)),
Op.RETURNDATASIZE(),
)
)
measure_code += Op.SSTORE(2, Op.RETURNDATASIZE())
for i in range(len(vector.expected) // 32):
measure_code += Op.RETURNDATACOPY(0, i * 32, 32)
measure_code += Op.SSTORE(i + 3, Op.MLOAD(0))
measure_code += Op.STOP()
return pre.deploy_contract(measure_code)

if call_succeeds:
code += Op.SSTORE(call_contract_post_storage.store_next(precompile_gas), gas_calculation)
code += Op.RETURNDATACOPY(dest_offset=0, offset=0, size=Op.RETURNDATASIZE())
code += Op.SSTORE(
call_contract_post_storage.store_next(keccak256(Bytes(modexp_expected))),
Op.SHA3(0, Op.RETURNDATASIZE()),
)
return pre.deploy_contract(code)


@pytest.fixture
def precompile_gas(fork: Fork, vector: Vector) -> int:
def precompile_gas(
fork: Fork, modexp_input: ModExpInput, gas_old: int | None, gas_new: int | None
) -> int:
"""Calculate gas cost for the ModExp precompile and verify it matches expected gas."""
spec = Spec if fork < Osaka else Spec7883
expected_gas = vector.gas_old if fork < Osaka else vector.gas_new
calculated_gas = spec.calculate_gas_cost(
len(vector.input.base),
len(vector.input.modulus),
len(vector.input.exponent),
vector.input.exponent,
)
assert calculated_gas == expected_gas, (
f"Calculated gas {calculated_gas} != Vector gas {expected_gas}\n"
f"Lengths: base: {hex(len(vector.input.base))} ({len(vector.input.base)}), "
f"exponent: {hex(len(vector.input.exponent))} ({len(vector.input.exponent)}), "
f"modulus: {hex(len(vector.input.modulus))} ({len(vector.input.modulus)})\n"
f"Exponent: {vector.input.exponent} "
f"({int.from_bytes(vector.input.exponent, byteorder='big')})"
)
return calculated_gas
try:
calculated_gas = spec.calculate_gas_cost(
len(modexp_input.base),
len(modexp_input.modulus),
len(modexp_input.exponent),
modexp_input.exponent,
)
if gas_old is not None and gas_new is not None:
expected_gas = gas_old if fork < Osaka else gas_new
assert calculated_gas == expected_gas, (
f"Calculated gas {calculated_gas} != Vector gas {expected_gas}\n"
f"Lengths: base: {hex(len(modexp_input.base))} ({len(modexp_input.base)}), "
f"exponent: {hex(len(modexp_input.exponent))} ({len(modexp_input.exponent)}), "
f"modulus: {hex(len(modexp_input.modulus))} ({len(modexp_input.modulus)})\n"
f"Exponent: {modexp_input.exponent} "
f"({int.from_bytes(modexp_input.exponent, byteorder='big')})"
)
return calculated_gas
except Exception as e:
print(f"Warning: Error calculating gas, using minimum: {e}")
return 500 if fork >= Osaka else 200


@pytest.fixture
def precompile_gas_modifier() -> int:
"""Return the gas modifier for the ModExp precompile."""
return 0


@pytest.fixture
def tx(
fork: Fork,
pre: Alloc,
gas_measure_contract: Address,
vector: Vector,
precompile_gas: int,
modexp_input: ModExpInput,
tx_gas_limit: int,
) -> Transaction:
"""Transaction to measure gas consumption of the ModExp precompile."""
intrinsic_gas_cost_calc = fork.transaction_intrinsic_cost_calculator()
intrinsic_gas_cost = intrinsic_gas_cost_calc(calldata=vector.input)
memory_expansion_gas_calc = fork.memory_expansion_gas_calculator()
memory_expansion_gas = memory_expansion_gas_calc(new_bytes=len(bytes(vector.input)))
sstore_gas = fork.gas_costs().G_STORAGE_SET * (len(vector.expected) // 32)
return Transaction(
sender=pre.fund_eoa(),
to=gas_measure_contract,
data=vector.input,
gas_limit=intrinsic_gas_cost
data=bytes(modexp_input),
gas_limit=tx_gas_limit,
)


@pytest.fixture
def tx_gas_limit(
fork: Fork, modexp_expected: bytes, modexp_input: ModExpInput, precompile_gas: int
) -> int:
"""Transaction gas limit used for the test (Can be overridden in the test)."""
intrinsic_gas_cost_calculator = fork.transaction_intrinsic_cost_calculator()
memory_expansion_gas_calculator = fork.memory_expansion_gas_calculator()
sstore_gas = fork.gas_costs().G_STORAGE_SET * (len(modexp_expected) // 32)
extra_gas = 100_000

total_gas = (
extra_gas
+ intrinsic_gas_cost_calculator(calldata=bytes(modexp_input))
+ memory_expansion_gas_calculator(new_bytes=len(bytes(modexp_input)))
+ precompile_gas
+ memory_expansion_gas
+ sstore_gas
+ 100_000,
)

tx_gas_limit_cap = fork.transaction_gas_limit_cap()

if tx_gas_limit_cap is not None:
return min(tx_gas_limit_cap, total_gas)
return total_gas


@pytest.fixture
def post(
gas_measure_contract: Address,
precompile_gas: int,
vector: Vector,
call_contract_post_storage: Storage,
) -> Dict[Address, Account]:
"""Return expected post state with gas consumption check."""
storage = Storage()
storage[0] = 1
storage[1] = precompile_gas
storage[2] = len(vector.expected)
for i in range(len(vector.expected) // 32):
storage[i + 3] = vector.expected[i * 32 : (i + 1) * 32]
return {gas_measure_contract: Account(storage=storage)}
return {
gas_measure_contract: Account(storage=call_contract_post_storage),
}
56 changes: 37 additions & 19 deletions tests/osaka/eip7883_modexp_gas_increase/helpers.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
"""Helper functions for the EIP-7883 ModExp gas cost increase tests."""

import json
import os
from typing import Annotated, List

from pydantic import BaseModel, Field, PlainValidator
import pytest
from pydantic import BaseModel, ConfigDict, Field, PlainValidator, RootModel, TypeAdapter
from pydantic.alias_generators import to_pascal

from ethereum_test_tools import Bytes

from ...byzantium.eip198_modexp_precompile.helpers import ModExpInput


def current_python_script_directory(*args: str) -> str:
"""Get the current Python script directory."""
return os.path.join(os.path.dirname(os.path.realpath(__file__)), *args)


class Vector(BaseModel):
"""A vector for the ModExp gas cost increase tests."""

input: Annotated[ModExpInput, PlainValidator(ModExpInput.from_bytes)] = Field(
modexp_input: Annotated[ModExpInput, PlainValidator(ModExpInput.from_bytes)] = Field(
..., alias="Input"
)
expected: Bytes = Field(..., alias="Expected")
modexp_expected: Bytes = Field(..., alias="Expected")
name: str = Field(..., alias="Name")
gas_old: int | None = Field(..., alias="GasOld")
gas_new: int | None = Field(..., alias="GasNew")
gas_old: int | None = Field(default=None, alias="GasOld")
gas_new: int | None = Field(default=None, alias="GasNew")

@staticmethod
def from_json(vector_json: dict) -> "Vector":
"""Create a Vector from a JSON dictionary."""
return Vector.model_validate(vector_json)
model_config = ConfigDict(alias_generator=to_pascal)

@staticmethod
def from_file(filename: str) -> List["Vector"]:
"""Create a list of Vectors from a file."""
with open(current_python_script_directory(filename), "r") as f:
vectors_json = json.load(f)
return [Vector.from_json(vector_json) for vector_json in vectors_json]
def to_pytest_param(self):
"""Convert the test vector to a tuple that can be used as a parameter in a pytest test."""
return pytest.param(
self.modexp_input, self.modexp_expected, self.gas_old, self.gas_new, id=self.name
)


def current_python_script_directory(*args: str) -> str:
"""Get the current Python script directory."""
return os.path.join(os.path.dirname(os.path.realpath(__file__)), *args)
class VectorList(RootModel):
"""A list of test vectors for the ModExp gas cost increase tests."""

root: List[Vector]


VectorListAdapter = TypeAdapter(VectorList)


def vectors_from_file(filename: str) -> List:
"""Load test vectors from a file."""
with open(
current_python_script_directory(
"vector",
filename,
),
"rb",
) as f:
return [v.to_pytest_param() for v in VectorListAdapter.validate_json(f.read()).root]
14 changes: 14 additions & 0 deletions tests/osaka/eip7883_modexp_gas_increase/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from dataclasses import dataclass

from ...byzantium.eip198_modexp_precompile.helpers import ModExpInput


@dataclass(frozen=True)
class ReferenceSpec:
Expand Down Expand Up @@ -32,11 +34,23 @@ class Spec:
LARGE_BASE_MODULUS_MULTIPLIER = 1
MAX_LENGTH_THRESHOLD = 32
EXPONENT_BYTE_MULTIPLIER = 8
MAX_LENGTH_BYTES = 1024

WORD_SIZE = 8
EXPONENT_THRESHOLD = 32
GAS_DIVISOR = 3

# Arbitrary Test Constants
modexp_input = ModExpInput(
base="e8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c0001020304050607",
exponent="01ffffff",
modulus="f01681d2220bfea4bb888a5543db8c0916274ddb1ea93b144c042c01d8164c950001020304050607",
)
modexp_expected = bytes.fromhex(
"1abce71dc2205cce4eb6934397a88136f94641342e283cbcd30e929e85605c6718ed67f475192ffd"
)
modexp_error = bytes()

@classmethod
def calculate_multiplication_complexity(cls, base_length: int, modulus_length: int) -> int:
"""Calculate the multiplication complexity of the ModExp precompile."""
Expand Down
Loading
Loading