1
1
from dataclasses import dataclass
2
2
from eip3540 import ValidationException
3
3
4
+
4
5
@dataclass
5
6
class FunctionType :
6
7
inputs : int
7
8
outputs : int
8
9
10
+
9
11
MAGIC = b'\xEF \x00 '
10
12
VERSION = 0x01
11
13
S_TERMINATOR = 0x00
@@ -43,6 +45,7 @@ class FunctionType:
43
45
for opcode in range (0x60 , 0x7f + 1 ): # PUSH1..PUSH32
44
46
immediate_sizes [opcode ] = opcode - 0x60 + 1
45
47
48
+
46
49
# Validate EOF code.
47
50
# Raises ValidationException on invalid code
48
51
def validate_eof (code : bytes ):
@@ -115,6 +118,7 @@ def validate_eof(code: bytes):
115
118
if len (section_sizes [S_TYPE ]) > 0 and (code [pos ] != 0 or code [pos + 1 ] != 0 ):
116
119
raise ValidationException ("invalid type of section 0" )
117
120
121
+
118
122
# Validates any code
119
123
def is_valid_eof (code : bytes ) -> bool :
120
124
try :
@@ -123,6 +127,7 @@ def is_valid_eof(code: bytes) -> bool:
123
127
return False
124
128
return True
125
129
130
+
126
131
# Raises ValidationException on invalid code
127
132
def validate_code_section (func_id : int , code : bytes , types : list [FunctionType ] = [FunctionType (0 , 0 )]):
128
133
# Note that EOF1 already asserts this with the code section requirements
@@ -142,7 +147,7 @@ def validate_code_section(func_id: int, code: bytes, types: list[FunctionType] =
142
147
if opcode == 0x5c or opcode == 0x5d :
143
148
if pos + 2 > len (code ):
144
149
raise ValidationException ("truncated relative jump offset" )
145
- offset = int .from_bytes (code [pos :pos + 2 ], byteorder = "big" , signed = True )
150
+ offset = int .from_bytes (code [pos :pos + 2 ], byteorder = "big" , signed = True )
146
151
147
152
rjumpdest = pos + 2 + offset
148
153
if rjumpdest < 0 or rjumpdest >= len (code ):
@@ -152,22 +157,22 @@ def validate_code_section(func_id: int, code: bytes, types: list[FunctionType] =
152
157
elif opcode == 0xb0 :
153
158
if pos + 2 > len (code ):
154
159
raise ValidationException ("truncated CALLF immediate" )
155
- section_id = int .from_bytes (code [pos :pos + 2 ], byteorder = "big" , signed = False )
160
+ section_id = int .from_bytes (code [pos :pos + 2 ], byteorder = "big" , signed = False )
156
161
157
162
if section_id >= len (types ):
158
163
raise ValidationException ("invalid section id" )
159
164
elif opcode == 0xb2 :
160
165
if pos + 2 > len (code ):
161
166
raise ValidationException ("truncated JUMPF immediate" )
162
- section_id = int .from_bytes (code [pos :pos + 2 ], byteorder = "big" , signed = False )
167
+ section_id = int .from_bytes (code [pos :pos + 2 ], byteorder = "big" , signed = False )
163
168
164
169
if section_id >= len (types ):
165
170
raise ValidationException ("invalid section id" )
166
171
167
172
if types [section_id ].outputs != types [func_id ].outputs :
168
- raise ValidationException ("incompatible function type for JUMPF" )
173
+ raise ValidationException ("incompatible function type for JUMPF" )
169
174
170
- # Save immediate value positions
175
+ # Save immediate value positions
171
176
immediates .update (range (pos , pos + immediate_sizes [opcode ]))
172
177
# Skip immediates
173
178
pos += immediate_sizes [opcode ]
0 commit comments