Library SetcodeConcrete.vmprogress

Gas progress for the generated SETCODE VM

A successful SETCODEFROM instruction spends gas in its Host callback. The callback premise below is an intermediate proof obligation; the concrete Host discharges it in host.setcodefrom_success_spends_gas. The other instructions reuse the shared opcode proofs.
From Stdlib Require Import NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmOpcodeProgress.
Require Import QedkConcrete.vmrun.
Set Default Goal Selector "!".
Local Open Scope N_scope.
Module V := Vm.Make.
Module MS := V.MachineState.

Section VM.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.

Definition setcodeGasProgress : Prop :=
  ∀ current source static gas state valid remaining after,
    V.Host.(Evmc.HOST.setcodefrom) current source static gas state =
      (inl (valid, remaining), after) → remaining < gas.

Lemma setcode_opcode_progress cold warm access write reserve :
  V.Params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  setcodeGasProgress → continuing_progress Opcode.Setcodefrom.
Proof.
  intros Hrevision Hhost state next Hbound Hrun.
  unfold dispatch, V.Executor.execute_opcode, V.Executor.setcodefrom in Hrun.
  rewrite Hrevision in Hrun.
  destruct (bind_success_decompose _ _ _ _ _ Hrun)
    as [popped [source [Hpop Hrest]]].
  pose proof (map_nonincreasing V.Address.of_u256_truncating V.pop
    pop_nonincreasing state source popped Hpop) as Hpopgas.
  destruct (bind_success_decompose _ _ _ _ _ Hrest)
    as [read [gas [Hread Hrest2]]].
  change (((inl (MS.t.gas popped) : N + Evmc.Result.StatusCode.t), popped) =
    (inl gas, read)) in Hread.
  inversion Hread; subst gas read.
  destruct (bind_success_decompose _ _ _ _ _ Hrest2)
    as [called [outcome [Hcall Hrest3]]].
  unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.lift in Hcall.
  unfold V.M.HostAPI.Base in Hcall.
  destruct (V.Host.(Evmc.HOST.setcodefrom) V.Executor.self source
    (negb (V.ExecutionEnvironment.t.write_permission
      (V.Executor.C.(Vm.Make.Executor_C_signature.execution_environment))))
    (MS.t.gas popped) (MS.t.host popped)) as [result after] eqn:Hcallback.
  inversion Hcall; subst outcome called.
  destruct result as [[valid remaining]|error]; [|discriminate Hrest3].
  pose proof (Hhost _ _ _ _ _ _ _ _ Hcallback) as Hspent.
  destruct (bind_success_decompose _ _ _ _ _ Hrest3)
    as [charged [updated [Hwrite Hrest4]]].
  destruct updated.
  inversion Hwrite; subst charged.
  destruct (bind_success_decompose _ _ _ _ _ Hrest4)
    as [pushed [unitValue [Hpush Hpc]]].
  destruct unitValue.
  pose proof (push_nonincreasing _ _ _ _ Hpush) as Hpushgas.
  pose proof (increase_pc_nonincreasing _ _ _ Hpc) as Hpcgas.
  cbn in Hpushgas. lia.
Qed.

Lemma setcode_clz_progress cold warm access write reserve :
  V.Params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  continuing_progress Opcode.Clz.
Proof.
  intro Hrevision. apply decreasing_handler_continues.
  unfold dispatch, V.Executor.execute_opcode, V.Executor.clz.
  rewrite Hrevision.
  eapply bind_decrease_after_preserve; [apply pop_nonincreasing |].
  intro value. eapply spend_then_preserving_decreases; [apply low_positive |].
  intros []. eapply bind_nonincreasing; [apply push_nonincreasing |].
  intros []. apply increase_pc_nonincreasing.
Qed.

Theorem setcode_dispatch_progress cold warm access write reserve opcode :
  V.Params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  setcodeGasProgress → well_formed_opcode opcode → continuing_progress opcode.
Proof.
  intros Hrevision Hhost Hvalid.
  apply dispatch_continuing_progress_core; [exact Hvalid | | |].
  - eapply setcode_clz_progress; exact Hrevision.
  - intros extra Hextra. destruct extra; cbn in Hextra; try contradiction;
      try solve [apply no_continuing_progress, undefined_no_continuing].
    eapply setcode_opcode_progress; eassumption.
  - exact unresolved_opcode_progress.
Qed.

The opcode selected by the real loop is well formed, including STOP past the end of the bytecode. No opcode-selection premise is assumed.
Lemma selected_opcode_well_formed state :
  well_formed_opcode (@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)); cbn.
  - match goal with |- context [if ?condition then _ else _] ⇒
      destruct condition end; [apply decoded_opcode_well_formed | exact I].
  - exact I.
Qed.

Theorem setcode_run_preserves cold warm access write reserve
    (invariant : @MS.t Host_t F → Prop) state :
  V.Params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  setcodeGasProgress →
  (∀ before, (Z.of_N (MS.t.gas before) < 2^63)%Z → invariant before →
    invariant (snd (dispatch (@run_selected_opcode Host_t F E before) before))) →
  (Z.of_N (MS.t.gas state) < 2^63)%Z → invariant state →
  invariant (snd (@generated_run Host_t F E state)).
Proof.
  intros Hrevision Hhost. apply run_preserves_with_progress.
  intros before after Hbound Hstep.
  exact (setcode_dispatch_progress _ _ _ _ _ _ Hrevision Hhost
    (selected_opcode_well_formed before) before after Hbound Hstep).
Qed.
End VM.