Library SetcodeConcrete.vmgasframe

Gas bounds including instruction failures

Ordinary instructions only spend gas; this includes their error exits. Host callbacks may change account state but their lift does not change machine gas. Recursive CALL/CREATE refunds and SETCODEFROM's returned gas are handled separately, rather than hidden in this relation.
From Stdlib Require Import List NArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe.
Set Default Goal Selector "!".
Local Open Scope N_scope.

Section Gas.
Context {Host_t : Set} {F : @V.FArgs Host_t}.

Definition gasBound {A : Set} (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, MS.t.gas (snd (action state)) ≤ MS.t.gas state.

Lemma gas_bind {A B : Set} (action : @V.M.t Host_t F A) next :
  gasBound action → (∀ value, gasBound (next value)) →
  gasBound (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Hfirst Hnext state. specialize (Hfirst state).
  change (MS.t.gas (snd (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle) end)) ≤ MS.t.gas state).
  destruct (action state) as [[value|error] middle]; cbn [snd] in *; [|exact Hfirst].
  specialize (Hnext value middle). lia.
Qed.
Lemma gas_return {A : Set} (value : A) : gasBound (@V.M._return Host_t F A value).
Proof. intro state. apply N.le_refl. Qed.
Lemma gas_fail {A : Set} error : gasBound (@V.M.fail Host_t F A error).
Proof. intro state. apply N.le_refl. Qed.
Lemma gas_read {A : Set} (read : @MS.t Host_t F → A) :
  gasBound (fun state ⇒ (inl (read state), state)).
Proof. intro state. apply N.le_refl. Qed.
Lemma gas_lens_read {A : Set} (lens : Lens.t (@MS.t Host_t F) A) :
  gasBound (@V.M.op_exclamation Host_t F A lens).
Proof. intro state. apply N.le_refl. Qed.
Lemma gas_write {A : Set} (lens : Lens.t (@MS.t Host_t F) A) value :
  (∀ state, MS.t.gas (Lens.t.set lens value state) ≤ MS.t.gas state) →
  gasBound (@V.M.op_coloneq Host_t F A lens value).
Proof. exact (fun H ⇒ H). Qed.
Lemma gas_update {A : Set} (lens : Lens.t (@MS.t Host_t F) A) f :
  (∀ state value, MS.t.gas (Lens.t.set lens value state) = MS.t.gas state) →
  gasBound (@V.M.update_field Host_t F A lens f).
Proof. intros H state. change (MS.t.gas (Lens.t.set lens (f (Lens.t.get lens state)) state) ≤ MS.t.gas state).
  rewrite H. apply N.le_refl. Qed.
Lemma gas_lift {A : Set} (action : Host_t → A × Host_t) :
  gasBound (@V.M.HostAPI.lift Host_t F A action).
Proof. intro state. unfold V.M.HostAPI.lift.
  destruct (action (MS.t.host state)). apply N.le_refl. Qed.
Lemma gas_fmap {A B : Set} (f : A → B) action :
  gasBound action → gasBound (@V.M.op_ltdollargt Host_t F A B f action).
Proof. intro H. change (gasBound (@V.M.op_letdollar Host_t F A B action
    (fun value ⇒ V.M._return (f value)))). apply gas_bind; [exact H|].
  intro value. apply gas_return. Qed.
Lemma gas_when condition action :
  gasBound action → gasBound (@V.M.when_ Host_t F condition action).
Proof. intro H. destruct condition; [exact H|apply gas_return]. Qed.
Lemma gas_option {A : Set} error (value : option A) :
  gasBound (@V.M.Option.or_fail Host_t F A error value).
Proof. destruct value; [apply gas_return|apply gas_fail]. Qed.
Lemma gas_pop : gasBound (@V.pop Host_t F).
Proof. intro state. unfold V.pop. destruct (MS.t.stack state); apply N.le_refl. Qed.
Lemma gas_pop2 : gasBound (@V.pop2 Host_t F).
Proof. intro state. unfold V.pop2. destruct (MS.t.stack state) as [|a [|b rest]]; apply N.le_refl. Qed.
Lemma gas_pop3 : gasBound (@V.pop3 Host_t F).
Proof. intro state. unfold V.pop3. destruct (MS.t.stack state) as [|a [|b [|c rest]]]; apply N.le_refl. Qed.
Lemma gas_push value : gasBound (@V.push Host_t F value).
Proof. intro state. unfold V.push. destruct (Z.geb (MS.t.stack_depth state) V.max_stack_depth); apply N.le_refl. Qed.
Lemma gas_spend amount : gasBound (@V.spend Host_t F amount).
Proof.
  intro state. unfold V.spend. destruct (Gas.op_lt (MS.t.gas state) amount) eqn:Hcost.
  - apply N.le_refl.
  - change ((MS.t.gas state <? amount) = false) in Hcost. apply N.ltb_ge in Hcost.
    change (Gas.op_minus (MS.t.gas state) amount ≤ MS.t.gas state).
    unfold Gas.op_minus, Support.UintN.op_minus.
    rewrite (proj2 (N.leb_le amount (MS.t.gas state)) Hcost). apply N.le_sub_l.
Qed.
Lemma gas_pc f : gasBound (@V.update_pc_and_continue Host_t F f).
Proof. intro state. apply N.le_refl. Qed.
Lemma gas_finish output : gasBound (@V.finish_execution Host_t F output).
Proof. unfold V.finish_execution. apply gas_bind.
  - apply gas_when, gas_write. intro state. apply N.le_refl.
  - intros []. apply gas_return.
Qed.
Lemma gas_seq_map {A B : Set} (f : A → @V.M.t Host_t F B) :
  (∀ x, gasBound (f x)) → ∀ values, gasBound (@V.M.Seq.mapM Host_t F A B f values).
Proof.
  intro Hf. induction values as [|value values IH].
  all: unfold V.M.Seq.mapM, Monad.Make_Monad.Seq.mapM, Monad.Make_Monad.Seq.sequence.
  all: rewrite Init.Wf.Fix_eq; [|intros seq left right Heq;
    destruct seq; cbn [OCamlSeq.uncons]; [reflexivity|rewrite Heq; reflexivity]].
  - apply gas_return.
  - cbn [OCamlSeq.uncons]. apply gas_bind; [apply Hf|]. intro result.
    apply gas_bind; [exact IH|]. intro results. apply gas_return.
Qed.
End Gas.

Ltac solve_gas_bound :=
  lazymatch goal with
  | |- gasBound (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply gas_bind; [solve_gas_bound|]; intro;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_gas_bound
  | |- gasBound (@V.M.op_gtgteq _ _ _ _ _ _) ⇒
    apply gas_bind; [solve_gas_bound|]; intro;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_gas_bound
  | |- gasBound (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒ apply gas_fmap; solve_gas_bound
  | |- gasBound (@V.M.fmap _ _ _ _ _ _) ⇒ apply gas_fmap; solve_gas_bound
  | |- gasBound (@V.M.when_ _ _ _ _) ⇒ apply gas_when; solve_gas_bound
  | |- gasBound (if ?test then _ else _) ⇒ destruct test; solve_gas_bound
  | |- gasBound (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒ destruct value; solve_gas_bound
  | |- gasBound (@V.M._return _ _ _ _) ⇒ apply gas_return
  | |- gasBound (@V.M.fail _ _ _ _) ⇒ apply gas_fail
  | |- gasBound (@V.pop _ _) ⇒ apply gas_pop
  | |- gasBound (@V.pop2 _ _) ⇒ apply gas_pop2
  | |- gasBound (@V.pop3 _ _) ⇒ apply gas_pop3
  | |- gasBound (@V.push _ _ _) ⇒ apply gas_push
  | |- gasBound (@V.spend _ _ _) ⇒ apply gas_spend
  | |- gasBound (@V.update_pc_and_continue _ _ _) ⇒ apply gas_pc
  | |- gasBound (@V.increase_pc_and_continue _ _) ⇒ apply gas_pc
  | |- gasBound (@V.finish_execution _ _ _) ⇒ apply gas_finish
  | |- gasBound (@V.M.op_exclamation _ _ _ _) ⇒ apply gas_lens_read
  | |- gasBound (@V.M.Option.or_fail _ _ _ _ _) ⇒ apply gas_option
  | |- gasBound (@V.M.op_coloneq _ _ _ _ _) ⇒ apply gas_write; intro; apply N.le_refl
  | |- gasBound (@V.M.update_field _ _ _ _ _) ⇒ apply gas_update; reflexivity
  | |- gasBound (@V.M.HostAPI.lift _ _ _ _) ⇒ apply gas_lift
  | |- gasBound (@V.M.Seq.mapM _ _ _ _ _ _) ⇒ apply gas_seq_map; intro; solve_gas_bound
  | |- gasBound (fun state ⇒ _) ⇒ apply gas_read
  | |- gasBound (match ?value with _ ⇒ _ end) ⇒ destruct value; solve_gas_bound
  end.