Library QedkConcrete.vmboundedframe

Propagating the signed gas bound to recursive calls

Gas bounds are needed only on paths that continue. An error still has to preserve the Host invariant, but has no continuation to justify.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress.
Set Default Goal Selector "!".

Section Frame.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (invariant : Host_t → Prop).

Definition preservesBoundedHost {A : Set} (action : @V.M.t Host_t F A) :=
  ∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z →
    invariant (MS.t.host state) → invariant (MS.t.host (snd (action state))).

Lemma bounded_from_unbounded {A : Set} (action : @V.M.t Host_t F A) :
  preservesHost invariant action → preservesBoundedHost action.
Proof. intros Hframe state Hgas Hinitial. apply Hframe. exact Hinitial. Qed.

Lemma bounded_frame_bind {A B : Set} (action : @V.M.t Host_t F A)
    (next : A → @V.M.t Host_t F B) :
  preservesBoundedHost action → gas_bounded_nonincreasing action →
  (∀ value, preservesBoundedHost (next value)) →
  preservesBoundedHost (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Hframe Hgas Hnext state Hbound Hinitial.
  specialize (Hframe state Hbound Hinitial).
  change (invariant (MS.t.host (snd
    (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle)
    end)))).
  destruct (action state) as [[value|error] middle] eqn:Haction.
  - pose proof (Hgas state value middle Hbound Haction) as Hnonincrease.
    apply Hnext; [lia|exact Hframe].
  - exact Hframe.
Qed.

Lemma bounded_frame_bind_prefix {A B : Set} (action : @V.M.t Host_t F A)
    (next : A → @V.M.t Host_t F B) :
  preservesHost invariant action → gas_nonincreasing action →
  (∀ value, preservesBoundedHost (next value)) →
  preservesBoundedHost (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Hframe Hgas Hnext. apply bounded_frame_bind.
  - apply bounded_from_unbounded. exact Hframe.
  - intros state value middle Hbound Haction. eapply Hgas. exact Haction.
  - exact Hnext.
Qed.

Lemma bounded_frame_bind_map_prefix {A B C : Set}
    (action : @V.M.t Host_t F A) (f : A → B)
    (next : B → @V.M.t Host_t F C) :
  preservesHost invariant action → gas_nonincreasing action →
  (∀ value, preservesBoundedHost (next (f value))) →
  preservesBoundedHost (@V.M.op_letdollar Host_t F B C
    (@V.M.op_ltdollargt Host_t F A B f action) next).
Proof.
  intros Hframe Hgas Hnext state Hbound Hinitial.
  specialize (Hframe state Hinitial).
  change (invariant (MS.t.host (snd
    (match (match action state with
      | (inl value, middle) ⇒ (inl (f value), middle)
      | (inr error, middle) ⇒ (inr error, middle)
      end) with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle)
    end)))).
  destruct (action state) as [[value|error] middle] eqn:Haction.
  - pose proof (Hgas state value middle Haction) as Hnonincrease.
    apply Hnext; [lia|exact Hframe].
  - exact Hframe.
Qed.

Lemma host_call_preserves msg :
  preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg) →
  preservesHost invariant (@V.M.HostAPI.call Host_t F msg).
Proof.
  intro Hcall. unfold V.M.HostAPI.call.
  apply preserves_bind; [apply preserves_lift; exact Hcall|].
  intro result. solve_host_invariant.
Qed.

Lemma bounded_frame_bind_suffix {A B : Set} (action : @V.M.t Host_t F A)
    (next : A → @V.M.t Host_t F B) :
  preservesBoundedHost action →
  (∀ value, preservesHost invariant (next value)) →
  preservesBoundedHost (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Hframe Hnext state Hbound Hinitial.
  specialize (Hframe state Hbound Hinitial).
  change (invariant (MS.t.host (snd
    (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle)
    end)))).
  destruct (action state) as [[value|error] middle].
  - apply Hnext. exact Hframe.
  - exact Hframe.
Qed.
End Frame.

A prefix can retain a stronger invariant until a child call. The child may create the tracked address, so the final invariant need not retain the prefix's "not created" condition.
Lemma bounded_frame_bind_between {Host_t : Set} {F : @V.FArgs Host_t}
    {A B : Set} (pre post : Host_t → Prop)
    (action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
  (∀ h, pre h → post h) →
  preservesHost pre action → gas_nonincreasing action →
  (∀ value state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
    post (MS.t.host (snd (next value state)))) →
  ∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
    post (MS.t.host (snd (@V.M.op_letdollar Host_t F A B action next state))).
Proof.
  intros Hweaken Hframe Hgas Hnext state Hbound Hinitial.
  pose proof (Hframe state Hinitial) as Hprefix.
  change (post (MS.t.host (snd
    (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle)
    end)))).
  destruct (action state) as [[value|error] middle] eqn:Haction.
  - pose proof (Hgas state value middle Haction) as Hnonincrease.
    apply Hnext; [lia|exact Hprefix].
  - apply Hweaken. exact Hprefix.
Qed.

Lemma bounded_frame_bind_map_between {Host_t : Set} {F : @V.FArgs Host_t}
    {A B C : Set} (pre post : Host_t → Prop)
    (action : @V.M.t Host_t F A) (f : A → B) (next : B → @V.M.t Host_t F C) :
  (∀ h, pre h → post h) →
  preservesHost pre action → gas_nonincreasing action →
  (∀ value state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
    post (MS.t.host (snd (next (f value) state)))) →
  ∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
    post (MS.t.host (snd (@V.M.op_letdollar Host_t F B C
      (@V.M.op_ltdollargt Host_t F A B f action) next state))).
Proof.
  intros Hweaken Hframe Hgas Hnext state Hbound Hinitial.
  pose proof (Hframe state Hinitial) as Hprefix.
  change (post (MS.t.host (snd
    (match (match action state with
      | (inl value, middle) ⇒ (inl (f value), middle)
      | (inr error, middle) ⇒ (inr error, middle)
      end) with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle)
    end)))).
  destruct (action state) as [[value|error] middle] eqn:Haction.
  - pose proof (Hgas state value middle Haction) as Hnonincrease.
    apply Hnext; [lia|exact Hprefix].
  - apply Hweaken. exact Hprefix.
Qed.

Lemma spend_nonincreasing {Host_t : Set} {F : @V.FArgs Host_t}
    {U : Basics.Unreachable Gas.t} amount :
  gas_nonincreasing (@V.spend Host_t F amount).
Proof.
  intros state [] next Hsuccess.
  unfold V.spend, Gas.op_lt, UintN.op_lt in Hsuccess.
  destruct (N.ltb (MS.t.gas state) amount) eqn:Hlt; [discriminate|].
  apply N.ltb_ge in Hlt.
  unfold Gas.op_minus, UintN.op_minus in Hsuccess.
  rewrite (proj2 (N.leb_le _ _) Hlt) in Hsuccess.
  inversion Hsuccess; subst next.
  cbn [MS.t.gas MS.t.with_gas]. lia.
Qed.

Print Assumptions bounded_frame_bind.
Print Assumptions host_call_preserves.