Library QedkConcrete.authorizationnonces

Predicting authorization effects without executing the VM

Signature recovery, chain ID, code eligibility and sequential nonces determine which EIP-7702 entries apply. Balances, storage and VM results do not participate. The projection below is intended for the consensus history connection; equality with the actual processor is proved here.
From Stdlib Require Import List String ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.authorizationcode
  QedkConcrete.authorization QedkConcrete.worldaccounts.
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.
Import ListNotations.
Module E := Execution.Make.
Module W := State.WorldState.
Module S := State.TransactionState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.

Section Accounts.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).

Lemma empty_account_nonce account :
  @A.is_empty account = true → A.t.nonce account = 0%Z.
Proof.
  unfold A.is_empty. intro Hempty.
  apply Bool.andb_true_iff in Hempty as [_ Hempty].
  apply Bool.andb_true_iff in Hempty as [Hnonce _].
  change (Z.eqb (match Z.compare (A.t.nonce account) 0 with
    Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hnonce.
  destruct (Z.compare_spec (A.t.nonce account) 0); cbn in Hnonce;
    [assumption|discriminate|discriminate].
Qed.

Lemma nonce_after_account_write keep world addr value query :
  stringmaps.WellFormed (W.t.accounts world) →
  A.t.nonce (readAccount (State.op_pointcaretlparenrparenltminus world
    (@W.account (Some keep) addr) value) query) =
  if String.eqb addr query then A.t.nonce value else A.t.nonce (readAccount world query).
Proof.
  intro Hmap. cbv beta. rewrite account_read_after_write by exact Hmap.
  destruct (String.eqb addr query); [|reflexivity].
  destruct keep; [reflexivity|].
  destruct (@A.is_empty value) eqn:Hempty; [|reflexivity].
  symmetry. exact (empty_account_nonce value Hempty).
Qed.
End Accounts.

Section Authorization.
Context {F : E.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%Z).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation recoveredAuthority := (@Chain.Ethereum.Transaction.Authorization.authority).

eligible a says address a has empty code or a delegation marker; nonces a is its nonce before this entry; auth is one signed EIP-7702 authorization. The chain ID is supplied by F.
Return (Some authority, after) if the entry passes the chain-ID, nonce-limit, signature, code and nonce-equality checks, with that address's nonce incremented in after. Otherwise return (None, nonces). This function calculates nonce changes; it does not write delegation code.
Definition authorizationNonceStep (eligible : Addr.t → bool)
    (nonces : Addr.t → Numeric.U64.t) (auth : T.Authorization.t)
    : option Addr.t × (Addr.t → Numeric.U64.t) :=
  if orb
    (andb (Numeric.U256.op_ltgt (T.Authorization.t.chain_id auth) (@Numeric.U256.zero))
      (Support.UintN.op_ltgt (@Numeric.U256.to_uint (T.Authorization.t.chain_id auth))
        (@E.Params F).(Execution.Make_Params_signature.chain_id)))
    (Numeric.U64.op_eq (T.Authorization.t.nonce auth) Numeric.U64.max_t)
  then (None, nonces)
  else match recoveredAuthority auth with
  | None ⇒ (None, nonces)
  | Some authority ⇒
      if andb (eligible authority) (Numeric.U64.op_eq (T.Authorization.t.nonce auth) (nonces authority))
      then (Some authority, fun query ⇒ if String.eqb authority query
        then @Numeric.U64.op_plus (nonces authority) (@Numeric.U64.one) else nonces query)
      else (None, nonces)
  end.

Lemma authority_code_eligible account :
  negb (@A.is_smart_contract account) =
    orb (Byte_string.Bytes.op_eq (A.t.code account) Byte_string.Bytes.empty)
      (@Delegation.is_valid_delegation (A.t.code account)).
Proof.
  unfold A.is_smart_contract.
  change (negb (andb (negb (Byte_string.Bytes.op_eq (A.t.code account) Byte_string.Bytes.empty))
    (negb (@Delegation.is_valid_delegation (A.t.code account)))) =
    orb (Byte_string.Bytes.op_eq (A.t.code account) Byte_string.Bytes.empty)
      (@Delegation.is_valid_delegation (A.t.code account))).
  destruct (Byte_string.Bytes.op_eq (A.t.code account) Byte_string.Bytes.empty),
    (@Delegation.is_valid_delegation (A.t.code account)); reflexivity.
Qed.

Lemma authorization_nonce_effect s auth query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  let result := @E.process_authorization F s auth in
  A.t.nonce (readAccount (S.t.world_state (snd result)) query) =
    if match fst result with Some addr ⇒ String.eqb addr query | None ⇒ false end
    then @Numeric.U64.op_plus (A.t.nonce (readAccount (S.t.world_state s) query)) (@Numeric.U64.one)
    else A.t.nonce (readAccount (S.t.world_state s) query).
Proof.
  intro Hmap. unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [reflexivity|].
  destruct (recoveredAuthority auth) as [authority|]; [|reflexivity].
  cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [|reflexivity].
  match goal with |- context [State.op_pointcaretlparenrparenltminus _
    (@S.account None authority) ?value] ⇒ set (updated := value) end.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  all: change (A.t.nonce (readAccount
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some false) authority) updated) query) =
    if String.eqb authority query then
      @Numeric.U64.op_plus (A.t.nonce (readAccount (S.t.world_state s) query)) (@Numeric.U64.one)
    else A.t.nonce (readAccount (S.t.world_state s) query)).
  all: cbv beta; rewrite nonce_after_account_write by exact Hmap.
  all: destruct (String.eqb authority query) eqn:Heq; [|reflexivity].
  all: apply String.eqb_eq in Heq; subst query; reflexivity.
Qed.

Lemma authorization_nonce_decision s auth :
  fst (@E.process_authorization F s auth) =
  fst (authorizationNonceStep
    (fun addr ⇒ negb (@A.is_smart_contract (readAccount (S.t.world_state s) addr)))
    (fun addr ⇒ A.t.nonce (readAccount (S.t.world_state s) addr)) auth).
Proof.
  unfold E.process_authorization, authorizationNonceStep.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [reflexivity|].
  destruct (recoveredAuthority auth) as [authority|]; [|reflexivity].
  cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
  rewrite authority_code_eligible.
  match goal with |- context [State.op_pointcaretlparenrparen ?state (@S.account None authority)] ⇒
    change (State.op_pointcaretlparenrparen state (@S.account None authority))
      with (readAccount (S.t.world_state s) authority)
  end.
  cbv beta.
  destruct (readAccount (S.t.world_state s) authority) as [nonce balance storage code].
  cbn beta iota zeta delta [A.t.nonce A.t.code A.t.balance A.t.storage].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [|reflexivity].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; reflexivity.
Qed.

Applying an authorization preserves the empty-or-delegated classification. In particular, replacing a delegation or clearing it does not require a new eligibility lookup from execution while scanning the next entry.
Lemma authorization_code_eligibility s auth query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  OCamlString.length (T.Authorization.t.address auth) = 20 →
  negb (@A.is_smart_contract (readAccount (S.t.world_state (snd (@E.process_authorization F s auth))) query)) =
  negb (@A.is_smart_contract (readAccount (S.t.world_state s) query)).
Proof.
  intros Hmap Hwidth. unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [reflexivity|].
  destruct (recoveredAuthority auth) as [authority|]; [|reflexivity].
  cbn beta iota zeta delta [S.t.with_accessed_addresses S.t.world_state].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b eqn:Hvalid end; [|reflexivity].
  apply Bool.andb_true_iff in Hvalid as [Hallowed Hnonce].
  assert (Hbefore : negb (@A.is_smart_contract (readAccount (S.t.world_state s) authority)) = true).
  { rewrite authority_code_eligible. exact Hallowed. }
  match goal with |- context [State.op_pointcaretlparenrparenltminus _
    (@S.account None authority) ?value] ⇒ set (updated := value) end.
  assert (Hafter : @A.is_smart_contract updated = false).
  { unfold updated. apply delegation_code_not_contract. exact Hwidth. }
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  all: change (negb (@A.is_smart_contract (readAccount
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some false) authority) updated) query)) =
    negb (@A.is_smart_contract (readAccount (S.t.world_state s) query))).
  all: cbv beta; rewrite account_read_after_write by exact Hmap.
  all: destruct (String.eqb authority query) eqn:Heq; [|reflexivity].
  all: apply String.eqb_eq in Heq; subst query.
  all: destruct (@A.is_empty updated); [exact (eq_sym Hbefore)|].
  all: rewrite Hafter; exact (eq_sym Hbefore).
Qed.

Lemma authorization_nonce_step_effect eligible nonces auth query :
  snd (authorizationNonceStep eligible nonces auth) query =
  if match fst (authorizationNonceStep eligible nonces auth) with
    Some authority ⇒ String.eqb authority query | None ⇒ false end
  then @Numeric.U64.op_plus (nonces query) (@Numeric.U64.one) else nonces query.
Proof.
  unfold authorizationNonceStep.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [reflexivity|].
  destruct (recoveredAuthority auth) as [authority|]; [|reflexivity].
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [|reflexivity].
  cbn beta iota zeta delta [fst snd].
  destruct (String.eqb authority query) eqn:Heq; [apply String.eqb_eq in Heq; subst; reflexivity|reflexivity].
Qed.

Lemma authorization_nonce_decision_ext eligible nonces otherEligible otherNonces auth :
  (∀ a, eligible a = otherEligible a) →
  (∀ a, nonces a = otherNonces a) →
  fst (authorizationNonceStep eligible nonces auth) =
    fst (authorizationNonceStep otherEligible otherNonces auth).
Proof.
  intros Heligible Hnonces. unfold authorizationNonceStep.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [reflexivity|].
  destruct (recoveredAuthority auth) as [authority|]; [|reflexivity].
  rewrite (Heligible authority), (Hnonces authority).
  destruct (andb _ _); reflexivity.
Qed.

Lemma authorization_nonce_projection s eligible nonces auth :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  OCamlString.length (T.Authorization.t.address auth) = 20 →
  (∀ a, negb (@A.is_smart_contract (readAccount (S.t.world_state s) a)) = eligible a) →
  (∀ a, A.t.nonce (readAccount (S.t.world_state s) a) = nonces a) →
  let actual := @E.process_authorization F s auth in
  let predicted := authorizationNonceStep eligible nonces auth in
  fst actual = fst predicted ∧
  ∀ a, negb (@A.is_smart_contract (readAccount (S.t.world_state (snd actual)) a)) = eligible a ∧
    A.t.nonce (readAccount (S.t.world_state (snd actual)) a) = snd predicted a.
Proof.
  intros Hmap Hwidth Heligible Hnonces. cbn zeta.
  assert (Hdecision : fst (@E.process_authorization F s auth) = fst (authorizationNonceStep eligible nonces auth)).
  { rewrite authorization_nonce_decision. apply authorization_nonce_decision_ext; assumption. }
  split; [exact Hdecision|]. intro a. split.
  - rewrite authorization_code_eligibility by assumption. apply Heligible.
  - rewrite authorization_nonce_effect by exact Hmap.
    rewrite authorization_nonce_step_effect, Hdecision, Hnonces. reflexivity.
Qed.

Calculate authorization nonce changes without executing contract code.
  • eligible a: true if address a has empty code or a delegation marker.
  • initial: addresses already counted as applied, and the nonce of each address before this authorization list. The transaction's sender nonce must already have been incremented by the caller.
  • auths: the transaction's authorization list, processed in its given order.
Result: the accumulated applied-address list and final nonce function. Each entry passing chain-ID, nonce-limit, signature, code-eligibility and nonce-equality checks increments its authority's nonce and prepends that address. Other entries leave both components unchanged. Repeated authorities are checked against the nonce resulting from earlier entries.
Definition authorizationNonces (eligible : Addr.t → bool)
    (initial : list Addr.t × (Addr.t → Numeric.U64.t)) (auths : list T.Authorization.t)
    : list Addr.t × (Addr.t → Numeric.U64.t) :=
  OCamlList.fold_left (fun '(applied, nonces) auth ⇒
    let '(authority, after) := authorizationNonceStep eligible nonces auth in
    match authority with Some a ⇒ (a :: applied, after) | None ⇒ (applied, after) end)
    initial auths.

This scan predicts the exact applied-authority list, not a superset of recovered signatures. Each accepted entry consumes its nonce before the next entry is tested. The proof uses pointwise equality of nonce maps and therefore needs no functional-extensionality axiom.
Theorem authorizations_nonce_projection eligible auths authorities nonces s :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) auths →
  (∀ a, negb (@A.is_smart_contract (readAccount (S.t.world_state s) a)) = eligible a) →
  (∀ a, A.t.nonce (readAccount (S.t.world_state s) a) = nonces a) →
  let actual := Result.List.fold_left
    (fun '(applied, state) auth ⇒
      match @E.process_authorization F state auth with
      | (Some a, next) ⇒ (a :: applied, next) | (None, next) ⇒ (applied, next) end)
    (authorities, s) auths in
  let predicted := authorizationNonces eligible (authorities, nonces) auths in
  fst actual = fst predicted ∧
  ∀ a, negb (@A.is_smart_contract (readAccount (S.t.world_state (snd actual)) a)) = eligible a ∧
    A.t.nonce (readAccount (S.t.world_state (snd actual)) a) = snd predicted a.
Proof.
  revert authorities nonces s.
  induction auths as [|auth rest IH]; intros authorities nonces s Hmap Hwidth Heligible Hnonces.
  - split; [reflexivity|]. intro a. split; [apply Heligible|apply Hnonces].
  - inversion Hwidth as [|? ? Hhead Htail]; subst.
    pose proof (authorization_nonce_projection s eligible nonces auth Hmap Hhead Heligible Hnonces) as Hstep.
    pose proof (@authorization_preserves_wellformed F s auth Hmap) as HnextMap.
    change (@Result.List.fold_left) with (@OCamlList.fold_left).
    unfold authorizationNonces. cbn [OCamlList.fold_left].
    destruct (@E.process_authorization F s auth) as [effect next].
    destruct (authorizationNonceStep eligible nonces auth) as [predicted nextNonces].
    cbn beta iota zeta delta [fst snd] in Hstep, HnextMap.
    destruct Hstep as [Heffect Hnext]. subst predicted.
    destruct effect; (apply IH;
      [exact HnextMap|exact Htail|intro a; exact (proj1 (Hnext a))|intro a; exact (proj2 (Hnext a))]).
Qed.

An ignored entry cannot supply an authority nonce to consensus, even if its signature recovered successfully.
Lemma authorization_nonce_step_rejects_wrong_nonce eligible nonces auth authority :
  recoveredAuthority auth = Some authority →
  Numeric.U64.op_eq (T.Authorization.t.nonce auth) (nonces authority) = false →
  authorizationNonceStep eligible nonces auth = (None, nonces).
Proof.
  intros Hrecovery Hnonce. unfold authorizationNonceStep.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; [reflexivity|].
  rewrite Hrecovery, Hnonce, Bool.andb_false_r. reflexivity.
Qed.

The sender nonce is consumed before its transaction's authorizations. This projection needs neither the upfront balance debit nor VM execution.
Definition transactionAuthorizationNonces (world : W.t) (sender : Addr.t) (tx : T.t)
    : list Addr.t × (Addr.t → Numeric.U64.t) :=
  authorizationNonces
    (fun a ⇒ negb (@A.is_smart_contract (readAccount world a)))
    ([], fun a ⇒ if String.eqb sender a
      then @Numeric.U64.op_plus (A.t.nonce (readAccount world a)) (@Numeric.U64.one)
      else A.t.nonce (readAccount world a))
    (T.authorization_list tx).

Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
  {UB : Basics.Unreachable Bloom.t}
  {U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.

Theorem transaction_authorization_nonce_projection block tx validation :
  stringmaps.WellFormed (W.t.accounts (State.BlockState.t.world_state block)) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20) (T.authorization_list tx) →
  let actual := @transactionhistory.transactionPrefix F block tx validation in
  let predicted := transactionAuthorizationNonces (State.BlockState.t.world_state block)
    (E.transaction_validation.sender validation) tx in
  fst actual = fst predicted ∧
  ∀ a, negb (@A.is_smart_contract (readAccount (S.t.world_state (snd actual)) a)) =
      negb (@A.is_smart_contract (readAccount (State.BlockState.t.world_state block) a)) ∧
    A.t.nonce (readAccount (S.t.world_state (snd actual)) a) = snd predicted a.
Proof.
  intros Hmap Hwidth. cbn zeta. unfold transactionhistory.transactionPrefix, transactionAuthorizationNonces.
  apply authorizations_nonce_projection.
  - apply account_write_wellformed. exact Hmap.
  - exact Hwidth.
  - intro a. rewrite !authority_code_eligible.
    assert (Hcode : A.t.code (readAccount
      (State.op_pointcaretlparenrparenltminus (State.BlockState.t.world_state block)
        (@W.account (Some false) (E.transaction_validation.sender validation))
        (A.t.with_balance
          (@Numeric.U256.op_minus
            (A.t.balance (readAccount (State.BlockState.t.world_state block) (E.transaction_validation.sender validation)))
            (E.transaction_validation.total_fee validation))
          (A.t.with_nonce
            (@Numeric.U64.op_plus
              (A.t.nonce (readAccount (State.BlockState.t.world_state block) (E.transaction_validation.sender validation)))
              (@Numeric.U64.one))
            (readAccount (State.BlockState.t.world_state block) (E.transaction_validation.sender validation))))) a) =
        A.t.code (readAccount (State.BlockState.t.world_state block) a)).
    { cbv beta. rewrite code_after_account_write by exact Hmap.
      destruct (String.eqb (E.transaction_validation.sender validation) a) eqn:Heq; [|reflexivity].
      apply String.eqb_eq in Heq. subst a. reflexivity. }
    exact (f_equal (fun code ⇒ orb (Byte_string.Bytes.op_eq code Byte_string.Bytes.empty)
      (@Delegation.is_valid_delegation code)) Hcode).
  - intro a.
    cbv beta.
    match goal with |- _ = ?rhs ⇒
    change (A.t.nonce (State.op_pointcaretlparenrparen
      (State.op_pointcaretlparenrparenltminus (State.BlockState.t.world_state block)
        (@W.account (Some false) (E.transaction_validation.sender validation))
        (A.t.with_balance
          (@Numeric.U256.op_minus
            (A.t.balance (readAccount (State.BlockState.t.world_state block) (E.transaction_validation.sender validation)))
            (E.transaction_validation.total_fee validation))
          (A.t.with_nonce
            (@Numeric.U64.op_plus
              (A.t.nonce (readAccount (State.BlockState.t.world_state block) (E.transaction_validation.sender validation)))
              (@Numeric.U64.one))
            (readAccount (State.BlockState.t.world_state block) (E.transaction_validation.sender validation)))))
      (@W.account None a)) = rhs)
    end.
    rewrite nonce_after_account_write by exact Hmap.
    destruct (String.eqb (E.transaction_validation.sender validation) a) eqn:Heq; [|reflexivity].
    apply String.eqb_eq in Heq. subst a. reflexivity.
Qed.

End Authorization.
Print Assumptions authorization_nonce_effect.
Print Assumptions authorization_nonce_decision.

Print Assumptions authorization_nonce_projection.

Print Assumptions authorizations_nonce_projection.

Print Assumptions transaction_authorization_nonce_projection.