Library QedkConcrete.storageassembly

Storage well-formedness through the recursive Host/VM assembly

The invariant holds at every recursive depth. A failed call restores a well-formed entry state, so no assumption about successful execution or precompile output lengths is required.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.vmhostframe
  QedkConcrete.contexthostinstance QedkConcrete.hoststoragewf QedkConcrete.storagehost.
Set Default Goal Selector "!".
Definition storageHost (host : Evmc.HOST (t := S.t)) :=
  PrimitiveHostFrame host hostStorageWf ∧
  (∀ s, snd (host.(Evmc.HOST.get_tx_context) s) = s) ∧
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    preservesCallback hostStorageWf (host.(Evmc.HOST.call) msg)).

Definition storageVm {T : Evmc.Vm_H_signature (t := S.t)}
    (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs T)) :=
  ∀ msg code, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    preservesCallback hostStorageWf (vm.(Evmc.Vm.SIG.execute) msg code).

Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs F)).

Lemma concrete_primitives_preserve_storage_wf vm : PrimitiveHostFrame (HostF vm) hostStorageWf.
Proof.
  constructor; unfold preservesCallback; intros.
  all: cbn -[H.account_exists H.get_balance H.access_account H.get_storage
    H.set_storage H.access_storage H.get_transient_storage H.set_transient_storage
    H.get_code_size H.get_code_hash H.copy_code H.get_block_hash H.emit_log
    H.selfdestruct hostStorageWf].
  all: try assumption.
  all: try (rewrite copy_code_read_only; assumption).
  all: lazymatch goal with
  | |- context [@H.set_storage] ⇒
    apply set_storage_preserves_wf; first [assumption|apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat]
  | |- context [@H.set_transient_storage] ⇒ assumption
  | |- context [@H.selfdestruct] ⇒
    apply selfdestruct_preserves_storage_wf; first [assumption|apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat]
  | _ ⇒ unfold H.access_account, H.access_storage;
    cbn -[hostStorageWf];
    match goal with |- context [if ?accessed then _ else _] ⇒ destruct accessed end;
    assumption
  end.
Qed.
Lemma host_constructor_preserves_storage_wf vm :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  storageVm vm → storageHost (HostF vm).
Proof.
  intros Hrevision Hvm. split; [apply concrete_primitives_preserve_storage_wf|].
  split; [apply concrete_context_read_only|].
  intros msg Hgas. apply call_preserves_storage_wf; assumption.
Qed.

Lemma recursive_assembly_preserves_storage_wf :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ host, storageHost host → storageVm (VmF host)) →
  storageHost (@Evmc.Instantiate.Host S.t (@recursiveArgs F)) ∧
  storageVm (@Evmc.Instantiate.Vm S.t (@recursiveArgs F)).
Proof.
  intros Hrevision Hvm.
  unfold Evmc.Instantiate.Host, Evmc.Instantiate.Vm,
    Evmc.Instantiate._recursive_modules_Host_Vm.
  generalize Evmc.Instantiate.recursive_module_fuel.
  intro fuel. induction fuel as [|fuel IH].
  - split.
    + apply host_constructor_preserves_storage_wf; [exact Hrevision|].
      intros msg code Hgas s Hwf. exact Hwf.
    + intros msg code Hgas s Hwf. exact Hwf.
  - cbn [recursive_module_unroll fst snd]. split.
    + apply host_constructor_preserves_storage_wf; [exact Hrevision|exact (proj2 IH)].
    + apply Hvm. exact (proj1 IH).
Qed.
End Assembly.
Print Assumptions recursive_assembly_preserves_storage_wf.