Library SetcodeConcrete.blocknonce

Nonce exhaustion is impossible for an admitted block

This covers the real block processor, including reserve-precompile activation, both system calls, transactions, and output verification. Only the nonce part of admission is required; other validation and block errors are not ruled out here. No reached-state nonce bound is assumed.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.blockentry QedkConcrete.worldaccounts QedkConcrete.accountnonces QedkConcrete.admissionnonces.
Require Import SetcodeConcrete.noncebudget SetcodeConcrete.noncesequence SetcodeConcrete.systemcodeframe
  SetcodeConcrete.codeframe SetcodeConcrete.nonceprecompile.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module W := State.WorldState.
Module B := State.BlockState.
Module Block := Chain.Ethereum.Block.
Module A := Chain.Ethereum.Account.
Local Abbreviation codeAt := (fun world addr ⇒
  A.t.code (State.op_pointcaretlparenrparen world (W.account None addr))).

Section Execution.
Context {F : E.FArgs}.
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
  Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).

Lemma block_entry_nonce world block query :
  query ≠ Reserve_balance.precompile_address →
  stringmaps.WellFormed (W.t.accounts world) →
  codeAt world E.beacon_roots_address = systemcode.beaconRootsCode →
  codeAt world E.history_storage_address = systemcode.historyStorageCode →
  stringmaps.WellFormed (W.t.accounts (B.t.world_state (blockTransactionEntry world block))) ∧
  nonceAt (B.t.world_state (blockTransactionEntry world block)) query = nonceAt world query.
Proof.
  intros Hnative Hmap Hbeacon Hhistory.
  set (activated := Reserve_balance.activate_precompile world).
  assert (Hactive : stringmaps.WellFormed (W.t.accounts activated))
    by (apply account_write_wellformed; exact Hmap).
  assert (Hnonce : nonceAt activated query = nonceAt world query).
  { unfold activated, Reserve_balance.activate_precompile, nonceAt.
    eapply account_write_other_nonce; [exact Hmap|congruence]. }
  assert (Hb : codeAt activated E.beacon_roots_address = systemcode.beaconRootsCode).
  { exact (eq_trans (activation_preserves_code world _ Hmap) Hbeacon). }
  assert (Hh : codeAt activated E.history_storage_address = systemcode.historyStorageCode).
  { exact (eq_trans (activation_preserves_code world _ Hmap) Hhistory). }
  unfold blockTransactionEntry. cbn zeta.
  apply (two_system_messages_nonce cold warm access write reserve Hrevision Hcold Hwarm
    (B.make activated block) _ _ query (nonceAt world query)); assumption.
Qed.

Lemma block_transaction_error_from_entry verify world block details :
  E.process_block verify world block = inr (Execution.Error.Invalid_transaction details) →
  Result.List.fold_leftM E.process_transaction (blockTransactionEntry world block)
    (Block.t.transactions block) = inr (Execution.Error.Invalid_transaction details).
Proof.
  intro Hrun. unfold E.process_block in Hrun. rewrite Hrevision in Hrun.
  change (@Result.op_letdollar) with
    (fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
      match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
  destruct (E.validate_block world block) as [[]|error] eqn:Hvalid.
  2: { cbn in Hrun. injection Hrun as Heq. subst error.
       exfalso. exact (validate_block_no_transaction_error world block details Hvalid). }
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [Result.List.fold_leftM ?step ?entry ?txs] ⇒
    change entry with (blockTransactionEntry world block) in Hrun end.
  destruct (Result.List.fold_leftM E.process_transaction (blockTransactionEntry world block)
    (Block.t.transactions block)) as [after|error] eqn:Htxs.
  2: { cbn in Hrun. injection Hrun as →. reflexivity. }
  cbn beta iota zeta in Hrun.
  match type of Hrun with context [E.validate_block ?w ?b] ⇒
    pose proof (validate_block_no_transaction_error w b details) as Hnot;
    destruct (E.validate_block w b) as [[]|error] eqn:Hpost end.
  2: { cbn in Hrun. injection Hrun as Heq. subst error. contradiction. }
  match type of Hrun with context [if verify then ?check else _] ⇒
    destruct verify; [destruct check as [[]|error] eqn:Hcheck|] end.
  - discriminate Hrun.
  - cbn in Hrun. injection Hrun as Heq. subst error.
    exfalso. eapply validate_output_no_transaction_error. exact Hcheck.
  - discriminate Hrun.
Qed.

world is the state before block; eligible is the public code eligibility function used for nonce admission. senders contains the recovered transaction senders. Initial nonces and the block's increment allowance are each below 2^63. The system contracts have their prescribed code in world. Neither validation success nor execution success is a premise. The conclusion excludes specifically Nonce_overflow.
Theorem admitted_block_does_not_exhaust_nonces verify world block senders eligible :
  NonceAdmission eligible (nonceAt world) (Block.t.transactions block) →
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  Forall (fun tx ⇒ ∀ a,
    Chain.Ethereum.Transaction.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
    In a senders) (Block.t.transactions block) →
  stringmaps.WellFormed (W.t.accounts world) →
  (∀ a, In a senders → 0 ≤ nonceAt world a < 2^63) →
  transactionsNonceBudget (Block.t.transactions block) < 2^63 →
  codeAt world E.beacon_roots_address = systemcode.beaconRootsCode →
  codeAt world E.history_storage_address = systemcode.historyStorageCode →
  ∀ error, E.process_block verify world block = inr error → ¬ nonceOverflowError error.
Proof.
  intros Hadmission Hnative Hsenders Hmap Hnonces Hbudget Hbeacon Hhistory error Hrun.
  destruct error as [details|details]; [exact (fun H ⇒ H)|].
  pose proof (block_transaction_error_from_entry verify world block details Hrun) as Htxs.
  pose proof (nonce_admission_senders eligible _ _ _ Hadmission Hsenders) as Hmembers.
  destruct (Block.t.transactions block) as [|first rest] eqn:Htransactions.
  - discriminate Htxs.
  - inversion Hmembers as [|? ? [sender [Hsender Hin]] Hrest]; subst.
    destruct (block_entry_nonce world block sender (Hnative sender Hin) Hmap Hbeacon Hhistory)
      as [Hmap' _].
    eapply (half_range_transactions_do_not_exhaust_nonces cold warm access write reserve
      Hrevision Hcold Hwarm (first :: rest) (blockTransactionEntry world block) senders).
    + exact Hnative.
    + exact Hmembers.
    + exact Hmap'.
    + intros a Ha. rewrite (proj2 (block_entry_nonce world block a
        (Hnative a Ha) Hmap Hbeacon Hhistory)). exact (Hnonces a Ha).
    + unfold signedNonceBounds. eapply nonce_admission_bounds.
      × exact Hadmission.
      × exact Hsenders.
      × intros a Ha. specialize (Hnonces a Ha). lia.
      × intro tx. unfold transactionNonceBudget. lia.
    + exact Hbudget.
    + exact Htxs.
Qed.
End Execution.

Print Assumptions admitted_block_does_not_exhaust_nonces.