Library SetcodeConcrete.validation

Previously admitted transactions after code adoption

Fresh admission still rejects regular code. The second theorem connects the existing public consensus checks to the generated MIP-19 validator: a migrated sender need not have the predicted nonce, but its actual nonce must not be maximal and its actual balance must cover the quoted fee.
Neither validation success nor VM success is a premise. Establishing the actual-balance and actual-nonce premises from an initially accepted block window is proved separately in windowbudget and consensusvalidation.
From Stdlib Require Import Bool NArith ZArith List Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution
  QedkConcrete.transactionchecks SetcodeConcrete.included SetcodeConcrete.noncebudget.
Require Import SetcodeConcrete.migratednonce.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.

Section Validation.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).

Theorem fresh_checks_reject_regular_code header tx predicted :
  transactionChecks header tx predicted true = false.
Proof.
  unfold transactionChecks. cbn [negb].
  rewrite Bool.andb_false_r. reflexivity.
Qed.

Lemma checked_nonce_matches header tx predicted contract :
  transactionChecks header tx predicted contract = true → T.nonce tx = predicted.
Proof.
  unfold transactionChecks. repeat rewrite Bool.andb_true_iff. intro H.
  assert (Hnonce : negb (Numeric.U64.op_ltgt predicted (T.nonce tx)) = true) by tauto.
  apply Bool.negb_true_iff in Hnonce.
  change (negb (Numeric.U64.op_eq predicted (T.nonce tx)) = false) in Hnonce.
  apply Bool.negb_false_iff in Hnonce. rewrite rules.u64_eq in Hnonce.
  apply Z.eqb_eq in Hnonce. symmetry. exact Hnonce.
Qed.

Theorem admitted_migrated_transaction_validates state tx sender predicted price :
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  transactionChecks (Chain.Ethereum.Block.t.header (B.t.current_block state))
    tx predicted false = true →
  A.is_smart_contract (readAccount (B.t.world_state state) sender) = true →
  Numeric.U64.op_eq (A.t.nonce (readAccount (B.t.world_state state) sender)) Numeric.U64.max_t = false →
  Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block state))) tx = Some price →
  (T.gas_limit tx × price ≤ Numeric.U256.to_uint
    (A.t.balance (readAccount (B.t.world_state state) sender)))%N →
  ∃ validation, E.validate_transaction state tx = inl validation ∧
    E.transaction_validation.sender validation = sender.
Proof.
  intros Henabled Hsender Hchecks Hcode Hactual Hprice Hbalance.
  unfold transactionChecks in Hchecks. rewrite Hprice in Hchecks.
  repeat rewrite Bool.andb_true_iff in Hchecks.
  destruct Hchecks as [[[[[[[[[Hchain Hoverflow] Hnonce] Hinit] Hintrinsic] Hcap] Hfloor] _] Hauth] Hbid].
  apply Bool.negb_true_iff in Hchain, Hoverflow, Hnonce, Hinit, Hintrinsic, Hcap, Hfloor, Hbid.
  assert (Hfee : Support.UintN.op_gt (Support.UintN.op_star (T.gas_limit tx) price)
    (Numeric.U256.to_uint (A.t.balance (readAccount (B.t.world_state state) sender))) = false).
  { apply N.ltb_ge. exact Hbalance. }
  assert (Hauthorization : E.validate_authorizations (B.t.current_block state) tx = inl tt).
  { unfold E.validate_authorizations; destruct tx; cbn beta iota zeta in Hauth |- *;
      try reflexivity.
    destruct s; cbn in Hauth |- ×.
    match type of Hauth with context [match ?l with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒
      destruct l eqn:Hlist end; [discriminate|].
    reflexivity. }
  unfold E.validate_transaction. cbn zeta.
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
      match first with inl value ⇒ next value | inr error ⇒ inr error end).
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
  change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
    if condition then failure else inl tt).
  destruct (T.chain_id tx) as [id|] eqn:Hid; cbn beta iota zeta in Hchain |- ×.
  all: try rewrite Hchain.
  all: rewrite Hoverflow, Hsender.
  all: cbv beta iota zeta.
  all: rewrite Henabled, Hcode; cbn [andb].
  all: rewrite Hactual.
  all: destruct (T.call_or_create_value tx) as [call|[code]];
    cbv beta iota zeta delta [T.call_or_create.Create.initcode] in Hinit |- ×.
  all: rewrite Hinit, Hintrinsic, Hcap, Hfloor.
  all: cbn [negb andb].
  all: rewrite Hauthorization, Hprice.
  all: cbv beta iota zeta.
  all: rewrite Hbid, Hfee; cbv beta iota zeta; eexists; split; reflexivity.
Qed.

Theorem admitted_unmigrated_transaction_validates state tx sender predicted price :
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  transactionChecks (Chain.Ethereum.Block.t.header (B.t.current_block state))
    tx predicted false = true →
  A.is_smart_contract (readAccount (B.t.world_state state) sender) = false →
  A.t.nonce (readAccount (B.t.world_state state) sender) = predicted →
  Numeric.U64.op_eq (A.t.nonce (readAccount (B.t.world_state state) sender)) Numeric.U64.max_t = false →
  Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block state))) tx = Some price →
  (T.gas_limit tx × price ≤ Numeric.U256.to_uint
    (A.t.balance (readAccount (B.t.world_state state) sender)))%N →
  ∃ validation, E.validate_transaction state tx = inl validation ∧
    E.transaction_validation.sender validation = sender.
Proof.
  intros Henabled Hsender Hchecks Hcode Hagree Hactual Hprice Hbalance.
  pose proof (checked_nonce_matches _ _ _ _ Hchecks) as Hsigned.
  unfold transactionChecks in Hchecks. rewrite Hprice in Hchecks.
  repeat rewrite Bool.andb_true_iff in Hchecks.
  destruct Hchecks as [[[[[[[[[Hchain Hoverflow] Hnonce] Hinit] Hintrinsic] Hcap] Hfloor] _] Hauth] Hbid].
  apply Bool.negb_true_iff in Hchain, Hoverflow, Hnonce, Hinit, Hintrinsic, Hcap, Hfloor, Hbid.
  assert (Hfee : Support.UintN.op_gt (Support.UintN.op_star (T.gas_limit tx) price)
    (Numeric.U256.to_uint (A.t.balance (readAccount (B.t.world_state state) sender))) = false).
  { apply N.ltb_ge. exact Hbalance. }
  assert (Hauthorization : E.validate_authorizations (B.t.current_block state) tx = inl tt).
  { unfold E.validate_authorizations; destruct tx; cbn beta iota zeta in Hauth |- *;
      try reflexivity.
    destruct s; cbn in Hauth |- ×.
    match type of Hauth with context [match ?l with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒
      destruct l eqn:Hlist end; [discriminate|].
    reflexivity. }
  unfold E.validate_transaction. cbn zeta.
  change (@Result.op_letdollar) with
    (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
      match first with inl value ⇒ next value | inr error ⇒ inr error end).
  change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
  change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
    if condition then failure else inl tt).
  destruct (T.chain_id tx) as [id|] eqn:Hid; cbn beta iota zeta in Hchain |- ×.
  all: try rewrite Hchain.
  all: rewrite Hoverflow, Hsender.
  all: cbv beta iota zeta.
  all: rewrite Henabled, Hcode; cbn [andb].
  all: rewrite Hagree.
  all: rewrite Hnonce.
  all: destruct (T.call_or_create_value tx) as [call|[code]];
    cbv beta iota zeta delta [T.call_or_create.Create.initcode] in Hinit |- ×.
  all: rewrite Hinit, Hintrinsic, Hcap, Hfloor.
  all: cbn [negb andb].
  all: rewrite Hauthorization, Hprice.
  all: cbv beta iota zeta.
  all: rewrite Hbid, Hfee; cbv beta iota zeta; eexists; split; reflexivity.
Qed.

Both kinds of already-admitted sender use the same public checks. Actual nonce equality is needed only before migration. Fee backing and actual nonce headroom are separate maintained execution invariants.
Theorem admitted_transaction_validates state tx sender predicted price :
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  transactionChecks (Chain.Ethereum.Block.t.header (B.t.current_block state))
    tx predicted false = true →
  (A.is_smart_contract (readAccount (B.t.world_state state) sender) = true ∨
    A.t.nonce (readAccount (B.t.world_state state) sender) = predicted) →
  Numeric.U64.op_eq (A.t.nonce (readAccount (B.t.world_state state) sender)) Numeric.U64.max_t = false →
  Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block state))) tx = Some price →
  (T.gas_limit tx × price ≤ Numeric.U256.to_uint
    (A.t.balance (readAccount (B.t.world_state state) sender)))%N →
  ∃ validation, E.validate_transaction state tx = inl validation ∧
    E.transaction_validation.sender validation = sender.
Proof.
  intros Henabled Hsender Hchecks Hagree Hactual Hprice Hbalance.
  destruct (A.is_smart_contract (readAccount (B.t.world_state state) sender)) eqn:Hcode.
  - exact (admitted_migrated_transaction_validates state tx sender predicted price
      Henabled Hsender Hchecks Hcode Hactual Hprice Hbalance).
  - destruct Hagree as [Hbad|Hnonce]; [discriminate|].
    exact (admitted_unmigrated_transaction_validates state tx sender predicted price
      Henabled Hsender Hchecks Hcode Hnonce Hactual Hprice Hbalance).
Qed.

Public admission checks and the two actual-state obligations imply an included failed receipt, without assuming validation or execution succeeds. predicted is the nonce used at admission; it need not equal the actual nonce in state. price is the effective gas price for this block.
Corollary admitted_migrated_transaction_is_included state tx sender predicted price :
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  transactionChecks (Chain.Ethereum.Block.t.header (B.t.current_block state))
    tx predicted false = true →
  A.is_smart_contract (readAccount (B.t.world_state state) sender) = true →
  Numeric.U64.op_eq (A.t.nonce (readAccount (B.t.world_state state) sender)) Numeric.U64.max_t = false →
  Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block state))) tx = Some price →
  (T.gas_limit tx × price ≤ Numeric.U256.to_uint
    (A.t.balance (readAccount (B.t.world_state state) sender)))%N →
  ∃ post receipt,
    E.process_transaction state tx = inl post ∧
    List.In (tx, receipt) (B.t.transactions_processed post) ∧
    Chain.Ethereum.Receipt.t.succeeded receipt = false ∧
    Chain.Ethereum.Receipt.t.cumulative_gas_used receipt = B.t.gas_used post.
Proof.
  intros Henabled Hsender Hchecks Hcode Hactual Hprice Hbalance.
  destruct (admitted_migrated_transaction_validates state tx sender predicted price
    Henabled Hsender Hchecks Hcode Hactual Hprice Hbalance)
    as [validation [Hvalidation Hidentity]].
  apply (included.migrated_transaction_is_included state tx validation Henabled Hvalidation).
  rewrite Hidentity. exact Hcode.
Qed.

Use the remaining-increment invariant to discharge the actual nonce-limit check. state is the state immediately before execution, and remaining counts unprocessed work. Deriving this invariant from InitialNonceBounds through the full VM is still required; it is not an initial-state premise disguised as a completed end-to-end result.
Corollary admitted_migrated_with_nonce_capacity state tx sender predicted price remaining :
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  transactionChecks (Chain.Ethereum.Block.t.header (B.t.current_block state))
    tx predicted false = true →
  A.is_smart_contract (readAccount (B.t.world_state state) sender) = true →
  nonceCapacity (nonceAt (B.t.world_state state) sender) predicted remaining →
  Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block state))) tx = Some price →
  (T.gas_limit tx × price ≤ Numeric.U256.to_uint
    (A.t.balance (readAccount (B.t.world_state state) sender)))%N →
  ∃ post receipt,
    E.process_transaction state tx = inl post ∧
    List.In (tx, receipt) (B.t.transactions_processed post) ∧
    Chain.Ethereum.Receipt.t.succeeded receipt = false ∧
    Chain.Ethereum.Receipt.t.cumulative_gas_used receipt = B.t.gas_used post.
Proof.
  intros Henabled Hsender Hchecks Hcode Hcapacity Hprice Hbalance.
  apply (admitted_migrated_transaction_is_included state tx sender predicted price
    Henabled Hsender Hchecks Hcode); [|exact Hprice|exact Hbalance].
  rewrite rules.u64_eq. apply Z.eqb_neq.
  destruct (capacity_has_nonmaximal_nonces _ _ _ Hcapacity) as [Hactual _].
  change (nonceAt (B.t.world_state state) sender ≠ 2^64-1)%Z. lia.
Qed.

Full migrated-transaction processing preserves the nonce bound needed for the remaining transactions. remaining excludes this transaction; the input allowance includes its sender, authorization and gas counts. Validation success and successful execution are conclusions, not premises. Fee coverage and the residual nonce condition are still obligations on the actual state before this transaction, not proved here from admission.
Theorem admitted_migrated_retains_nonce_capacity state tx sender predicted price remaining :
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  transactionChecks (Chain.Ethereum.Block.t.header (B.t.current_block state))
    tx predicted false = true →
  A.is_smart_contract (readAccount (B.t.world_state state) sender) = true →
  stringmaps.WellFormed (W.t.accounts (B.t.world_state state)) →
  nonceCapacity (nonceAt (B.t.world_state state) sender) predicted
    (transactionNonceBudget tx + remaining) →
  (0 ≤ remaining)%Z →
  Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
    (Chain.Ethereum.Block.t.header (B.t.current_block state))) tx = Some price →
  (T.gas_limit tx × price ≤ Numeric.U256.to_uint
    (A.t.balance (readAccount (B.t.world_state state) sender)))%N →
  ∃ post,
    E.process_transaction state tx = inl post ∧
    stringmaps.WellFormed (W.t.accounts (B.t.world_state post)) ∧
    (0 ≤ nonceAt (B.t.world_state post) sender ∧
      nonceAt (B.t.world_state post) sender + remaining < 2^64-1)%Z.
Proof.
  intros Henabled Hsender Hchecks Hcode Hwf Hcapacity Hr Hprice Hbalance.
  pose proof (checked_nonce_matches _ _ _ _ Hchecks) as Hnonce.
  destruct (capacity_has_nonmaximal_nonces _ _ _ Hcapacity) as [Ha Hp].
  assert (Hactual : Numeric.U64.op_eq (nonceAt (B.t.world_state state) sender) Numeric.U64.max_t = false).
  { rewrite rules.u64_eq. apply Z.eqb_neq. change (nonceAt (B.t.world_state state) sender ≠ 2^64-1)%Z. lia. }
  destruct (admitted_migrated_transaction_validates state tx sender predicted price
    Henabled Hsender Hchecks Hcode Hactual Hprice Hbalance) as [validation [Hv Hidentity]].
  pose proof (migratednonce.migrated_transaction_nonce_bound state tx validation Henabled Hv) as Hpost.
  cbn zeta in Hpost. rewrite Hidentity in Hpost.
  destruct (Hpost Hcode Hwf Ha ltac:(rewrite Hnonce; exact Hp)) as [post [Hrun [Hmap Hbound]]].
  ∃ post. split; [exact Hrun|]. split; [exact Hmap|].
  unfold nonceCapacity, transactionNonceBudget in Hcapacity. rewrite Hnonce in Hbound.
  split; lia.
Qed.

The maximum actual nonce is an actual transaction-processing error, not an included failure. This obstruction cannot be removed by proving fee coverage. The signed native test reaches it with one child CREATE after migration from an initial nonce of 2^64-3.
Theorem migrated_maximum_nonce_rejects state tx sender :
  Setcode_rules.enabled E.Params.(Execution.Make_Params_signature.revision) = true →
  (match T.chain_id tx with
   | Some id ⇒ Support.UintN.op_ltgt id E.Params.(Execution.Make_Params_signature.chain_id)
   | None ⇒ false end) = false →
  Numeric.U64.op_eq (T.nonce tx) Numeric.U64.max_t = false →
  T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
  A.is_smart_contract (readAccount (B.t.world_state state) sender) = true →
  Numeric.U64.op_eq (A.t.nonce (readAccount (B.t.world_state state) sender)) Numeric.U64.max_t = true →
  E.process_transaction state tx =
    E.invalid_transaction (B.t.current_block state) tx Execution.Error.Nonce_overflow.
Proof.
  intros Henabled Hchain Htx Hsender Hcode Hactual.
  assert (Hvalidation : E.validate_transaction state tx =
    E.invalid_transaction (B.t.current_block state) tx Execution.Error.Nonce_overflow).
  { unfold E.validate_transaction. cbn zeta.
    change (@Result.op_letdollar) with
      (fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
        match first with inl value ⇒ next value | inr error ⇒ inr error end).
    change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
    change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
      if condition then failure else inl tt).
    destruct (T.chain_id tx) as [id|] eqn:Hid; cbn beta iota zeta in Hchain |- ×.
    all: try rewrite Hchain.
    all: rewrite Htx, Hsender.
    all: cbv beta iota zeta.
    all: rewrite Henabled, Hcode, Hactual.
    all: reflexivity. }
  unfold E.process_transaction. rewrite Hvalidation. reflexivity.
Qed.
End Validation.

Print Assumptions admitted_migrated_transaction_validates.
Print Assumptions admitted_migrated_transaction_is_included.
Print Assumptions admitted_migrated_with_nonce_capacity.
Print Assumptions admitted_migrated_retains_nonce_capacity.
Print Assumptions migrated_maximum_nonce_rejects.