std:EVM/Model
lib/EVM/Model.omg
lib/EVM/Model.omg485 lines
1{- The reference model of the EVM signature (docs/evm.md, section 7.2).
2
3 It gives `std:EVM/Signature` its meaning over exact naturals: the same
4 operations the compiler implements, run on a world instead of staged
5 into code, so that a law about an interpretation of the signature
6 (`Base.cps (ops frame)`) is checked here before it is trusted of the
7 compiled contract.
8
9 Executions use native `Word 256` values and `Bool` flags. The external
10 testing interface exposes words as `Natural`s. The
11 world is the contract's storage, keyed structurally by `Key Natural`
12 (no hashing), its transient storage, the storage of the other
13 accounts, the logs emitted so far and the return data of the last
14 call. An execution is a function of the world to how it halts: it
15 returns its data and the new world, or it reverts with its data, and
16 then its storage writes and logs are gone.
17
18 A call is answered by the frame's `callee`, which the test supplies: a
19 mock, or other contracts run in the model (`contract`), each on its
20 own storage among the accounts.
21
22 The model runs the very term the compiler compiles: an entry point at
23 the base `std:EVM/Base.cps (ops frame)`. Storage is the key-value map
24 of the signature; a contract's own state shape belongs to its layout,
25 which the caller has already read and written.
26-}
27
28let L = std:List
29
30let N = std:Natural
31
32let Signature = std:EVM/Signature
33
34let Base = std:EVM/Base
35
36let Key = Signature.Key Natural
37
38-- A log: the address of the contract that emitted it, its topics, and its
39-- data as bytes. The data of a halt are bytes too.
40let RawLog = { address : Natural, topics : List Natural, data : List Natural }
41
42let Storage = Key -> Natural
43
44-- The storage of every other account, by address.
45let Accounts = Natural -> Storage
46
47let World =
48 { storage : Storage
49 , transient : Natural -> Natural
50 , accounts : Accounts
51 , logs : List RawLog
52 , returndata : List Natural
53 }
54
55let Halt = < Returned : { data : List Natural, world : World } | Reverted : List Natural >
56
57let Code = World -> Halt
58
59-- A call to another account, and how it ends: whether it succeeded, its
60-- return (or revert) data, and the accounts and the logs after it (as
61-- before it when it failed). A call runs in the block of its caller: it
62-- carries the block's timestamp.
63let Request =
64 { from : Natural
65 , to : Natural
66 , value : Natural
67 , input : List Natural
68 , static : Bool
69 , timestamp : Natural
70 }
71
72let Answer = { ok : Bool, output : List Natural, accounts : Accounts, logs : List RawLog }
73
74let Callee = Request -> Accounts -> List RawLog -> Answer
75
76-- Every account without code: a call succeeds and returns nothing.
77let noCode
78 : Callee
79 = \(_ : Request) (accounts : Accounts) (logs : List RawLog) ->
80 { ok = True, output = ([] : List Natural), accounts, logs }
81
82-- The frame of a call: who calls, the contract's address, with what
83-- value, the call data (as bytes), the block's timestamp, the immutables
84-- of the code, the oracle of the hashes of texts (selectors, topics and
85-- error signatures: the caller supplies real Keccak-256 hashes, or any
86-- stand-in it likes), and the other accounts' answers to calls.
87let Frame =
88 { caller : Natural
89 , address : Natural
90 , callvalue : Natural
91 , calldata : List Natural
92 , timestamp : Natural
93 , immutables : List Natural
94 , names : Text -> Natural
95 , callee : Callee
96 }
97
98-- The 32 bytes of a word, big-endian: the word is reduced to 256 bits
99-- first, as the EVM's register is.
100let wordBytes
101 : Natural -> List Natural
102 = \w ->
103 let v = Word/toNatural (Word/fromNatural 256 w)
104 in L.generate
105 32
106 (\(i : Natural) ->
107 Natural/mod (Natural/div v (N.pow2 (8 * Natural/subtract (i + 1) 32))) 256)
108
109-- The bytes of data: each chunk's first bytes, in order.
110let bytesOf
111 : List (Signature.Chunk Natural) -> List Natural
112 = L.concatMap (\(c : Signature.Chunk Natural) -> L.take c.size (wordBytes c.word))
113
114-- The bytes of pieces of data: a copy of call data is the bytes from its
115-- offset (zero beyond the end), then zeros up to a multiple of 32 bytes.
116let piecesOf
117 : List Natural -> List (Signature.Piece Natural) -> List Natural
118 = \calldata ->
119 L.concatMap
120 (\(p : Signature.Piece Natural) ->
121 merge
122 { Chunk = \(c : Signature.Chunk Natural) -> bytesOf [ c ]
123 , Copy =
124 \(r : { offset : Natural, size : Natural }) ->
125 let present = L.take r.size (L.drop r.offset calldata)
126 in present
127 # L.replicate (Natural/subtract (L.length present) r.size) 0
128 # L.replicate
129 (Natural/mod (Natural/subtract (Natural/mod r.size 32) 32) 32)
130 0
131 }
132 p)
133
134-- Bytes as words, 32 at a time (the last one padded with zeros).
135let wordsOf
136 : List Natural -> List Natural
137 = \bs ->
138 L.generate
139 (Natural/div (L.length bs + 31) 32)
140 (\(i : Natural) ->
141 L.foldLeft
142 (\(w : Natural) (b : Natural) -> w * 256 + b)
143 0
144 (L.take 32 (L.drop (32 * i) bs # L.replicate 32 0)))
145
146-- Keys are equal when they are of one form with equal fields, a direct
147-- index modulo 2^bits (the slot the compiler gives it).
148let Parts = { domain : Natural, parts : List Natural }
149
150let Direct = { base : Natural, bits : Natural, index : Natural }
151
152let keyEqual
153 : Key -> Key -> Bool
154 = \a b ->
155 let no = \(t : Type) (_ : t) -> False
156 let sameParts =
157 \(p : Parts) (q : Parts) ->
158 N.equal p.domain q.domain && L.equal N.equal p.parts q.parts
159 in merge
160 { Fixed =
161 \(n : Natural) ->
162 merge
163 { Fixed = N.equal n
164 , Direct = no Direct
165 , Hashed = no Parts
166 , Solidity = no Parts
167 }
168 b
169 , Direct =
170 \(d : Direct) ->
171 merge
172 { Fixed = no Natural
173 , Direct =
174 \(e : Direct) ->
175 N.equal d.base e.base
176 && N.equal d.bits e.bits
177 && N.equal
178 (Natural/mod d.index (N.pow2 d.bits))
179 (Natural/mod e.index (N.pow2 e.bits))
180 , Hashed = no Parts
181 , Solidity = no Parts
182 }
183 b
184 , Hashed =
185 \(h : Parts) ->
186 merge
187 { Fixed = no Natural
188 , Direct = no Direct
189 , Hashed = sameParts h
190 , Solidity = no Parts
191 }
192 b
193 , Solidity =
194 \(h : Parts) ->
195 merge
196 { Fixed = no Natural
197 , Direct = no Direct
198 , Hashed = no Parts
199 , Solidity = sameParts h
200 }
201 b
202 }
203 a
204
205-- CALLDATALOAD: the 32 bytes from the offset, big-endian, zero beyond the
206-- end of the call data.
207let cdload
208 : List Natural -> Natural -> Natural
209 = \calldata offset ->
210 let present = L.take 32 (L.drop offset calldata)
211 let padded = present # L.replicate (Natural/subtract (L.length present) 32) 0
212 in L.foldLeft (\(word : Natural) (byte : Natural) -> word * 256 + byte) 0 padded
213
214-- Interpret the effectful signature once, independently of the concrete
215-- representation of run-time words. The boundary conversions keep the
216-- testing API (states, call data and mocks) in Natural; calculations between
217-- effects stay entirely in Val.
218let opsWith
219 : forall {Val : Type}
220 -> Signature.Words Val Bool
221 -> (Natural -> Val)
222 -> (Val -> Natural)
223 -> Frame
224 -> Signature.Ops Val Bool Code
225 = \{Val} w literal value frame ->
226 let key
227 : Signature.Key Val -> Key
228 = \k ->
229 merge
230 { Fixed = Key.Fixed
231 , Direct =
232 \(d : { base : Natural, bits : Natural, index : Val }) ->
233 Key.Direct { base = d.base, bits = d.bits, index = value d.index }
234 , Hashed =
235 \(h : { domain : Natural, parts : List Val }) ->
236 Key.Hashed { domain = h.domain, parts = L.map value h.parts }
237 , Solidity =
238 \(h : { domain : Natural, parts : List Val }) ->
239 Key.Solidity { domain = h.domain, parts = L.map value h.parts }
240 }
241 k
242 let chunk = \(c : Signature.Chunk Val) -> { size = c.size, word = value c.word }
243 let chunks = L.map chunk
244 let piece
245 : Signature.Piece Val -> Signature.Piece Natural
246 = \p ->
247 merge
248 { Chunk = \(c : Signature.Chunk Val) -> (Signature.Piece Natural).Chunk (chunk c)
249 , Copy =
250 \(c : { offset : Val, size : Val }) ->
251 (Signature.Piece Natural).Copy
252 { offset = value c.offset, size = value c.size }
253 }
254 p
255 let answer =
256 \(request : Request) (k : Bool -> Code) (world : World) ->
257 let a = frame.callee request world.accounts world.logs
258 in k
259 a.ok
260 { storage = world.storage
261 , transient = world.transient
262 , accounts = a.accounts
263 , logs = a.logs
264 , returndata = a.output
265 }
266 let Case = Signature.Case Val Code
267 let Iterate = Natural -> List Val -> Code
268 in { words = w
269 , keccakText = \(t : Text) -> w.lit (frame.names t)
270 , caller = literal frame.caller
271 , address = literal frame.address
272 , callvalue = literal frame.callvalue
273 , cdsize = literal (L.length frame.calldata)
274 , cdload = \(offset : Val) -> literal (cdload frame.calldata (value offset))
275 , timestamp = literal frame.timestamp
276 , immutable =
277 \(n : Natural) -> literal (std:Optional.default 0 (L.index n frame.immutables))
278 , sload =
279 \(slot : Signature.Key Val) (k : Val -> Code) (world : World) ->
280 k (literal (world.storage (key slot))) world
281 , sstore =
282 \(slot : Signature.Key Val) (v : Val) (k : Code) (world : World) ->
283 k
284 (world with storage =
285 (\(other : Key) ->
286 if keyEqual other (key slot) then value v else world.storage other))
287 , tload =
288 \(slot : Val) (k : Val -> Code) (world : World) ->
289 k (literal (world.transient (value slot))) world
290 , tstore =
291 \(slot : Val) (v : Val) (k : Code) (world : World) ->
292 k
293 (world with transient =
294 (\(other : Natural) ->
295 if N.equal other (value slot) then value v else world.transient other))
296 , log =
297 \(topics : List Val)
298 (data : List (Signature.Chunk Val))
299 (k : Code)
300 (world : World) ->
301 k
302 (world with logs =
303 world.logs
304 # [ { address = frame.address
305 , topics = L.map value topics
306 , data = bytesOf (chunks data)
307 }
308 ])
309 , call =
310 \(req : Signature.Call Val) ->
311 answer
312 { from = frame.address
313 , to = value req.to
314 , value = value req.value
315 , input = bytesOf (chunks req.input)
316 , static = False
317 , timestamp = frame.timestamp
318 }
319 , staticcall =
320 \(req : Signature.StaticCall Val) ->
321 answer
322 { from = frame.address
323 , to = value req.to
324 , value = 0
325 , input = bytesOf (chunks req.input)
326 , static = True
327 , timestamp = frame.timestamp
328 }
329 , rdsize =
330 \(k : Val -> Code) (world : World) -> k (literal (L.length world.returndata)) world
331 , rdload =
332 \(offset : Val) (k : Val -> Code) (world : World) ->
333 k (literal (cdload world.returndata (value offset))) world
334 , branch = \(c : Bool) (x : Code) (y : Code) -> if c then x else y
335 , switch =
336 \(v : Val) (cases : List Case) (other : Code) ->
337 merge
338 { Some = \(c : Case) -> c.code, None = other }
339 (L.find (\(c : Case) -> w.eq c.key v) cases)
340 , loop =
341 \(n : Val)
342 (init : List Val)
343 (body : Val -> List Val -> (List Val -> Code) -> Code)
344 (k : List Val -> Code) ->
345 N.fold
346 (value n)
347 (\(rest : Iterate) (i : Natural) (s : List Val) ->
348 body (literal i) s (rest (i + 1)))
349 (\(_ : Natural) (s : List Val) -> k s)
350 0
351 init
352 , share = \(ws : List Val) (f : List Val -> Code) -> f ws
353 , join = \(A : Type) (k : A -> Code) (f : (A -> Code) -> Code) -> f k
354 , ret =
355 \(data : List (Signature.Chunk Val)) (world : World) ->
356 Halt.Returned { data = bytesOf (chunks data), world }
357 , output =
358 \(data : List (Signature.Piece Val)) (world : World) ->
359 Halt.Returned { data = piecesOf frame.calldata (L.map piece data), world }
360 , revert =
361 \(data : List (Signature.Chunk Val)) (_ : World) ->
362 Halt.Reverted (bytesOf (chunks data))
363 , bubble = \(world : World) -> Halt.Reverted world.returndata
364 }
365
366-- Executable runs use native words, including across loops and joins.
367let lit : Natural -> Word 256 = Word/fromNatural 256
368
369let value : Word 256 -> Natural = Word/toNatural
370
371let words = Signature.nativeWords
372
373let naturalWords = Signature.naturalWords
374
375let natural = Signature.natural
376
377let ops : Frame -> Signature.Ops (Word 256) Bool Code = opsWith words lit value
378
379-- Falling off the end of the code: STOP, which returns nothing.
380let stop
381 : Code
382 = \world -> Halt.Returned { data = ([] : List Natural), world }
383
384let Main = forall {Val : Type} {Flag : Type} {m : Type -> Type} -> Base.Base Val Flag m -> m unit
385
386-- Run an entry point in a frame, on a world.
387let runIn
388 : Frame -> World -> Main -> Halt
389 = \frame world main -> main (Base.cps (ops frame)) (\(_ : unit) -> stop) world
390
391-- A world of a storage, the other accounts' storages, and nothing else.
392let fresh
393 : Storage -> Accounts -> World
394 = \storage accounts ->
395 { storage
396 , transient = \(_ : Natural) -> 0
397 , accounts
398 , logs = ([] : List RawLog)
399 , returndata = ([] : List Natural)
400 }
401
402let empty
403 : Storage
404 = \(_ : Key) -> 0
405
406let nobody
407 : Accounts
408 = \(_ : Natural) -> empty
409
410-- Run an entry point in a frame, on a storage.
411let run
412 : Frame -> Storage -> Main -> Halt
413 = \frame storage main -> runIn frame (fresh storage nobody) main
414
415-- A contract at an address, run in the model when it is called (on its
416-- storage among the accounts, with its immutables), and the callee
417-- `others` for the other addresses. Its own calls go to `others`.
418let contract
419 : Natural -> List Natural -> (Text -> Natural) -> Main -> Callee -> Callee
420 = \address immutables names main others req accounts logs ->
421 if N.equal req.to address
422 then let frame =
423 { caller = req.from
424 , address
425 , callvalue = req.value
426 , calldata = req.input
427 , timestamp = req.timestamp
428 , immutables
429 , names
430 , callee = others
431 }
432 in merge
433 { Returned =
434 \(r : { data : List Natural, world : World }) ->
435 { ok = True
436 , output = r.data
437 , accounts =
438 \(a : Natural) ->
439 if N.equal a address then r.world.storage else r.world.accounts a
440 , logs = r.world.logs
441 }
442 , Reverted =
443 \(data : List Natural) -> { ok = False, output = data, accounts, logs }
444 }
445 (runIn
446 frame
447 { storage = accounts address
448 , transient = \(_ : Natural) -> 0
449 , accounts
450 , logs
451 , returndata = ([] : List Natural)
452 }
453 main)
454 else others req accounts logs
455
456in { words
457 , naturalWords
458 , natural
459 , bytesOf
460 , piecesOf
461 , wordsOf
462 , RawLog
463 , Storage
464 , Accounts
465 , World
466 , Halt
467 , Code
468 , Request
469 , Answer
470 , Callee
471 , noCode
472 , Frame
473 , keyEqual
474 , cdload
475 , ops
476 , opsWith
477 , stop
478 , Main
479 , runIn
480 , fresh
481 , empty
482 , nobody
483 , run
484 , contract
485 }