Library QedkConcrete.transactionentrywords

Numeric VM-entry facts derived from transaction validation

Paying a positive gas limit bounds the effective gas price by the sender's balance. The base price is no larger. These are consequences of validation and the supply bound, not assumptions about VM outputs.
From Stdlib Require Import List NArith ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.validation QedkConcrete.transactiondata
  QedkConcrete.transactionfees QedkConcrete.supply QedkConcrete.wordrange
  QedkConcrete.hostcontextwords.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Module S := State.TransactionState.

Section Validation.
Context {F : E.FArgs}.

Lemma validated_gas_lower_bound block tx validation :
  @E.validate_transaction F block tx = inl validation →
  (21000 ≤ T.gas_limit tx)%N.
Proof.
  intro Hvalidated. pose proof (validated_gas block tx validation Hvalidated) as [Hgas _].
  assert (Hcost : E.transaction_validation.intrinsic_gas validation = @Gas.tx_intrinsic_gas tx).
  { unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
    do 13 (apply validation.bind_success in Hvalidated;
      let value := fresh "value" in let Hvalue := fresh "Hvalue" in
      destruct Hvalidated as [value [Hvalue Hvalidated]];
      cbn beta iota zeta in Hvalidated).
    injection Hvalidated as <-. reflexivity. }
  rewrite Hcost in Hgas.
  unfold Gas.tx_intrinsic_gas, Gas.op_plus, Support.UintN.op_plus in Hgas.
  change (@Gas.tx_base_gas) with 21000%N in Hgas. lia.
Qed.

Lemma validated_effective_price block tx validation :
  @E.validate_transaction F block tx = inl validation →
  @Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
      (Chain.Ethereum.Block.t.header (B.t.current_block block))) tx =
      Some (E.transaction_validation.effective_gas_price validation).
Proof.
  intro Hvalidated. unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
  do 13 (apply validation.bind_success in Hvalidated;
    let value := fresh "value" in let Hvalue := fresh "Hvalue" in
    destruct Hvalidated as [value [Hvalue Hvalidated]];
    cbn beta iota zeta in Hvalidated).
  injection Hvalidated as <-. cbn [E.transaction_validation.effective_gas_price].
  match goal with H : (match @Gas.tx_effective_gas_price ?base ?t with
    Some _ ⇒ _ | None ⇒ _ end) = inl _ |- _ ⇒
    destruct (@Gas.tx_effective_gas_price base t) as [price|] eqn:Hprice;
      [injection H as <-; reflexivity|discriminate H]
  end.
Qed.

Lemma validated_prices_words block tx validation :
  @E.validate_transaction F block tx = inl validation →
  balanceInvariant (B.t.world_state block) →
  wordRange (Z.of_N (E.transaction_validation.effective_gas_price validation)) ∧
  wordRange (Z.of_N (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block block)))).
Proof.
  intros Hvalidated Hinitial.
  pose proof (validated_gas_lower_bound block tx validation Hvalidated) as Hgas.
  pose proof (validated_priority_price block tx validation Hvalidated) as Hbase.
  pose proof (validated_fee block tx validation Hvalidated) as [_ Hfee].
  pose proof (@account_balance_bounded_by_supply (B.t.world_state block)
    (E.transaction_validation.sender validation) (proj1 (proj2 Hinitial))) as Hbalance.
  pose proof (proj2 (proj2 Hinitial)) as Htotal.
  rewrite reservecheck.u256_to_uint in Hfee by exact (proj1 Hbalance).
  apply N2Z.inj_le in Hfee. rewrite Z2N.id in Hfee by exact (proj1 Hbalance).
  rewrite N2Z.inj_mul in Hfee.
  apply N2Z.inj_le in Hgas. apply N2Z.inj_le in Hbase.
  pose proof (N2Z.is_nonneg (E.transaction_validation.effective_gas_price validation)).
  pose proof (N2Z.is_nonneg (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block block)))).
  change (0 ≤ Z.of_N (E.transaction_validation.effective_gas_price validation) < 2^256 ∧
    0 ≤ Z.of_N (Chain.Ethereum.Block.Header.t.base_fee_per_gas
      (Chain.Ethereum.Block.t.header (B.t.current_block block))) < 2^256).
  cbn beta in Hbalance. split; split; nia.
Qed.

The remaining premises describe the header encoding and chain ID. Transaction fee fields, including the price stored by S.make, are derived above. The existing context-frame theorem preserves this fact through nested execution.
Lemma transaction_initial_context_words
    {UI0 : Basics.Unreachable Support.UintN.Impl.t}
    {UB : Basics.Unreachable Bloom.t}
    {U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}
    params block tx validation :
  @E.validate_transaction F block tx = inl validation →
  balanceInvariant (B.t.world_state block) →
  wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
  let header := Chain.Ethereum.Block.t.header (B.t.current_block block) in
  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 →
  hostContextWords params
    (@S.make
      block (E.transaction_validation.sender validation) tx).
Proof.
  intros Hvalidated Hinitial Hchain header Hheader Hrandao.
  pose proof (validated_prices_words block tx validation Hvalidated Hinitial) as [Hprice Hbase].
  pose proof (validated_effective_price block tx validation Hvalidated) as Heffective.
  unfold hostContextWords, S.make.
  cbn -[wordRange Gas.tx_effective_gas_price Option.get Support.UintN.to_z].
  rewrite Heffective. split.
  - constructor; [exact Hprice|]. constructor; [exact Hbase|].
    constructor; [exact Hchain|constructor].
  - split; assumption.
Qed.
End Validation.

Print Assumptions transaction_initial_context_words.