Library QedkConcrete.executioncreation

Concrete execution preserves code outside newly created addresses

This closes the Host/VM induction for creation and destruction tracking. It does not assume a VM code-frame contract. Excluding CREATE/CREATE2 at signer addresses is a separate cryptographic premise, not proved here.
From Stdlib Require Import List ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution MonadVM.Vm QedkConcrete.hostcode
  QedkConcrete.creationhost QedkConcrete.creationassembly QedkConcrete.vmframe
  QedkConcrete.vmhostframe QedkConcrete.contexthostinstance
  QedkConcrete.executioncontext QedkConcrete.hostframe.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.

Section Execution.
Context {F : E.FArgs}.

Local Abbreviation args := (@E.Instantiation_fargs F).

Local Abbreviation invariant := (@creationInvariant).
Local Abbreviation preserves := preservesCreation.

Lemma execution_vm_constructor_preserves_creation host :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  creationHost host →
  creationVm ((@recursiveArgs args).(Evmc.Instantiate.VmF) host).
Proof.
  intros Hrevision [Hprimitive [Hread Hcall]] msg code Hgas initial Hwf.
  change (invariant initial
    (snd (((@recursiveArgs args).(Evmc.Instantiate.VmF) host)
      .(Evmc.Vm.SIG.execute) msg code initial))).
  cbn -[Vm.Make.execute invariant].
  eapply execute_preserves with (invariant := invariant initial).
  - exact Hrevision.
  - reflexivity.
  - apply Hprimitive.
  - intros s Hs. rewrite Hread. exact Hs.
  - intros nested Hbound. apply creation_callback, Hcall, Hbound.
  - exact Hgas.
  - split; [exact Hwf|apply creation_frame_refl].
Qed.

Theorem execution_vm_preserves_creation msg code :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preserves (@Host.Instantiate.Vm.execute args msg code).
Proof.
  intros Hrevision Hgas.
  pose proof (@recursive_assembly_preserves_creation args Hrevision
    (fun host ⇒ execution_vm_constructor_preserves_creation host Hrevision)) as Hassembly.
  exact (proj2 Hassembly msg code Hgas).
Qed.

Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.

Theorem execution_call_from_eoa_preserves_creation tx msg :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preserves (@E.Host.call_from_eoa F
    tx msg).
Proof.
  intros Hrevision Hgas.
  unfold E.Host.call_from_eoa.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    Host.Make.functor Host.Make.Make_result.call_from_eoa Host.Make.call_from_eoa].
  apply (@call_impl_preserves_creation args
    (@Host.Make.Vm (@Host.Instantiate.Host_fargs args))
    (Host.Make.Eoa_tx tx) msg).
  - exact Hrevision.
  - intros nested code Hbound.
    pose proof (execution_vm_preserves_creation nested code Hrevision Hbound) as Hvm.
    cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
    exact Hvm.
  - exact Hgas.
Qed.

Theorem execution_call_from_monad_system_transaction_preserves_creation msg :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preserves (@E.Host.call_from_monad_system_transaction F
    msg).
Proof.
  intros Hrevision Hgas.
  unfold E.Host.call_from_monad_system_transaction.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    Host.Make.functor Host.Make.Make_result.call_from_monad_system_transaction
    Host.Make.call_from_monad_system_transaction].
  apply (@call_impl_preserves_creation args
    (@Host.Make.Vm (@Host.Instantiate.Host_fargs args))
    Host.Make.System_tx msg).
  - exact Hrevision.
  - intros nested code Hbound.
    pose proof (execution_vm_preserves_creation nested code Hrevision Hbound) as Hvm.
    cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
    exact Hvm.
  - exact Hgas.
Qed.
Local Abbreviation callFromEoa := (@E.Host.call_from_eoa F).
Local Abbreviation readAccount := (fun world addr ⇒ State.op_pointcaretlparenrparen
  world (@State.WorldState.account None addr)).

The reserve exemptions are unavailable for an address not created by this call, provided it had no preexisting destruction entry. The latter set is empty at transaction entry. No premise about final code is needed.
Corollary execution_call_noncreated_code tx msg initial addr :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  stringmaps.WellFormed (State.WorldState.t.accounts (State.TransactionState.t.world_state initial)) →
  ¬ List.In addr (State.TransactionState.t.self_destruct initial) →
  let after := snd (callFromEoa tx msg initial) in
  ¬ List.In addr (State.TransactionState.t.accounts_created_in_current_transaction after) →
  Chain.Ethereum.Account.t.code (readAccount (State.TransactionState.t.world_state after) addr) =
    Chain.Ethereum.Account.t.code (readAccount (State.TransactionState.t.world_state initial) addr) ∧
  Chain.Ethereum.Address._Set.(OCamlSet.S.mem) addr (State.TransactionState.t.self_destruct after) = false.
Proof.
  intros Hrevision Hgas Hwf Hbefore. cbn zeta. intro Hcreated.
  destruct (execution_call_from_eoa_preserves_creation tx msg Hrevision Hgas initial Hwf)
    as [_ Hframe].
  split; [exact (noncreated_code _ _ Hframe addr Hcreated)|].
  destruct (Chain.Ethereum.Address._Set.(OCamlSet.S.mem) addr
    (State.TransactionState.t.self_destruct (snd (callFromEoa tx msg initial)))) eqn:Hmem; [|reflexivity].
  apply address_set_mem_member in Hmem.
  destruct (destruction_covered _ _ Hframe addr Hmem); contradiction.
Qed.

Local Abbreviation emptying := (@Reserve_balance.is_emptying_tx).

Lemma emptying_same_context_code before after :
  transactionContext after = transactionContext before →
  Chain.Ethereum.Account.t.code (readAccount (State.TransactionState.t.world_state after)
    (State.TransactionState.t.tx_origin before)) =
  Chain.Ethereum.Account.t.code (readAccount (State.TransactionState.t.world_state before)
    (State.TransactionState.t.tx_origin before)) →
  fst (emptying after) = fst (emptying before).
Proof.
  intros Hcontext Hcode. unfold transactionContext in Hcontext.
  injection Hcontext as Hinitial Hblock Horigin Hlimit Hprice Hhistory Hcounter.
  unfold Reserve_balance.is_emptying_tx.
  cbn -[State.WorldState.account State.WorldState.next_emptying_transaction_block_for
    Delegation.is_valid_delegation].
  rewrite Horigin, Hblock.
  cbv beta in Hcode.
  match goal with |- andb _ ?counter = ?rhs ⇒
    change (andb (negb (@Delegation.is_valid_delegation (Chain.Ethereum.Account.t.code (readAccount (State.TransactionState.t.world_state after)
        (State.TransactionState.t.tx_origin before))))) counter = rhs)
  end.
  cbv beta. rewrite Hcode.
  unfold State.WorldState.next_emptying_transaction_block_for.
  cbn -[Chain.Ethereum.Address.Map.find_opt]. rewrite Hcounter. reflexivity.
Qed.

VM calls cannot change emptying eligibility by changing the sender's delegation, block metadata or counter, unless they create at its address. Authorization processing has already happened before this call.
Corollary execution_call_preserves_emptying tx msg initial :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  stringmaps.WellFormed (State.WorldState.t.accounts (State.TransactionState.t.world_state initial)) →
  let after := snd (callFromEoa tx msg initial) in
  ¬ List.In (State.TransactionState.t.tx_origin initial)
    (State.TransactionState.t.accounts_created_in_current_transaction after) →
  fst (@Reserve_balance.is_emptying_tx after) =
  fst (@Reserve_balance.is_emptying_tx initial).
Proof.
  intros Hrevision Hgas Hwf. cbn zeta. intro Hcreated.
  destruct (execution_call_from_eoa_preserves_creation tx msg Hrevision Hgas initial Hwf)
    as [_ Hframe].
  pose proof (noncreated_code _ _ Hframe _ Hcreated) as Hcode.
  pose proof (@execution_call_from_eoa_preserves_context F
    tx msg Hrevision Hgas initial) as Hcontext.
  apply emptying_same_context_code; assumption.
Qed.
System messages use Host.call, not the transaction-only entry point. Its nonce-overflow path does not weaken creation/code tracking.
Theorem execution_host_call_preserves_creation msg :
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
  preserves (@E.Host.call F
    msg).
Proof.
  intros Hrevision Hgas.
  unfold E.Host.call.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    Host.Make.functor Host.Make.Make_result.call].
  apply (@call_preserves_creation args
    (@Host.Make.Vm (@Host.Instantiate.Host_fargs args))).
  - exact Hrevision.
  - intros nested code Hbound.
    pose proof (execution_vm_preserves_creation nested code Hrevision Hbound) as Hvm.
    cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hvm |- ×.
    exact Hvm.
  - exact Hgas.
Qed.
End Execution.

Print Assumptions execution_vm_preserves_creation.
Print Assumptions execution_call_from_eoa_preserves_creation.

Print Assumptions execution_call_preserves_emptying.
Print Assumptions execution_host_call_preserves_creation.