Library QedkConcrete.vmstoragewords
Storage instructions preserve word widths
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vmmachinewords QedkConcrete.wordrange
QedkConcrete.bytewords QedkConcrete.vmhostwords QedkConcrete.hoststoragewords
QedkConcrete.hostframe QedkConcrete.hostbalance QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UU : Basics.Unreachable unit}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}.
Lemma sload_words_from_host :
(∀ addr key, machineSafe invariant (fun _ ⇒ True)
(@V.M.HostAPI.access_storage Host_t F addr key)) →
(∀ addr key, machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_storage Host_t F addr key)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.sload Host_t F E).
Proof.
intros Haccess Hstorage. unfold V.Executor.sload.
eapply safe_bind; [apply safe_pop|]. intros key _.
eapply safe_bind; [apply Haccess|]. intros access _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind; [apply safe_fmap, Hstorage|]. intros value [bytes [Hbytes ->]].
eapply safe_bind.
- apply safe_push, word_repr. lia.
- intros [] _. apply safe_increase_pc.
Qed.
Lemma sstore_words_from_host
:
(∀ addr key, machineSafe invariant (fun _ ⇒ True)
(@V.M.HostAPI.access_storage Host_t F addr key)) →
(∀ addr key value, OCamlString.length value = 32 →
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.set_storage Host_t F addr key value)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.sstore Host_t F E).
Proof.
intros Haccess Hstorage. unfold V.Executor.sstore.
eapply safe_bind; [apply safe_pop2|]. intros [key value] _.
eapply safe_bind.
- apply (safe_read invariant (fun _ ⇒ True)). intros; exact I.
- intros gas _. eapply safe_bind; [apply safe_when, safe_fail|]. intros [] _.
eapply safe_bind.
+ unfold V.Executor.check_write_permissions.
match goal with |- machineSafe _ _ (if ?allowed then _ else _) ⇒ destruct allowed end.
× apply (safe_return invariant (fun _ ⇒ True)). exact I.
× apply safe_fail.
+ intros [] _. eapply safe_bind; [apply Haccess|]. intros access _.
eapply safe_bind; [apply Hstorage, word_encoding_length|]. intros status _.
cbn zeta. eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind with (P := fun _ : unit ⇒ True).
× intros state Hstate. split; [exact Hstate|exact I].
× intros [] _. apply safe_increase_pc.
Qed.
Lemma tload_words_from_host :
(∀ addr key, machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_transient_storage Host_t F addr key)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.tload Host_t F E).
Proof.
intro Hstorage. unfold V.Executor.tload.
eapply safe_bind; [apply safe_pop|]. intros key _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind; [apply safe_fmap, Hstorage|]. intros value [bytes [Hbytes ->]].
eapply safe_bind.
- apply safe_push, word_repr. lia.
- intros [] _. apply safe_increase_pc.
Qed.
Lemma tstore_words_from_host :
(∀ addr key value, OCamlString.length value = 32 →
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.set_transient_storage Host_t F addr key value)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.tstore Host_t F E).
Proof.
intro Hstorage. unfold V.Executor.tstore.
eapply safe_bind; [apply safe_pop2|]. intros [key value] _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- unfold V.Executor.check_write_permissions.
match goal with |- machineSafe _ _ (if ?allowed then _ else _) ⇒ destruct allowed end.
+ apply (safe_return invariant (fun _ ⇒ True)). exact I.
+ apply safe_fail.
- intros [] _. eapply safe_bind; [apply Hstorage, word_encoding_length|].
intros [] _. apply safe_increase_pc.
Qed.
End Instructions.
Section Concrete.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Context (params : Vm.Make_Params_signature).
Local Abbreviation VF := (V.Build_FArgs params (HostF vm)).
Local Abbreviation invariant := (fun s ⇒ accountStorageWords (S.t.world_state s)).
Lemma concrete_get_storage_words addr key :
machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_storage S.t VF addr key).
Proof.
apply safe_host_lift. intros s Hinv. split.
- exact Hinv.
- cbn -[H.get_storage]. apply get_storage_word_length. exact Hinv.
Qed.
Lemma concrete_access_storage_words addr key :
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.access_storage S.t VF addr key).
Proof.
apply safe_host_frame. intros s Hinv.
change (accountStorageWords (S.t.world_state
(snd ((HostF vm).(Evmc.HOST.access_storage) addr key s)))).
cbn -[H.access_storage S.t.world_state accountStorageWords].
rewrite access_storage_preserves_world. exact Hinv.
Qed.
Lemma concrete_set_storage_words addr key value :
OCamlString.length value = 32 →
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.set_storage S.t VF addr key value).
Proof.
intro Hvalue. apply safe_host_frame. intros s Hinv.
cbn -[H.set_storage S.t.world_state accountStorageWords].
apply set_storage_preserves_words; assumption.
Qed.
Context {E : @V.Executor.FArgs S.t VF}.
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UU : Basics.Unreachable unit}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}.
Lemma concrete_sload_words : machineSafe invariant (fun _ ⇒ True)
(@V.Executor.sload S.t VF E).
Proof.
apply sload_words_from_host; [apply concrete_access_storage_words|apply concrete_get_storage_words].
Qed.
Lemma concrete_sstore_words
:
machineSafe invariant (fun _ ⇒ True) (@V.Executor.sstore S.t VF E).
Proof.
apply sstore_words_from_host; [apply concrete_access_storage_words|apply concrete_set_storage_words].
Qed.
End Concrete.
Section ConcreteTransient.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Context (params : Vm.Make_Params_signature).
Local Abbreviation VF := (V.Build_FArgs params (HostF vm)).
Lemma concrete_get_transient_storage_words addr key :
machineSafe transientStorageWords (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_transient_storage S.t VF addr key).
Proof.
apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
cbn -[H.get_transient_storage]. apply get_transient_storage_word_length. exact Hinv.
Qed.
Lemma concrete_set_transient_storage_words addr key value :
OCamlString.length value = 32 →
machineSafe transientStorageWords (fun _ ⇒ True)
(@V.M.HostAPI.set_transient_storage S.t VF addr key value).
Proof.
intro Hvalue. apply safe_host_frame. intros s Hinv.
cbn -[H.set_transient_storage transientStorageWords].
apply set_transient_storage_preserves_words; assumption.
Qed.
Context {E : @V.Executor.FArgs S.t VF}.
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UU : Basics.Unreachable unit}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}.
Lemma concrete_tload_words : machineSafe transientStorageWords (fun _ ⇒ True)
(@V.Executor.tload S.t VF E).
Proof. apply tload_words_from_host, concrete_get_transient_storage_words. Qed.
Lemma concrete_tstore_words : machineSafe transientStorageWords (fun _ ⇒ True)
(@V.Executor.tstore S.t VF E).
Proof. apply tstore_words_from_host, concrete_set_transient_storage_words. Qed.
End ConcreteTransient.
Print Assumptions concrete_sload_words.
Print Assumptions concrete_sstore_words.
Print Assumptions concrete_tload_words.
Print Assumptions concrete_tstore_words.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vmmachinewords QedkConcrete.wordrange
QedkConcrete.bytewords QedkConcrete.vmhostwords QedkConcrete.hoststoragewords
QedkConcrete.hostframe QedkConcrete.hostbalance QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UU : Basics.Unreachable unit}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}.
Lemma sload_words_from_host :
(∀ addr key, machineSafe invariant (fun _ ⇒ True)
(@V.M.HostAPI.access_storage Host_t F addr key)) →
(∀ addr key, machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_storage Host_t F addr key)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.sload Host_t F E).
Proof.
intros Haccess Hstorage. unfold V.Executor.sload.
eapply safe_bind; [apply safe_pop|]. intros key _.
eapply safe_bind; [apply Haccess|]. intros access _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind; [apply safe_fmap, Hstorage|]. intros value [bytes [Hbytes ->]].
eapply safe_bind.
- apply safe_push, word_repr. lia.
- intros [] _. apply safe_increase_pc.
Qed.
Lemma sstore_words_from_host
:
(∀ addr key, machineSafe invariant (fun _ ⇒ True)
(@V.M.HostAPI.access_storage Host_t F addr key)) →
(∀ addr key value, OCamlString.length value = 32 →
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.set_storage Host_t F addr key value)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.sstore Host_t F E).
Proof.
intros Haccess Hstorage. unfold V.Executor.sstore.
eapply safe_bind; [apply safe_pop2|]. intros [key value] _.
eapply safe_bind.
- apply (safe_read invariant (fun _ ⇒ True)). intros; exact I.
- intros gas _. eapply safe_bind; [apply safe_when, safe_fail|]. intros [] _.
eapply safe_bind.
+ unfold V.Executor.check_write_permissions.
match goal with |- machineSafe _ _ (if ?allowed then _ else _) ⇒ destruct allowed end.
× apply (safe_return invariant (fun _ ⇒ True)). exact I.
× apply safe_fail.
+ intros [] _. eapply safe_bind; [apply Haccess|]. intros access _.
eapply safe_bind; [apply Hstorage, word_encoding_length|]. intros status _.
cbn zeta. eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind with (P := fun _ : unit ⇒ True).
× intros state Hstate. split; [exact Hstate|exact I].
× intros [] _. apply safe_increase_pc.
Qed.
Lemma tload_words_from_host :
(∀ addr key, machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_transient_storage Host_t F addr key)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.tload Host_t F E).
Proof.
intro Hstorage. unfold V.Executor.tload.
eapply safe_bind; [apply safe_pop|]. intros key _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind; [apply safe_fmap, Hstorage|]. intros value [bytes [Hbytes ->]].
eapply safe_bind.
- apply safe_push, word_repr. lia.
- intros [] _. apply safe_increase_pc.
Qed.
Lemma tstore_words_from_host :
(∀ addr key value, OCamlString.length value = 32 →
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.set_transient_storage Host_t F addr key value)) →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.tstore Host_t F E).
Proof.
intro Hstorage. unfold V.Executor.tstore.
eapply safe_bind; [apply safe_pop2|]. intros [key value] _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- unfold V.Executor.check_write_permissions.
match goal with |- machineSafe _ _ (if ?allowed then _ else _) ⇒ destruct allowed end.
+ apply (safe_return invariant (fun _ ⇒ True)). exact I.
+ apply safe_fail.
- intros [] _. eapply safe_bind; [apply Hstorage, word_encoding_length|].
intros [] _. apply safe_increase_pc.
Qed.
End Instructions.
Section Concrete.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Context (params : Vm.Make_Params_signature).
Local Abbreviation VF := (V.Build_FArgs params (HostF vm)).
Local Abbreviation invariant := (fun s ⇒ accountStorageWords (S.t.world_state s)).
Lemma concrete_get_storage_words addr key :
machineSafe invariant (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_storage S.t VF addr key).
Proof.
apply safe_host_lift. intros s Hinv. split.
- exact Hinv.
- cbn -[H.get_storage]. apply get_storage_word_length. exact Hinv.
Qed.
Lemma concrete_access_storage_words addr key :
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.access_storage S.t VF addr key).
Proof.
apply safe_host_frame. intros s Hinv.
change (accountStorageWords (S.t.world_state
(snd ((HostF vm).(Evmc.HOST.access_storage) addr key s)))).
cbn -[H.access_storage S.t.world_state accountStorageWords].
rewrite access_storage_preserves_world. exact Hinv.
Qed.
Lemma concrete_set_storage_words addr key value :
OCamlString.length value = 32 →
machineSafe invariant (fun _ ⇒ True) (@V.M.HostAPI.set_storage S.t VF addr key value).
Proof.
intro Hvalue. apply safe_host_frame. intros s Hinv.
cbn -[H.set_storage S.t.world_state accountStorageWords].
apply set_storage_preserves_words; assumption.
Qed.
Context {E : @V.Executor.FArgs S.t VF}.
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UU : Basics.Unreachable unit}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}.
Lemma concrete_sload_words : machineSafe invariant (fun _ ⇒ True)
(@V.Executor.sload S.t VF E).
Proof.
apply sload_words_from_host; [apply concrete_access_storage_words|apply concrete_get_storage_words].
Qed.
Lemma concrete_sstore_words
:
machineSafe invariant (fun _ ⇒ True) (@V.Executor.sstore S.t VF E).
Proof.
apply sstore_words_from_host; [apply concrete_access_storage_words|apply concrete_set_storage_words].
Qed.
End Concrete.
Section ConcreteTransient.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Context (params : Vm.Make_Params_signature).
Local Abbreviation VF := (V.Build_FArgs params (HostF vm)).
Lemma concrete_get_transient_storage_words addr key :
machineSafe transientStorageWords (fun bytes ⇒ OCamlString.length bytes = 32)
(@V.M.HostAPI.get_transient_storage S.t VF addr key).
Proof.
apply safe_host_lift. intros s Hinv. split; [exact Hinv|].
cbn -[H.get_transient_storage]. apply get_transient_storage_word_length. exact Hinv.
Qed.
Lemma concrete_set_transient_storage_words addr key value :
OCamlString.length value = 32 →
machineSafe transientStorageWords (fun _ ⇒ True)
(@V.M.HostAPI.set_transient_storage S.t VF addr key value).
Proof.
intro Hvalue. apply safe_host_frame. intros s Hinv.
cbn -[H.set_transient_storage transientStorageWords].
apply set_transient_storage_preserves_words; assumption.
Qed.
Context {E : @V.Executor.FArgs S.t VF}.
Context {UR : Basics.Unreachable U.Repr.t} {UF : Basics.Unreachable U.t}
{UG : Basics.Unreachable Gas.t} {UU : Basics.Unreachable unit}
{UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
{UP : Basics.Unreachable (int × int)}.
Lemma concrete_tload_words : machineSafe transientStorageWords (fun _ ⇒ True)
(@V.Executor.tload S.t VF E).
Proof. apply tload_words_from_host, concrete_get_transient_storage_words. Qed.
Lemma concrete_tstore_words : machineSafe transientStorageWords (fun _ ⇒ True)
(@V.Executor.tstore S.t VF E).
Proof. apply tstore_words_from_host, concrete_set_transient_storage_words. Qed.
End ConcreteTransient.
Print Assumptions concrete_sload_words.
Print Assumptions concrete_sstore_words.
Print Assumptions concrete_tload_words.
Print Assumptions concrete_tstore_words.