Library QedkConcrete.hostchecked

Host composition for the recursive supply proof

Child return-data bounds must travel with the state invariant: the caller can copy those bytes into EVM memory. The internal proofs retain an allocation-guard failure alternative; host_checked_sound eliminates it using the concrete runtime guard.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.hostsupply
  QedkConcrete.hostaccountformat QedkConcrete.hostcontextwords
  QedkConcrete.vmsupplywords QedkConcrete.vmmemorywords QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Definition hostChecked {A : Set} (invariant : S.t → Prop) (P : A → Prop)
    (action : S.t → A × S.t) : Prop :=
  ∀ s, invariant s →
    (invariant (snd (action s)) ∧ P (fst (action s))) ∨ badMemoryInitComparison.

Lemma host_checked_sound {A : Set} invariant (P : A → Prop)
    (action : S.t → A × S.t) :
  hostChecked invariant P action →
  ∀ s, invariant s → invariant (snd (action s)) ∧ P (fst (action s)).
Proof.
  intros Hchecked s Hinitial.
  destruct (Hchecked s Hinitial) as [Hsafe|Hbad]; [exact Hsafe|].
  exfalso. exact (no_bad_memory_init_comparison Hbad).
Qed.

Definition resultOutputBound (result : Evmc.Result.t) : Prop :=
  OCamlString.length (Evmc.Result.t.output_data result) < 2^256.

Code already stored in an account is bounded by the preserved account format, including code adopted while executing initcode.
Lemma read_account_code_checked params limit addr :
  hostChecked (hostSupplyContext params limit)
    (fun code ⇒ OCamlString.length code < 2^62)
    (S.M.(Monad.State.State_result.op_exclamation)
      (Lens.Infix.op_pipeminusminus (@S.account None addr) Chain.Ethereum.Account.TLens.code)).
Proof.
  intros s Hinitial. left. split; [exact Hinitial|].
  exact (proj2 (read_account_format (S.t.world_state s) addr
    (proj1 (proj2 (proj1 Hinitial))))).
Qed.

Lemma host_checked_bind {A B : Set} invariant (P : A → Prop) (Q : B → Prop)
    (action : S.t → A × S.t) (next : A → S.t → B × S.t) :
  hostChecked invariant P action →
  (∀ value, P value → hostChecked invariant Q (next value)) →
  hostChecked invariant Q (S.M.(Monad.State.State_result.op_letdollar) action next).
Proof.
  intros Haction Hnext s Hinitial.
  specialize (Haction s Hinitial).
  destruct (action s) as [value middle] eqn:Hrun.
  change ((invariant (snd (let '(v, st) := action s in next v st)) ∧
    Q (fst (let '(v, st) := action s in next v st))) ∨ badMemoryInitComparison).
  rewrite Hrun. destruct Haction as [[Hmiddle Hvalue]|Hbad].
  - exact (Hnext value Hvalue middle Hmiddle).
  - right. exact Hbad.
Qed.

Lemma host_checked_return {A : Set} invariant (P : A → Prop) value :
  P value → hostChecked invariant P (S.M.(Monad.State.State_result._return) value).
Proof. intros Hvalue s Hinitial. left. split; assumption. Qed.

Lemma host_checked_frame {A : Set} invariant (action : S.t → A × S.t) :
  preservesCallback invariant action → hostChecked invariant (fun _ ⇒ True) action.
Proof. intros Hframe s Hinitial. left. split; [apply Hframe; exact Hinitial|exact I]. Qed.

Lemma host_supply_context_frame {A : Set} params limit (action : S.t → A × S.t) :
  preservesSupply action → preservesCallback hostFormat action →
  preservesContext action →
  preservesCallback (hostSupplyContext params limit) action.
Proof.
  intros Hsupply Hformat Hcontext s [[[Hbalance Hlimit] Hwords] Hctx].
  destruct (Hsupply s Hbalance) as [Hbalance' Htotal].
  split.
  - split; [split; [exact Hbalance'|lia]|apply Hformat; exact Hwords].
  - eapply host_context_words_preserved; [apply Hcontext|exact Hctx].
Qed.