-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution_state.v
319 lines (241 loc) · 8 KB
/
execution_state.v
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(**
This file includes definitions related to an execution state (concrete)
**)
Require Import bbv.Word.
Require Import FORVES2.constants.
Import Constants.
Require Import FORVES2.sha3.
Import SHA3.
Require Import List.
Import ListNotations.
Require Import Coq.Logic.FunctionalExtensionality.
Require Import Arith.
Require Import Nat.
Require Import Bool.
Require Import bbv.Word.
Require Import Coq.NArith.NArith.
Require Import List.
Import ListNotations.
Module ExecutionState.
(*** Execution State and its auxiliary data-structures ***)
(* Stack is a list of EVMWord *)
Definition stack : Type := list EVMWord.
Definition empty_stack : stack := [].
(*
Memory is a mapping from N to EVMByte. We do not keep its accessed
size, i.e., don't handle memory expansion, because we don't track gas
consumption. We also use an infinite memory (the domain is N), but
even if the domain is N, we will use addresses that can be represented
using EVMWords only.
*)
Definition memory : Type := N -> EVMByte.
Definition empty_memory : memory := fun _ => BZero.
(*
Storage is a function from N (key) to values EVMWord (value). Similar
to memory, we only use keys that can be represented using EVMWord. We
don't model the warm/cold properties since we don't track gas
consumption, etc.
*)
Definition storage : Type := N -> EVMWord.
Definition empty_storage : storage := fun _ => WZero.
(*
Externals is a structure that we use to encapsulates all
contract/blockchain information, and operations that we don't want to
implement such as KECCAK256 (correctness will be shown for any value
of such operations). The structure is immutable.
*)
Inductive code_info :=
| CodeInfo (size : nat) (content : word size) (hash : EVMWord).
Inductive block_info :=
| BlockInfo (size : nat) (content : word size) (timestamp: EVMWord) (hash : EVMWord).
Inductive sha3_info :=
| SHA3Info (f: sha3_op) (H_sha3: (sha3_op_assumptions f)).
Definition get_sha3_info_op (sha3 :sha3_info) :=
match sha3 with
| SHA3Info f _ => f
end.
Inductive chunk :=
| Chunk (size : nat) (content : word size).
Inductive externals :=
| Exts
(address : EVMAddr)
(balance : EVMAddr -> EVMWord)
(origin : EVMAddr)
(caller : EVMAddr)
(callvalue : EVMWord)
(data: chunk)
(code : EVMAddr -> code_info )
(gasprice : EVMWord)
(outdata: chunk)
(blocks : EVMWord -> block_info)
(miner : EVMAddr)
(currblock : EVMWord)
(gaslimit : EVMWord)
(chainid : EVMWord)
(basefee : EVMWord)
(keccak256 : sha3_info)
(tags : N -> N -> EVMWord)
(_extra_2 : nat)
(_extra_3 : nat)
(_extra_4 : nat)
(_extra_5 : nat).
(*
The empty_externals will be used only for the purpose of testing the
concrete interpreter. It will not be used in any theorem/lemma/etc.
*)
Definition empty_externals : externals :=
Exts
AZero (* (address : EVMAddr) *)
(fun _ => WZero) (* (balance : EVMAddr -> EVMWord) *)
AZero (* (origin : EVMAddr) *)
AZero (* (caller : EVMAddr) *)
WZero (* (callvalue : EVMWord) *)
(Chunk 0 WO) (* (data: chunk) *)
(fun _ => CodeInfo 0 WO WZero) (* (code : EVMAddr -> code_info ) *)
WZero (* (gasprice : EVMWord) *)
(Chunk 0 WO) (* (outdata: chunk) *)
(fun _ => BlockInfo 0 WO WZero WZero) (* (blocks : EVMWord -> block_info) *)
AZero (* (miner : EVMAddr) *)
WZero (* (currblock : EVMWord) *)
WZero (* (gaslimit : EVMWord) *)
WZero (* (chainid : EVMWord) *)
WZero (* (basefee : EVMWord) *)
(SHA3Info dummy_sha3 dummy_sha3_assumptions)
(fun cat v => (NToWord EVMWordSize (cat + v))) (* tags: N -> EVMWord *)
0 (* (_extra_2 : nat) *)
0 (* (_extra_3 : nat) *)
0 (* (_extra_4 : nat) *)
0. (* (_extra_5 : nat) *)
(* Exts _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ *)
Definition get_address_exts (c : externals) :=
match c with
| Exts x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_balance_exts (c : externals) :=
match c with
| Exts _ x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_origin_exts (c : externals) :=
match c with
| Exts _ _ x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_caller_exts (c : externals) :=
match c with
| Exts _ _ _ x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_callvalue_exts (c : externals) :=
match c with
| Exts _ _ _ _ x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_data_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_code_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ x _ _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_gasprice_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ x _ _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_outdata_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ x _ _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_blocks_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ x _ _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_miner_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ _ x _ _ _ _ _ _ _ _ _ _ => x
end.
Definition get_currblock_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ _ _ x _ _ _ _ _ _ _ _ _ => x
end.
Definition get_gaslimit_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ _ _ _ x _ _ _ _ _ _ _ _ => x
end.
Definition get_chainid_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ _ _ _ _ x _ _ _ _ _ _ _ => x
end.
Definition get_basefee_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ _ _ _ _ _ x _ _ _ _ _ _ => x
end.
Definition get_keccak256_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ x _ _ _ _ _ => x
end.
Definition get_tags_exts (c : externals) :=
match c with
| Exts _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ x _ _ _ _ => x
end.
(*
An execution state consists of a stack, memory, storage and externals.
*)
Inductive state :=
| ExState (stk: stack) (mem: memory) (strg: storage) (exts :externals).
Definition make_st (stk: stack) (mem: memory) (strg: storage) (exts : externals) : state :=
ExState stk mem strg exts.
(*
The empty state is used just for the sake of testing the concrete
interpreter, it will no be used in any theorem/lemma/etc.
*)
Definition empty_state := make_st empty_stack empty_memory
empty_storage empty_externals.
Definition get_stack_st (st: state) : stack :=
match st with
| ExState stk _ _ _ => stk
end.
Definition set_stack_st (st: state) (stk: stack) : state :=
match st with
| ExState _ mem strg exts => ExState stk mem strg exts
end.
Definition get_memory_st (st: state) : memory :=
match st with
| ExState _ mem _ _=> mem
end.
Definition set_memory_st (st: state) (mem: memory) : state :=
match st with
| ExState stk _ strg exts => ExState stk mem strg exts
end.
Definition get_storage_st (st: state) : storage :=
match st with
| ExState _ _ strg _ => strg
end.
Definition set_store_st (st: state) (strg: storage) : state :=
match st with
| ExState stk mem _ exts => ExState stk mem strg exts
end.
Definition get_externals_st (st: state) : externals :=
match st with
| ExState _ _ _ exts => exts
end.
Definition set_externals_st (st: state) (exts: externals) : state :=
match st with
| ExState stk mem strg _ => ExState stk mem strg exts
end.
(*
When two state are equivalent. It is not simply equivalence of
terms because memory and storage are functions, so we need functional
equivalence as well -- see execution_state_facts.
*)
Definition eq_stack (stk1 stk2: stack) : Prop :=
stk1 = stk2.
Definition eq_memory (mem1 mem2: memory) : Prop :=
forall w, mem1 w = mem2 w.
Definition eq_storage (strg1 strg2: storage) : Prop :=
forall w, strg1 w = strg2 w.
Definition eq_externals (exts1 exts2: externals) : Prop :=
exts1 = exts2.
Definition eq_execution_states (st1 st2: state) : Prop :=
eq_stack (get_stack_st st1) (get_stack_st st2) /\
eq_memory (get_memory_st st1) (get_memory_st st2) /\
eq_storage (get_storage_st st1) (get_storage_st st2) /\
eq_externals (get_externals_st st1) (get_externals_st st2).
End ExecutionState.