Library QedkConcrete.vmentryframe

Frame through the actual VM entry point

Initialization and receipt construction preserve the Host invariant. The loop uses the fallback arguments selected by the generated Executor functor; no equality between runtime fallback values is assumed.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmUnsignedGas.
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.

Initializing the machine and returning its result introduce no extra Host writes. This connects the loop lemma to the actual VM entry point.
Lemma execute_preserves_from_calls_at
    (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)) →
  (∀ (env : @V.Executor.FArgs Host_t F) opcode before,
    @V.Executor.self Host_t F env = Evmc.Message.t.recipient msg →
    recursiveCallOpcode opcode = true →
    (Z.of_N (MS.t.gas before) < 2^63)%Z → invariant (MS.t.host before) →
    invariant (MS.t.host (snd (@dispatch Host_t F env
    opcode before)))) →
  (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 Hcalls Hgas Hinitial.
  pose proof (Hread host Hinitial) as Hcontext.
  unfold V.execute.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial].
  destruct ((@V.Host Host_t F).(Evmc.HOST.get_tx_context) host)
    as [context middle] eqn:HreadResult.
  cbn [snd] in Hcontext.
  cbn [Monad.Identity.Impl.op_gtgteq].
  match goal with |- context [match ?execution with (_, _) ⇒ _ end] ⇒
    lazymatch execution with
    | @V.Executor.run _ _ ?env ?state ⇒
      assert (Hrun : invariant (MS.t.host (snd execution))) by
        (apply (@run_preserves_from_calls_at Host_t F
          env invariant state);
        [exact Hrevision|exact Hdebug|exact Hprimitive|exact (fun opcode before ⇒ Hcalls env opcode before eq_refl)|
         change (Z.of_N (@Gas.of_uint64 (Evmc.Message.t.gas msg)) < 2^63)%Z;
         apply initial_gas_fits_signed_int64; exact Hgas|exact Hcontext]);
      destruct execution as [result finalState]
    end
  end.
  exact Hrun.
Qed.

Lemma execute_preserves_from_calls
    (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 →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.get_tx_context)) →
  (∀ (env : @V.Executor.FArgs Host_t F) opcode before,
    recursiveCallOpcode opcode = true →
    (Z.of_N (MS.t.gas before) < 2^63)%Z → invariant (MS.t.host before) →
    invariant (MS.t.host (snd (@dispatch Host_t F env
    opcode before)))) →
  (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 Hcalls Hgas Hinitial.
  apply execute_preserves_from_calls_at; try assumption.
  - apply primitive_frame_at. exact Hprimitive.
  - intros env opcode before _. apply Hcalls.
Qed.

Entering an account with empty code performs no Host writes or calls. This is essential when the recipient is the protected undelegated account: the unrelated-account callback lemma need not cover executing its code.
Lemma execute_empty_preserves_host
    msg host :
  (@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
  snd ((@V.Host Host_t F).(Evmc.HOST.get_tx_context) host) = host →
  snd (@V.execute Host_t F
    msg Byte_string.Bytes.empty host) = host.
Proof.
  intros Hdebug Hread.
  unfold V.execute.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial].
  destruct ((@V.Host Host_t F).(Evmc.HOST.get_tx_context) host)
    as [context middle] eqn:Hcontext.
  cbn [snd] in Hread. subst middle.
  cbn [Monad.Identity.Impl.op_gtgteq].
  match goal with |- context [@V.Executor.run ?ht ?f ?env ?state] ⇒
    pose proof (@generated_run_stop_exact ht f env
      state Hdebug eq_refl) as Hrun;
    unfold generated_run in Hrun;
    destruct (@V.Executor.run ht f env state)
      as [result finalState] eqn:Hresult;
    inversion Hrun; subst
  end.
  reflexivity.
Qed.
End Opcodes.

Print Assumptions execute_preserves_from_calls.

Print Assumptions execute_empty_preserves_host.