Library QedkConcrete.vmenvironmentwords

Bounds for words obtained from the execution environment

Integer aliases in the generated code do not enforce word widths. Directly exposed context fields therefore need representation bounds; address encoding and truncating conversions establish their own bounds. These are input-format conditions, not assumed VM preservation laws.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.bytewords
  QedkConcrete.vmmachinewords QedkConcrete.memorywords.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.

Module Env := V.ExecutionEnvironment.
Module Header := Env.ExecutionBlockHeader.

Definition environmentWords {Host_t : Set} {F : @V.FArgs Host_t} (env : @Env.t Host_t F) : Prop :=
  Forall wordRange
    [Env.t.price env; Env.t.value env; Header.t.number (Env.t.header env);
     Header.t.timestamp (Env.t.header env); Header.t.gas_limit (Env.t.header env);
     Header.t.prev_randao (Env.t.header env); Header.t.base_fee (Env.t.header env);
     Header.t.chain_id (Env.t.header env); Env.t.blob_base_fee env] ∧
  OCamlString.length (Env.t.data env) < 2^256 ∧
  OCamlString.length (Env.t.bytecode env) < 2^256 ∧
  Forall (fun hash ⇒ OCamlString.length hash ≤ 32) (Env.t.blob_versioned_hashes env).

Lemma address_word {UF : Basics.Unreachable U.t} {US : Basics.Unreachable string}
    {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii} addr :
  wordRange (@Chain.Ethereum.Address.to_u256 addr).
Proof.
  apply word_repr.
  assert (Hlength : OCamlString.length (@Chain.Ethereum.Address.to_bytes32 addr) = 32)
    by (apply init_length; lia).
  lia.
Qed.

Lemma uint_truncated_word {UF : Basics.Unreachable U.t}
    {UT : Basics.Unreachable (Z.t → U.t)} value :
  wordRange (@U.of_uint_truncating value).
Proof. apply low_bits_range. Qed.

Lemma nth_option_property {A : Set} {UO : Basics.Unreachable (option A)}
    (P : A → Prop) values index value :
  0 ≤ index → Forall P values →
  @OCamlList.nth_opt A UO values index = Some value → P value.
Proof.
  intros Hindex Hvalues.
  assert (Htest : Z.ltb index 0 = false) by (apply Z.ltb_ge; exact Hindex).
  unfold OCamlList.nth_opt. rewrite Htest.
  clear Hindex Htest. revert index.
  induction Hvalues as [|head tail Hhead Htail IH]; intros index Hlookup; [discriminate|].
  cbn beta iota zeta in Hlookup.
  match type of Hlookup with (if ?test then _ else _) = _ ⇒ destruct test end.
  - inversion Hlookup; subst. exact Hhead.
  - eapply IH. exact Hlookup.
Qed.

Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.t}.
Context {US : Basics.Unreachable string} {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}.
Local Abbreviation env := ((@V.Executor.C Host_t F E).(V.Executor_C_signature.execution_environment)).

Lemma fetch_word value : wordRange value → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.fetch_environment_variable_opcode_impl Host_t F E value).
Proof.
  intro Hvalue. unfold V.Executor.fetch_environment_variable_opcode_impl.
  eapply safe_bind; [apply safe_spend|]. intros [] _.
  eapply safe_bind; [apply safe_push; exact Hvalue|]. intros [] _. apply safe_increase_pc.
Qed.

Lemma fetch_word_action action : machineSafe invariant wordRange action →
  machineSafe invariant (fun _ ⇒ True)
    (@V.Executor.fetch_environment_variable_opcode_impl' Host_t F E action).
Proof.
  intro Haction. unfold V.Executor.fetch_environment_variable_opcode_impl'.
  eapply safe_bind; [apply safe_spend|]. intros [] _.
  eapply safe_bind; [exact Haction|]. intros value Hvalue.
  eapply safe_bind; [apply safe_push; exact Hvalue|]. intros [] _. apply safe_increase_pc.
Qed.

Lemma address_preserves_words : machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.address Host_t F E).
Proof. apply fetch_word, address_word. Qed.

Lemma origin_preserves_words : machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.origin Host_t F E).
Proof. apply fetch_word, address_word. Qed.

Lemma caller_preserves_words : machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.caller Host_t F E).
Proof. apply fetch_word, address_word. Qed.

Lemma coinbase_preserves_words : machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.coinbase Host_t F E).
Proof. apply fetch_word, address_word. Qed.

Local Ltac context_word :=
  intros [Hfields _]; apply fetch_word;
  rewrite Forall_forall in Hfields; apply Hfields; cbn [In]; tauto.

Lemma callvalue_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.callvalue Host_t F E).
Proof. context_word. Qed.

Lemma gasprice_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.gasprice Host_t F E).
Proof. context_word. Qed.

Lemma number_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.number Host_t F E).
Proof. context_word. Qed.

Lemma timestamp_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.timestamp Host_t F E).
Proof. context_word. Qed.

Lemma gaslimit_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.gaslimit Host_t F E).
Proof. context_word. Qed.

Lemma prevrandao_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.prevrandao Host_t F E).
Proof. context_word. Qed.

Lemma basefee_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.basefee Host_t F E).
Proof. context_word. Qed.

Lemma chainid_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.chainid Host_t F E).
Proof. context_word. Qed.

Lemma blobbasefee_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.blobbasefee Host_t F E).
Proof. context_word. Qed.

Lemma calldatasize_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.calldatasize Host_t F E).
Proof.
  intros [_ [Hlength _]]. apply fetch_word, word_int.
  change (0 ≤ OCamlString.length (Env.t.data env) < 2^256).
  unfold OCamlString.length in ×. lia.
Qed.

Lemma codesize_preserves_words : environmentWords env → machineSafe invariant (fun _ ⇒ True)
  (@V.Executor.codesize Host_t F E).
Proof.
  intros [_ [_ [Hlength _]]]. apply fetch_word, word_int.
  change (0 ≤ OCamlString.length (Env.t.bytecode env) < 2^256).
  unfold OCamlString.length in ×. lia.
Qed.

Lemma blobhash_preserves_words {UO : Basics.Unreachable (option string)} :
  environmentWords env → machineSafe invariant (fun _ ⇒ True)
    (@V.Executor.blobhash Host_t F E).
Proof.
  intros [_ [_ [_ Hhashes]]]. unfold V.Executor.blobhash.
  eapply safe_bind; [apply safe_pop|]. intros index Hindex.
  eapply safe_bind; [apply safe_spend|]. intros [] _.
  eapply safe_bind.
  - apply safe_push. destruct (@U.to_int_opt index) as [i|] eqn:Hi; [|apply word_zero].
    apply word_to_int_opt in Hi. subst i.
    match goal with |- wordRange (match ?lookup with _ ⇒ _ end) ⇒
      destruct lookup as [hash|] eqn:Hlookup end; [|apply word_zero].
    apply word_repr.
    unfold V.M.List.nth_opt, Monad.Make_Monad.List.nth_opt in Hlookup.
    lazymatch type of Hlookup with
    | @OCamlList.nth_opt _ ?witness _ _ = _ ⇒
        eapply (@nth_option_property _ witness (fun bytes ⇒ OCamlString.length bytes ≤ 32))
    end.
    + exact (proj1 Hindex).
    + exact Hhashes.
    + exact Hlookup.
  - intros [] _. apply safe_increase_pc.
Qed.

Lemma msize_preserves_words {UN : Basics.Unreachable Support.UintN.t}
    {UT : Basics.Unreachable (Z.t → U.t)} :
  machineSafe invariant (fun _ ⇒ True) (@V.Executor.msize Host_t F E).
Proof.
  unfold V.Executor.msize. apply fetch_word_action.
  eapply safe_bind; [apply safe_memory_read|]. intros memory _.
  apply safe_return, uint_truncated_word.
Qed.

Lemma gas_preserves_words {UT : Basics.Unreachable (Z.t → U.t)} :
  machineSafe invariant (fun _ ⇒ True) (@V.Executor.gas_ Host_t F E).
Proof.
  unfold V.Executor.gas_. eapply safe_bind; [apply safe_spend|]. intros [] _.
  eapply safe_bind.
  - apply (safe_read invariant (fun _ ⇒ True)). intros; exact I.
  - intros gas _. eapply safe_bind; [apply safe_push, uint_truncated_word|].
    intros [] _. apply safe_increase_pc.
Qed.

End Instructions.

The environment constructor only copies fields and converts three uint64 header fields. This isolates the representation conditions that must be established when entering the VM, rather than assuming bounds of every instruction result.
Definition contextWords (ctx : Evmc.TxContext.t) : Prop :=
  Forall wordRange
    [Evmc.TxContext.t.tx_gas_price ctx; Evmc.TxContext.t.block_prev_randao ctx;
     Evmc.TxContext.t.block_base_fee ctx; Evmc.TxContext.t.chain_id ctx;
     Evmc.TxContext.t.blob_base_fee ctx] ∧
  Forall (fun n ⇒ -(2^63) ≤ n < 2^63)
    [Evmc.TxContext.t.block_number ctx; Evmc.TxContext.t.block_timestamp ctx;
     Evmc.TxContext.t.block_gas_limit ctx] ∧
  Forall (fun hash ⇒ OCamlString.length hash ≤ 32) (Evmc.TxContext.t.blob_hashes ctx).

Lemma uint64_word {UF : Basics.Unreachable U.t} n :
  -(2^63) ≤ n < 2^63 → wordRange (@U.of_uint64 n).
Proof.
  intro Hn. change (wordRange (@U.of_z_exn (OCamlZ.of_int64_unsigned n))).
  apply word_of_z_range. unfold wordRange, U.to_z, OCamlZ.of_int64_unsigned.
  destruct (Z.ltb n 0) eqn:Hnegative.
  - apply Z.ltb_lt in Hnegative. change (0 ≤ n + 2^64 < 2^256). lia.
  - apply Z.ltb_ge in Hnegative. change (0 ≤ n < 2^256). lia.
Qed.

Lemma environment_make_words {Host_t : Set} {F : @V.FArgs Host_t}
    {UF : Basics.Unreachable U.t} ctx msg code :
  contextWords ctx → 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 F ctx msg code).
Proof.
  intros [Hfields [Hheader Hhashes]] Hvalue Hdata Hcode.
  unfold environmentWords, Env.make, Header.of_tx_context.
  cbn -[U.of_uint64 wordRange].
  split.
  - repeat match goal with |- Forall _ _ ⇒ constructor end.
    all: first [exact Hvalue |
      apply uint64_word; rewrite Forall_forall in Hheader; apply Hheader; cbn [In]; tauto |
      rewrite Forall_forall in Hfields; apply Hfields; cbn [In]; tauto].
  - split; [exact Hdata|]. split; assumption.
Qed.

Print Assumptions address_preserves_words.
Print Assumptions callvalue_preserves_words.
Print Assumptions msize_preserves_words.
Print Assumptions blobhash_preserves_words.
Print Assumptions environment_make_words.