Repository navigation
Expand file tree
/
Copy pathDecode.lean
More file actions
277 lines (253 loc) · 14.8 KB
/
Copy pathDecode.lean
File metadata and controls
277 lines (253 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import Fe.Sonatina.Hints
import Reasoning.Memory
/-!
# Decoding bytes fast in the kernel
The checker is decided by the kernel, which runs EVMLean's `parseInstr`, a match on the byte,
by comparing the byte with each pattern in turn, and reads the byte at an offset by walking the
code to it. `parseByte` reads the opcode from a table indexed by the byte's value instead
(`parseByte_eq`); `instrs` decodes the code in one pass, and `codeTree` keeps the instructions
in a search tree by offset, which the kernel builds once and reads in a few comparisons.
`codeTree_find`: what it finds is what `Hints.decodeAt` decodes.
-/
namespace Fe.Sonatina.Check
open Ethereum Ethereum.EVM
/-- The opcode of each byte, as `parseInstr` gives it. -/
def opTable : List (Option Operation) := [
some (.StopArith .STOP), some (.StopArith .ADD), some (.StopArith .MUL), some (.StopArith .SUB),
some (.StopArith .DIV), some (.StopArith .SDIV), some (.StopArith .MOD),
some (.StopArith .SMOD), some (.StopArith .ADDMOD), some (.StopArith .MULMOD),
some (.StopArith .EXP), some (.StopArith .SIGNEXTEND), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID), some (.CompBit .LT),
some (.CompBit .GT), some (.CompBit .SLT), some (.CompBit .SGT), some (.CompBit .EQ),
some (.CompBit .ISZERO), some (.CompBit .AND), some (.CompBit .OR), some (.CompBit .XOR),
some (.CompBit .NOT), some (.CompBit .BYTE), some (.CompBit .SHL), some (.CompBit .SHR),
some (.CompBit .SAR), some (.CompBit .CLZ), some (.System .INVALID),
some (.Keccak .KECCAK256), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.Env .ADDRESS), some (.Env .BALANCE), some (.Env .ORIGIN),
some (.Env .CALLER), some (.Env .CALLVALUE), some (.Env .CALLDATALOAD),
some (.Env .CALLDATASIZE), some (.Env .CALLDATACOPY), some (.Env .CODESIZE),
some (.Env .CODECOPY), some (.Env .GASPRICE), some (.Env .EXTCODESIZE),
some (.Env .EXTCODECOPY), some (.Env .RETURNDATASIZE), some (.Env .RETURNDATACOPY),
some (.Env .EXTCODEHASH), some (.Block .BLOCKHASH), some (.Block .COINBASE),
some (.Block .TIMESTAMP), some (.Block .NUMBER), some (.Block .PREVRANDAO),
some (.Block .GASLIMIT), some (.Block .CHAINID), some (.Block .SELFBALANCE),
some (.Block .BASEFEE), some (.Block .BLOBHASH), some (.Block .BLOBBASEFEE),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.StackMemFlow .POP),
some (.StackMemFlow .MLOAD), some (.StackMemFlow .MSTORE), some (.StackMemFlow .MSTORE8),
some (.StackMemFlow .SLOAD), some (.StackMemFlow .SSTORE), some (.StackMemFlow .JUMP),
some (.StackMemFlow .JUMPI), some (.StackMemFlow .PC), some (.StackMemFlow .MSIZE),
some (.StackMemFlow .GAS), some (.StackMemFlow .JUMPDEST), some (.StackMemFlow .TLOAD),
some (.StackMemFlow .TSTORE), some (.StackMemFlow .MCOPY), some (.Push .PUSH0),
some (.Push .PUSH1), some (.Push .PUSH2), some (.Push .PUSH3), some (.Push .PUSH4),
some (.Push .PUSH5), some (.Push .PUSH6), some (.Push .PUSH7), some (.Push .PUSH8),
some (.Push .PUSH9), some (.Push .PUSH10), some (.Push .PUSH11), some (.Push .PUSH12),
some (.Push .PUSH13), some (.Push .PUSH14), some (.Push .PUSH15), some (.Push .PUSH16),
some (.Push .PUSH17), some (.Push .PUSH18), some (.Push .PUSH19), some (.Push .PUSH20),
some (.Push .PUSH21), some (.Push .PUSH22), some (.Push .PUSH23), some (.Push .PUSH24),
some (.Push .PUSH25), some (.Push .PUSH26), some (.Push .PUSH27), some (.Push .PUSH28),
some (.Push .PUSH29), some (.Push .PUSH30), some (.Push .PUSH31), some (.Push .PUSH32),
some (.Dup .DUP1), some (.Dup .DUP2), some (.Dup .DUP3), some (.Dup .DUP4), some (.Dup .DUP5),
some (.Dup .DUP6), some (.Dup .DUP7), some (.Dup .DUP8), some (.Dup .DUP9), some (.Dup .DUP10),
some (.Dup .DUP11), some (.Dup .DUP12), some (.Dup .DUP13), some (.Dup .DUP14),
some (.Dup .DUP15), some (.Dup .DUP16), some (.Exchange .SWAP1), some (.Exchange .SWAP2),
some (.Exchange .SWAP3), some (.Exchange .SWAP4), some (.Exchange .SWAP5),
some (.Exchange .SWAP6), some (.Exchange .SWAP7), some (.Exchange .SWAP8),
some (.Exchange .SWAP9), some (.Exchange .SWAP10), some (.Exchange .SWAP11),
some (.Exchange .SWAP12), some (.Exchange .SWAP13), some (.Exchange .SWAP14),
some (.Exchange .SWAP15), some (.Exchange .SWAP16), some (.Log .LOG0), some (.Log .LOG1),
some (.Log .LOG2), some (.Log .LOG3), some (.Log .LOG4), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .CREATE), some (.System .CALL),
some (.System .CALLCODE), some (.System .RETURN), some (.System .DELEGATECALL),
some (.System .CREATE2), some (.System .INVALID), some (.System .INVALID),
some (.System .INVALID), some (.System .INVALID), some (.System .STATICCALL),
some (.System .INVALID), some (.System .INVALID), some (.System .REVERT),
some (.System .INVALID), some (.System .SELFDESTRUCT)
]
/-- `parseInstr`, read from `opTable`. -/
def parseByte (b : UInt8) : Option Operation := opTable.getD b.toNat none
set_option maxRecDepth 10000 in
theorem parseByte_eq : parseByte = parseInstr := by
funext b
have h : ∀ n < 256, parseByte (UInt8.ofNat n) = parseInstr (UInt8.ofNat n) := by decide +kernel
have := h b.toNat b.toNat_lt
rwa [UInt8.ofNat_toNat] at this
/-- The instructions of the bytes `bs`, which start at offset `i` of the code: each with its
offset, its opcode, its immediate as `Hints.decodeAt` gives it, and where the next starts.
At most `fuel` of them, in one pass over the bytes. -/
def instrs : Nat → List UInt8 → Nat → List (Nat × Operation × Option Nat × Nat)
| 0, _, _ => []
| _, [], _ => []
| fuel + 1, b :: bs, i => match parseByte b with
| none => []
| some op =>
let w := argOnNBytesOfInstr op
let arg := if w == 0 then (if op == .PUSH0 then some 0 else none)
else some (fromBytes' (bs.take w).reverse % UInt256.size)
(i, op, arg, i + 1 + w) :: instrs fuel (bs.drop w) (i + 1 + w)
/-- `instrs` with the instructions so far in an accumulator, which the compiled code runs in its
place (`instrs_eq_instrsTR`): a large section has more instructions than the stack frames
`instrs` would take. The kernel runs `instrs`. -/
def instrsTR (fuel : Nat) (bs : List UInt8) (i : Nat) :
List (Nat × Operation × Option Nat × Nat) :=
go fuel bs i #[]
where
go : Nat → List UInt8 → Nat → Array (Nat × Operation × Option Nat × Nat) →
List (Nat × Operation × Option Nat × Nat)
| 0, _, _, acc | _, [], _, acc => acc.toList
| fuel + 1, b :: bs, i, acc => match parseByte b with
| none => acc.toList
| some op =>
let w := argOnNBytesOfInstr op
let arg := if w == 0 then (if op == .PUSH0 then some 0 else none)
else some (fromBytes' (bs.take w).reverse % UInt256.size)
go fuel (bs.drop w) (i + 1 + w) (acc.push (i, op, arg, i + 1 + w))
theorem instrsTR_go : ∀ (fuel : Nat) (bs : List UInt8) (i : Nat) acc,
instrsTR.go fuel bs i acc = acc.toList ++ instrs fuel bs i
| 0, _, _, _ => by simp [instrsTR.go, instrs]
| _ + 1, [], _, _ => by simp [instrsTR.go, instrs]
| fuel + 1, b :: bs, i, acc => by
unfold instrsTR.go instrs
cases parseByte b with
| none => simp
| some op => simp [instrsTR_go fuel]
@[csimp] theorem instrs_eq_instrsTR : @instrs = @instrsTR := by
funext fuel bs i
simp [instrsTR, instrsTR_go]
/-- A search tree keyed by offsets. -/
inductive PcTree (α : Type) where
| leaf
| node (l : PcTree α) (pc : Nat) (v : α) (r : PcTree α)
/-- The tree of the first `n` entries of `xs`, balanced if they are sorted by offset, at most
`depth` deep. -/
def PcTree.build {α : Type} : Nat → List (Nat × α) → Nat → PcTree α
| 0, _, _ => .leaf
| depth + 1, xs, n => if n = 0 then .leaf else
match xs.drop (n / 2) with
| [] => .leaf
| (p, v) :: rest =>
.node (build depth (xs.take (n / 2)) (n / 2)) p v (build depth rest (n - n / 2 - 1))
def PcTree.find {α : Type} : PcTree α → Nat → Option α
| .leaf, _ => none
| .node l p v r, q => if q < p then l.find q else if q = p then some v else r.find q
/-- The code's instructions, in one pass. -/
def codeInstrs (code : ByteArray) : List (Nat × Operation × Option Nat × Nat) :=
instrs code.size code.data.toList 0
/-- The code's instructions by offset: the kernel reads one in a few comparisons, where
`decode` reads the byte at the offset by walking the code to it. -/
def codeTree (code : ByteArray) : PcTree (Operation × Option Nat × Nat) :=
.build 16 (codeInstrs code) (codeInstrs code).length
/-- The offsets of the code's `JUMPDEST`s. -/
def jumpdests (code : ByteArray) : List Nat :=
(codeInstrs code).filterMap fun (pc, op, _) => if op == .JUMPDEST then some pc else none
/-! ## The tree finds what `decodeAt` decodes -/
theorem PcTree.mem_of_find {α : Type} {q : Nat} {v : α} :
∀ {t : PcTree α} {xs : List (Nat × α)} {n d : Nat}, t = .build d xs n → t.find q = some v →
(q, v) ∈ xs
| .leaf, _, _, _, _, h => by simp [find] at h
| .node l p w r, xs, n, d, ht, h => by
cases d with
| zero => simp [build] at ht
| succ d =>
simp only [build] at ht
split at ht
· cases ht
split at ht
· cases ht
next p' w' rest hdrop =>
simp only [node.injEq] at ht
obtain ⟨hl, rfl, rfl, hr⟩ := ht
have hmem : (p, w) ∈ xs ∧ ∀ x ∈ rest, x ∈ xs := by
refine ⟨?_, fun x hx => ?_⟩
· exact List.mem_of_mem_drop (by rw [hdrop]; exact List.mem_cons_self ..)
· exact List.mem_of_mem_drop (by rw [hdrop]; exact List.mem_cons_of_mem _ hx)
simp only [find] at h
split_ifs at h with h1 h2
· exact List.mem_of_mem_take (mem_of_find hl h)
· cases h; subst h2; exact hmem.1
· exact hmem.2 _ (mem_of_find hr h)
theorem get?_drop (c : ByteArray) (i : Nat) : c.get? i = (c.data.toList.drop i).head? := by
rw [List.head?_drop, Array.getElem?_toList]
unfold ByteArray.get?
split
· next h => rw [Array.getElem?_eq_getElem h]; rfl
· next h => rw [Array.getElem?_eq_none (Nat.le_of_not_lt h)]
theorem extract'_toList (a : ByteArray) (b e : Nat) :
(a.extract' b e).data.toList = (a.data.toList.drop b).take (e - b) := by
unfold ByteArray.extract'
split
· rw [ByteArray.data_extract, Array.toList_extract, List.extract_eq_take_drop]
· show List.take (e - b) (List.drop b a.toList) = _
rw [Reasoning.Theory.byteArray_toList_eq]
/-- Each instruction of the one pass is the one `decodeAt` decodes at its offset. -/
theorem mem_instrs {c : ByteArray} (hc : c.size < UInt256.size) :
∀ (f i : Nat) (bs : List UInt8), bs = c.data.toList.drop i → ∀ pc d,
(pc, d) ∈ instrs f bs i → Hints.decodeAt c pc = some d
| 0, _, _, _, _, _, h => by simp [instrs] at h
| _ + 1, _, [], _, _, _, h => by simp [instrs] at h
| f + 1, i, b :: bs, hbs, pc, d, h => by
have hlen : c.data.toList.length = c.size := Array.length_toList
have hi : i < c.size := by
by_contra hn
rw [List.drop_eq_nil_of_le (by omega)] at hbs
cases hbs
have hrest : bs = c.data.toList.drop (i + 1) := by
rw [← List.drop_drop, ← hbs, List.drop_one, List.tail_cons]
unfold instrs at h
cases hop : parseByte b with
| none => simp only [hop] at h; cases h
| some op =>
simp only [hop] at h
rcases List.mem_cons.mp h with h | h
· simp only [Prod.mk.injEq] at h
obtain ⟨hp, rfl⟩ := h
rw [hp]
have hget : c.get? i = some b := by rw [get?_drop, ← hbs]; rfl
rw [parseByte_eq] at hop
have hpc : (UInt256.ofNat i).toNat = i := Nat.mod_eq_of_lt (lt_trans hi hc)
simp only [Hints.decodeAt, decode, hpc, hget, Option.bind_eq_bind, Option.bind_some, hop,
Option.map_some]
have hx : (c.extract' i.succ (i.succ + argOnNBytesOfInstr op)).data.toList =
bs.take (argOnNBytesOfInstr op) := by
rw [extract'_toList, ← hrest, Nat.succ_eq_add_one, Nat.add_sub_cancel_left]
by_cases hw : argOnNBytesOfInstr op = 0
· simp [hw]
· simp only [hw, beq_iff_eq, ite_false, Option.map_some, Option.orElse_some,
Option.getD_some, Option.some.injEq, Prod.mk.injEq, and_true, true_and]
show (fromBytes' (c.extract' _ _).data.toList.reverse) % UInt256.size = _
rw [hx]
· exact mem_instrs hc f _ _ (by rw [hrest, List.drop_drop]) pc d h
/-- An instruction the tree finds is the one `decodeAt` decodes. -/
theorem codeTree_find {c : ByteArray} {pc : Nat} {d : Operation × Option Nat × Nat}
(hc : c.size < UInt256.size) (h : (codeTree c).find pc = some d) :
Hints.decodeAt c pc = some d :=
mem_instrs hc _ 0 _ (by simp) pc d (PcTree.mem_of_find rfl h)
end Fe.Sonatina.Check