Skip to content

Commit 3afee13

Browse files
committed
reference implementation
1 parent d6e0255 commit 3afee13

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

spec/eofv0_verkle.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,69 @@ During execution of a jump two checks must be done in this order:
159159
It is possible to reconstruct sparse account code prior to execution with all the submitted chunks of the transaction
160160
and perform `JUMPDEST`-validation to build up a relevant *valid `JUMPDEST` locations* map instead.
161161

162-
#### Analysis
162+
#### Reference encoding implementation
163+
164+
```python
165+
class Scheme:
166+
VALUE_MAX = 32
167+
VALUE_WIDTH = VALUE_MAX.bit_length()
168+
VALUE_MOD = VALUE_MAX + 1
169+
170+
def __init__(self, name: str, width: int):
171+
self.name = name
172+
self.WIDTH = width
173+
174+
payload_max = 2 ** (width - 1) - 1
175+
176+
self.SKIP_ONLY = 1 << (self.WIDTH - 1)
177+
self.VALUE_SKIP_MAX = (payload_max - self.VALUE_MAX) // self.VALUE_MOD
178+
self.SKIP_BIAS = self.VALUE_SKIP_MAX + 1
179+
180+
def encode(self, chunks: list[Chunk]) -> tuple[list[int], int]:
181+
skip_only_max = self.SKIP_ONLY - 1
182+
183+
ops = []
184+
last_chunk_index = 0
185+
for i, ch in enumerate(chunks):
186+
if not ch.contains_invalid_jumpdest:
187+
continue # skip chunks without invalid jumpdests
188+
189+
delta = i - last_chunk_index
190+
191+
# Generate skips if needed.
192+
while delta > self.VALUE_SKIP_MAX:
193+
d = min(delta - self.SKIP_BIAS, skip_only_max)
194+
assert 0 <= d <= skip_only_max
195+
ops.append(self.SKIP_ONLY | d)
196+
delta -= d + self.SKIP_BIAS
197+
198+
assert 0 <= delta <= self.VALUE_SKIP_MAX
199+
assert 0 <= ch.first_instruction_offset <= 32
200+
ops.append(delta * self.VALUE_MOD + ch.first_instruction_offset)
201+
202+
last_chunk_index = i
203+
204+
return ops, self.WIDTH * len(ops)
205+
206+
def decode(self, ops: list[int]) -> dict[int, int]:
207+
m = dict()
208+
i = 0
209+
for op in ops:
210+
if op & self.SKIP_ONLY:
211+
delta = (op ^ self.SKIP_ONLY) + self.SKIP_BIAS
212+
value = None
213+
else:
214+
delta = op // self.VALUE_MOD
215+
value = op % self.VALUE_MOD
216+
i += delta
217+
print(f"{delta:+4}")
218+
if value is not None:
219+
m[i] = value
220+
print(f"{i:4}: {value}")
221+
return m
222+
```
223+
224+
#### Example
163225

164226
We have analyzed two contracts, Arbitrum validator and Uniswap router.
165227

0 commit comments

Comments
 (0)