Repository navigation
Expand file tree
/
Copy pathSetTwo.lean
More file actions
140 lines (132 loc) · 7.98 KB
/
Copy pathSetTwo.lean
File metadata and controls
140 lines (132 loc) · 7.98 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
import FeProofs.IndexedPackedStore.Dispatch
import FeProofs.IndexedPackedStore.Elems
import FeProofs.Common.Basic
import Reasoning.ExternalCall
/-!
# FeIndexedPackedStore `setTwo(uint256,uint256,uint256,uint256)`
Arm entry pc 136: call-value check, head check (pc 142), the first `index < 64` check (pc 154), the
first lane write followed by the second index check (pc 165), then the second lane write and an
empty return (pc 222). A bad second index reverts after the first write, which the revert
discards; under `STATICCALL` the first `SSTORE` halts.
-/
open Stele ABI Ethereum Ethereum.EVM Reasoning.Theory Reasoning.Reach
namespace FeProofs.IndexedPackedStore
set_option maxRecDepth 2000000
abbrev setTwoStore (I : ExecutionEnv) : Store :=
((((∅ : Store).insert "first_index" (.int (Int.ofNat (calldataWord I.calldata 4).toNat))).insert
"first_value" (.int (Int.ofNat (calldataWord I.calldata 36).toNat))).insert "second_index"
(.int (Int.ofNat (calldataWord I.calldata 68).toNat))).insert "second_value"
(.int (Int.ofNat (calldataWord I.calldata 100).toNat))
theorem feIndexedPackedStoreSetTwoBody {σ σ₀ A I} {g : UInt256}
(hcode : I.code = feIndexedPackedStoreBytecode) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (feIndexedPackedStoreSelBytes 1)) :
runtimeRefinementFor config contract σ σ₀ g A I := by
have hsz : 4 ≤ I.calldata.size := calldata_size_ge_of_selIs I (feIndexedPackedStoreSelBytes 1) rfl hsel
have hd := feIndexedPackedStoreDispatch_setTwo hsel
obtain ⟨k, C, rd⟩ := feIndexedPackedStoreReachSetTwoBody (g := Sat256.ofUInt256 g) (σ := σ)
(σ₀ := σ₀) (A := A) hcode hsz hsize hsel
by_cases hwv : I.weiValue = ⟨0⟩
swap
· nonpayable_revert rd hcode hd hwv
by_cases hok : 132 ≤ I.calldata.size ∧ I.calldata.size < 2 ^ 255 + 4
swap
· have hbad : I.calldata.size < 132 ∨ 2 ^ 255 + 4 ≤ I.calldata.size := by omega
rd_run rd using [hwv, solcDecodeLenCheckBad 4 hsz hsize hbad]
exact RDrev.reEquivDecodingFailed hcode rd hd
(hbad.elim
(fun h => decodeCalldata_scalar_none_short (by decide) (by simpa using! h))
(fun h => decodeCalldata_scalar_none_huge (by decide) rfl h))
obtain ⟨hlen, hhi⟩ := hok
rd_run rd to 154 using [hwv,
solcCalldataStaticLenCheckOk (sz := I.calldata.size) (words := 4) (by omega) hhi hsize]
have hdec : config.externalABI.decodeCalldata? (setTwoTransition.params.map Param.name)
(transitionSignature setTwoTransition).paramTypes I.calldata = some (setTwoStore I) := by
show decodeCalldata ["first_index", "first_value", "second_index", "second_value"]
[abiUInt256, abiUInt256, abiUInt256, abiUInt256] I.calldata = _
exact decodeCalldata_uint256s (cd := I.calldata)
["first_index", "first_value", "second_index", "second_value"]
(by simpa using hlen) (by intro _; exact hhi)
set i1 := (calldataWord I.calldata 4).toNat with hi1
set i2 := (calldataWord I.calldata 68).toNat with hi2
set evm := initState σ σ₀ (Sat256.ofUInt256 g) A I with hevm
have hx1 : (setTwoStore I).get? "first_index" = some (.int (Int.ofNat i1)) := by
rw [store_get_ne _ _ (by decide), store_get_ne _ _ (by decide), store_get_ne _ _ (by decide),
store_get_self]
have hv1 : (setTwoStore I).get? "first_value" =
some (.int (Int.ofNat (calldataWord I.calldata 36).toNat)) := by
rw [store_get_ne _ _ (by decide), store_get_ne _ _ (by decide), store_get_self]
have hx2 : (setTwoStore I).get? "second_index" = some (.int (Int.ofNat i2)) := by
rw [store_get_ne _ _ (by decide), store_get_self]
have hv2 : (setTwoStore I).get? "second_value" =
some (.int (Int.ofNat (calldataWord I.calldata 100).toNat)) := store_get_self _ _ _
have hreq := ExecStmt.requireTrue (cfg := config)
(stele := { contract := contract, locals := setTwoStore I })
(evalCallvalueEq_true (evm := evm) (by simp only [hevm, initState]; exact hwv))
by_cases hin1 : i1 < 64
swap
· rd_run rd using [(by rw [ult_zero (by exact Nat.le_of_not_lt hin1)]; decide)]
exact RDrev.reEquivExecutionRevert hcode rd hd hdec
(ExecFuncBody.execBlockRevert (ExecBlock.consNormal hreq (ExecBlock.consRevert
(ExecStmt.requireFalse (by rw [evalIndexLt hx1, decide_eq_false hin1])))))
rd_run rd to 165 using [(by rw [ult_one (by exact hin1)]; decide)]
have hreq1 := ExecStmt.requireTrue (cfg := config)
(stele := { contract := contract, locals := setTwoStore I }) (evm := evm)
(by rw [evalIndexLt hx1, decide_eq_true hin1])
have hval1 := evalCastU8 (evm := evm) (L := setTwoStore I) hv1
have hbase : (setTwoStore I).get? "pack" = none := by store_get
have hasg1 := assignElem (evm := evm) hin1 hx1 hbase (calldataWord I.calldata 36)
set evm1 := Stele.EVM.storageStore evm evm.executionEnv.codeOwner (elemSlot i1)
(setPackedByte (Stele.EVM.storageLoad evm evm.executionEnv.codeOwner (elemSlot i1))
(calldataWord I.calldata 36) (elemByte i1)) with hevm1
by_cases hperm : I.perm = true
swap
· have hpf : I.perm = false := by simpa using hperm
rd_step rd
exact RDstatic.reEquivStaticHalt hcode rd
hd hdec (ExecFuncBody.execBlockStatic (ExecBlock.consNormal hreq (ExecBlock.consNormal hreq1
(ExecBlock.consStatic
(ExecStmt.assignStatic hval1 hasg1 (by simp only [hevm, initState]; exact hpf))))))
have hp0 : evm.executionEnv.perm = true := by simp only [hevm, initState]; exact hperm
have hp1 : evm1.executionEnv.perm = true := by
rw [hevm1, storageStore_executionEnv]; exact hp0
have hassign1 := ExecStmt.assign (Or.inr hp0) hval1 hasg1
by_cases hin2 : i2 < 64
swap
· rd_run rd using [(by rw [ult_zero (by exact Nat.le_of_not_lt hin2)]; decide)]
exact RDrev.reEquivExecutionRevert hcode rd hd hdec
(ExecFuncBody.execBlockRevert (ExecBlock.consNormal hreq (ExecBlock.consNormal hreq1
(ExecBlock.consNormal hassign1 (ExecBlock.consRevert (ExecStmt.requireFalse
(by rw [evalIndexLt hx2, decide_eq_false hin2])))))))
rd_run rd using [(by rw [ult_one (by exact hin2)]; decide)]
have hreq2 := ExecStmt.requireTrue (cfg := config)
(stele := { contract := contract, locals := setTwoStore I }) (evm := evm1)
(by rw [evalIndexLt hx2, decide_eq_true hin2])
have hval2 := evalCastU8 (evm := evm1) (L := setTwoStore I) hv2
have hasg2 := assignElem (evm := evm1) hin2 hx2 hbase (calldataWord I.calldata 100)
refine (RDret.of_output_eq rd (byteArray_readWithPadding_zero _ _)).reEquivExecutionGen hcode hd
hdec (ExecFuncBody.execBlockOK (ExecBlock.consNormal hreq (ExecBlock.consNormal hreq1
(ExecBlock.consNormal hassign1 (ExecBlock.consNormal hreq2 (ExecBlock.consNormal
(ExecStmt.assign (Or.inr hp1) hval2 hasg2) ExecBlock.nil))))))
?_ (returnEquiv.fallthrough rfl rfl (by native_decide))
have hs0 : SourceState evm I σ evm := ⟨rfl, rfl, rfl⟩
have hs1 := hs0.storageWrite (elemSlot i1)
(setPackedByte (solcSlotWord σ I (elemSlot i1)) (calldataWord I.calldata 36) (elemByte i1))
have hevm1' : evm1 = Stele.EVM.storageStore evm I.codeOwner (elemSlot i1)
(setPackedByte (solcSlotWord σ I (elemSlot i1)) (calldataWord I.calldata 36)
(elemByte i1)) := by
rw [hevm1, hs0.storageRead]
rfl
have hs2 := hs1.storageWrite (elemSlot i2)
(setPackedByte (solcSlotWord (sstoreAccountMap I.codeOwner σ (elemSlot i1)
(setPackedByte (solcSlotWord σ I (elemSlot i1)) (calldataWord I.calldata 36)
(elemByte i1))) I (elemSlot i2)) (calldataWord I.calldata 100) (elemByte i2))
rw [keccakWord_mapSlot, keccakWord_mapSlot, laneWrite_eq, laneWrite_eq, shr5_eq, shr5_eq]
refine hs2.accounts.trans ?_
have hload : Stele.EVM.storageLoad evm1 evm1.executionEnv.codeOwner (elemSlot i2) =
solcSlotWord (sstoreAccountMap I.codeOwner σ (elemSlot i1)
(setPackedByte (solcSlotWord σ I (elemSlot i1)) (calldataWord I.calldata 36)
(elemByte i1))) I (elemSlot i2) := by
rw [hevm1']; exact hs1.storageRead (elemSlot i2)
have hco : evm1.executionEnv.codeOwner = I.codeOwner := by rw [hevm1', hs1.env]
rw [hload, hco, ← hevm1']
end FeProofs.IndexedPackedStore