Library QedkConcrete.authorization

Authorization processing in the concrete executor

EIP-7702 can change code and nonce before the VM runs, including when the VM later reverts. It does not itself transfer balance. We prove this for the generated authorization processor, including invalid entries.
From Stdlib Require Import List ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.worldaccounts QedkConcrete.hostframe QedkConcrete.supply.
Require Import QedkConcrete.hostaccountformat QedkConcrete.bytewords.
Set Default Goal Selector "!".

Module E := MonadVM.Execution.Make.
Module S := MonadVM.State.TransactionState.

The checked address constructor establishes the width required below. The original OCaml type is private; Gallina erases that abstraction.
Lemma decoded_address_length bytes addr :
  @Byte_string.B20.of_bytes bytes = Some addr → OCamlString.length addr = 20%Z.
Proof.
  change ((if Z.eqb (OCamlString.length bytes) 20%Z then Some bytes else None) = Some addr →
    OCamlString.length addr = 20%Z).
  destruct (Z.eqb (OCamlString.length bytes) 20%Z) eqn:Hlength;
    intro Hdecoded; [injection Hdecoded as Heq; subst addr; apply Z.eqb_eq; exact Hlength|discriminate].
Qed.

An authorization installs at most the three-byte delegation marker and its twenty-byte target. The generated address type is an unrefined string, so the target-width condition is explicit here.
Lemma delegation_code_length addr :
  OCamlString.length addr = 20%Z →
  (OCamlString.length (@Delegation.delegation_code addr) ≤ 23)%Z.
Proof.
  intro Haddr. unfold Delegation.delegation_code.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  - change (0 ≤ 23)%Z. lia.
  - change (OCamlString.length (String.append
      (String.append Delegation.eoa_delegation_prefix addr) String.EmptyString) ≤ 23)%Z.
    rewrite !byte_append_length. change (3 + OCamlString.length addr + 0 ≤ 23)%Z. lia.
Qed.

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

Lemma authorization_preserves_balance s auth query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  U.to_z (A.t.balance (readAccount
    (S.t.world_state (snd (@E.process_authorization F s auth))) query)) =
  U.to_z (A.t.balance (readAccount (S.t.world_state s) query)).
Proof.
  intro Hwf. unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b eqn:Hchain end.
  { reflexivity. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] eqn:Hauthority end.
  2: 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:Heligible end.
  2: 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 eqn:Hrefund end;
    change (U.to_z (A.t.balance (readAccount
      (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
        (@W.account (Some false) authority) updated) query)) =
      U.to_z (A.t.balance (readAccount (S.t.world_state s) query)));
    cbn beta; rewrite balance_after_account_write by exact Hwf.
  all: destruct (String.eqb authority query) eqn:Heq; [|reflexivity].
  all: apply String.eqb_eq in Heq; subst query; reflexivity.
Qed.

Lemma authorization_preserves_wellformed s auth :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  stringmaps.WellFormed (W.t.accounts
    (S.t.world_state (snd (@E.process_authorization F s auth)))).
Proof.
  intro Hwf. unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  { exact Hwf. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] end.
  2: exact Hwf.
  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.
  2: exact Hwf.
  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;
    change (stringmaps.WellFormed (W.t.accounts
      (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
        (@W.account (Some false) authority) updated)));
    apply account_write_wellformed; exact Hwf.
Qed.

Lemma authorization_preserves_context s auth :
  transactionContext (snd (@E.process_authorization F s auth)) = transactionContext s.
Proof.
  unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  { reflexivity. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] end.
  2: reflexivity.
  cbn beta iota zeta.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  2: reflexivity.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end; reflexivity.
Qed.

Lemma authorization_preserves_supply s auth :
  totalBalance (S.t.world_state (snd (@E.process_authorization F s auth))) =
    totalBalance (S.t.world_state s).
Proof.
  unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  { reflexivity. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] end.
  2: 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.
  2: reflexivity.
  match goal with |- context [State.op_pointcaretlparenrparenltminus _
    (@S.account None authority) ?value] ⇒ set (updated := value) end.
  assert (Hbalance : U.to_z (A.t.balance updated) =
    U.to_z (A.t.balance (readAccount (S.t.world_state s) authority))) by reflexivity.
  cbn beta in Hbalance.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end;
    change (totalBalance (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some false) authority) updated) = totalBalance (S.t.world_state s));
    rewrite account_write_total, Hbalance; lia.
Qed.

The fold is the one in Execution.process_transaction. Entries are processed sequentially; an invalid entry is not recorded as an applied authority. Repeated authorities require no uniqueness premise.
Lemma authorizations_preserve_balances auths authorities s :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  let result := MonadVM.Result.List.fold_left
    (fun '(authorities, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (authorities, s) auths in
  stringmaps.WellFormed (W.t.accounts (S.t.world_state (snd result))) ∧
  transactionContext (snd result) = transactionContext s ∧
  ∀ query,
    U.to_z (A.t.balance (readAccount (S.t.world_state (snd result)) query)) =
    U.to_z (A.t.balance (readAccount (S.t.world_state s) query)).
Proof.
  revert authorities s. induction auths as [|auth auths IH]; intros authorities s Hwf.
  { split; [exact Hwf|]. split; [reflexivity|intros; reflexivity]. }
  change (@MonadVM.Result.List.fold_left) with (@OCamlList.fold_left) in ×.
  cbn [OCamlList.fold_left].
  pose proof (authorization_preserves_wellformed s auth Hwf) as Hnext.
  pose proof (fun q ⇒ authorization_preserves_balance s auth q Hwf) as Hbalance.
  pose proof (authorization_preserves_context s auth) as Hcontext.
  destruct (@E.process_authorization F s auth) as [[authority|] next] eqn:Hprocess;
    cbn [snd] in Hnext, Hbalance, Hcontext.
  - specialize (IH (authority :: authorities) next Hnext). destruct IH as [Hwf' [Hctx IH]].
    split; [exact Hwf'|]. split; [exact (eq_trans Hctx Hcontext)|].
    intro query. exact (eq_trans (IH query) (Hbalance query)).
  - specialize (IH authorities next Hnext). destruct IH as [Hwf' [Hctx IH]].
    split; [exact Hwf'|]. split; [exact (eq_trans Hctx Hcontext)|].
    intro query. exact (eq_trans (IH query) (Hbalance query)).
Qed.

Authorization processing cannot mint balance, including by creating an otherwise absent account or removing an empty one. This is independent of the arithmetic supply-bound premise.
Lemma authorizations_preserve_supply auths authorities s :
  let result := MonadVM.Result.List.fold_left
    (fun '(authorities, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (authorities, s) auths in
  totalBalance (S.t.world_state (snd result)) = totalBalance (S.t.world_state s).
Proof.
  revert authorities s. induction auths as [|auth auths IH]; intros authorities s.
  { reflexivity. }
  change (@MonadVM.Result.List.fold_left) with (@OCamlList.fold_left) in ×.
  cbn [OCamlList.fold_left].
  pose proof (authorization_preserves_supply s auth) as Hsupply.
  destruct (@E.process_authorization F s auth) as [[authority|] next]; cbn [snd] in Hsupply.
  - exact (eq_trans (IH (authority :: authorities) next) Hsupply).
  - exact (eq_trans (IH authorities next) Hsupply).
Qed.

The fee-debit and authorization prefix of process_transaction charges only the sender, even if an authorization targets that sender or another funded account. The debit range must follow from transaction validation; this component lemma does not assume the rest of the transaction succeeds.
Lemma transaction_entry_balances s sender fee auths :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  (0 ≤ U.to_z (A.t.balance (readAccount (S.t.world_state s) sender)) - U.to_z fee < 2^256)%Z →
  let charged := State.op_pointcaretdollarlparenrparenltminus s
    (@S.account None sender)
    (fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
      (A.t.with_nonce (@Numeric.U64.op_plus (A.t.nonce account)
        (@Numeric.U64.one)) account)) in
  let result := MonadVM.Result.List.fold_left
    (fun '(authorities, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (nil, charged) auths in
  stringmaps.WellFormed (W.t.accounts (S.t.world_state (snd result))) ∧
  transactionContext (snd result) = transactionContext s ∧
  ∀ query,
    U.to_z (A.t.balance (readAccount (S.t.world_state (snd result)) query)) =
    if String.eqb sender query then
      (U.to_z (A.t.balance (readAccount (S.t.world_state s) query)) - U.to_z fee)%Z
    else U.to_z (A.t.balance (readAccount (S.t.world_state s) query)).
Proof.
  intros Hwf Hrange charged.
  assert (Hcharged : stringmaps.WellFormed (W.t.accounts (S.t.world_state charged))).
  { unfold charged. apply account_write_wellformed. exact Hwf. }
  pose proof (authorizations_preserve_balances auths nil charged Hcharged)
    as [Hwf' [Hcontext Hbalance]].
  split; [exact Hwf'|]. split; [exact Hcontext|].
  intro query. eapply eq_trans; [exact (Hbalance query)|].
  exact (@upfront_fee_balance (S.t.world_state s) sender fee query Hwf Hrange).
Qed.

Applying or ignoring an authorization preserves storage widths and the code-size bound needed by the VM. No assumption about signature recovery, the authority's nonce, or uniqueness of authorities is needed.
Lemma authorization_preserves_format s auth :
  OCamlString.length (Chain.Ethereum.Transaction.Authorization.t.address auth) = 20%Z →
  hostFormat s → hostFormat (snd (@E.process_authorization F s auth)).
Proof.
  intros Hwidth [Hworld Htransient]. unfold E.process_authorization.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  { split; assumption. }
  match goal with |- context [match ?a with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct a as [authority|] end.
  2: split; assumption.
  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.
  2: split; assumption.
  match goal with |- context [State.op_pointcaretlparenrparenltminus _
    (@S.account None authority) ?value] ⇒ set (updated := value) end.
  assert (Hupdated : accountFormat updated).
  { unfold updated. apply code_update_format.
    - apply nonce_update_format, read_account_format. exact Hworld.
    - pose proof (@delegation_code_length
        _ Hwidth) as Hlength. lia. }
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  all: split; [|exact Htransient].
  all: change (worldFormat (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some false) authority) updated));
    apply write_account_format; assumption.
Qed.

Lemma authorizations_preserve_format auths authorities s :
  Forall (fun auth ⇒ OCamlString.length
    (Chain.Ethereum.Transaction.Authorization.t.address auth) = 20%Z) auths →
  hostFormat s →
  let result := MonadVM.Result.List.fold_left
    (fun '(authorities, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (authorities, s) auths in
  hostFormat (snd result).
Proof.
  intro Hwidth. revert authorities s.
  induction Hwidth as [|auth auths Hwidth Hrest IH]; intros authorities s Hformat.
  { exact Hformat. }
  change (@MonadVM.Result.List.fold_left) with (@OCamlList.fold_left) in ×.
  cbn [OCamlList.fold_left].
  pose proof (authorization_preserves_format s auth Hwidth Hformat) as Hnext.
  destruct (@E.process_authorization F s auth) as [[authority|] next];
    cbn [snd] in Hnext; apply IH; exact Hnext.
Qed.

The actual fee/nonce write followed by authorization processing establishes the same representation invariant as ordinary Host writes. Fee arithmetic need not be bounded for this lemma: it cannot change code or storage.
Lemma transaction_entry_format s sender fee auths :
  Forall (fun auth ⇒ OCamlString.length
    (Chain.Ethereum.Transaction.Authorization.t.address auth) = 20%Z) auths →
  hostFormat s →
  let charged := State.op_pointcaretdollarlparenrparenltminus s
    (@S.account None sender)
    (fun account ⇒ A.t.with_balance (@U.op_minus (A.t.balance account) fee)
      (A.t.with_nonce (@Numeric.U64.op_plus (A.t.nonce account)
        (@Numeric.U64.one)) account)) in
  let result := MonadVM.Result.List.fold_left
    (fun '(authorities, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (authorities, next)
      | (Some authority, next) ⇒ (authority :: authorities, next)
      end) (nil, charged) auths in
  hostFormat (snd result).
Proof.
  intros Hwidth [Hworld Htransient] charged.
  apply authorizations_preserve_format; [exact Hwidth|].
  split; [|exact Htransient].
  unfold charged. apply write_account_format; [exact Hworld|].
  apply balance_update_format, nonce_update_format, read_account_format. exact Hworld.
Qed.

End Authorization.

Print Assumptions authorizations_preserve_balances.
Print Assumptions transaction_entry_balances.
Print Assumptions authorizations_preserve_supply.
Print Assumptions transaction_entry_format.