Library QedkConcrete.systemnonces

System messages preserve the nonce scan

The prescribed bytecode contains no calls or creations. Its storage writes therefore leave sender and authority nonces unchanged, including delegated accounts. This covers the Host entry and rollback, not just the bytecode loop.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.executionnocalls
  QedkConcrete.systemmessages QedkConcrete.persistentnoncehost
  QedkConcrete.hostnonce QedkConcrete.hostnocalls QedkConcrete.systemcode
  QedkConcrete.hostcontextwords QedkConcrete.hashwords QedkConcrete.wordrange
  QedkConcrete.assemblychecked QedkConcrete.contexthostinstance
  QedkConcrete.vmhostframe QedkConcrete.hostframe QedkConcrete.systemvmentry
  QedkConcrete.vmsupplywords QedkConcrete.vmcallchecked
  QedkConcrete.transactionchecked QedkConcrete.hostaccountformat.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Import ListNotations.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module H := Host.Make.
Section Execution.
Context {F : E.FArgs}.

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

Local Opaque Evmc.Instantiate._recursive_modules_Host_Vm.
Section Calls.
Context (limit : Z).
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs args)).
Local Abbreviation VmF := (@Evmc.Instantiate.VmF S.t (@recursiveArgs args)).

Local Abbreviation invariant := (hostSupplyContext args.(Host.Instantiate.ChainParams) limit).

Local Abbreviation native := (@Reserve_balance.precompile_address).

Theorem execution_system_call_nonce msg initial query :
  query ≠ native →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → @vmMessageBounds msg →
  Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call →
  Numeric.U256.to_z (Evmc.Message.t.value msg) = 0 →
  S.t.accounts_created_in_current_transaction initial = [] →
  callFreeBytes (A.t.code (State.op_pointcaretlparenrparen (S.t.world_state initial)
    (@W.account None (Evmc.Message.t.code_address msg)))) = true →
  invariant initial →
  A.t.nonce (State.op_pointcaretlparenrparen
    (S.t.world_state (snd (@E.Host.call F
      msg initial))) (@W.account None query)) =
  A.t.nonce (State.op_pointcaretlparenrparen (S.t.world_state initial)
    (@W.account None query)).
Proof.
  intros Hdifferent Hrevision Hhash Hmsg Hkind Hzero Hcreated Hcode Hinitial.
  set (nonce := A.t.nonce (State.op_pointcaretlparenrparen
    (S.t.world_state initial) (@W.account None query))).
  assert (Hnonce : @nonceInvariant query nonce initial).
  { split; [exact (proj1 (proj1 (proj1 (proj1 Hinitial))))|reflexivity]. }
  assert (Hafter : @nonceInvariant query nonce (snd
    ((HostF (@H.Vm (@Host.Instantiate.Host_fargs args))).(Evmc.HOST.call) msg initial))).
  { apply (@system_call_free_preserves args
      (@H.Vm (@Host.Instantiate.Host_fargs args)) limit
      (@nonceInvariant query nonce) msg initial); try assumption.
    - intros code Hsize Hfree middle Hcontext Hbefore.
      pose proof (@execution_vm_call_free_preserves F
        limit
        (@nonceInvariant query nonce) msg code middle Hrevision
        Hhash Hmsg Hsize Hfree (fun vm ⇒ primitive_frame_at _ _ _
          (@concrete_primitives_preserve_nonce args vm query nonce)) Hcontext Hbefore) as Hresult.
      cbv beta iota zeta delta [Host.Instantiate.Vm.execute Host.Instantiate.Vm] in Hresult |- ×.
      exact Hresult.
    - intros action Hselected.
      exact (@native_preserves_other_nonce args
        (@H.Vm (@Host.Instantiate.Host_fargs args)) query nonce msg
        (Evmc.Message.t.code_address msg) action
        Hdifferent Hrevision Hselected).
    - intros before Hcontext Hbefore.
      exact (@transfer_preserves_nonce (H.Build_FArgs args.(Host.Instantiate.ChainParams)
          (@H.Vm (@Host.Instantiate.Host_fargs args)))
        query nonce (Evmc.Message.t.sender msg) (Evmc.Message.t.recipient msg)
        (Evmc.Message.t.value msg) before Hbefore). }
  unfold E.Host.call.
  cbv beta iota zeta delta [E.Instantiation Host.Instantiate.functor
    Host.Instantiate.Instantiate_result.Host Host.Instantiate.Host
    H.functor H.Make_result.call].
  exact (proj2 Hafter).
Qed.
End Calls.

Local Abbreviation params := args.(Host.Instantiate.ChainParams).
Local Abbreviation readAccount := (fun world addr ⇒ State.op_pointcaretlparenrparen
  world (@W.account None addr)).
Local Abbreviation systemMessage := (@E.process_system_message F).
Local Abbreviation hostCall := (@E.Host.call F).

Local Abbreviation native := (@Reserve_balance.precompile_address).

Theorem system_message_nonce block addr data query :
  query ≠ native →
  (@E.Params F).(Execution.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  keccak256OutputSize → wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  headerWords (Chain.Ethereum.Block.t.header (B.t.current_block block)) →
  OCamlString.length data < 2^62 →
  supply.balanceInvariant (B.t.world_state block) → worldFormat (B.t.world_state block) →
  callFreeBytes (A.t.code (readAccount (B.t.world_state block) addr)) = true →
  A.t.nonce (readAccount (B.t.world_state (snd (systemMessage block addr data))) query) =
    A.t.nonce (readAccount (B.t.world_state block) query).
Proof.
  intros Hdifferent Hrevision Hhash Hchain Hheader Hdata Hbalance Hformat Hcode.
  unfold E.process_system_message.
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  { reflexivity. }
  cbn beta iota zeta.
  match goal with |- context [E.Host.call ?message ?entry] ⇒
    set (msg := message); set (initial := entry) end.
  assert (Hmsg : @vmMessageBounds msg).
  { split; [change (0 ≤ 30000000 < 2^63); lia|].
    split; [change (0 ≤ 0 < 2^256); lia|]. split; [exact Hdata|].
    apply (encoded_capacity_all_decoders (UI := Opaque.legacy_witness 0%Z)).
    unfold E.vm_max_memory_usage. rewrite Hrevision. vm_compute. reflexivity. }
  assert (Hentry : hostSupplyContext params (supply.totalBalance (B.t.world_state block)) initial).
  { split.
    - split; [split; [exact Hbalance|apply Z.le_refl]|].
      split; [exact Hformat|constructor].
    - destruct Hheader as [Hbase [Hnumbers Hrandao]]. split.
      + constructor; [change (0 ≤ 0 < 2^256); lia|].
        constructor; [exact Hbase|]. constructor; [exact Hchain|constructor].
      + split; assumption. }
  pose proof (execution_system_call_nonce (supply.totalBalance (B.t.world_state block))
    msg initial query Hdifferent Hrevision Hhash Hmsg eq_refl eq_refl eq_refl Hcode Hentry) as Hnonce.
  destruct (hostCall msg initial) as [result afterVm].
  exact Hnonce.
Qed.
End Execution.