Skip to content

Commit 197625e

Browse files
refactor(tests): add helper for invalid case
1 parent 8e1a260 commit 197625e

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

tests/osaka/eip7883_modexp_gas_increase/test_modexp_thresholds.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,64 +43,75 @@ def test_vectors_from_file(
4343
)
4444

4545

46-
@pytest.mark.parametrize(
47-
"modexp_input,modexp_expected,call_succeeds",
48-
[
46+
def create_modexp_input(
47+
bsize: int, esize: int, msize: int, e_data: str = "", m_data: str = "", b_data: str = ""
48+
) -> bytes:
49+
"""
50+
Create ModExp input data with specified sizes and data.
51+
52+
Args:
53+
bsize: Base size in bytes
54+
esize: Exponent size in bytes
55+
msize: Modulus size in bytes
56+
e_data: Exponent data (hex string, if not provided, will be padded with FF)
57+
m_data: Modulus data (hex string, if not provided, will be padded with FF)
58+
b_data: Base data (hex string, if not provided, will be padded with FF)
59+
60+
Returns:
61+
ModExp input as bytes
62+
63+
"""
64+
e_padded = "FF" * esize if not e_data else e_data
65+
m_padded = "FF" * msize if not m_data else m_data
66+
b_padded = "FF" * bsize if not b_data else b_data
67+
68+
# Format sizes as 32-byte hex strings
69+
bsize_hex = format(bsize, "032x")
70+
esize_hex = format(esize, "032x")
71+
msize_hex = format(msize, "032x")
72+
73+
# Concatenate all parts
74+
input_hex = bsize_hex + esize_hex + msize_hex + e_padded + m_padded + b_padded
75+
return bytes.fromhex(input_hex)
76+
77+
78+
def generate_invalid_inputs_cases():
79+
"""Generate test cases for invalid ModExp inputs."""
80+
return [
4981
pytest.param(bytes(), bytes(), False, id="zero-length-calldata"),
5082
pytest.param(
51-
bytes.fromhex(
52-
format(10, "032x") # Bsize
53-
+ format(11, "032x") # Esize
54-
+ format(12, "032x") # Msize
55-
+ "FF" * 9 # E
56-
+ "FF" * 11 # M
57-
+ "FF" * 12 # B
58-
),
83+
create_modexp_input(10, 11, 12, b_data="FF" * 9),
5984
bytes(),
6085
False,
6186
id="b-too-short",
6287
),
6388
pytest.param(
64-
bytes.fromhex(
65-
format(10, "032x") # Bsize
66-
+ format(11, "032x") # Esize
67-
+ format(12, "032x") # Msize
68-
+ "FF" * 10 # E
69-
+ "FF" * 10 # M
70-
+ "FF" * 12 # B
71-
),
89+
create_modexp_input(10, 11, 12, m_data="FF" * 10),
7290
bytes(),
7391
False,
7492
id="m-too-short",
7593
),
7694
pytest.param(
77-
bytes.fromhex(
78-
format(10, "032x") # Bsize
79-
+ format(11, "032x") # Esize
80-
+ format(12, "032x") # Msize
81-
+ "FF" * 10 # E
82-
+ "FF" * 11 # M
83-
+ "FF" * 11 # B
84-
),
95+
create_modexp_input(10, 11, 12, e_data="FF" * 11),
8596
bytes(),
8697
False,
8798
id="e-too-short",
8899
),
89-
# Not sure if this is valid
90100
pytest.param(
91-
bytes.fromhex(
92-
format(0, "032x") # Bsize
93-
+ format(0, "032x") # Esize
94-
+ format(0, "032x") # Msize
95-
),
101+
create_modexp_input(0, 0, 0),
96102
bytes(),
97103
False,
98104
id="all-zeros",
99105
),
100-
],
106+
]
107+
108+
109+
@pytest.mark.parametrize(
110+
"modexp_input,modexp_expected,call_succeeds",
111+
generate_invalid_inputs_cases(),
101112
)
102113
@EIPChecklist.Precompile.Test.Inputs.AllZeros
103-
def test_modexp_invalid(
114+
def test_modexp_invalid_inputs(
104115
state_test: StateTestFiller,
105116
pre: Alloc,
106117
tx: Transaction,

0 commit comments

Comments
 (0)