Library QedkConcrete.vmdispatchwords

Word preservation through the generated instruction dispatch

These are instruction obligations for the recursive VM proof, not whole-VM assumptions. Environment and returned-data bounds must still be established at entry and preserved across calls.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmopcodeframe QedkConcrete.vmarithmeticwords
  QedkConcrete.vmstackwords QedkConcrete.vmenvironmentwords
  QedkConcrete.vmbytewords QedkConcrete.vmhashwords QedkConcrete.vmmiscwords
  QedkConcrete.vmmemorywords QedkConcrete.vmchecked QedkConcrete.vmsizes.
Require Import QedkConcrete.vmmachinewords QedkConcrete.wordrange QedkConcrete.hashwords
  QedkConcrete.vmrun.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Require Import QedkConcrete.memorymodel.
Set Default Goal Selector "!".

Definition opcodeWordParameters (opcode : Opcode.t) : Prop :=
  match opcode with
  | Opcode.Push count ⇒ (0 ≤ count ≤ 32)%Z
  | Opcode.Dup index ⇒ (1 ≤ index ≤ 16)%Z
  | _ ⇒ True
  end.

Lemma decoded_opcode_word_parameters byte : opcodeWordParameters (Opcode.of_byte byte).
Proof.
  destruct byte as [a b c d e f g h].
  destruct a, b, c, d, e, f, g, h; vm_compute; intuition discriminate.
Qed.

Section Opcodes.
  Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_t}.
  Context {E : @MonadVM.Vm.Make.Executor.FArgs Host_t F}.
  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.

Local Abbreviation executeOpcode := (@dispatch Host_t F E).

Lemma selected_opcode_word_parameters state :
  opcodeWordParameters (@run_selected_opcode Host_t F E state).
Proof.
  unfold run_selected_opcode, V.select_opcode.
  destruct (@Numeric.U256.to_int_opt (MS.t.pc state)); [|exact I].
  match goal with |- context [if ?inside then _ else _] ⇒ destruct inside end;
    [apply decoded_opcode_word_parameters|exact I].
Qed.

Lemma returndatasize_preserves_words (invariant : Host_t → Prop) state :
  (OCamlString.length (MS.t.output_buffer state) < 2^256)%Z →
  machineWords invariant state →
  machineWords invariant (snd (@V.Executor.returndatasize Host_t F E state)).
Proof.
  intros Hlength Hinitial.
  assert (Heq : @V.Executor.returndatasize Host_t F E state =
    @V.Executor.fetch_environment_variable_opcode_impl Host_t F E
      (@Numeric.U256.of_int (OCamlString.length (MS.t.output_buffer state))) state).
  { unfold V.Executor.returndatasize, V.Executor.fetch_environment_variable_opcode_impl',
      V.Executor.fetch_environment_variable_opcode_impl, V.spend.
    cbv beta iota zeta delta -[V.push V.increase_pc_and_continue Gas.op_lt Gas.base
      Gas.op_minus Numeric.U256.of_int OCamlString.length].
    match goal with |- context [if ?test then _ else _] ⇒ destruct test end; reflexivity. }
  rewrite Heq. apply (proj1 (@fetch_word Host_t F E invariant
    (@Numeric.U256.of_int (OCamlString.length (MS.t.output_buffer state)))
    ltac:(apply word_int; unfold OCamlString.length in *; lia) state Hinitial)).
Qed.

All machine-only instructions, with the actual decoder's parameter bounds. RETURNDATASIZE additionally uses the current returned-data bound; preserving that bound across recursive calls remains a separate obligation.
Lemma machine_only_preserves_words_core
    (invariant : Host_t → Prop) opcode state :
  BoundedMemory F →
  machineSafe invariant (fun _ ⇒ True) (executeOpcode Opcode.Clz) →
  (∀ extra, extra = opcode → extension_opcode extra →
    machineWords invariant (snd (executeOpcode extra state)) ∨ badMemoryInitComparison) →
  (@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C Host_t F E).(V.Executor_C_signature.execution_environment)) →
  opcodeWordParameters opcode →
  machineOnlyOpcode opcode = true →
  (OCamlString.length (MS.t.output_buffer state) < 2^256)%Z →
  memoryFits (MS.t.memory state) →
  machineWords invariant state →
  machineWords invariant (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
  intros Hrevision Hclz Hextension Hdebug Hhash Henv Hparams Hlocal Hbuffer Hfits Hinitial.
  assert (Hlift : ∀ action : @V.M.t Host_t F bool,
    memorySafe invariant (fun _ ⇒ True) action →
    machineWords invariant (snd (action state)) ∨ badMemoryInitComparison).
  { intros action Hsafe. specialize (Hsafe state Hinitial). tauto. }
  assert (Hchecked : ∀ action : @V.M.t Host_t F bool,
    checkedSafe invariant (fun _ ⇒ True) action →
    machineWords invariant (snd (action state)) ∨ badMemoryInitComparison).
  { intros action Hsafe. specialize (Hsafe state (conj Hinitial (conj Hfits Hbuffer))).
    destruct Hsafe as [[Hafter _]|Hbad]; [left; exact (proj1 Hafter)|right; exact Hbad]. }
  destruct (arithmeticOpcode opcode) eqn:Harithmetic.
  { apply Hlift, memory_safe_lift, arithmetic_preserves_words_core;
      assumption. }
  destruct opcode; cbn [arithmeticOpcode] in Harithmetic;
    cbn [machineOnlyOpcode] in Hlocal; try discriminate;
    try solve [apply Hextension; [reflexivity|exact I]].
  all: cbn [opcodeWordParameters] in Hparams; cbn [dispatch V.Executor.execute_opcode].
  all: try solve [left; apply returndatasize_preserves_words; assumption].
  all: try solve [apply Hchecked; lazymatch goal with
  | |- context [@V.Executor.calldatacopy] ⇒ apply calldatacopy_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.codecopy] ⇒ apply codecopy_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.returndatacopy] ⇒ apply returndatacopy_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.mcopy] ⇒ apply mcopy_preserves_words_or_bad_comparison_bounded; assumption
  end].
  all: apply Hlift.
  all: lazymatch goal with
  | |- context [@V.Executor.stop] ⇒ apply memory_safe_lift, stop_preserves_words; assumption
  | |- context [@V.Executor.keccak] ⇒ apply memory_safe_lift, keccak_preserves_words_bounded; assumption
  | |- context [@V.Executor.address] ⇒ apply memory_safe_lift, address_preserves_words; assumption
  | |- context [@V.Executor.origin] ⇒ apply memory_safe_lift, origin_preserves_words; assumption
  | |- context [@V.Executor.caller] ⇒ apply memory_safe_lift, caller_preserves_words; assumption
  | |- context [@V.Executor.callvalue] ⇒ apply memory_safe_lift, callvalue_preserves_words; assumption
  | |- context [@V.Executor.calldataload] ⇒ apply memory_safe_lift, calldataload_preserves_words; assumption
  | |- context [@V.Executor.calldatasize] ⇒ apply memory_safe_lift, calldatasize_preserves_words; assumption
  | |- context [@V.Executor.codesize] ⇒ apply memory_safe_lift, codesize_preserves_words; assumption
  | |- context [@V.Executor.gasprice] ⇒ apply memory_safe_lift, gasprice_preserves_words; assumption
  | |- context [@V.Executor.coinbase] ⇒ apply memory_safe_lift, coinbase_preserves_words; assumption
  | |- context [@V.Executor.timestamp] ⇒ apply memory_safe_lift, timestamp_preserves_words; assumption
  | |- context [@V.Executor.number] ⇒ apply memory_safe_lift, number_preserves_words; assumption
  | |- context [@V.Executor.prevrandao] ⇒ apply memory_safe_lift, prevrandao_preserves_words; assumption
  | |- context [@V.Executor.gaslimit] ⇒ apply memory_safe_lift, gaslimit_preserves_words; assumption
  | |- context [@V.Executor.chainid] ⇒ apply memory_safe_lift, chainid_preserves_words; assumption
  | |- context [@V.Executor.basefee] ⇒ apply memory_safe_lift, basefee_preserves_words; assumption
  | |- context [@V.Executor.blobhash] ⇒ apply memory_safe_lift, blobhash_preserves_words; assumption
  | |- context [@V.Executor.blobbasefee] ⇒ apply memory_safe_lift, blobbasefee_preserves_words; assumption
  | |- context [@V.Executor.pop_] ⇒ apply memory_safe_lift, pop_preserves_words; assumption
  | |- context [@V.Executor.jump] ⇒ apply memory_safe_lift, jump_preserves_words; assumption
  | |- context [@V.Executor.jumpi] ⇒ apply memory_safe_lift, jumpi_preserves_words; assumption
  | |- context [@V.Executor.pc_] ⇒ apply memory_safe_lift, pc_preserves_words; assumption
  | |- context [@V.Executor.msize] ⇒ apply memory_safe_lift, msize_preserves_words; assumption
  | |- context [@V.Executor.gas_] ⇒ apply memory_safe_lift, gas_preserves_words; assumption
  | |- context [@V.Executor.jumpdest] ⇒ apply memory_safe_lift, jumpdest_preserves_words; assumption
  | |- context [@V.Executor.push_] ⇒ apply memory_safe_lift, push_preserves_words; assumption
  | |- context [@V.Executor.dup] ⇒ apply memory_safe_lift, dup_preserves_words; assumption
  | |- context [@V.Executor.swap] ⇒ apply memory_safe_lift, swap_preserves_words; assumption
  | |- context [@V.Executor.return_] ⇒ apply memory_safe_lift, return_preserves_words_bounded; assumption
  | |- context [@V.Executor.revert] ⇒ apply memory_safe_lift, revert_preserves_words_bounded; assumption
  | |- context [@V.Executor.mload] ⇒ apply mload_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.mstore] ⇒ apply mstore_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.mstore8] ⇒ apply mstore8_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.calldatacopy] ⇒ apply calldatacopy_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.codecopy] ⇒ apply codecopy_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.returndatacopy] ⇒ apply returndatacopy_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.mcopy] ⇒ apply mcopy_preserves_words_or_bad_comparison_bounded; assumption
  | |- context [@V.Executor.invalid] ⇒ unfold V.Executor.invalid; apply memory_safe_lift, safe_fail
  | |- context [@V.Executor.undefined] ⇒ unfold V.Executor.undefined; apply memory_safe_lift, safe_fail
  end.
Qed.

Lemma machine_only_preserves_words_or_bad_comparison
    (invariant : Host_t → Prop) opcode state :
  (@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 →
  keccak256OutputSize →
  environmentWords ((@V.Executor.C Host_t F E).(V.Executor_C_signature.execution_environment)) →
  opcodeWordParameters opcode →
  machineOnlyOpcode opcode = true →
  (OCamlString.length (MS.t.output_buffer state) < 2^256)%Z →
  memoryFits (MS.t.memory state) →
  machineWords invariant state →
  machineWords invariant (snd (executeOpcode opcode state)) ∨ badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Henv Hparams Hlocal Hbuffer Hfits Hinitial.
  eapply machine_only_preserves_words_core; try eassumption.
  - apply eleven_bounded_memory; exact Hrevision.
  - apply arithmetic_preserves_words; [exact Hrevision|reflexivity].
  - intros extra _ Hextra. destruct extra; cbn in Hextra; try contradiction.
    all: cbn [dispatch V.Executor.execute_opcode];
      lazymatch goal with |- machineWords _ (snd (?handler ?st)) ∨ _ ⇒
        let rec head e := lazymatch e with ?f ?x ⇒ head f | _ ⇒ e end in
        let implementation := head handler in unfold implementation
      end;
      try rewrite Hrevision; try unfold V.Executor.undefined;
      left; exact Hinitial.
Qed.

End Opcodes.

Print Assumptions machine_only_preserves_words_or_bad_comparison.