Library QedkConcrete.authorizationnoncecore

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.authorizationcodecore
  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.

Require Export QedkConcrete.accountnonces QedkConcrete.authorizationnoncechecks.
Definition empty_account_nonce := @accountnonces.empty_account_nonce.
Definition nonce_after_account_write := @accountnonces.nonce_after_account_write.

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).

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.

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.

End Authorization.