Library QedkConcrete.vmchecked
Joint word and byte-size invariant
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmmachinewords QedkConcrete.memorycomposition
QedkConcrete.vmsizes QedkConcrete.vmchildsizes QedkConcrete.memorywords
QedkConcrete.wordrange QedkConcrete.vmbytewords.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress.
Require Import QedkConcrete.memorymodel.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Machine.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (invariant : Host_t → Prop).
Definition checkedMachine (state : @MS.t Host_t F) : Prop :=
machineWords invariant state ∧ machineByteSizes state.
Definition checkedSafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
∀ state, checkedMachine state →
(checkedMachine (snd (action state)) ∧
match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
badMemoryInitComparison.
Lemma checked_safe_combine {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
memorySafe invariant P action → sizesSafe (fun _ ⇒ True) action → checkedSafe P action.
Proof.
intros Hwords Hsizes state [Hw Hs].
destruct (Hwords state Hw) as [[Hafter Hvalue]|Hfailure]; [|right; exact Hfailure].
left. split; [split; [exact Hafter|exact (proj1 (Hsizes state Hs))]|exact Hvalue].
Qed.
Lemma checked_safe_bind {A B : Set} (P : A → Prop) (Q : B → Prop)
(action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
checkedSafe P action → (∀ value, P value → checkedSafe Q (next value)) →
checkedSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
intros Haction Hnext state Hinitial.
destruct (Haction state Hinitial) as [Hprefix|Hfailure]; [|right; exact Hfailure].
change ((checkedMachine (snd
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
match fst
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
with inl v ⇒ Q v | inr _ ⇒ True end) ∨ badMemoryInitComparison).
destruct (action state) as [[value|error] middle]; cbn [fst snd] in Hprefix.
- apply Hnext; [exact (proj2 Hprefix)|exact (proj1 Hprefix)].
- left. exact Hprefix.
Qed.
Lemma checked_safe_return {A : Set} (P : A → Prop) value :
P value → checkedSafe P (@V.M._return Host_t F A value).
Proof. intros HP state Hinitial. left. split; assumption. Qed.
Lemma checked_safe_fail {A : Set} (P : A → Prop) error :
checkedSafe P (@V.M.fail Host_t F A error).
Proof. intros state Hinitial. left. split; [exact Hinitial|exact I]. Qed.
Lemma checked_safe_read {A : Set} (P : A → Prop) (lens : Lens.t (@MS.t Host_t F) A) :
(∀ state, checkedMachine state → P (Lens.t.get lens state)) →
checkedSafe P (@V.M.op_exclamation Host_t F A lens).
Proof. intros Hread state Hinitial. left. split; [exact Hinitial|apply Hread; exact Hinitial]. Qed.
Lemma checked_safe_memory_read :
checkedSafe (fun memory ⇒ memoryWordChunks memory ∧ memoryFits memory)
(@V.M.op_exclamation Host_t F _ MS.memory).
Proof.
apply checked_safe_read. intros state [[_ [_ [_ Hwords]]] [Hfits _]]. split; assumption.
Qed.
Lemma checked_safe_write {A : Set} (lens : Lens.t (@MS.t Host_t F) A) value :
(∀ state, checkedMachine state → checkedMachine (Lens.t.set lens value state)) →
checkedSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F A lens value).
Proof. intros Hwrite state Hinitial. left. split; [apply Hwrite; exact Hinitial|exact I]. Qed.
Lemma checked_safe_update_frame {A : Set} (lens : Lens.t (@MS.t Host_t F) A) f :
(∀ state value, checkedMachine state → checkedMachine (Lens.t.set lens value state)) →
checkedSafe (fun _ ⇒ True) (@V.M.update_field Host_t F A lens f).
Proof. intros Hupdate state Hinitial. left. split; [apply Hupdate; exact Hinitial|exact I]. Qed.
Lemma checked_safe_output_write bytes : OCamlString.length bytes < 2^256 →
checkedSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F _ MS.output_buffer bytes).
Proof.
intro Hbytes. apply checked_safe_write. intros state [Hwords [Hmemory Houtput]].
split; [exact Hwords|split; assumption].
Qed.
Lemma checked_safe_fmap_ignore {A B : Set} (f : A → B) action :
checkedSafe (fun _ ⇒ True) action →
checkedSafe (fun _ ⇒ True) (@V.M.op_ltdollargt Host_t F A B f action).
Proof.
intro Haction. change (checkedSafe (fun _ ⇒ True)
(@V.M.op_letdollar Host_t F A B action (fun v ⇒ @V.M._return Host_t F B (f v)))).
eapply checked_safe_bind; [exact Haction|]. intros. apply checked_safe_return. exact I.
Qed.
Lemma checked_safe_push value : wordRange value →
checkedSafe (fun _ ⇒ True) (@V.push Host_t F value).
Proof.
intro Hvalue. apply checked_safe_combine;
[apply memory_safe_lift, safe_push; exact Hvalue|apply sizes_safe_push].
Qed.
Lemma checked_safe_increase_pc {UF : Basics.Unreachable U.t} :
checkedSafe (fun _ ⇒ True) (@V.increase_pc_and_continue Host_t F).
Proof.
apply checked_safe_combine;
[apply memory_safe_lift, safe_increase_pc|apply sizes_safe_increase_pc].
Qed.
Lemma checked_safe_host {A : Set} (P : A → Prop) (action : Host_t → A × Host_t) :
(∀ host, invariant host →
(invariant (snd (action host)) ∧ P (fst (action host))) ∨ badMemoryInitComparison) →
checkedSafe P (@V.M.HostAPI.lift Host_t F A action).
Proof.
intros Haction state [[Hstack [Hpc [Hhost Hmemory]]] Hsizes].
destruct (Haction (MS.t.host state) Hhost) as [[Hafter Hresult]|Hfailure]; [|right; exact Hfailure].
unfold V.M.HostAPI.lift. destruct (action (MS.t.host state)) as [value host].
left. split; [split|exact Hresult].
- exact (conj Hstack (conj Hpc (conj Hafter Hmemory))).
- exact Hsizes.
Qed.
Lemma checked_safe_call msg :
(∀ host, invariant host →
let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
(invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
checkedSafe (fun result ⇒ OCamlString.length (Evmc.Result.t.output_data result) < 2^256)
(@V.M.HostAPI.call Host_t F msg).
Proof.
intro Hcall. unfold V.M.HostAPI.call.
eapply checked_safe_bind with
(P := fun result ⇒ OCamlString.length (Evmc.Result.t.output_data result) < 2^256).
- apply checked_safe_host. exact Hcall.
- intros result Hlength.
match goal with |- checkedSafe _ (if ?test then _ else _) ⇒ destruct test end.
+ apply checked_safe_fail.
+ apply checked_safe_return. exact Hlength.
Qed.
Lemma checked_safe_memory_update f :
(∀ memory, memoryWordChunks memory → memoryFits memory →
(memoryWordChunks (f memory) ∧ memoryFits (f memory)) ∨ badMemoryInitComparison) →
checkedSafe (fun _ ⇒ True) (@V.M.update_field Host_t F _ MS.memory f).
Proof.
intros Hf state [[Hstack [Hpc [Hhost Hwords]]] [Hfits Houtput]].
destruct (Hf (MS.t.memory state) Hwords Hfits) as [[HnewWords HnewFits]|Hfailure];
[|right; exact Hfailure].
left. split; [split|exact I].
- exact (conj Hstack (conj Hpc (conj Hhost HnewWords))).
- split; assumption.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmmachinewords QedkConcrete.memorycomposition
QedkConcrete.vmsizes QedkConcrete.vmchildsizes QedkConcrete.memorywords
QedkConcrete.wordrange QedkConcrete.vmbytewords.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress.
Require Import QedkConcrete.memorymodel.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Machine.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (invariant : Host_t → Prop).
Definition checkedMachine (state : @MS.t Host_t F) : Prop :=
machineWords invariant state ∧ machineByteSizes state.
Definition checkedSafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
∀ state, checkedMachine state →
(checkedMachine (snd (action state)) ∧
match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
badMemoryInitComparison.
Lemma checked_safe_combine {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
memorySafe invariant P action → sizesSafe (fun _ ⇒ True) action → checkedSafe P action.
Proof.
intros Hwords Hsizes state [Hw Hs].
destruct (Hwords state Hw) as [[Hafter Hvalue]|Hfailure]; [|right; exact Hfailure].
left. split; [split; [exact Hafter|exact (proj1 (Hsizes state Hs))]|exact Hvalue].
Qed.
Lemma checked_safe_bind {A B : Set} (P : A → Prop) (Q : B → Prop)
(action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
checkedSafe P action → (∀ value, P value → checkedSafe Q (next value)) →
checkedSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
intros Haction Hnext state Hinitial.
destruct (Haction state Hinitial) as [Hprefix|Hfailure]; [|right; exact Hfailure].
change ((checkedMachine (snd
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
match fst
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
with inl v ⇒ Q v | inr _ ⇒ True end) ∨ badMemoryInitComparison).
destruct (action state) as [[value|error] middle]; cbn [fst snd] in Hprefix.
- apply Hnext; [exact (proj2 Hprefix)|exact (proj1 Hprefix)].
- left. exact Hprefix.
Qed.
Lemma checked_safe_return {A : Set} (P : A → Prop) value :
P value → checkedSafe P (@V.M._return Host_t F A value).
Proof. intros HP state Hinitial. left. split; assumption. Qed.
Lemma checked_safe_fail {A : Set} (P : A → Prop) error :
checkedSafe P (@V.M.fail Host_t F A error).
Proof. intros state Hinitial. left. split; [exact Hinitial|exact I]. Qed.
Lemma checked_safe_read {A : Set} (P : A → Prop) (lens : Lens.t (@MS.t Host_t F) A) :
(∀ state, checkedMachine state → P (Lens.t.get lens state)) →
checkedSafe P (@V.M.op_exclamation Host_t F A lens).
Proof. intros Hread state Hinitial. left. split; [exact Hinitial|apply Hread; exact Hinitial]. Qed.
Lemma checked_safe_memory_read :
checkedSafe (fun memory ⇒ memoryWordChunks memory ∧ memoryFits memory)
(@V.M.op_exclamation Host_t F _ MS.memory).
Proof.
apply checked_safe_read. intros state [[_ [_ [_ Hwords]]] [Hfits _]]. split; assumption.
Qed.
Lemma checked_safe_write {A : Set} (lens : Lens.t (@MS.t Host_t F) A) value :
(∀ state, checkedMachine state → checkedMachine (Lens.t.set lens value state)) →
checkedSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F A lens value).
Proof. intros Hwrite state Hinitial. left. split; [apply Hwrite; exact Hinitial|exact I]. Qed.
Lemma checked_safe_update_frame {A : Set} (lens : Lens.t (@MS.t Host_t F) A) f :
(∀ state value, checkedMachine state → checkedMachine (Lens.t.set lens value state)) →
checkedSafe (fun _ ⇒ True) (@V.M.update_field Host_t F A lens f).
Proof. intros Hupdate state Hinitial. left. split; [apply Hupdate; exact Hinitial|exact I]. Qed.
Lemma checked_safe_output_write bytes : OCamlString.length bytes < 2^256 →
checkedSafe (fun _ ⇒ True) (@V.M.op_coloneq Host_t F _ MS.output_buffer bytes).
Proof.
intro Hbytes. apply checked_safe_write. intros state [Hwords [Hmemory Houtput]].
split; [exact Hwords|split; assumption].
Qed.
Lemma checked_safe_fmap_ignore {A B : Set} (f : A → B) action :
checkedSafe (fun _ ⇒ True) action →
checkedSafe (fun _ ⇒ True) (@V.M.op_ltdollargt Host_t F A B f action).
Proof.
intro Haction. change (checkedSafe (fun _ ⇒ True)
(@V.M.op_letdollar Host_t F A B action (fun v ⇒ @V.M._return Host_t F B (f v)))).
eapply checked_safe_bind; [exact Haction|]. intros. apply checked_safe_return. exact I.
Qed.
Lemma checked_safe_push value : wordRange value →
checkedSafe (fun _ ⇒ True) (@V.push Host_t F value).
Proof.
intro Hvalue. apply checked_safe_combine;
[apply memory_safe_lift, safe_push; exact Hvalue|apply sizes_safe_push].
Qed.
Lemma checked_safe_increase_pc {UF : Basics.Unreachable U.t} :
checkedSafe (fun _ ⇒ True) (@V.increase_pc_and_continue Host_t F).
Proof.
apply checked_safe_combine;
[apply memory_safe_lift, safe_increase_pc|apply sizes_safe_increase_pc].
Qed.
Lemma checked_safe_host {A : Set} (P : A → Prop) (action : Host_t → A × Host_t) :
(∀ host, invariant host →
(invariant (snd (action host)) ∧ P (fst (action host))) ∨ badMemoryInitComparison) →
checkedSafe P (@V.M.HostAPI.lift Host_t F A action).
Proof.
intros Haction state [[Hstack [Hpc [Hhost Hmemory]]] Hsizes].
destruct (Haction (MS.t.host state) Hhost) as [[Hafter Hresult]|Hfailure]; [|right; exact Hfailure].
unfold V.M.HostAPI.lift. destruct (action (MS.t.host state)) as [value host].
left. split; [split|exact Hresult].
- exact (conj Hstack (conj Hpc (conj Hafter Hmemory))).
- exact Hsizes.
Qed.
Lemma checked_safe_call msg :
(∀ host, invariant host →
let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
(invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
checkedSafe (fun result ⇒ OCamlString.length (Evmc.Result.t.output_data result) < 2^256)
(@V.M.HostAPI.call Host_t F msg).
Proof.
intro Hcall. unfold V.M.HostAPI.call.
eapply checked_safe_bind with
(P := fun result ⇒ OCamlString.length (Evmc.Result.t.output_data result) < 2^256).
- apply checked_safe_host. exact Hcall.
- intros result Hlength.
match goal with |- checkedSafe _ (if ?test then _ else _) ⇒ destruct test end.
+ apply checked_safe_fail.
+ apply checked_safe_return. exact Hlength.
Qed.
Lemma checked_safe_memory_update f :
(∀ memory, memoryWordChunks memory → memoryFits memory →
(memoryWordChunks (f memory) ∧ memoryFits (f memory)) ∨ badMemoryInitComparison) →
checkedSafe (fun _ ⇒ True) (@V.M.update_field Host_t F _ MS.memory f).
Proof.
intros Hf state [[Hstack [Hpc [Hhost Hwords]]] [Hfits Houtput]].
destruct (Hf (MS.t.memory state) Hwords Hfits) as [[HnewWords HnewFits]|Hfailure];
[|right; exact Hfailure].
left. split; [split|exact I].
- exact (conj Hstack (conj Hpc (conj Hhost HnewWords))).
- split; assumption.
Qed.
A block copied into memory has already passed an extent check. The
bound is needed by native word-count and padding arithmetic.
Lemma checked_safe_write_block start bytes :
OCamlString.length bytes < 2^32 →
checkedSafe (fun _ ⇒ True) (@V.M.update_field Host_t F _ MS.memory
(@Mem.write_block_at Host_t F start bytes)).
Proof.
intro Hsize. apply checked_safe_memory_update. intros mem Hwords Hfits.
destruct (@write_block_chunks_or_bad_comparison Host_t F
(Opaque.legacy_witness tt) (Opaque.legacy_witness nil)
(Opaque.legacy_witness String.EmptyString) start bytes mem Hwords Hsize)
as [Hafter|Hbad]; [|right; exact Hbad].
left. split; [exact Hafter|apply write_block_preserves_sizes; exact Hfits].
Qed.
Lemma checked_safe_spend {UG : Basics.Unreachable Gas.t} amount :
checkedSafe (fun _ ⇒ True) (@V.spend Host_t F amount).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_spend|apply sizes_safe_spend].
Qed.
Lemma checked_safe_pop : checkedSafe wordRange (@V.pop Host_t F).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_pop|apply sizes_safe_pop].
Qed.
Lemma checked_safe_pop2 : checkedSafe (fun p ⇒ wordRange (fst p) ∧ wordRange (snd p))
(@V.pop2 Host_t F).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_pop2|apply sizes_safe_pop2].
Qed.
Lemma checked_safe_pop3 : checkedSafe
(fun p ⇒ wordRange (fst (fst p)) ∧ wordRange (snd (fst p)) ∧ wordRange (snd p))
(@V.pop3 Host_t F).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_pop3|apply sizes_safe_pop3].
Qed.
Lemma checked_safe_fmap {A B : Set} (P : A → Prop) (f : A → B) action :
checkedSafe P action →
checkedSafe (fun result ⇒ ∃ value, P value ∧ result = f value)
(@V.M.op_ltdollargt Host_t F A B f action).
Proof.
intro Haction. change (checkedSafe (fun result ⇒ ∃ value, P value ∧ result = f value)
(@V.M.op_letdollar Host_t F A B action (fun v ⇒ @V.M._return Host_t F B (f v)))).
eapply checked_safe_bind; [exact Haction|]. intros value Hvalue.
apply checked_safe_return. ∃ value. split; [exact Hvalue|reflexivity].
Qed.
Section Extension.
Context {E : @V.Executor.FArgs Host_t F}
{UN : Basics.Unreachable Support.UintN.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UI : Basics.Unreachable int}
{Ucost : Basics.Unreachable (Support.UintN.t → Support.UintN.t)}
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}.
Lemma checked_safe_extend_memory_bounded start size :
BoundedMemory F →
checkedSafe (fun _ ⇒ True) (@V.Executor.extend_memory_to Host_t F E start size).
Proof.
intro Hrevision. apply checked_safe_combine.
- apply memory_safe_lift, (extend_memory_preserves_words_bounded invariant ). exact Hrevision.
- apply sizes_safe_extend_memory_bounded. exact Hrevision.
Qed.
Lemma checked_safe_extend_memory start size :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe (fun _ ⇒ True) (@V.Executor.extend_memory_to Host_t F E start size).
Proof. intros; eapply checked_safe_extend_memory_bounded with (start := start) (size := size); eauto using eleven_bounded_memory. Qed.
Lemma checked_safe_extend_input_bounded start size :
BoundedMemory F →
0 ≤ U.to_z start → 0 ≤ U.to_z size →
checkedSafe (fun _ ⇒ 0 ≤ U.to_z size < 2^32)
(@V.Executor.extend_memory_to Host_t F E start size).
Proof.
intros Hrevision Hstart Hsize state Hinitial.
destruct (checked_safe_extend_memory_bounded start size Hrevision state Hinitial)
as [[Hafter _]|Hfailure]; [|right; exact Hfailure].
left. split; [exact Hafter|].
destruct (@V.Executor.extend_memory_to Host_t F E start size state)
as [[gas|error] after] eqn:Hrun; [|exact I].
exact (@extended_input_size_bounded_bounded Host_t F E UF UG
(Opaque.legacy_witness 0%Z) start size state gas after
Hrevision Hstart Hsize (proj2 Hinitial) Hrun).
Qed.
Lemma checked_safe_extend_input start size :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
0 ≤ U.to_z start → 0 ≤ U.to_z size →
checkedSafe (fun _ ⇒ 0 ≤ U.to_z size < 2^32)
(@V.Executor.extend_memory_to Host_t F E start size).
Proof. intros; eapply checked_safe_extend_input_bounded with (start := start) (size := size); eauto using eleven_bounded_memory. Qed.
End Extension.
Definition boundedCheckedSafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
∀ state, Z.of_N (MS.t.gas state) < 2^63 → checkedMachine state →
(checkedMachine (snd (action state)) ∧
match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
badMemoryInitComparison.
Lemma bounded_checked_from_unbounded {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
checkedSafe P action → boundedCheckedSafe P action.
Proof. intros Haction state _ Hinitial. apply Haction. exact Hinitial. Qed.
Lemma bounded_checked_bind_prefix {A B : Set} (P : A → Prop) (Q : B → Prop)
(action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
checkedSafe P action → gas_nonincreasing action →
(∀ value, P value → boundedCheckedSafe Q (next value)) →
boundedCheckedSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
intros Haction Hgas Hnext state Hbound Hinitial.
destruct (Haction state Hinitial) as [Hprefix|Hfailure]; [|right; exact Hfailure].
change ((checkedMachine (snd
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
match fst
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
with inl v ⇒ Q v | inr _ ⇒ True end) ∨ badMemoryInitComparison).
destruct (action state) as [[value|error] middle] eqn:Hrun; cbn [fst snd] in Hprefix.
- apply Hnext; [exact (proj2 Hprefix)| |exact (proj1 Hprefix)].
pose proof (Hgas state value middle Hrun). lia.
- left. exact Hprefix.
Qed.
Lemma bounded_checked_bind_suffix {A B : Set} (P : A → Prop) (Q : B → Prop)
(action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
boundedCheckedSafe P action → (∀ value, P value → checkedSafe Q (next value)) →
boundedCheckedSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
intros Haction Hnext state Hbound Hinitial.
destruct (Haction state Hbound Hinitial) as [Hprefix|Hfailure]; [|right; exact Hfailure].
change ((checkedMachine (snd
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
match fst
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
with inl v ⇒ Q v | inr _ ⇒ True end) ∨ badMemoryInitComparison).
destruct (action state) as [[value|error] middle]; cbn [fst snd] in Hprefix.
- apply Hnext; [exact (proj2 Hprefix)|exact (proj1 Hprefix)].
- left. exact Hprefix.
Qed.
End Machine.
Print Assumptions checked_safe_call.
OCamlString.length bytes < 2^32 →
checkedSafe (fun _ ⇒ True) (@V.M.update_field Host_t F _ MS.memory
(@Mem.write_block_at Host_t F start bytes)).
Proof.
intro Hsize. apply checked_safe_memory_update. intros mem Hwords Hfits.
destruct (@write_block_chunks_or_bad_comparison Host_t F
(Opaque.legacy_witness tt) (Opaque.legacy_witness nil)
(Opaque.legacy_witness String.EmptyString) start bytes mem Hwords Hsize)
as [Hafter|Hbad]; [|right; exact Hbad].
left. split; [exact Hafter|apply write_block_preserves_sizes; exact Hfits].
Qed.
Lemma checked_safe_spend {UG : Basics.Unreachable Gas.t} amount :
checkedSafe (fun _ ⇒ True) (@V.spend Host_t F amount).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_spend|apply sizes_safe_spend].
Qed.
Lemma checked_safe_pop : checkedSafe wordRange (@V.pop Host_t F).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_pop|apply sizes_safe_pop].
Qed.
Lemma checked_safe_pop2 : checkedSafe (fun p ⇒ wordRange (fst p) ∧ wordRange (snd p))
(@V.pop2 Host_t F).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_pop2|apply sizes_safe_pop2].
Qed.
Lemma checked_safe_pop3 : checkedSafe
(fun p ⇒ wordRange (fst (fst p)) ∧ wordRange (snd (fst p)) ∧ wordRange (snd p))
(@V.pop3 Host_t F).
Proof.
apply checked_safe_combine; [apply memory_safe_lift, safe_pop3|apply sizes_safe_pop3].
Qed.
Lemma checked_safe_fmap {A B : Set} (P : A → Prop) (f : A → B) action :
checkedSafe P action →
checkedSafe (fun result ⇒ ∃ value, P value ∧ result = f value)
(@V.M.op_ltdollargt Host_t F A B f action).
Proof.
intro Haction. change (checkedSafe (fun result ⇒ ∃ value, P value ∧ result = f value)
(@V.M.op_letdollar Host_t F A B action (fun v ⇒ @V.M._return Host_t F B (f v)))).
eapply checked_safe_bind; [exact Haction|]. intros value Hvalue.
apply checked_safe_return. ∃ value. split; [exact Hvalue|reflexivity].
Qed.
Section Extension.
Context {E : @V.Executor.FArgs Host_t F}
{UN : Basics.Unreachable Support.UintN.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UI : Basics.Unreachable int}
{Ucost : Basics.Unreachable (Support.UintN.t → Support.UintN.t)}
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}.
Lemma checked_safe_extend_memory_bounded start size :
BoundedMemory F →
checkedSafe (fun _ ⇒ True) (@V.Executor.extend_memory_to Host_t F E start size).
Proof.
intro Hrevision. apply checked_safe_combine.
- apply memory_safe_lift, (extend_memory_preserves_words_bounded invariant ). exact Hrevision.
- apply sizes_safe_extend_memory_bounded. exact Hrevision.
Qed.
Lemma checked_safe_extend_memory start size :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe (fun _ ⇒ True) (@V.Executor.extend_memory_to Host_t F E start size).
Proof. intros; eapply checked_safe_extend_memory_bounded with (start := start) (size := size); eauto using eleven_bounded_memory. Qed.
Lemma checked_safe_extend_input_bounded start size :
BoundedMemory F →
0 ≤ U.to_z start → 0 ≤ U.to_z size →
checkedSafe (fun _ ⇒ 0 ≤ U.to_z size < 2^32)
(@V.Executor.extend_memory_to Host_t F E start size).
Proof.
intros Hrevision Hstart Hsize state Hinitial.
destruct (checked_safe_extend_memory_bounded start size Hrevision state Hinitial)
as [[Hafter _]|Hfailure]; [|right; exact Hfailure].
left. split; [exact Hafter|].
destruct (@V.Executor.extend_memory_to Host_t F E start size state)
as [[gas|error] after] eqn:Hrun; [|exact I].
exact (@extended_input_size_bounded_bounded Host_t F E UF UG
(Opaque.legacy_witness 0%Z) start size state gas after
Hrevision Hstart Hsize (proj2 Hinitial) Hrun).
Qed.
Lemma checked_safe_extend_input start size :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
0 ≤ U.to_z start → 0 ≤ U.to_z size →
checkedSafe (fun _ ⇒ 0 ≤ U.to_z size < 2^32)
(@V.Executor.extend_memory_to Host_t F E start size).
Proof. intros; eapply checked_safe_extend_input_bounded with (start := start) (size := size); eauto using eleven_bounded_memory. Qed.
End Extension.
Definition boundedCheckedSafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
∀ state, Z.of_N (MS.t.gas state) < 2^63 → checkedMachine state →
(checkedMachine (snd (action state)) ∧
match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
badMemoryInitComparison.
Lemma bounded_checked_from_unbounded {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
checkedSafe P action → boundedCheckedSafe P action.
Proof. intros Haction state _ Hinitial. apply Haction. exact Hinitial. Qed.
Lemma bounded_checked_bind_prefix {A B : Set} (P : A → Prop) (Q : B → Prop)
(action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
checkedSafe P action → gas_nonincreasing action →
(∀ value, P value → boundedCheckedSafe Q (next value)) →
boundedCheckedSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
intros Haction Hgas Hnext state Hbound Hinitial.
destruct (Haction state Hinitial) as [Hprefix|Hfailure]; [|right; exact Hfailure].
change ((checkedMachine (snd
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
match fst
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
with inl v ⇒ Q v | inr _ ⇒ True end) ∨ badMemoryInitComparison).
destruct (action state) as [[value|error] middle] eqn:Hrun; cbn [fst snd] in Hprefix.
- apply Hnext; [exact (proj2 Hprefix)| |exact (proj1 Hprefix)].
pose proof (Hgas state value middle Hrun). lia.
- left. exact Hprefix.
Qed.
Lemma bounded_checked_bind_suffix {A B : Set} (P : A → Prop) (Q : B → Prop)
(action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
boundedCheckedSafe P action → (∀ value, P value → checkedSafe Q (next value)) →
boundedCheckedSafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
intros Haction Hnext state Hbound Hinitial.
destruct (Haction state Hbound Hinitial) as [Hprefix|Hfailure]; [|right; exact Hfailure].
change ((checkedMachine (snd
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)) ∧
match fst
(match action state with (inl v, st) ⇒ next v st | (inr e, st) ⇒ (inr e, st) end)
with inl v ⇒ Q v | inr _ ⇒ True end) ∨ badMemoryInitComparison).
destruct (action state) as [[value|error] middle]; cbn [fst snd] in Hprefix.
- apply Hnext; [exact (proj2 Hprefix)|exact (proj1 Hprefix)].
- left. exact Hprefix.
Qed.
End Machine.
Print Assumptions checked_safe_call.