Library QedkConcrete.hostcallframe

Context frame through the concrete Host call paths

The recursive VM premise is kept explicit as an induction obligation. Value transfers, code lookup, native dispatch and rollback are proved from the generated Host rather than included in that premise.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostsupply QedkConcrete.supply QedkConcrete.worldaccounts.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.hostcomposition
  QedkConcrete.precompilesframe QedkConcrete.hostaccountformat QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Section Calls.
Context `{_fargs : H.FArgs}.

Local Abbreviation processCall := (@H.process_call _fargs).

Local Abbreviation processSyscall := (@H.process_syscall _fargs).

Local Abbreviation callImpl := (@H.call_impl _fargs).

Local Abbreviation hostCall := (@H.call _fargs).

Local Abbreviation callFromEoa := (@H.call_from_eoa _fargs).

Local Abbreviation callFromSyscall := (@H.call_from_monad_system_transaction _fargs).

Lemma process_syscall_preserves_context msg :
  preservesContext (processSyscall msg).
Proof.
  unfold H.process_syscall, H.M. cbn zeta.
  match goal with |- preservesContext (match ?lookup with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct lookup as [endpoint|] eqn:Hfind end.
  - exact (@syscall_endpoint_preserves_context (@H.Precompiles_fargs _fargs) msg _ endpoint Hfind).
  - apply context_return.
Qed.

Lemma process_call_preserves_context from_tx msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ code, preservesContext ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) msg code)) →
  preservesContext (processCall from_tx msg).
Proof.
  intros Hrevision Hvm. unfold H.process_call, H.M. cbn zeta.
  apply context_mbind.
  - destruct (H.should_transfer msg); [apply transfer_preserves_context|apply context_return].
  - intros transfer_ok. destruct transfer_ok; [|apply context_return].
    apply context_mbind.
    + apply context_mbind; [apply context_read|]. intro code.
      destruct from_tx; destruct (Delegation.get_delegated_address code);
        solve_context_frame.
    + intros [addr code].
      match goal with |- preservesContext (match ?selection with Some _ ⇒ _ | None ⇒ _ end) ⇒
        destruct selection as [action|] eqn:Hselected end.
      × exact (@try_precompile_preserves_context (@H.Precompiles_fargs _fargs) msg addr action Hrevision Hselected).
      × apply Hvm.
Qed.

Lemma call_impl_preserves_context kind msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesContext ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesContext (callImpl kind msg).
Proof.
  intros Hrevision Hvm Hgas s.
  pose proof (fun from_tx ⇒ process_call_preserves_context from_tx msg Hrevision
    (fun code ⇒ Hvm msg code Hgas)) as Hcall.
  pose proof (process_syscall_preserves_context msg) as Hsyscall.
  pose proof (@process_create_preserves_context _fargs
    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 : transactionContext (snd run) = transactionContext s) by
      (etransitivity;
       [lazymatch run with
        | context [H.process_call ?ft] ⇒ apply (Hcall ft)
        | context [H.process_syscall] ⇒ apply Hsyscall
        | context [H.process_create] ⇒ apply Hcreate
        end | reflexivity]);
    destruct run as [result intermediate] eqn:Hrun
  end.
  all: destruct result as [status gasleft refund output address].
  all: destruct status;
    cbn; unfold Monad.Make_Monad.when_; cbn;
    try reflexivity; try exact Hcore.
  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; [reflexivity|exact Hcore].
Qed.
Lemma call_preserves_context msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesContext ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesContext (hostCall msg).
Proof.
  intros Hrevision Hvm Hgas. unfold H.call, H.M.
  apply context_mbind.
  - match goal with |- preservesContext (if ?test then _ else _) ⇒ destruct test end.
    + unfold H.try_increment_nonce, H.M. solve_context_frame.
    + apply context_return.
  - intros noOverflow. destruct noOverflow; [|apply context_return].
    apply call_impl_preserves_context; assumption.
Qed.

Lemma call_from_eoa_preserves_context tx msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesContext ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesContext (callFromEoa tx msg).
Proof. apply call_impl_preserves_context. Qed.

Lemma call_from_monad_system_transaction_preserves_context msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesContext ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesContext (callFromSyscall msg).
Proof. apply call_impl_preserves_context. Qed.

Lemma process_call_preserves_supply from_tx msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ code, preservesSupply ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) msg code)) →
  (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
  preservesSupply (processCall from_tx msg).
Proof.
  Local Transparent H.process_call.
  intros Hrevision Hvm Hvalue. unfold H.process_call, H.M. cbn zeta.
  apply supply_mbind.
  - destruct (H.should_transfer msg); [apply supply_transfer; exact Hvalue|apply supply_return].
  - intros transfer_ok. destruct transfer_ok; [|apply supply_return].
    apply supply_mbind.
    + apply supply_mbind; [apply supply_read|]. intro code.
      destruct from_tx; destruct (Delegation.get_delegated_address code);
        solve_supply_frame.
    + intros [addr code].
      match goal with |- preservesSupply (match ?selection with Some _ ⇒ _ | None ⇒ _ end) ⇒
        destruct selection as [action|] eqn:Hselected end.
      × exact (@try_precompile_preserves_supply (@H.Precompiles_fargs _fargs) msg addr action Hrevision Hselected).
      × apply Hvm.
Qed.

Lemma call_impl_preserves_supply from_tx msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value nested))%Z →
    preservesSupply ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
  preservesSupply (callImpl (match from_tx with Some tx ⇒ H.Eoa_tx tx | None ⇒ H.Evm_call end) msg).
Proof.
  intros Hrevision Hvm Hgas Hvalue s Hinv.
  pose proof (process_call_preserves_supply from_tx msg Hrevision
    (fun code ⇒ Hvm msg code Hgas Hvalue) Hvalue) as Hcall.
  pose proof (@process_create_preserves_supply _fargs
    msg Hvm Hgas Hvalue) 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 from_tx as [tx|]; cbn.
  all: match goal with
  | |- context [Monad.Identity.Impl.op_gtgteq ?run _] ⇒
    assert (Hcore : balanceInvariant (S.t.world_state (snd run)) ∧
      (totalBalance (S.t.world_state (snd run)) ≤ totalBalance (S.t.world_state s))%Z) by
      (lazymatch run with ?f ?st ⇒
         first [exact (Hcall st Hinv)|exact (Hcreate st Hinv)] end);
    destruct run as [result intermediate] eqn:Hrun
  end.
  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 Hinv|apply Z.le_refl]).
  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 Hinv|apply Z.le_refl]|exact Hcore].
Qed.
Lemma call_preserves_supply msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value nested))%Z →
    preservesSupply ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z → preservesSupply (hostCall msg).
Proof.
  intros Hrevision Hvm Hgas Hvalue. unfold H.call, H.M.
  apply supply_mbind.
  - match goal with |- preservesSupply (if ?test then _ else _) ⇒ destruct test end.
    + unfold H.try_increment_nonce, H.M. solve_supply_frame.
    + apply supply_return.
  - intros noOverflow. destruct noOverflow; [|apply supply_return].
    apply (call_impl_preserves_supply None msg); assumption.
Qed.

Lemma call_from_eoa_preserves_supply tx msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value nested))%Z →
    preservesSupply ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z → preservesSupply (callFromEoa tx msg).
Proof. exact (call_impl_preserves_supply (Some tx) msg). Qed.

Lemma process_syscall_preserves_format msg :
  preservesCallback hostFormat (processSyscall msg).
Proof.
  Local Transparent H.process_syscall.
  unfold H.process_syscall, H.M. cbn zeta.
  match goal with |- preservesCallback hostFormat (match ?lookup with Some _ ⇒ _ | None ⇒ _ end) ⇒
    destruct lookup as [endpoint|] eqn:Hfind end.
  - exact (@syscall_endpoint_preserves_format (@H.Precompiles_fargs _fargs) msg _ endpoint Hfind).
  - apply format_return.
Qed.

Lemma process_call_preserves_format from_tx msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ code, preservesCallback hostFormat ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) msg code)) →
  preservesCallback hostFormat (processCall from_tx msg).
Proof.
  Local Transparent H.process_call.
  intros Hrevision Hvm. unfold H.process_call, H.M. cbn zeta.
  apply format_bind.
  - destruct (H.should_transfer msg); [intro; apply transfer_preserves_format|apply format_return].
  - intros transfer_ok. destruct transfer_ok; [|apply format_return].
    apply format_bind.
    + apply format_bind; [apply format_read|]. intro code.
      destruct from_tx; destruct (Delegation.get_delegated_address code);
        solve_format.
    + intros [addr code].
      match goal with |- preservesCallback hostFormat (match ?selection with Some _ ⇒ _ | None ⇒ _ end) ⇒
        destruct selection as [action|] eqn:Hselected end.
      × exact (@try_precompile_preserves_format (@H.Precompiles_fargs _fargs) msg addr action Hrevision Hselected).
      × apply Hvm.
Qed.

Lemma call_impl_preserves_format kind msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesCallback hostFormat ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preservesCallback hostFormat (callImpl kind msg).
Proof.
  intros Hrevision Hvm Hgas s Hformat.
  pose proof (fun from_tx ⇒ process_call_preserves_format from_tx msg Hrevision
    (fun code ⇒ Hvm msg code Hgas)) as Hcall.
  pose proof (process_syscall_preserves_format msg) as Hsyscall.
  pose proof (@process_create_preserves_format _fargs
    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 : hostFormat (snd run)) by
      (lazymatch run with ?f ?st ⇒
        lazymatch f with
        | context [H.process_call ?ft] ⇒ exact (Hcall ft st Hformat)
        | context [H.process_syscall] ⇒ exact (Hsyscall st Hformat)
        | context [H.process_create] ⇒ exact (Hcreate st Hformat)
        end end);
    destruct run as [result intermediate] eqn:Hrun
  end.
  all: destruct result as [status gasleft refund output address].
  all: destruct status;
    cbn; unfold Monad.Make_Monad.when_; cbn;
    try exact Hcore; try exact Hformat.
  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; [exact Hformat|exact Hcore].
Qed.
Lemma call_preserves_format msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesCallback hostFormat ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback hostFormat (hostCall msg).
Proof.
  intros Hrevision Hvm Hgas. unfold H.call, H.M.
  apply format_bind.
  - match goal with |- preservesCallback hostFormat (if ?test then _ else _) ⇒ destruct test end.
    + unfold H.try_increment_nonce, H.M. solve_format.
    + apply format_return.
  - intros noOverflow. destruct noOverflow; [|apply format_return].
    apply call_impl_preserves_format; assumption.
Qed.

Lemma call_from_eoa_preserves_format tx msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesCallback hostFormat ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback hostFormat (callFromEoa tx msg).
Proof. apply call_impl_preserves_format. Qed.

Lemma call_from_monad_system_transaction_preserves_format msg :
  (@H.ChainParams _fargs).(Chain.Monad.PARAMS.revision) = Support.Variants.Revision.Eleven →
  (∀ nested code, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    preservesCallback hostFormat ((@H.Vm _fargs).(Evmc.Vm.SIG.execute) nested code)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → preservesCallback hostFormat (callFromSyscall msg).
Proof. apply call_impl_preserves_format. Qed.

End Calls.

Print Assumptions call_preserves_context.
Print Assumptions call_from_eoa_preserves_context.

Print Assumptions call_from_eoa_preserves_supply.

Print Assumptions call_from_eoa_preserves_format.