Library QedkConcrete.vmdelegatedframe

Complete VM frame in a delegated account context

From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmcreateframe QedkConcrete.vmcallframe
  QedkConcrete.vmhostframe QedkConcrete.vminvariant QedkConcrete.vmboundedframe
  QedkConcrete.vmentryframe QedkConcrete.vmdelegatedcreate.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".

Section Opcodes.
  Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_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 Host_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 Host_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 Host_t F →
        @MonadVM.Vm.Make.M.Option.t Host_t F
          (@MonadVM.Vm.Make.Memory.t Host_t F))}.
  Context `{U33 :
    RocqOfOCaml.Basics.Unreachable
      (@MonadVM.Vm.Make.Memory.t Host_t F →
        MonadVM.Support.UintN.t)}.

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

A complete VM execution in a delegated account context requires no creation-callback premise. This is a consequence of the opcode guard, not an assumption that delegated code preserves nonces.
Lemma execute_preserves_delegated_context
    {Uint64 : Basics.Unreachable MonadVM.Int64.t}
    {Uresult : Basics.Unreachable Evmc.Result.t}
    {Urun : Basics.Unreachable (sum unit Evmc.Result.StatusCode.t × State)}
    (invariant : Host_t → Prop) msg code host :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (Evmc.Message.t.recipient msg) →
  preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.get_tx_context)) →
  (∀ host, invariant host → @Delegation.is_valid_delegation
    (fst ((@V.Host Host_t F).(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0%Z
      (@Delegation.eoa_delegated_code_length) host)) = true) →
  (∀ 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 →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → invariant host →
  invariant (snd (@V.execute Host_t F
    msg code host)).
Proof.
  intros Hrevision Hdebug Hprimitive Hread Hdelegated Hcall Hgas Hinitial.
  apply execute_preserves_from_calls_at; try assumption.
  intros env opcode before Hself Hopcode Hbound Hbefore.
  assert (Henv : PrimitiveHostFrameAt (@V.Host Host_t F) invariant
    (@V.Executor.self Host_t F env)) by (rewrite Hself; exact Hprimitive).
  assert (Hnested : ∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F env) nested →
    Evmc.Message.t.kind nested ≠ Evmc.Message.CallKind.Create →
    Evmc.Message.t.kind nested ≠ Evmc.Message.CallKind.Create2 →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)).
  { rewrite Hself. exact Hcall. }
  assert (Hmarker : ∀ h, invariant h → @Delegation.is_valid_delegation
    (fst ((@V.Host Host_t F).(Evmc.HOST.copy_code) (@V.Executor.self Host_t F env) 0%Z
      (@Delegation.eoa_delegated_code_length) h)) = true).
  { rewrite Hself. exact Hdelegated. }
  match goal with |- invariant (MS.t.host (snd (?action before))) ⇒
    assert (Hframe : preservesBoundedHost invariant action)
  end.
  { destruct opcode; cbn [recursiveCallOpcode] in Hopcode; try discriminate.
    all: cbn [dispatch V.Executor.execute_opcode].
    - apply bounded_from_unbounded, create_delegated_preserves; assumption.
    - apply call_preserves_without_creation; assumption.
    - apply callcode_preserves_without_creation; assumption.
    - apply delegatecall_preserves_without_creation; assumption.
    - apply bounded_from_unbounded, create2_delegated_preserves; assumption.
    - apply staticcall_preserves_without_creation; assumption. }
  exact (Hframe before Hbound Hbefore).
Qed.
End Opcodes.