Library SetcodeConcrete.noncebudget

Initial nonce bounds and the remaining increment allowance

The initial conditions are nonce < 2^63 for each protected sender and B < 2^63, where B counts all transactions, all authorization entries, and all transaction gas limits in the window. Counts use mathematical integers; they are not native counters and cannot wrap.
The condition preserved after a prefix is weaker than the initial half-range bound: each actual and predicted nonce, plus the remaining allowance, is strictly below 2^64-1. In particular, nonces may cross 2^63 during execution. Checking the initial condition again on a suffix would incorrectly exclude that case.
These arithmetic lemmas do not assume a VM invariant. The connection to concrete VM/Host calls is proved in executionnonce, and lifted to complete blocks by consensusnonce.
From Stdlib Require Import List ZArith Lia.
Require Import MonadVM.State SetcodeConcrete.rules.
Import ListNotations.
Local Open Scope Z_scope.
Set Default Goal Selector "!".
Module T := Chain.Ethereum.Transaction.
Module Block := Chain.Ethereum.Block.
Module Addr := Chain.Ethereum.Address.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.

tx is one decoded transaction. Count its sender increment, every authorization entry (including entries later skipped), and its gas limit. The gas term is a conservative allowance for CREATE/CREATE2 increments, not a claim that every gas unit causes an increment.
Definition transactionNonceBudget (tx : T.t) : Z :=
  1 + Z.of_nat (length (T.authorization_list tx)) + Z.of_N (T.gas_limit tx).

txs are in execution order. Return their total increment allowance.
Definition transactionsNonceBudget (txs : list T.t) : Z :=
  fold_right (fun tx total ⇒ transactionNonceBudget tx + total) 0 txs.

blocks is the accepted window. Empty blocks contribute zero.
Definition windowNonceBudget (blocks : list Block.t) : Z :=
  fold_right (fun block total ⇒
    transactionsNonceBudget (Block.t.transactions block) + total) 0 blocks.

Definition nonceAt (world : W.t) (addr : Addr.t) : Z :=
  A.t.nonce (State.op_pointcaretlparenrparen world (W.account None addr)).

Initial conditions only: senders contains the protected transaction senders, snapshot is the state used for admission, and blocks is the whole accepted window. This does not assume any execution outcome.
Record InitialNonceBounds (senders : list Addr.t) (snapshot : W.t)
    (blocks : list Block.t) : Prop := {
  initial_nonce_half_range : ∀ a, In a senders →
    0 ≤ nonceAt snapshot a < 2^63;
  initial_window_nonce_budget : windowNonceBudget blocks < 2^63
}.

actual is the execution nonce; predicted is the admission nonce for the same address. remaining counts work not yet processed. The two nonces need not agree after code adoption.
Definition nonceCapacity (actual predicted remaining : Z) : Prop :=
  0 ≤ actual ∧ 0 ≤ predicted ∧ 0 ≤ remaining ∧
  Z.max actual predicted + remaining < 2^64-1.

Lemma transaction_nonce_budget_positive tx : 1 ≤ transactionNonceBudget tx.
Proof. unfold transactionNonceBudget. lia. Qed.

Lemma transactions_nonce_budget_nonnegative txs :
  0 ≤ transactionsNonceBudget txs.
Proof.
  unfold transactionsNonceBudget. induction txs as [|tx rest IH]; cbn [fold_right].
  - lia.
  - pose proof (transaction_nonce_budget_positive tx). lia.
Qed.

Lemma window_nonce_budget_nonnegative blocks : 0 ≤ windowNonceBudget blocks.
Proof.
  unfold windowNonceBudget. induction blocks as [|block rest IH]; cbn [fold_right].
  - lia.
  - pose proof (transactions_nonce_budget_nonnegative (Block.t.transactions block)). lia.
Qed.

Lemma transactions_nonce_budget_app prefix rest :
  transactionsNonceBudget (prefix ++ rest) =
  transactionsNonceBudget prefix + transactionsNonceBudget rest.
Proof.
  unfold transactionsNonceBudget. induction prefix; cbn [app fold_right];
    [reflexivity|rewrite IHprefix; lia].
Qed.

Lemma window_nonce_budget_app prefix rest :
  windowNonceBudget (prefix ++ rest) = windowNonceBudget prefix + windowNonceBudget rest.
Proof.
  unfold windowNonceBudget. induction prefix; cbn [app fold_right];
    [reflexivity|rewrite IHprefix; lia].
Qed.

Lemma half_ranges_give_capacity nonce budget :
  0 ≤ nonce < 2^63 → 0 ≤ budget < 2^63 → nonceCapacity nonce nonce budget.
Proof. unfold nonceCapacity. intros Hn Hb. rewrite Z.max_id. repeat split; lia. Qed.

Theorem initial_nonce_capacity senders snapshot blocks a :
  InitialNonceBounds senders snapshot blocks → In a senders →
  nonceCapacity (nonceAt snapshot a) (nonceAt snapshot a) (windowNonceBudget blocks).
Proof.
  intros [Hnonces Hbudget] Hin. apply half_ranges_give_capacity.
  - exact (Hnonces a Hin).
  - split; [apply window_nonce_budget_nonnegative|exact Hbudget].
Qed.

Lemma capacity_has_nonmaximal_nonces actual predicted remaining :
  nonceCapacity actual predicted remaining →
  0 ≤ actual < 2^64-1 ∧ 0 ≤ predicted < 2^64-1.
Proof. unfold nonceCapacity. intros H. lia. Qed.

A common upper bound is essential: the migrated sender update can jump from the actual nonce to the larger predicted nonce before adding one.
Lemma capacity_after_increment actual predicted remaining nextActual nextPredicted :
  nonceCapacity actual predicted (1 + remaining) →
  0 ≤ remaining →
  0 ≤ nextActual ≤ Z.max actual predicted + 1 →
  0 ≤ nextPredicted ≤ Z.max actual predicted + 1 →
  nonceCapacity nextActual nextPredicted remaining.
Proof. unfold nonceCapacity. intros H Hr Ha Hp. repeat split; lia. Qed.

Lemma capacity_after_gas actual predicted gas remaining nextActual :
  nonceCapacity actual predicted (gas + remaining) →
  0 ≤ gas → 0 ≤ remaining → 0 ≤ nextActual ≤ actual + gas →
  nonceCapacity nextActual predicted remaining.
Proof. unfold nonceCapacity. intros H Hg Hr Ha. repeat split; lia. Qed.

Rollback or a skipped authorization can discard an allocated unit without incrementing either nonce.
Lemma capacity_discard actual predicted extra remaining :
  nonceCapacity actual predicted (extra + remaining) →
  0 ≤ extra → 0 ≤ remaining → nonceCapacity actual predicted remaining.
Proof. unfold nonceCapacity. intros H He Hr. repeat split; lia. Qed.

The actual generated max-and-increment helper cannot reject or wrap under the remaining allowance. The updated admission nonce is the ordinary predicted nonce plus one, not the actual execution nonce.
Theorem migrated_increment_preserves_capacity actual predicted remaining :
  nonceCapacity actual predicted (1 + remaining) → 0 ≤ remaining →
  ∃ next,
    Setcode_rules.next_sender_nonce true actual predicted = Some next ∧
    nonceCapacity next (predicted + 1) remaining.
Proof.
  intros Hcapacity Hremaining.
  destruct (capacity_has_nonmaximal_nonces actual predicted (1+remaining) Hcapacity)
    as [Ha Hp].
  ∃ (Z.max actual predicted + 1). split.
  - apply migrated_nonce; assumption.
  - apply capacity_after_increment with (actual := actual) (predicted := predicted);
      try assumption; lia.
Qed.

The strict half-range bounds leave at least one unused nonce even at their largest permitted inputs. This is a kernel-checked arithmetic test.
Example largest_initial_values_are_safe :
  nonceCapacity (2^63-1) (2^63-1) (2^63-1).
Proof. apply half_ranges_give_capacity; lia. Qed.

Example crossing_half_range_is_safe :
  Setcode_rules.next_sender_nonce true (2^63-1) (2^63-1) = Some (2^63) ∧
  nonceCapacity (2^63) (2^63) 1.
Proof.
  split.
  - rewrite migrated_nonce by lia. rewrite Z.max_id. reflexivity.
  - unfold nonceCapacity. rewrite Z.max_id. repeat split; lia.
Qed.

Print Assumptions initial_nonce_capacity.
Print Assumptions migrated_increment_preserves_capacity.