Library QedkConcrete.creationassembly

Code and destruction tracking through the generated recursion

A fixed entry state turns the two-state creation relation into a VM invariant. CALL rollback restores that relation; CREATE extends the recorded set before invoking initcode.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.creationhost QedkConcrete.vmhostframe
  QedkConcrete.hostcode QedkConcrete.hostframe QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.

Section Invariant.
Context {U64 : Basics.Unreachable Numeric.U64.t}
  {UF : Basics.Unreachable Numeric.U256.t}.

Definition creationInvariant (initial s : State.TransactionState.t) :=
  stringmaps.WellFormed (State.WorldState.t.accounts
    (State.TransactionState.t.world_state s)) ∧ @creationFrame initial s.

Lemma creation_callback {A : Set} initial (action : State.TransactionState.t → A × State.TransactionState.t) :
  @preservesCreation A action →
  preservesCallback (creationInvariant initial) action.
Proof.
  intros Haction s [Hwf Hbefore]. destruct (Haction s Hwf) as [Hafter Hframe].
  split; [exact Hafter|eapply creation_frame_trans; eassumption].
Qed.
End Invariant.

Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation invariant := (@creationInvariant).
Local Abbreviation preserves := preservesCreation.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF State.TransactionState.t (@recursiveArgs F)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF State.TransactionState.t (@recursiveArgs F)).

Lemma concrete_primitives_preserve_creation vm initial :
  PrimitiveHostFrame (HostF vm) (invariant initial).
Proof.
  constructor; intros.
  all: cbn -[Host.Make.account_exists Host.Make.get_balance Host.Make.access_account
    Host.Make.get_storage Host.Make.set_storage Host.Make.access_storage
    Host.Make.get_transient_storage Host.Make.set_transient_storage
    Host.Make.get_code_size Host.Make.get_code_hash Host.Make.copy_code
    Host.Make.get_block_hash Host.Make.emit_log Host.Make.selfdestruct invariant].
  all: apply creation_callback.
  all: try lazymatch goal with
    | |- preserves (Host.Make.set_storage _ _ _) ⇒ apply storage_preserves_creation
    | |- preserves (Host.Make.selfdestruct _ _) ⇒ apply selfdestruct_preserves_creation
    end.
  all: intros s Hwf.
  all: try (rewrite copy_code_read_only; split; [exact Hwf|apply creation_frame_refl]).
  all: try (split; [exact Hwf|apply creation_unchanged_fields; reflexivity]).
  all: unfold Host.Make.access_account, Host.Make.access_storage; cbn.
  all: match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
    (split; [exact Hwf|apply creation_unchanged_fields; reflexivity]).
Qed.

Definition creationHost (host : Evmc.HOST (t := State.TransactionState.t)) :=
  (∀ initial, PrimitiveHostFrame host (invariant initial)) ∧
  (∀ s, snd (host.(Evmc.HOST.get_tx_context) s) = s) ∧
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    preserves (host.(Evmc.HOST.call) msg)).

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

Lemma host_constructor_preserves_creation vm :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  creationVm vm → creationHost (HostF vm).
Proof.
  intros Hrevision Hvm. split; [apply concrete_primitives_preserve_creation|].
  split; [apply concrete_context_read_only|].
  intros msg Hgas. apply call_preserves_creation; assumption.
Qed.

Lemma recursive_assembly_preserves_creation :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ host, creationHost host → creationVm (VmF host)) →
  creationHost (@Evmc.Instantiate.Host State.TransactionState.t (@recursiveArgs F)) ∧
  creationVm (@Evmc.Instantiate.Vm State.TransactionState.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_creation; [exact Hrevision|].
      intros msg code Hgas s Hwf. split; [exact Hwf|apply creation_frame_refl].
    + intros msg code Hgas s Hwf. split; [exact Hwf|apply creation_frame_refl].
  - cbn [recursive_module_unroll fst snd]. split.
    + apply host_constructor_preserves_creation; [exact Hrevision|exact (proj2 IH)].
    + apply Hvm. exact (proj1 IH).
Qed.
End Assembly.

Print Assumptions recursive_assembly_preserves_creation.