Library QedkConcrete.vmrun

Concrete VM loop invariants

The generated loop contains an arbitrary fallback if an instruction continues without decreasing gas. The pinned opcode-progress proof rules that branch out below the signed gas limit. Thus an invariant proved for each actual instruction carries through the loop, including error exits; no behavioral assumption about the whole VM is needed for this step.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".

Section Run.
  Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_t}.
  Context {E : @MonadVM.Vm.Make.Executor.FArgs Host_t F}.

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

  Local Abbreviation run := (@generated_run Host_t F E).
  Local Abbreviation step := (fun state : State ⇒
    @dispatch Host_t F E

      (@run_selected_opcode Host_t F E state) state).
  Local Abbreviation progress := (@run_selected_dispatch_guard_true Host_t F E).

This premise is an instruction-level proof obligation, not a new VM axiom. It will be discharged from Host effects and machine-local steps.
  Lemma run_preserves_with_progress (invariant : State → Prop) (state : State) :
    (∀ before after, (Z.of_N (Vm.Make.MachineState.t.gas before) < 2^63)%Z →
      step before = (inl true, after) →
      (Vm.Make.MachineState.t.gas after < Vm.Make.MachineState.t.gas before)%N) →
    (∀ before, (Z.of_N (Vm.Make.MachineState.t.gas before) < 2^63)%Z →
      invariant before → invariant (snd (step before))) →
    (Z.of_N (Vm.Make.MachineState.t.gas state) < 2^63)%Z →
    invariant state → invariant (snd (run state)).
  Proof.
    intros Hprogress Hstep.
    remember (Vm.Make.MachineState.t.gas state) as budget eqn:Hbudget.
    revert state Hbudget.
    induction budget as [budget IH] using (well_founded_induction N.lt_wf_0).
    intros state Hbudget Hbound Hinitial.
    rewrite Hbudget in Hbound.
    rewrite generated_run_step_eq. unfold generated_run_step.
    pose proof (Hstep state Hbound Hinitial) as Hafter.
    destruct (step state) as [[continue|err] next] eqn:Hdispatch.
    - destruct continue.
      + pose proof (Hprogress state next Hbound Hdispatch) as Hdecreases.
        assert (Hguard : Gas.op_lt (Vm.Make.MachineState.t.gas next)
          (Vm.Make.MachineState.t.gas state) = true).
        { apply N.ltb_lt. exact Hdecreases. }
        rewrite Hguard.
        apply (IH (Vm.Make.MachineState.t.gas next)).
        × unfold Gas.op_lt, Support.UintN.op_lt in Hguard. apply N.ltb_lt in Hguard. lia.
        × reflexivity.
        × lia.
        × exact Hafter.
      + exact Hafter.
    - exact Hafter.
  Qed.

The original revision-Eight/Eleven interface is unchanged. New instructions discharge their progress obligation separately.
  Lemma run_preserves (invariant : State → Prop) (state : State) :
    ((@Vm.Make.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eight ∨
     (@Vm.Make.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven) →
    (∀ before, (Z.of_N (Vm.Make.MachineState.t.gas before) < 2^63)%Z →
      invariant before → invariant (snd (step before))) →
    (Z.of_N (Vm.Make.MachineState.t.gas state) < 2^63)%Z →
    invariant state → invariant (snd (run state)).
  Proof.
    intro Hrevision. apply run_preserves_with_progress.
    intros before after Hbound Hstep.
    destruct (progress before after ltac:(tauto) Hbound Hstep) as [Hgas _].
    apply N.ltb_lt. exact Hgas.
  Qed.
End Run.

Print Assumptions run_preserves.