Library SetcodeConcrete.systemvm

Call-free bytecode preserves a Host invariant

The proof checks every selected opcode against the actual bytes. It also proves the program counter remains unsigned, so the negative-index fallback cannot manufacture a CALL or CREATE. No stack-value, memory, child-execution or cryptographic assumption is needed for this result.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
  QedkConcrete.vmhostframe QedkConcrete.hostframe QedkConcrete.wordrange
  QedkConcrete.systemcode.
Require Import SetcodeConcrete.vmprogress SetcodeConcrete.vmframes
  SetcodeConcrete.vmpc SetcodeConcrete.vmpcopcodes SetcodeConcrete.codebytes.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section VM.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (cold warm access write reserve : N).
Context (Hrevision : V.Params.(Vm.Make_Params_signature.revision) =
  Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : V.Params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hprogress : @setcodeGasProgress Host_t F).
Context (property : Host_t → Prop).
Context (Hprimitive : PrimitiveHostFrame V.Host property).

Section Loop.
Context {E : @V.Executor.FArgs Host_t F}.
Context (Hsetcode : adoptionFreeBytes
    (V.Executor.C.(V.Executor_C_signature.execution_environment).(V.ExecutionEnvironment.t.bytecode)) = false →
  ∀ current source static gas,
    preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas)).

Lemma setcode_pc_safe : pcSafe (@V.Executor.setcodefrom Host_t F E).
Proof.
  unfold V.Executor.setcodefrom. rewrite Hrevision.
  unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.Base. solve_pc_safe.
Qed.

Theorem call_free_run_preserves state :
  callFreeBytes (V.Executor.C.(V.Executor_C_signature.execution_environment)
    .(V.ExecutionEnvironment.t.bytecode)) = true →
  Z.of_N (MS.t.gas state) < 2^63 → wordRange (MS.t.pc state) →
  property (MS.t.host state) →
  property (MS.t.host (snd (@generated_run Host_t F E state))).
Proof.
  intros Hcode Hgas Hpc Hinitial.
  assert (Hrun : wordRange (MS.t.pc (snd (@generated_run Host_t F E state))) ∧
    property (MS.t.host (snd (@generated_run Host_t F E state)))).
  { apply (setcode_run_preserves cold warm access write reserve
      (fun st ⇒ wordRange (MS.t.pc st) ∧ property (MS.t.host st)) state Hrevision Hprogress).
    - intros before Hbound [Hcounter Hbefore].
      set (opcode := @run_selected_opcode Host_t F E before).
      assert (HnoCalls : recursiveCallOpcode opcode = false).
      { unfold opcode. rewrite run_selected_opcode_eq_model.
        apply call_free_selected_opcode; [exact Hcode|exact (proj1 Hcounter)]. }
      split.
      + destruct (machineOnlyOpcode opcode) eqn:Hmachine.
        × apply (machine_pc_safe opcode); [| |exact Hdebug|exact Hmachine|exact Hcounter].
          -- unfold dispatch, V.Executor.execute_opcode, V.Executor.clz.
             rewrite Hrevision. solve_pc_safe.
          -- intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
             ++ exact setcode_pc_safe.
             ++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply pc_fail.
        × eapply primitive_pc_safe; [exact Hdebug| |exact Hcounter].
          destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.
      + destruct (machineOnlyOpcode opcode) eqn:Hmachine.
        × apply (machine_only_preserves_host_at property opcode);
            [| |exact Hdebug|exact Hmachine|exact Hbefore].
          -- eapply setcode_clz_preserves_host; exact Hrevision.
          -- intros extra Hequal Hextra. destruct extra; cbn in Hextra; try contradiction.
             ++ eapply setcode_opcode_preserves_host; [exact Hrevision|].
                apply Hsetcode.
                destruct (adoptionFreeBytes
                  (V.Executor.C.(V.Executor_C_signature.execution_environment)
                    .(V.ExecutionEnvironment.t.bytecode))) eqn:Hfree; [|reflexivity].
                assert (Hselected : notAdoption opcode = true).
                { unfold opcode. rewrite run_selected_opcode_eq_model.
                  apply adoption_free_selected_opcode; [exact Hfree|exact (proj1 Hcounter)]. }
                rewrite <- Hequal in Hselected. discriminate.
             ++ unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined. apply preserves_fail.
        × eapply primitive_host_preserves_at_any_revision;
            [exact Hdebug|apply primitive_frame_at; exact Hprimitive| |exact Hbefore].
          destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *; congruence.
    - exact Hgas.
    - split; assumption. }
  exact (proj2 Hrun).
Qed.
End Loop.

Context (Hcontext : ∀ host, snd (V.Host.(Evmc.HOST.get_tx_context) host) = host).
Context (Hsize : ∀ addr host, snd (V.Host.(Evmc.HOST.get_code_size) addr host) = host).
Context (Hcopy : ∀ addr offset size host, snd (V.Host.(Evmc.HOST.copy_code) addr offset size host) = host).

msg, code, and host are the real VM inputs. The bytes contain no CALL/CREATE; the primitive callbacks and code-adoption callback preserve property. Then the complete VM, including delegation reads and all return/revert/error branches, preserves it without a child-call premise.
Theorem call_free_execute_preserves_guarded msg code host :
  (adoptionFreeBytes code = false → ∀ current source static gas,
    preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas)) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 → callFreeBytes code = true →
  property host → property (snd (@V.execute Host_t F msg code host)).
Proof.
  intros Hsetcode Hgas Hcode Hinitial.
  unfold V.execute.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  pose proof (Hcontext host) as Hread.
  destruct (V.Host.(Evmc.HOST.get_tx_context) host) as [context middle] eqn:Hctx.
  cbn [snd] in Hread. subst middle.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  rewrite Hrevision.
  pose proof (Hsize (Evmc.Message.t.recipient msg) host) as HreadSize.
  destruct (V.Host.(Evmc.HOST.get_code_size) (Evmc.Message.t.recipient msg) host)
    as [size middle] eqn:Hsz. cbn [snd] in HreadSize. subst middle.
  cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hlength end.
  2: cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
  1: (pose proof (Hcopy (Evmc.Message.t.recipient msg) 0 Delegation.eoa_delegated_code_length host) as HreadCopy;
    destruct (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0
      Delegation.eoa_delegated_code_length host) as [ownCode middle] eqn:HownCode;
    cbn [snd] in HreadCopy; subst middle;
    change (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0 23 host = (ownCode, host)) in HownCode;
    rewrite HownCode;
    cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
  all: match goal with |- context [@V.Executor.run _ _ ?env ?state] ⇒
    pose proof (@call_free_run_preserves env Hsetcode state Hcode
      ltac:(apply initial_gas_fits_signed_int64; exact Hgas) word_zero Hinitial) as Hrun;
    unfold generated_run in Hrun;
    destruct (@V.Executor.run Host_t F env state) as [outcome after]
  end.
  all: destruct outcome as [[]|error]; [exact Hrun|].
  all: destruct error; exact Hrun.
Qed.

The unrestricted code-adoption callback suffices even if code contains SETCODEFROM. The guarded version also covers prescribed system bytecode without assuming anything about an unreachable adoption callback.
Theorem call_free_execute_preserves
    (Hsetcode : ∀ current source static gas,
      preservesCallback property (V.Host.(Evmc.HOST.setcodefrom) current source static gas))
    msg code host :
  0 ≤ Evmc.Message.t.gas msg < 2^63 → callFreeBytes code = true →
  property host → property (snd (@V.execute Host_t F msg code host)).
Proof.
  apply call_free_execute_preserves_guarded. intros _. exact Hsetcode.
Qed.
End VM.

Print Assumptions call_free_execute_preserves.