Repository navigation
Expand file tree
/
Copy pathModel.lean
More file actions
75 lines (58 loc) · 2.07 KB
/
Copy pathModel.lean
File metadata and controls
75 lines (58 loc) · 2.07 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
import Fe.EVM.Refine
/-!
# FeScalarStore model
Written by hand, in the shape the exporter should print.
```fe
pub contract FeScalarStore {
mut stored: u256
init(initial: u256) uses (mut stored) { stored = initial }
recv FeScalarStoreMsg {
Get -> u256 uses (stored) { stored }
Set { value } uses (mut stored) { stored = value }
}
}
```
-/
open Ethereum Ethereum.EVM Fe Fe.EVM
namespace FeProofs.ScalarStore.Model
/-- The storage root of the contract field `stored`. -/
def storedSlot : UInt256 := ⟨0⟩
/-- `init(initial: u256) uses (mut stored) { stored = initial }` -/
def initBody (initial : UInt256) : FeM Unit := sstore storedSlot initial
/-- `Get -> u256 uses (stored) { stored }` -/
def get : FeM UInt256 := sload storedSlot
/-- `Set { value } uses (mut stored) { stored = value }` -/
def set (value : UInt256) : FeM Unit := sstore storedSlot value
/-- `sol("set(uint256)")` -/
def setSelector : ByteArray := ⟨#[0x60, 0xfe, 0x47, 0xb1]⟩
/-- `sol("get()")` -/
def getSelector : ByteArray := ⟨#[0x6d, 0x4c, 0xe6, 0x3c]⟩
/-- The constructor: reject call value, decode one word from the argument bytes, run `init`. -/
def init (args : ByteArray) : FeM Unit := do
nonPayable
if args.size < 32 then
M.revert ByteArray.empty
else
initBody (uInt256OfByteArray (args.readBytes 0 32))
/-- The runtime entry. Each arm rejects call value, then checks that the calldata holds its
argument words, with Solidity's signed length check. -/
def runtime : FeM ByteArray := do
let cd ← calldata
if cd.size < 4 then
M.revert ByteArray.empty
else if setSelector == cd.extract 0 4 then
nonPayable
if BadHead cd 1 then
M.revert ByteArray.empty
else
set (uInt256OfByteArray (cd.readBytes 4 32))
return ByteArray.empty
else if getSelector == cd.extract 0 4 then
nonPayable
let result ← get
return result.toByteArray
else
M.revert ByteArray.empty
def contract : ContractModel :=
{ init := fun args => init args >>= fun _ => pure ByteArray.empty, runtime := runtime }
end FeProofs.ScalarStore.Model