Library QedkConcrete.executionprotectedstorage

Protected native storage in Execution's generated VM/Host

The finite recursive-module induction discharges the VM premises of the Host isolation lemmas. The remaining native-call premise concerns the chosen storage property: it must be proved for the setter, including slot separation. It is not a balance or arbitrary-VM contract.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution MonadVM.Vm QedkConcrete.hostframe
  QedkConcrete.vmhostframe QedkConcrete.contexthostinstance QedkConcrete.vmframe
  QedkConcrete.vminvariant QedkConcrete.hoststoragewf
  QedkConcrete.storagehost QedkConcrete.storageassembly
  QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
  QedkConcrete.vmprotectedframe QedkConcrete.nondebitframe QedkConcrete.hostcode
  QedkConcrete.vmentryframe QedkConcrete.vmcallframe QedkConcrete.assemblychecked.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).

Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).
Context (query : Chain.Ethereum.Address.t)
  (P : Support.UintN.t → list (String.string × String.string) → Prop).
Local Abbreviation protected := (@protectedStorage query P).

Definition protectsStorageVm (vm : Evmc.Vm.SIG (_fargs :=
    Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs args)))) : Prop :=
  ∀ msg code, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    ∀ s, protected s →
    (Evmc.Message.t.recipient msg = query → code = Byte_string.Bytes.empty) →
    protected (snd (vm.(Evmc.Vm.SIG.execute) msg code s)).

Lemma execution_vm_constructor_protects_storage vm :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (∀ msg addr action, @nativeDispatch args vm msg addr = Some action →
    preservesCallback protected action) →
  protectsStorageVm vm → protectsStorageVm (VmF (HostF vm)).
Proof.
  intros Hrevision Hnative Hvm msg code Hgas initial Hinitial Hcode.
  destruct (String.string_dec (Evmc.Message.t.recipient msg) query) as [Heq|Hneq].
  - rewrite (Hcode Heq).
    change (protected (snd ((VmF (HostF vm)).(Evmc.Vm.SIG.execute)
      msg Byte_string.Bytes.empty initial))).
    cbn -[V.execute].
    match goal with |- protectedStorage _ _ (snd (@V.execute ?ht ?vf ?message ?body ?state)) ⇒
      pose proof (@execute_empty_preserves_host ht vf
        message state eq_refl (@concrete_context_read_only args vm state)) as Hsame
    end.
    exact (eq_ind_r protected Hinitial Hsame).
  - change (protected (snd ((VmF (HostF vm)).(Evmc.Vm.SIG.execute) msg code initial))).
    cbn -[V.execute].
    match goal with |- protectedStorage _ _ (snd (@V.execute ?ht ?vf ?message ?body ?state)) ⇒
      eapply (execute_preserves_protected_recipient (F := vf) protected query message body state)
    end.
    + exact Hrevision.
    + reflexivity.
    + exact (@concrete_primitives_preserve_protected_storage args vm
        (Evmc.Message.t.recipient msg) query P Hneq).
    + intros s Hs. exact Hs.
    + exact Hneq.
    + intros size s Hs. exact (@concrete_copy_empty_code args vm query size s
        (protected_storage_code _ _ _ Hs)).
    + intros nested Hbound Horigin Htarget.
      exact (@call_preserves_protected_storage args vm (Evmc.Message.t.recipient msg)
        query P nested Hnative Hvm Hbound Hneq Horigin Htarget).
    + exact Hgas.
    + exact Hinitial.
  Unshelve. all: lazymatch goal with |- Basics.Unreachable _ ⇒
    apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat end.
Qed.

Theorem execution_vm_protects_storage :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (∀ vm msg addr action, @nativeDispatch args vm msg addr = Some action →
    preservesCallback protected action) →
  protectsStorageVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs args)).
Proof.
  intros Hrevision Hnative.
  assert (Hseed : protectsStorageVm (@Evmc.Instantiate.recursive_module_base_vm
    S.t (@recursiveArgs args))).
  { intros msg code Hgas s Hinitial Hempty. exact Hinitial. }
  assert (Hstep : ∀ vm, protectsStorageVm vm → protectsStorageVm (VmF (HostF vm))).
  { intros vm Hvm. apply execution_vm_constructor_protects_storage;
      [exact Hrevision|apply Hnative|exact Hvm]. }
  assert (Hunroll : ∀ fuel,
    let pair := recursive_module_unroll fuel
      (@Evmc.Instantiate.recursive_module_seed S.t (@recursiveArgs args))
      (fun pair ⇒ (HostF (snd pair), VmF (fst pair))) in
    protectsStorageVm (snd pair) ∧ ∃ vm, protectsStorageVm vm ∧ fst pair = HostF vm).
  { intro fuel. induction fuel as [|fuel IH].
    - split; [exact Hseed|]. eexists; split; [exact Hseed|reflexivity].
    - cbn [recursive_module_unroll fst snd].
      destruct IH as [Hvm [inner [Hinner Hhost]]]. split.
      + rewrite Hhost. apply Hstep. exact Hinner.
      + eexists; split; [exact Hvm|reflexivity]. }
  exact (proj1 (Hunroll Evmc.Instantiate.recursive_module_fuel)).
Qed.

Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.

Theorem execution_call_from_eoa_protects_storage tx msg :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (∀ vm nested addr action, @nativeDispatch args vm nested addr = Some action →
    preservesCallback protected action) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
  preservesCallback protected (@E.Host.call_from_eoa F
    tx msg).
Proof.
  intros Hrevision Hnative Hgas Htarget.
  unfold E.Host.call_from_eoa.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    H.functor H.Make_result.call_from_eoa H.call_from_eoa].
  apply (@call_impl_preserves_protected_storage args
    (@H.Vm (@Host.Instantiate.Host_fargs args)) (Some tx) query P msg).
  - apply Hnative.
  - apply execution_vm_protects_storage; assumption.
  - exact Hgas.
  - exact Htarget.
Qed.

Theorem execution_call_from_monad_system_transaction_protects_storage msg :
  MonadVM.Staking.staking_address ≠ query →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  preservesCallback protected (@E.Host.call_from_monad_system_transaction F
    msg).
Proof.
  intros Hneq Hkind.
  unfold E.Host.call_from_monad_system_transaction.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    H.functor H.Make_result.call_from_monad_system_transaction H.call_from_monad_system_transaction].
  apply (@call_from_monad_system_transaction_preserves_protected_storage args
    (@H.Vm (@Host.Instantiate.Host_fargs args)) query P msg Hneq Hkind).
Qed.

System calls bypass the transaction reserve check, but still use the same code selection, CREATE collision check and storage callbacks.
Theorem execution_call_protects_storage msg :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (∀ vm nested addr action, @nativeDispatch args vm nested addr = Some action →
    preservesCallback protected action) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  Evmc.Message.t.sender msg ≠ query →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 ∨
    (Evmc.Message.t.recipient msg = query → Evmc.Message.t.code_address msg = query)) →
  preservesCallback protected (@E.Host.call F
    msg).
Proof.
  intros Hrevision Hnative Hgas Hsender Hkind Htarget. unfold E.Host.call.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    H.functor H.Make_result.call].
  apply (@call_preserves_protected_storage args
    (@H.Vm (@Host.Instantiate.Host_fargs args)) (Evmc.Message.t.sender msg)).
  - apply Hnative.
  - apply execution_vm_protects_storage; assumption.
  - exact Hgas.
  - exact Hsender.
  - unfold childCallFrom. rewrite Hkind. reflexivity.
  - exact Htarget.
Qed.
End Execution.

Print Assumptions execution_vm_protects_storage.
Print Assumptions execution_call_from_eoa_protects_storage.
Print Assumptions execution_call_protects_storage.