Skip to content

Commit 87771b0

Browse files
authored
fix(tests): Fix ModExpInput.from_bytes (#1999)
* fix(tests): ModExpInput parser * fix(tests): Fix guido-4-even * fix: Print more info on modexp gas calc mismatch * fix: Tox
1 parent 2b6e0f9 commit 87771b0

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

tests/byzantium/eip198_modexp_precompile/helpers.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ModExpInput(TestParameterGroup):
2323
exponent: Bytes
2424
modulus: Bytes
2525
extra_data: Bytes = Field(default_factory=Bytes)
26+
raw_input: Bytes | None = None
2627

2728
@property
2829
def length_base(self) -> Bytes:
@@ -41,6 +42,8 @@ def length_modulus(self) -> Bytes:
4142

4243
def __bytes__(self):
4344
"""Generate input for the MODEXP precompile."""
45+
if self.raw_input is not None:
46+
return self.raw_input
4447
return (
4548
self.length_base
4649
+ self.length_exponent
@@ -60,20 +63,28 @@ def from_bytes(cls, input_data: Bytes | str) -> "ModExpInput":
6063
"""
6164
if isinstance(input_data, str):
6265
input_data = Bytes(input_data)
63-
base_length = int.from_bytes(input_data[0:32], byteorder="big")
64-
exponent_length = int.from_bytes(input_data[32:64], byteorder="big")
65-
modulus_length = int.from_bytes(input_data[64:96], byteorder="big")
66+
assert not isinstance(input_data, str)
67+
padded_input_data = input_data
68+
if len(padded_input_data) < 96:
69+
padded_input_data = Bytes(padded_input_data.ljust(96, b"\0"))
70+
base_length = int.from_bytes(padded_input_data[0:32], byteorder="big")
71+
exponent_length = int.from_bytes(padded_input_data[32:64], byteorder="big")
72+
modulus_length = int.from_bytes(padded_input_data[64:96], byteorder="big")
73+
74+
total_required_length = 96 + base_length + exponent_length + modulus_length
75+
if len(padded_input_data) < total_required_length:
76+
padded_input_data = Bytes(padded_input_data.ljust(total_required_length, b"\0"))
6677

6778
current_index = 96
68-
base = input_data[current_index : current_index + base_length]
79+
base = padded_input_data[current_index : current_index + base_length]
6980
current_index += base_length
7081

71-
exponent = input_data[current_index : current_index + exponent_length]
82+
exponent = padded_input_data[current_index : current_index + exponent_length]
7283
current_index += exponent_length
7384

74-
modulus = input_data[current_index : current_index + modulus_length]
85+
modulus = padded_input_data[current_index : current_index + modulus_length]
7586

76-
return cls(base=base, exponent=exponent, modulus=modulus)
87+
return cls(base=base, exponent=exponent, modulus=modulus, raw_input=input_data)
7788

7889

7990
class ModExpOutput(TestParameterGroup):

tests/osaka/eip7883_modexp_gas_increase/conftest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ def precompile_gas(fork: Fork, vector: Vector) -> int:
6868
vector.input.exponent,
6969
)
7070
assert calculated_gas == expected_gas, (
71-
f"Calculated gas {calculated_gas} != Vector gas {expected_gas}"
71+
f"Calculated gas {calculated_gas} != Vector gas {expected_gas}\n"
72+
f"Lengths: base: {hex(len(vector.input.base))} ({len(vector.input.base)}), "
73+
f"exponent: {hex(len(vector.input.exponent))} ({len(vector.input.exponent)}), "
74+
f"modulus: {hex(len(vector.input.modulus))} ({len(vector.input.modulus)})\n"
75+
f"Exponent: {vector.input.exponent} "
76+
f"({int.from_bytes(vector.input.exponent, byteorder='big')})"
7277
)
7378
return calculated_gas
7479

tests/osaka/eip7883_modexp_gas_increase/vectors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
},
142142
{
143143
"Input": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000181000000000000000000000000000000000000000000000000000000000000000801ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2cffffffffffffffffffffffffffffffffffffffffffffffffffffffff3b10000000006c01ffffffffffffffffffffffffffffffffffffffffffffffdffffb97ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3bffffffffffffffffffffffffffffffffffffffffffffffffffffffffebafd93b37ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc5bb6affffffff3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2a",
144-
"Expected": "0001",
144+
"Expected": "0001000000000000",
145145
"Name": "guido-4-even",
146146
"GasOld": 1026,
147147
"GasNew": 94448

0 commit comments

Comments
 (0)