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
  QedkConcrete.stakingaccounting.
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 staking_contract_preserves_creation fb0 fb1 fb2 msg :
  preserves (MonadVM.Staking.staking_contract fb0 fb1 fb2 msg).
Proof.
  intros s Hwf.
  destruct (staking_contract_uframe fb0 fb1 fb2 msg s)
    as [[_ [_ [_ [Hcreated [_ [_ [_ [Hdestruct _]]]]]]]] [_ [Haccounts _]]].
  destruct (Haccounts Hwf) as [Hwf' Hfields].
  split; [exact Hwf'|].
  constructor.
  - exact (eq_ind_r (fun l ⇒ incl (S.t.accounts_created_in_current_transaction s) l)
      (incl_refl _) Hcreated).
  - exact (eq_ind_r (fun m ⇒ ∀ a, List.In a m →
      List.In a (S.t.self_destruct s) ∨
      List.In a (S.t.accounts_created_in_current_transaction (snd (MonadVM.Staking.staking_contract fb0 fb1 fb2 msg s))))
      (fun a Hin ⇒ or_introl Hin) Hdestruct).
  - intros a _. exact (proj1 (proj2 (Hfields a))).
Qed.

Lemma staking_syscalls_preserves_creation fb0 msg :
  preserves (MonadVM.Staking.staking_syscalls fb0 msg).
Proof.
  intros s Hwf.
  destruct (staking_syscalls_uframe fb0 msg s)
    as [[[_ [_ [_ [Hcreated [_ [_ [_ [Hdestruct _]]]]]]]] [_ [Haccounts _]]] |
        [[_ [_ [_ [Hcreated [_ [_ [_ [Hdestruct _]]]]]]]] [_ [Haccounts _]]]].
  - destruct (Haccounts Hwf) as [Hwf' Hfields].
    split; [exact Hwf'|].
    constructor.
    + exact (eq_ind_r (fun l ⇒ incl (S.t.accounts_created_in_current_transaction s) l)
        (incl_refl _) Hcreated).
    + exact (eq_ind_r (fun m ⇒ ∀ a, List.In a m →
        List.In a (S.t.self_destruct s) ∨
        List.In a (S.t.accounts_created_in_current_transaction (snd (MonadVM.Staking.staking_syscalls fb0 msg s))))
        (fun a Hin ⇒ or_introl Hin) Hdestruct).
    + intros a _. exact (proj1 (proj2 (proj2 (Hfields a)))).
  - destruct (creditStakingAccount_fields (Evmc.Message.t.value msg) s) as [_ [_ [_ Hc_acc]]].
    destruct (Hc_acc Hwf) as [Hwf1 Hfields1].
    destruct (Haccounts Hwf1) as [Hwf' Hfields2].
    split; [exact Hwf'|].
    constructor.
    + exact (eq_ind_r (fun l ⇒ incl (S.t.accounts_created_in_current_transaction s) l)
        (incl_refl _) Hcreated).
    + exact (eq_ind_r (fun m ⇒ ∀ a, List.In a m →
        List.In a (S.t.self_destruct s) ∨
        List.In a (S.t.accounts_created_in_current_transaction (snd (MonadVM.Staking.staking_syscalls fb0 msg s))))
        (fun a Hin ⇒ or_introl Hin) Hdestruct).
    + intros a _.
      etransitivity; [exact (proj1 (proj2 (proj2 (Hfields2 a)))) | exact (proj1 (proj2 (Hfields1 a)))].
Qed.

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_pair_value in Hnative as [[_ ->] | [_ ->]];
      match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end;
      inversion Hselected; subst action.
    + apply creation_return.
    + apply reserve_precompile_preserves_creation.
    + apply creation_return.
    + apply staking_contract_preserves_creation.
Qed.

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

Local Abbreviation processSyscall := (@H.process_syscall (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_syscall_preserves_creation msg :
  preserves (processSyscall msg).
Proof.
  unfold H.process_syscall, H.M. cbn zeta.
  match goal with |- preserves (match ?lookup with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct lookup as [endpoint|] eqn:Hfind end.
  - unfold Precompiles.Make.syscall_endpoints in Hfind.
    apply precompilesframe.find_singleton_value in Hfind as [_ ->].
    apply staking_syscalls_preserves_creation.
  - apply creation_return.
Qed.

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 kind 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 kind msg).
Proof.
  intros Hrevision Hvm Hgas s Hwf.
  pose proof (fun from_tx ⇒ process_call_preserves_creation from_tx msg Hrevision
    (fun code ⇒ Hvm msg code Hgas)) as Hcall.
  pose proof (process_syscall_preserves_creation msg) as Hsyscall.
  pose proof (process_create_preserves_creation
    (F := H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg Hvm Hgas) as Hcreate.
  unfold H.call_impl.
  Local Opaque H.process_call H.process_syscall H.process_create
    Chain.Ethereum.Address.of_contract_creation Reserve_balance.dipped_into_reserve.
  destruct (Evmc.Message.t.kind msg); destruct kind as [|tx|]; cbn.
  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 ⇒
         lazymatch action with
         | context [H.process_call ?ft] ⇒ exact (Hcall ft initial Hwf)
         | context [H.process_syscall] ⇒ exact (Hsyscall initial Hwf)
         | context [H.process_create] ⇒
           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
       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 status;
    cbn; unfold Monad.Make_Monad.when_; cbn;
    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 H.Evm_call); assumption.
Qed.

End Host.

Print Assumptions native_preserves_creation.
Print Assumptions call_impl_preserves_creation.