Library QedkConcrete.hostcontextwords

Deriving VM context bounds from transaction and block fields

The Host constructs the context; it is not an arbitrary external oracle. Header and fee representation bounds suffice for that construction, and the proved transaction-context frame keeps them valid across VM execution.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Host QedkConcrete.hostframe QedkConcrete.wordrange
  QedkConcrete.bytewords QedkConcrete.vmenvironmentwords.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.

Definition hostContextWords (params : Chain.Monad.PARAMS) (s : S.t) : Prop :=
  let header := Chain.Ethereum.Block.t.header (S.t.current_block s) in
  Forall wordRange
    [Support.UintN.to_z (S.t.tx_gas_price s);
     Support.UintN.to_z (Chain.Ethereum.Block.Header.t.base_fee_per_gas header);
     Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)] ∧
  Forall (fun n ⇒ 0 ≤ n < 2^64)
    [Support.UintN.to_z (Chain.Ethereum.Block.Header.t.number header);
     Chain.Ethereum.Block.Header.t.timestamp header;
     Support.UintN.to_z (Chain.Ethereum.Block.Header.t.gas_limit header)] ∧
  OCamlString.length (Chain.Ethereum.Block.Header.t.prev_randao header) ≤ 32.

Lemma uint64_conversion_range {UF : Basics.Unreachable int64} value :
  0 ≤ value < 2^64 → -(2^63) ≤ @OCamlZ.to_int64_unsigned UF value < 2^63.
Proof.
  intro Hvalue. unfold OCamlZ.to_int64_unsigned.
  assert (Hlo : Z.leb 0 value = true) by (apply Z.leb_le; lia).
  assert (Hhi : Z.ltb value (2^64) = true) by (apply Z.ltb_lt; lia).
  rewrite Hlo, Hhi. unfold OCamlZ.signed_repr. cbn beta iota zeta.
  rewrite Z.mod_small by exact Hvalue.
  change (-(2^63) ≤ (if Z.leb (2^63) value then value - 2^64 else value) < 2^63).
  destruct (Z.leb (2^63) value) eqn:Hsign.
  - apply Z.leb_le in Hsign. lia.
  - apply Z.leb_gt in Hsign. lia.
Qed.

Lemma get_tx_context_words {F : H.FArgs}
    {UI : Basics.Unreachable MonadVM.Uint64.t} {UF : Basics.Unreachable U.t}
    {UC : Basics.Unreachable ascii} s :
  hostContextWords F.(H.ChainParams) s →
  contextWords (fst (@H.get_tx_context F s)).
Proof.
  intros [Hfields [Hheader Hrandao]].
  unfold contextWords. cbn -[U.of_uint_exn U.of_repr U.one Support.UintN.to_uint64 U.to_uint64 Gas.to_uint64].
  split.
  - repeat match goal with |- Forall _ _ ⇒ constructor end.
    all: first [apply word_repr; exact Hrandao | apply word_one |
      apply word_of_z_range; rewrite Forall_forall in Hfields;
      apply Hfields; cbn [In]; tauto].
  - split.
    + repeat match goal with |- Forall _ _ ⇒ constructor end.
      all: apply uint64_conversion_range;
        rewrite Forall_forall in Hheader; apply Hheader; cbn [In]; tauto.
    + constructor.
Qed.

Lemma host_context_words_preserved params initial final :
  transactionContext final = transactionContext initial →
  hostContextWords params initial → hostContextWords params final.
Proof.
  unfold transactionContext. intro Hcontext.
  injection Hcontext as Hinitial Hblock Horigin Hgas Hprice Hhistory Hemptying.
  unfold hostContextWords. rewrite Hblock, Hprice. tauto.
Qed.

At any nested VM entry, the already-proved context frame reduces these environment bounds to the original transaction's header and fee fields. Message value and byte lengths remain call-specific obligations.
Lemma host_environment_words {F : H.FArgs}
    {Host_t : Set} {VF : @Vm.Make.FArgs Host_t}
    {UI : Basics.Unreachable MonadVM.Uint64.t} {UF : Basics.Unreachable U.t}
    {UC : Basics.Unreachable ascii} initial s msg code :
  transactionContext s = transactionContext initial →
  hostContextWords F.(H.ChainParams) initial →
  wordRange (Evmc.Message.t.value msg) →
  OCamlString.length (Evmc.Message.t.input_data msg) < 2^256 →
  OCamlString.length code < 2^256 →
  environmentWords (@Env.make Host_t VF (fst (@H.get_tx_context F s)) msg code).
Proof.
  intros Hcontext Hinitial Hvalue Hdata Hcode.
  apply environment_make_words; try assumption.
  apply get_tx_context_words. eapply host_context_words_preserved; eassumption.
Qed.

Print Assumptions get_tx_context_words.
Print Assumptions host_context_words_preserved.
Print Assumptions host_environment_words.