Skip to content

Commit bae0a34

Browse files
Execution summary frame theorems (#1994) (#2009)
* Add execution summary frame theorems * chore: auto-refresh derived artifacts --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3268129 commit bae0a34

3 files changed

Lines changed: 213 additions & 4 deletions

File tree

Compiler.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ import Compiler.Proofs.IRGeneration.FunctionBody
3939
import Compiler.Proofs.IRGeneration.ParamLoading
4040
import Compiler.Proofs.IRGeneration.SupportedSpec
4141
import Compiler.Proofs.IRGeneration.SourceSemantics
42+
import Compiler.Proofs.Frames
4243
import Compiler.Proofs.YulGeneration.PatchRulesProofs
4344
import Compiler.Proofs.EndToEnd

Compiler/Proofs/Frames.lean

Lines changed: 198 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,90 @@ theorem stmtWritesOnly_tstore
284284
abbrev 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+
287371
theorem 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-
324405
theorem 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+
356551
end Compiler.Proofs.Frames

PrintAxioms.lean

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,10 +1634,23 @@ end Verity.AxiomAudit
16341634
Compiler.Proofs.Frames.stmtWritesOnly_assignVar
16351635
Compiler.Proofs.Frames.stmtWritesOnly_mstore
16361636
Compiler.Proofs.Frames.stmtWritesOnly_tstore
1637+
Compiler.Proofs.Frames.ExecutionSummary.refl
1638+
Compiler.Proofs.Frames.ExecutionSummary.weaken
1639+
-- Compiler.Proofs.Frames.not_mem_append_left -- private
1640+
-- Compiler.Proofs.Frames.not_mem_append_right -- private
1641+
Compiler.Proofs.Frames.ExecutionSummary.trans
16371642
Compiler.Proofs.Frames.execStmt_letVar_preserves_bindings_except
16381643
Compiler.Proofs.Frames.execStmt_mstore_preserves_bindings_except
16391644
Compiler.Proofs.Frames.execStmt_letVar_preserves_selector_calldata
16401645
Compiler.Proofs.Frames.execStmt_mstore_preserves_selector_calldata
1646+
Compiler.Proofs.Frames.writeUintSlots_preserves_storage_except
1647+
Compiler.Proofs.Frames.writeStorageWordSlots_preserves_storage_except
1648+
Compiler.Proofs.Frames.writeStorageWordSlots_preserves_address_except
1649+
Compiler.Proofs.Frames.writeAddressSlots_preserves_address_except
1650+
Compiler.Proofs.Frames.writeStorageArray_preserves_arrays_except
1651+
Compiler.Proofs.Frames.execStmt_setStorage_execution_summary
1652+
Compiler.Proofs.Frames.execStmt_setStorageAddr_execution_summary
1653+
Compiler.Proofs.Frames.execStmtList_execution_summary_cons
16411654

16421655
-- Compiler/Proofs/HelperStepProofs.lean
16431656
Compiler.Proofs.HelperStepProofs.allHelperInterfacesSatisfied_of_helperSurfaceClosed
@@ -5585,4 +5598,4 @@ end Verity.AxiomAudit
55855598
Compiler.Proofs.YulGeneration.YulTransaction.ofIR_args
55865599
]
55875600

5588-
-- Total: 5227 theorems/lemmas (3620 public, 1607 private, 0 sorry'd)
5601+
-- Total: 5240 theorems/lemmas (3631 public, 1609 private, 0 sorry'd)

0 commit comments

Comments
 (0)