Library QedkConcrete.creationhost

Creation tracking through the concrete Host constructor

The only stateful native precompile changes configuration storage, not code. Nested calls and failed creations preserve the code/creation relation; the recursive VM premise is discharged by the assembly proof.
From Stdlib Require Import List ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostcode QedkConcrete.assemblychecked
  QedkConcrete.precompilesframe QedkConcrete.contexthostinstance.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module H := Host.Make.
Module S := State.TransactionState.
Module W := State.WorldState.

Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation frame := (@creationFrame).
Local Abbreviation preserves := preservesCreation.
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)))).

Lemma native_preserves_creation msg addr action :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  nativeDispatch vm msg addr = Some action → preserves action.
Proof.
  intros Hrevision Hselected. unfold nativeDispatch, Precompiles.Make.try_precompile in Hselected.
  match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
    destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
  - destruct (Evmc.Message.t.delegated msg) in Hselected; inversion Hselected; subst action;
      apply creation_return.
  - unfold Precompiles.Make.monad_precompiles in Hselected.
    change (@Precompiles.Make.Revision (@H.Precompiles_fargs
      (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).(Precompiles.Make_Revision_signature.revision)
      with F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) in Hselected.
    rewrite Hrevision in Hselected.
    match type of Hselected with context [@Chain.Ethereum.Address.Map.find_opt ?T addr ?entries] ⇒
      destruct (@Chain.Ethereum.Address.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
    2: discriminate.
    apply precompilesframe.find_singleton_value in Hnative. subst native.
    match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
    + inversion Hselected; subst action. apply creation_return.
    + inversion Hselected; subst action. apply reserve_precompile_preserves_creation.
Qed.

Local Abbreviation processCall := (@H.process_call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).

Local Abbreviation callImpl := (@H.call_impl (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).

Local Abbreviation hostCall := (@H.call (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) ).

Lemma process_call_preserves_creation from_tx msg :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ code, preserves (vm.(Evmc.Vm.SIG.execute) msg code)) →
  preserves (processCall from_tx msg).
Proof.
  intros Hrevision Hvm. unfold H.process_call, H.M. cbn zeta.
  apply creation_bind.
  - destruct (H.should_transfer msg); [apply transfer_preserves_creation|apply creation_return].
  - intros transfer_ok. destruct transfer_ok; [|apply creation_return].
    apply creation_bind.
    + apply creation_bind; [apply creation_read|]. intro code.
      destruct from_tx.
      2: apply creation_return.
      match goal with |- context [match ?delegation with Some _ ⇒ _ | None ⇒ _ end] ⇒
        destruct delegation end;
        first [apply creation_return|apply creation_bind; [apply creation_read|intro; apply creation_return]].
    + intros [addr code].
      match goal with |- preserves (match ?selection with Some _ ⇒ _ | None ⇒ _ end) ⇒
        destruct selection as [action|] eqn:Hselected end.
      × apply (native_preserves_creation msg addr action Hrevision). exact Hselected.
      × apply Hvm.
Qed.

Lemma call_impl_preserves_creation from_tx msg :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preserves (callImpl from_tx msg).
Proof.
  intros Hrevision Hvm Hgas s Hwf.
  pose proof (process_call_preserves_creation from_tx msg Hrevision
    (fun code ⇒ Hvm msg code Hgas)) as Hcall.
  pose proof (process_create_preserves_creation
    (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg Hvm Hgas) as Hcreate.
  unfold H.call_impl.
  destruct (Evmc.Message.t.kind msg);
    cbn -[H.process_call H.process_create
      Chain.Ethereum.Address.of_contract_creation Reserve_balance.dipped_into_reserve].
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hcore : stringmaps.WellFormed (W.t.accounts (S.t.world_state (snd run))) ∧
      frame s (snd run)) by
      (lazymatch run with ?action ?initial ⇒
         first [exact (Hcall initial Hwf)|
           let Hstep := fresh "Hstep" in
           pose proof (Hcreate initial Hwf) as Hstep;
           destruct Hstep as [Hnext Hframe]; split; [exact Hnext|];
           eapply (@creation_frame_trans s initial);
           [apply creation_unchanged_fields; reflexivity|exact Hframe]]
end);
    destruct run as [result intermediate] eqn:Hrun
  end.
  all: cbn beta iota zeta delta [snd] in Hcore.
  all: destruct result as [status gasleft refund output address].
  all: destruct from_tx as [tx|]; destruct status;
    cbn -[Reserve_balance.dipped_into_reserve];
    unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve];
    try exact Hcore;
    try (split; [exact Hwf|apply creation_unchanged_fields; reflexivity]).
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq (Monad.Identity.Impl.op_gtgteq ?check _) _] ⇒
    assert (Hcheck : snd check = intermediate) by reflexivity;
    destruct check as [dipped checked]; cbn [snd] in Hcheck; subst checked
  end.
  all: destruct dipped; cbn; [split; [exact Hwf|apply creation_unchanged_fields; reflexivity]|exact Hcore].
Qed.
Lemma call_preserves_creation msg :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preserves (vm.(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preserves (hostCall msg).
Proof.
  intros Hrevision Hvm Hgas. unfold H.call, H.M.
  apply creation_bind.
  - match goal with |- preserves (if ?test then _ else _) ⇒ destruct test end.
    + intros s Hwf. unfold H.try_increment_nonce, H.M.
      cbn -[State.WorldState.account Numeric.U64.op_eq Numeric.U64.op_plus].
      match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
      × split; [exact Hwf|apply creation_frame_refl].
      × apply world_write_code_frame; [exact Hwf|left; reflexivity].
    + apply creation_return.
  - intros noOverflow. destruct noOverflow; [|apply creation_return].
    apply call_impl_preserves_creation; assumption.
Qed.

End Host.

Print Assumptions native_preserves_creation.
Print Assumptions call_impl_preserves_creation.