Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1785a46
chore(ci): update fill prepatched script.
spencer-tb Aug 21, 2025
674885a
refactor(eip7883): update vector input structure
LouisTsai-Csie Jul 21, 2025
d1566f9
feat: add eip7702, gas usage and extra edge cases
LouisTsai-Csie Jul 21, 2025
a8640f8
feat: add fork transition test
LouisTsai-Csie Jul 21, 2025
7a092b2
test: add extra invalid cases
LouisTsai-Csie Jul 22, 2025
6a66a1e
refactor(tests): Improve fixture and test descriptions for clarity
LouisTsai-Csie Jul 22, 2025
0c02660
refactor(tests): add helper for invalid case
LouisTsai-Csie Jul 23, 2025
1e6ecec
feat: add invalud boundary test cases
LouisTsai-Csie Jul 23, 2025
7c6cef8
chore: update boundary input case
LouisTsai-Csie Jul 29, 2025
e15e66c
refactor(tests): simplify boundary modexp test cases
LouisTsai-Csie Jul 30, 2025
418e90a
fix(tests): update fork transition test
LouisTsai-Csie Jul 30, 2025
cf3f302
feat(test): add gas formula egde cases
LouisTsai-Csie Jul 31, 2025
44fbe37
test: add extra casefor modexp invalid input
LouisTsai-Csie Jul 31, 2025
4bbcd5d
tests: port legacy modexp test
LouisTsai-Csie Aug 4, 2025
5d60873
refactor: update vector and data types
LouisTsai-Csie Aug 7, 2025
e22e1f8
refactor: remove valid case from invalid scenario
LouisTsai-Csie Aug 11, 2025
6f08447
refactor: update result comparison method and test case
LouisTsai-Csie Aug 12, 2025
689b61a
refactor: update comment
LouisTsai-Csie Aug 12, 2025
fcadcb5
refactor: update naming and new cases
LouisTsai-Csie Aug 21, 2025
8d0fc3e
refactor: update valid fork configuration
LouisTsai-Csie Aug 21, 2025
60cf012
chore(ci): try diff approach.
spencer-tb Aug 22, 2025
32bc967
chore
spencer-tb Aug 22, 2025
7d5769f
chore: set -e/+e.
spencer-tb Aug 22, 2025
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
19 changes: 9 additions & 10 deletions .github/scripts/fill_prepatched_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ echo "Select files that were changed and exist on the main branch:"
echo "$MODIFIED_DELETED_FILES"

rm -rf fixtures
rm -f filloutput.log

uv run fill $MODIFIED_DELETED_FILES --clean --until=$FILL_UNTIL --evm-bin evmone-t8n --block-gas-limit $BLOCK_GAS_LIMIT -m "state_test or blockchain_test" --output $BASE_TEST_PATH > >(tee -a filloutput.log) 2> >(tee -a filloutput.log >&2)

if grep -q "FAILURES" filloutput.log; then
echo "Error: failed to generate .py tests from before the PR."
exit 1
fi

if grep -q "ERROR collecting test session" filloutput.log; then
set +e
uv run fill $MODIFIED_DELETED_FILES --clean --until=$FILL_UNTIL --evm-bin evmone-t8n --block-gas-limit $BLOCK_GAS_LIMIT -m "state_test or blockchain_test" --output $BASE_TEST_PATH
FILL_RETURN_CODE=$?
set -e
if [ $FILL_RETURN_CODE -eq 5 ]; then
echo "any_modified_fixtures=false" >> "$GITHUB_OUTPUT"
exit 0
elif [ $FILL_RETURN_CODE -ne 0 ]; then
echo "Error: failed to generate .py tests from before the PR."
exit 1
exit $FILL_RETURN_CODE
fi

git checkout $PATCH_COMMIT
Expand Down
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]
Loading
Loading