Library SetcodeConcrete.accounthost

Concrete Host calls preserve creation-or-account invariants

This generic version admits nonce increments. The more specific nonce theorem additionally uses the VM's CREATE guard.
From Stdlib Require Import List String ZArith NArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.hostframe
  QedkConcrete.hostcode QedkConcrete.vmhostframe QedkConcrete.recursiveassembly.
Require Import SetcodeConcrete.accountcreate SetcodeConcrete.accountframe SetcodeConcrete.creationtracking.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Lemma account_bind {U64 : Basics.Unreachable Numeric.U64.t}
    {UF : Basics.Unreachable Numeric.U256.t} {A B : Set} query P
    (action : S.t → A × S.t) (next : A → S.t → B × S.t) :
  preservesCallback (@accountFrame query P) action →
  (∀ value, preservesCallback (@accountFrame query P) (next value)) →
  preservesCallback (@accountFrame query P) (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
  intros Haction Hnext s Hinitial.
  change (@accountFrame query P (snd (let '(value, middle) := action s in next value middle))).
  specialize (Haction s Hinitial). destruct (action s) as [value middle].
  apply Hnext. exact Haction.
Qed.

Section Host.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation protected := accountFrame.
Context (cold warm access write reserve : N).
Context (query : Addr.t) (Hdifferent : query ≠ Reserve_balance.precompile_address).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs (@Evmc.Instantiate.T S.t (@recursiveArgs F)))).
Local Abbreviation nativeDispatch := (@Precompiles.Make.try_precompile
  (@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))).
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_account_frame from_tx P msg :
  (∀ addr action, nativeDispatch msg addr = Some action →
    preservesCallback (protected query P) action) →
  (∀ body s, protected query P s →
    protected query P (snd (vm.(Evmc.Vm.SIG.execute) msg body s))) →
  preservesCallback (protected query P) (processCall from_tx msg).
Proof.
  intros Hnative Hvm s Hinitial.
  assert (Htransfer : protected query P (snd
    ((if @H.should_transfer (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm) msg
      then @H.transfer_ether (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
        (Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg) (Evmc.Message.t.value msg)
      else fun st ⇒ (true, st)) s))).
  { destruct (H.should_transfer msg); [apply transfer_account_frame|]; exact Hinitial. }
  unfold H.process_call, H.M.
  cbn -[H.should_transfer H.transfer_ether W.account Precompiles.Make.try_precompile].
  match goal with |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hmiddle : protected query P (snd run)) by exact Htransfer;
    destruct run as [ok middle] eqn:Hrun end.
  cbn beta iota zeta delta [snd] in Hmiddle.
  destruct ok; [|exact Hmiddle].
  destruct from_tx as [tx|].
  all: cbn -[W.account Precompiles.Make.try_precompile Delegation.get_delegated_address].
  all: try match goal with |- context [@Delegation.get_delegated_address ?bytes] ⇒
    destruct (@Delegation.get_delegated_address bytes) as [delegated|] eqn:Hdelegation end.
  all: cbn -[W.account Precompiles.Make.try_precompile].
  all: match goal with |- context [match ?selection with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct selection as [action|] eqn:Hselected end.
  all: try (eapply Hnative; eassumption).
  all: apply Hvm; exact Hmiddle.
Qed.
Lemma call_impl_account_frame from_tx P msg :
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  (∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    ∀ initial, preservesCallback (creationsRetained initial)
      (vm.(Evmc.Vm.SIG.execute) nested body)) →
  (∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesCallback (protected query P) (vm.(Evmc.Vm.SIG.execute) nested body)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback (protected query P) (callImpl from_tx msg).
Proof.
  intros Hrevision Htracking Hvm Hgas s Hinitial.
  pose proof (process_call_account_frame from_tx P msg
    (fun addr action Hselected ⇒ @precompile_account_frame
      (@H.Precompiles_fargs (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm))
      cold warm access write reserve query P msg addr action Hrevision Hdifferent Hselected)
    (fun body ⇒ Hvm msg body Hgas)) as Hcall.
  pose proof (@process_create_account_frame
    (H.Build_FArgs F.(Host.Instantiate.ChainParams) vm)
    query P msg Htracking (fun nested body Hbound s HP _ ⇒ Hvm nested body Hbound s HP) 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 accountFrame].
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hcore : protected query P (snd run)) by
      (lazymatch run with ?action ?initial ⇒
        assert (Hentry : protected query P initial) by
          (exact Hinitial);
        first [exact (Hcall initial Hentry)|exact (Hcreate initial Hentry)]
       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 accountFrame];
    unfold Monad.Make_Monad.when_; cbn -[Reserve_balance.dipped_into_reserve accountFrame];
    try exact Hcore;
    try (exact Hinitial).
  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 -[accountFrame]; [exact Hinitial|exact Hcore].
Qed.
Lemma call_account_frame P msg :
  (∀ code nonce, P code nonce → P code (Numeric.U64.op_plus nonce Numeric.U64.one)) →
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  (∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    ∀ initial, preservesCallback (creationsRetained initial)
      (vm.(Evmc.Vm.SIG.execute) nested body)) →
  (∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesCallback (protected query P) (vm.(Evmc.Vm.SIG.execute) nested body)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback (protected query P) (hostCall msg).
Proof.
  intros Hnext Hrevision Htracking Hvm Hgas. unfold H.call, H.M.
  apply account_bind.
  - match goal with |- preservesCallback _ (if ?test then _ else _) ⇒ destruct test end.
    + apply increment_account_frame. exact Hnext.
    + intros s Hinitial. exact Hinitial.
  - intros ok. destruct ok.
    + apply call_impl_account_frame; assumption.
    + intros s Hinitial. exact Hinitial.
Qed.
Lemma call_account_frame_noncreator P msg :
  (Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create ∨
    Evmc.Message.t.kind msg = Evmc.Message.CallKind.Create2 →
    Evmc.Message.t.sender msg ≠ query) →
  F.(Host.Instantiate.ChainParams).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  (∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    ∀ initial, preservesCallback (creationsRetained initial)
      (vm.(Evmc.Vm.SIG.execute) nested body)) →
  (∀ nested body, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesCallback (protected query P) (vm.(Evmc.Vm.SIG.execute) nested body)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback (protected query P) (hostCall msg).
Proof.
  intros Hcreator Hrevision Htracking Hvm Hgas. unfold H.call, H.M.
  apply account_bind.
  - match goal with |- preservesCallback _ (if ?test then _ else _) ⇒ destruct test eqn:Hkind end.
    + apply increment_other_account_frame. apply Hcreator.
      destruct (Evmc.Message.t.kind msg); cbn in *; auto; discriminate.
    + intros s Hinitial. exact Hinitial.
  - intros ok. destruct ok.
    + apply call_impl_account_frame; assumption.
    + intros s Hinitial. exact Hinitial.
Qed.
End Host.