Library QedkConcrete.vmframe

Frame for the complete generated VM

All instructions, including recursive calls, preserve any Host invariant preserved by the concrete callbacks. Child gas bounds are derived from the instruction implementations, not assumed of the VM.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe
  QedkConcrete.vmboundedframe QedkConcrete.vmcreateframe QedkConcrete.vmcallframe
  QedkConcrete.vmentryframe.
Require Import 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}.
  #[local] Instance U9 : Basics.Unreachable Gas.t := ltac:(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat).
  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.

Lemma recursive_opcode_preserves (env : @V.Executor.FArgs Host_t F)
    (invariant : Host_t → Prop) opcode :
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
  recursiveCallOpcode opcode = true →
  preservesBoundedHost invariant (@dispatch Host_t F env
    opcode).
Proof.
  intros Hprimitive Hcall Hopcode.
  destruct opcode; cbn [recursiveCallOpcode] in Hopcode; try discriminate.
  all: cbn [dispatch V.Executor.execute_opcode].
  - apply create_preserves; assumption.
  - apply call_preserves; assumption.
  - apply callcode_preserves; assumption.
  - apply delegatecall_preserves; assumption.
  - apply create2_preserves; assumption.
  - apply staticcall_preserves; assumption.
Qed.

Lemma execute_preserves
    (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)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    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 Hcall Hgas Hinitial.
  apply execute_preserves_from_calls; try assumption.
  intros env opcode before Hopcode Hbound Hbefore.
  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 create_preserves; assumption.
    - apply call_preserves; assumption.
    - apply callcode_preserves; assumption.
    - apply delegatecall_preserves; assumption.
    - apply create2_preserves; assumption.
    - apply staticcall_preserves; assumption. }
  exact (Hframe before Hbound Hbefore).
Qed.

The current execution account is fixed by the message recipient. The proof now needs write/SELFDESTRUCT callbacks only at that address, and recursive calls only with the caller/context selected by the opcode.
Lemma execute_preserves_from_self
    (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)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (Evmc.Message.t.recipient msg) nested →
    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 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 →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)).
  { rewrite Hself. exact Hcall. }
  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 create_preserves_from_self; assumption.
    - apply call_preserves_from_self; assumption.
    - apply callcode_preserves_from_self; assumption.
    - apply delegatecall_preserves_from_self; assumption.
    - apply create2_preserves_from_self; assumption.
    - apply staticcall_preserves_from_self; assumption. }
  exact (Hframe before Hbound Hbefore).
Qed.
End Opcodes.

Print Assumptions execute_preserves.

Print Assumptions execute_preserves_from_self.