Library QedkConcrete.vmuncreatednonce

VM nonce frame until creation

Empty accounts cannot initiate CREATE. A CALL to such an account resolves to empty code. If a child creates the tracked address, later instructions retain that recorded exception instead of claiming its nonce is unchanged.
From Stdlib Require Import ZArith List String.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmcreateframe QedkConcrete.vmcallframe
  QedkConcrete.vmhostframe QedkConcrete.vminvariant QedkConcrete.vmboundedframe
  QedkConcrete.vmentryframe QedkConcrete.vmprotectedframe QedkConcrete.vmemptycall
  QedkConcrete.uncreatednonce QedkConcrete.creationassembly QedkConcrete.vmframe
  QedkConcrete.hostframe QedkConcrete.hostcode.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".

Section Opcodes.
  Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
  Context {F : @MonadVM.Vm.Make.FArgs S.t}.
  Context `{U0 : RocqOfOCaml.Basics.Unreachable Z.t}.
  Context `{U1 :
    RocqOfOCaml.Basics.Unreachable (RocqOfOCaml.OCamlSeq.t int)}.
  Context `{U2 : RocqOfOCaml.Basics.Unreachable RocqOfOCaml.OCamlInt32.t}.
  Context `{U3 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}.
  Context `{U4 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}.
  Context `{U5 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}.
  Context `{U7 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.Integer.t}.
  Context `{U8 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.I256.t}.
  Context `{U9 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}.
  Context `{U10 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Delegation.Address.t}.
  Context `{U11 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}.
  Context `{U12 :
    RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.Bytes.t}.
  Context `{U13 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.M.Option.t S.t F MonadVM.Numeric.U256.t)}.
  Context `{U15 : RocqOfOCaml.Basics.Unreachable unit}.
  Context `{U16 : RocqOfOCaml.Basics.Unreachable string}.
  Context `{U17 :
    RocqOfOCaml.Basics.Unreachable (option MonadVM.Numeric.U256.t)}.
  Context `{U18 : RocqOfOCaml.Basics.Unreachable (option string)}.
  Context `{U19 :
    RocqOfOCaml.Basics.Unreachable
      (list MonadVM.Byte_string.Bytes.t)}.
  Context `{U20 : RocqOfOCaml.Basics.Unreachable (list int)}.
  Context `{U21 : RocqOfOCaml.Basics.Unreachable int}.
  Context `{U22 : RocqOfOCaml.Basics.Unreachable ascii}.
  Context `{U23 : RocqOfOCaml.Basics.Unreachable (int × int)}.
  Context `{U24 :
    RocqOfOCaml.Basics.Unreachable
      (Z.t → MonadVM.Numeric.U256.t)}.
  Context `{U25 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Support.UintN.t → MonadVM.Support.UintN.t)}.
  Context `{U26 :
    RocqOfOCaml.Basics.Unreachable
      (unit → RocqOfOCaml.OCamlSeq.node int)}.
  Context `{U27 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.opcode_impl S.t F)}.
  Context `{U28 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.t)}.
  Context `{U31 :
    RocqOfOCaml.Basics.Unreachable
      MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}.
  Context `{U32 :
    RocqOfOCaml.Basics.Unreachable
      (MonadVM.Numeric.U256.t → MonadVM.Numeric.U256.t →
        @MonadVM.Vm.Make.Memory.t S.t F →
        @MonadVM.Vm.Make.M.Option.t S.t F
          (@MonadVM.Vm.Make.Memory.t S.t F))}.
  Context `{U33 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.Memory.t S.t F →
        MonadVM.Support.UintN.t)}.

  Let State := @MonadVM.Vm.Make.MachineState.t S.t F.

Context (query : Addr.t) (nonce : Numeric.U64.t).
Local Abbreviation protected := (@uncreatedNonce query nonce).
Local Abbreviation stillUncreated := (fun s ⇒ protected s ∧
  ¬ List.In query (S.t.accounts_created_in_current_transaction s)).

Lemma execute_preserves_uncreated_nonce_context
    {Uint64 : Basics.Unreachable MonadVM.Int64.t}
    {Uresult : Basics.Unreachable Evmc.Result.t}
    {Urun : Basics.Unreachable (sum unit Evmc.Result.StatusCode.t × State)}
    msg code host :
  (@V.Params S.t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (@V.Params S.t F).(Vm.Make_Params_signature.debug_tstore) = false →
  PrimitiveHostFrame (@V.Host S.t F) protected →
  PrimitiveHostFrame (@V.Host S.t F) stillUncreated →
  (∀ initial, PrimitiveHostFrame (@V.Host S.t F) (@creationInvariant initial)) →
  preservesCallback protected ((@V.Host S.t F).(Evmc.HOST.get_tx_context)) →
  Evmc.Message.t.recipient msg ≠ query →
  (∀ size h, stillUncreated h → (@V.Host S.t F).(Evmc.HOST.copy_code)
    query 0%Z size h = (Byte_string.Bytes.empty, h)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    @preservesCreation _ ((@V.Host S.t F).(Evmc.HOST.call) nested)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (Evmc.Message.t.recipient msg) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback protected ((@V.Host S.t F).(Evmc.HOST.call) nested)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → protected host →
  protected (snd (@V.execute S.t F
    msg code host)).
Proof.
  intros Hrevision Hdebug Hprimitive Hpre Hcreation Hread Hdifferent Hcopy Htracking Hcall Hgas Hinitial.
  apply execute_preserves_from_calls_at; try assumption; [apply primitive_frame_at; exact Hprimitive|].
  intros env opcode before Hself Hopcode Hbound Hbefore.
  destruct (List.in_dec String.string_dec query
    (S.t.accounts_created_in_current_transaction (MS.t.host before))) as [Hin|Hnot].
  - match goal with |- protected (MS.t.host (snd (?action before))) ⇒
      assert (Hframe : preservesBoundedHost (@creationInvariant (MS.t.host before)) action)
    end.
    { apply recursive_opcode_preserves; [apply Hcreation| |exact Hopcode].
      intros nested Hnested. apply creation_callback, Htracking. exact Hnested. }
    destruct (Hframe before Hbound (conj (proj1 Hbefore) (creation_frame_refl _))) as [Hwf Hkeep].
    split; [exact Hwf|left; exact (creations_retained _ _ Hkeep query Hin)].
  - assert (Henv : PrimitiveHostFrameAt (@V.Host S.t F) protected (@V.Executor.self S.t F env))
      by (apply primitive_frame_at; exact Hprimitive).
    assert (HenvPre : PrimitiveHostFrameAt (@V.Host S.t F) stillUncreated (@V.Executor.self S.t F env))
      by (apply primitive_frame_at; exact Hpre).
    assert (HselfDifferent : @V.Executor.self S.t F env ≠ query) by congruence.
    assert (Hnested : ∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
      childCallFrom (@V.Executor.self S.t F env) nested →
      (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
        Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
        (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
      preservesCallback protected ((@V.Host S.t F).(Evmc.HOST.call) nested)).
    { rewrite Hself. exact Hcall. }
    destruct opcode; cbn [recursiveCallOpcode] in Hopcode; try discriminate.
    all: cbn [dispatch V.Executor.execute_opcode].
    + apply (create_preserves_protected_recipient protected query Henv Hnested before Hbound Hbefore).
    + eapply call_preserves_empty_recipient_between with (pre := stillUncreated) (post := protected) (query := query);
        try eassumption; [intros h [Hh _]; exact Hh|split; assumption].
    + apply (callcode_preserves_protected_recipient protected query Henv HselfDifferent Hnested before Hbound Hbefore).
    + apply (delegatecall_preserves_protected_recipient protected query Henv HselfDifferent Hnested before Hbound Hbefore).
    + apply (create2_preserves_protected_recipient protected query Henv Hnested before Hbound Hbefore).
    + eapply staticcall_preserves_empty_recipient_between with (pre := stillUncreated) (post := protected) (query := query);
        try eassumption; [intros h [Hh _]; exact Hh|split; assumption].
Qed.
End Opcodes.