@@ -284,6 +284,90 @@ theorem stmtWritesOnly_tstore
284284abbrev PreservesBindingsExcept (st s : RuntimeState) (written : List String) : Prop :=
285285 forall key, key ∉ written -> lookupValue s.bindings key = lookupValue st.bindings key
286286
287+ abbrev PreservesStorageExcept (st s : RuntimeState) (writtenSlots : List Nat) : Prop :=
288+ forall slot, slot ∉ writtenSlots -> s.world.storage slot = st.world.storage slot
289+
290+ abbrev PreservesAddressStorageExcept (st s : RuntimeState) (writtenSlots : List Nat) : Prop :=
291+ forall slot, slot ∉ writtenSlots -> s.world.storageAddr slot = st.world.storageAddr slot
292+
293+ abbrev PreservesStorageArraysExcept (st s : RuntimeState) (writtenSlots : List Nat) : Prop :=
294+ forall slot, slot ∉ writtenSlots -> s.world.storageArray slot = st.world.storageArray slot
295+
296+ abbrev PreservesSelectorCalldata (st s : RuntimeState) : Prop :=
297+ s.selector = st.selector /\ s.world.calldata = st.world.calldata
298+
299+ structure ExecutionSummary (st s : RuntimeState)
300+ (writtenBindings : List String) (writtenStorageSlots : List Nat)
301+ (writtenAddressSlots : List Nat) (writtenArraySlots : List Nat) : Prop where
302+ bindings : PreservesBindingsExcept st s writtenBindings
303+ storage : PreservesStorageExcept st s writtenStorageSlots
304+ addressStorage : PreservesAddressStorageExcept st s writtenAddressSlots
305+ storageArrays : PreservesStorageArraysExcept st s writtenArraySlots
306+ selectorCalldata : PreservesSelectorCalldata st s
307+
308+ theorem ExecutionSummary.refl
309+ (st : RuntimeState) :
310+ ExecutionSummary st st [] [] [] [] := by
311+ constructor
312+ · intro _ _; rfl
313+ · intro _ _; rfl
314+ · intro _ _; rfl
315+ · intro _ _; rfl
316+ · exact And.intro rfl rfl
317+
318+ theorem ExecutionSummary.weaken
319+ {st s : RuntimeState}
320+ {ws ws' wa wa' wsa wsa' : List Nat}
321+ {writtenBindings writtenBindings' : List String}
322+ (h : ExecutionSummary st s writtenBindings ws wa wsa)
323+ (hb : ∀ key, key ∈ writtenBindings -> key ∈ writtenBindings')
324+ (hs : ∀ slot, slot ∈ ws -> slot ∈ ws')
325+ (ha : ∀ slot, slot ∈ wa -> slot ∈ wa')
326+ (hsa : ∀ slot, slot ∈ wsa -> slot ∈ wsa') :
327+ ExecutionSummary st s writtenBindings' ws' wa' wsa' := by
328+ constructor
329+ · intro key hnot
330+ exact h.bindings key (fun hm => hnot (hb key hm))
331+ · intro slot hnot
332+ exact h.storage slot (fun hm => hnot (hs slot hm))
333+ · intro slot hnot
334+ exact h.addressStorage slot (fun hm => hnot (ha slot hm))
335+ · intro slot hnot
336+ exact h.storageArrays slot (fun hm => hnot (hsa slot hm))
337+ · exact h.selectorCalldata
338+
339+ private theorem not_mem_append_left {α : Type } [DecidableEq α] {x : α} {xs ys : List α}
340+ (h : x ∉ xs ++ ys) : x ∉ xs := by
341+ intro hx
342+ exact h (List.mem_append_left ys hx)
343+
344+ private theorem not_mem_append_right {α : Type } [DecidableEq α] {x : α} {xs ys : List α}
345+ (h : x ∉ xs ++ ys) : x ∉ ys := by
346+ intro hy
347+ exact h (List.mem_append_right xs hy)
348+
349+ theorem ExecutionSummary.trans
350+ {st mid s : RuntimeState}
351+ {wb₁ wb₂ : List String} {ws₁ ws₂ wa₁ wa₂ wsa₁ wsa₂ : List Nat}
352+ (h₁ : ExecutionSummary st mid wb₁ ws₁ wa₁ wsa₁)
353+ (h₂ : ExecutionSummary mid s wb₂ ws₂ wa₂ wsa₂) :
354+ ExecutionSummary st s (wb₁ ++ wb₂) (ws₁ ++ ws₂) (wa₁ ++ wa₂) (wsa₁ ++ wsa₂) := by
355+ constructor
356+ · intro key hnot
357+ rw [h₂.bindings key (not_mem_append_right hnot),
358+ h₁.bindings key (not_mem_append_left hnot)]
359+ · intro slot hnot
360+ rw [h₂.storage slot (not_mem_append_right hnot),
361+ h₁.storage slot (not_mem_append_left hnot)]
362+ · intro slot hnot
363+ rw [h₂.addressStorage slot (not_mem_append_right hnot),
364+ h₁.addressStorage slot (not_mem_append_left hnot)]
365+ · intro slot hnot
366+ rw [h₂.storageArrays slot (not_mem_append_right hnot),
367+ h₁.storageArrays slot (not_mem_append_left hnot)]
368+ · exact ⟨h₂.selectorCalldata.1 .trans h₁.selectorCalldata.1 ,
369+ h₂.selectorCalldata.2 .trans h₁.selectorCalldata.2 ⟩
370+
287371theorem execStmt_letVar_preserves_bindings_except
288372 (st s : RuntimeState) (name : String) (e : Expr)
289373 (h : execStmt [] st (.letVar name e) = .continue s) :
@@ -318,9 +402,6 @@ theorem execStmt_mstore_preserves_bindings_except
318402 injection h with hh; subst hh
319403 intro key _; rfl
320404
321- abbrev PreservesSelectorCalldata (st s : RuntimeState) : Prop :=
322- s.selector = st.selector /\ s.world.calldata = st.world.calldata
323-
324405theorem execStmt_letVar_preserves_selector_calldata
325406 (st s : RuntimeState) (name : String) (e : Expr)
326407 (h : execStmt [] st (.letVar name e) = .continue s) :
@@ -353,4 +434,118 @@ theorem execStmt_mstore_preserves_selector_calldata
353434 injection h with hh; subst hh
354435 exact And.intro rfl rfl
355436
437+ theorem writeUintSlots_preserves_storage_except
438+ (world : Verity.ContractState) (slots : List Nat) (value slot : Nat)
439+ (hslot : slot ∉ slots.map wordNormalize) :
440+ (writeUintSlots world slots value).storage slot = world.storage slot := by
441+ have hcontains : (slots.map wordNormalize).contains slot = false := by
442+ simpa [List.elem_eq_contains] using hslot
443+ simp only [writeUintSlots]
444+ rw [hcontains]
445+ simp
446+
447+ theorem writeStorageWordSlots_preserves_storage_except
448+ (world : Verity.ContractState) (slots : List Nat) (wordOffset value slot : Nat)
449+ (hslot : slot ∉ slots.map (fun base => wordNormalize (base + wordOffset))) :
450+ (writeStorageWordSlots world slots wordOffset value).storage slot = world.storage slot := by
451+ have hcontains :
452+ (slots.map (fun base => wordNormalize (base + wordOffset))).contains slot = false := by
453+ simpa [List.elem_eq_contains] using hslot
454+ simp only [writeStorageWordSlots]
455+ rw [hcontains]
456+ simp
457+
458+ theorem writeStorageWordSlots_preserves_address_except
459+ (world : Verity.ContractState) (slots : List Nat) (wordOffset value slot : Nat)
460+ (hslot : slot ∉ slots.map (fun base => wordNormalize (base + wordOffset))) :
461+ (writeStorageWordSlots world slots wordOffset value).storageAddr slot =
462+ world.storageAddr slot := by
463+ have hcontains :
464+ (slots.map (fun base => wordNormalize (base + wordOffset))).contains slot = false := by
465+ simpa [List.elem_eq_contains] using hslot
466+ simp only [writeStorageWordSlots]
467+ rw [hcontains]
468+ simp
469+
470+ theorem writeAddressSlots_preserves_address_except
471+ (world : Verity.ContractState) (slots : List Nat) (value slot : Nat)
472+ (hslot : slot ∉ slots.map wordNormalize) :
473+ (writeAddressSlots world slots value).storageAddr slot = world.storageAddr slot := by
474+ have hcontains : (slots.map wordNormalize).contains slot = false := by
475+ simpa [List.elem_eq_contains] using hslot
476+ simp only [writeAddressSlots]
477+ rw [hcontains]
478+ simp
479+
480+ theorem writeStorageArray_preserves_arrays_except
481+ (world : Verity.ContractState) (arraySlot slot : Nat) (values : List Verity.Core.Uint256)
482+ (hslot : slot ∉ [arraySlot]) :
483+ (writeStorageArray world arraySlot values).storageArray slot = world.storageArray slot := by
484+ have hne : slot ≠ arraySlot := by simpa using hslot
485+ simp [writeStorageArray, hne, BEq.beq]
486+
487+ theorem execStmt_setStorage_execution_summary
488+ (fields : List Compiler.CompilationModel.Field)
489+ (st s : RuntimeState) (fieldName : String) (value : Expr) (slots : List Nat)
490+ (hslots : Compiler.CompilationModel.findFieldWriteSlots fields fieldName = some slots)
491+ (h : execStmt fields st (.setStorage fieldName value) = .continue s) :
492+ ExecutionSummary st s [] (slots.map wordNormalize) [] [] := by
493+ rw [show execStmt fields st (.setStorage fieldName value) =
494+ (match Compiler.CompilationModel.findFieldWriteSlots fields fieldName, evalExpr fields st value with
495+ | some slots, some resolved =>
496+ .continue { st with world := writeUintSlots st.world slots resolved }
497+ | _, _ => .revert) from rfl] at h
498+ rw [hslots] at h
499+ cases hval : evalExpr fields st value with
500+ | none => rw [hval] at h; exact absurd h (by simp)
501+ | some resolved =>
502+ rw [hval] at h
503+ injection h with hh; subst hh
504+ constructor
505+ · intro _ _; rfl
506+ · intro slot hslot
507+ exact writeUintSlots_preserves_storage_except st.world slots resolved slot hslot
508+ · intro _ _; rfl
509+ · intro _ _; rfl
510+ · exact And.intro rfl rfl
511+
512+ theorem execStmt_setStorageAddr_execution_summary
513+ (fields : List Compiler.CompilationModel.Field)
514+ (st s : RuntimeState) (fieldName : String) (value : Expr) (slots : List Nat)
515+ (hslots : Compiler.CompilationModel.findFieldWriteSlots fields fieldName = some slots)
516+ (h : execStmt fields st (.setStorageAddr fieldName value) = .continue s) :
517+ ExecutionSummary st s [] [] (slots.map wordNormalize) [] := by
518+ rw [show execStmt fields st (.setStorageAddr fieldName value) =
519+ (match Compiler.CompilationModel.findFieldWriteSlots fields fieldName, evalExpr fields st value with
520+ | some slots, some resolved =>
521+ .continue { st with world := writeAddressSlots st.world slots resolved }
522+ | _, _ => .revert) from rfl] at h
523+ rw [hslots] at h
524+ cases hval : evalExpr fields st value with
525+ | none => rw [hval] at h; exact absurd h (by simp)
526+ | some resolved =>
527+ rw [hval] at h
528+ injection h with hh; subst hh
529+ constructor
530+ · intro _ _; rfl
531+ · intro _ _; rfl
532+ · intro slot hslot
533+ exact writeAddressSlots_preserves_address_except st.world slots resolved slot hslot
534+ · intro _ _; rfl
535+ · exact And.intro rfl rfl
536+
537+ theorem execStmtList_execution_summary_cons
538+ (fields : List Compiler.CompilationModel.Field)
539+ (st mid s : RuntimeState) (stmt : Stmt) (rest : List Stmt)
540+ {wb₁ wb₂ : List String} {ws₁ ws₂ wa₁ wa₂ wsa₁ wsa₂ : List Nat}
541+ (hstmt : execStmt fields st stmt = .continue mid)
542+ (hrest : execStmtList fields mid rest = .continue s)
543+ (hs₁ : ExecutionSummary st mid wb₁ ws₁ wa₁ wsa₁)
544+ (hs₂ : ExecutionSummary mid s wb₂ ws₂ wa₂ wsa₂) :
545+ execStmtList fields st (stmt :: rest) = .continue s /\
546+ ExecutionSummary st s (wb₁ ++ wb₂) (ws₁ ++ ws₂) (wa₁ ++ wa₂) (wsa₁ ++ wsa₂) := by
547+ constructor
548+ · simp [execStmtList, hstmt, hrest]
549+ · exact ExecutionSummary.trans hs₁ hs₂
550+
356551end Compiler.Proofs.Frames
0 commit comments