Library QedkConcrete.authorizationcode

Code changes caused by concrete authorization processing

An applied EIP-7702 entry installs a delegation marker or clears code; it never creates a non-delegated contract. Unapplied entries do not change code, including for authorities whose signature was recovered but whose nonce check failed. These facts are derived from process_authorization.
From Stdlib Require Import List String ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.authorization QedkConcrete.worldaccounts
  QedkConcrete.transactionhistory.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.

Lemma empty_account_code {U64 : Basics.Unreachable Numeric.U64.t}
    {UF : Basics.Unreachable Numeric.U256.t} account :
  @A.is_empty account = true → A.t.code account = String.EmptyString.
Proof.
  unfold A.is_empty. intro Hempty.
  apply Bool.andb_true_iff in Hempty as [_ Hempty].
  apply Bool.andb_true_iff in Hempty as [_ Hcode].
  change (Z.eqb (OCamlString.compare (A.t.code account) String.EmptyString) 0%Z
    = true) in Hcode.
  apply Z.eqb_eq in Hcode. apply reservecheck.address_compare_eq. exact Hcode.
Qed.

Lemma delegation_code_not_contract
    {UA : Basics.Unreachable Delegation.Address.t} {UI : Basics.Unreachable int} addr account :
  OCamlString.length addr = 20%Z →
  @A.is_smart_contract
    (A.t.with_code (@Delegation.delegation_code addr) account) = false.
Proof.
  intro Hwidth. unfold Delegation.delegation_code.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  - reflexivity.
  - change (@A.is_smart_contract (A.t.with_code
      (String.append Delegation.eoa_delegation_prefix (String.append addr String.EmptyString)) account) = false).
    assert (Hempty : ∀ s, String.append s String.EmptyString = s).
    { intro s. induction s; simpl; congruence. }
    rewrite Hempty.
    assert (Hdelegation : @Delegation.is_valid_delegation
      (String.append Delegation.eoa_delegation_prefix addr) = true).
    { unfold Delegation.is_valid_delegation.
      unfold Byte_string.Bytes.length, MonadVM.String.length.
      rewrite bytewords.byte_append_length.
      change (andb (Z.eqb (3 + OCamlString.length addr)%Z 23%Z) true = true).
      rewrite Hwidth. reflexivity. }
    unfold A.is_smart_contract. cbn beta iota zeta delta [A.t.with_code A.t.code].
    rewrite Hdelegation. apply Bool.andb_false_r.
Qed.

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

Deleting an empty account preserves its empty code.
Lemma code_after_account_write keep world addr value query :
  stringmaps.WellFormed (W.t.accounts world) →
  A.t.code (readAccount (State.op_pointcaretlparenrparenltminus world
    (@W.account (Some keep) addr) value) query) =
    if String.eqb addr query then A.t.code value else A.t.code (readAccount world query).
Proof.
  intro Hwf. cbv beta.
  etransitivity; [exact (f_equal A.t.code
    (account_read_after_write keep world addr value query Hwf))|].
  destruct (String.eqb addr query); [|reflexivity].
  destruct keep; [reflexivity|].
  destruct (@A.is_empty value) eqn:Hempty; [|reflexivity].
  symmetry. exact (empty_account_code value Hempty).
Qed.
End Accounts.

Section Authorization.
Context {F : E.FArgs}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%N).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation UI := (Opaque.legacy_witness 0%Z).
Local Abbreviation UA := (Opaque.legacy_witness String.EmptyString).
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).

The returned Some authority, rather than signature recovery alone, identifies whose code changed. Invalid entries can warm access sets, but cannot change account code.
Theorem authorization_code_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.code (readAccount (S.t.world_state (snd result)) query) =
    if match fst result with Some addr ⇒ String.eqb addr query | None ⇒ false end
    then @Delegation.delegation_code (T.Authorization.t.address auth)
    else A.t.code (readAccount (S.t.world_state s) query).
Proof.
  intro Hwf. 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.
  match goal with |- context [if ?b then _ else _] ⇒ destruct b end.
  all: change (A.t.code (readAccount
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some false) authority) updated) query) =
    if String.eqb authority query
    then @Delegation.delegation_code (T.Authorization.t.address auth)
    else A.t.code (readAccount (S.t.world_state s) query)).
  all: cbv beta; rewrite code_after_account_write by exact Hwf;
    destruct (String.eqb authority query); reflexivity.
Qed.

EIP-7702 alone cannot move an EOA into the reserve check's smart-contract exemption. This includes repeated, ignored, and undelegating entries.
Theorem authorization_preserves_noncontract s auth query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  OCamlString.length (T.Authorization.t.address auth) = 20%Z →
  @A.is_smart_contract (readAccount (S.t.world_state s) query) = false →
  @A.is_smart_contract
    (readAccount (S.t.world_state (snd (@E.process_authorization F s auth))) query) = false.
Proof.
  intros Hwf Hwidth Hbefore.
  pose proof (authorization_code_effect s auth query Hwf) as Hcode. cbv beta zeta in Hcode.
  change (andb
    (Byte_string.Bytes.op_ltgt (A.t.code (readAccount
      (S.t.world_state (snd (@E.process_authorization F s auth))) query)) Byte_string.Bytes.empty)
    (negb (@Delegation.is_valid_delegation (A.t.code (readAccount
      (S.t.world_state (snd (@E.process_authorization F s auth))) query)))) = false).
  cbv beta.
  rewrite Hcode.
  destruct (match fst (@E.process_authorization F s auth) with
    Some addr ⇒ String.eqb addr query | None ⇒ false end); [|exact Hbefore].
  change (@A.is_smart_contract (A.t.with_code
    (@Delegation.delegation_code (T.Authorization.t.address auth))
    (readAccount (S.t.world_state s) query)) = false).
  apply delegation_code_not_contract. exact Hwidth.
Qed.

Lemma authorizations_retain_authorities auths authorities s query :
  List.In query authorities →
  List.In query (fst (Result.List.fold_left
    (fun '(applied, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (applied, next)
      | (Some authority, next) ⇒ (authority :: applied, next)
      end) (authorities, s) auths)).
Proof.
  revert authorities s. induction auths as [|auth auths IH]; intros authorities s Hin.
  - exact Hin.
  - change (@Result.List.fold_left) with (@OCamlList.fold_left).
    cbn [OCamlList.fold_left].
    destruct (@E.process_authorization F s auth) as [[authority|] next].
    + apply IH. right. exact Hin.
    + apply IH. exact Hin.
Qed.

Accounts absent from the applied-authority list retain their code, even if invalid authorizations named them. The sender's fee/nonce write is separate and also does not install code.
Theorem authorizations_preserve_other_code auths authorities s query :
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  let result := Result.List.fold_left
    (fun '(applied, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (applied, next)
      | (Some authority, next) ⇒ (authority :: applied, next)
      end) (authorities, s) auths in
  ¬ List.In query (fst result) →
  A.t.code (readAccount (S.t.world_state (snd result)) query) =
    A.t.code (readAccount (S.t.world_state s) query).
Proof.
  revert authorities s. induction auths as [|auth auths IH]; intros authorities s Hwf.
  - cbn zeta. intros _. reflexivity.
  - change (@Result.List.fold_left) with (@OCamlList.fold_left) in ×.
    cbn [OCamlList.fold_left].
    pose proof (@authorization_preserves_wellformed F s auth Hwf) as HnextWf.
    pose proof (authorization_code_effect s auth query Hwf) as Hcode.
    destruct (@E.process_authorization F s auth) as [[authority|] next] eqn:Hstep;
      cbn beta iota zeta delta [fst snd] in Hcode, HnextWf.
    + intro Habsent.
      transitivity (A.t.code (readAccount (S.t.world_state next) query)).
      { exact (IH (authority :: authorities) next HnextWf Habsent). }
      cbv beta. rewrite Hcode.
      destruct (String.eqb authority query) eqn:Heq; [|reflexivity].
      apply String.eqb_eq in Heq. subst query. exfalso. apply Habsent.
      apply authorizations_retain_authorities. left. reflexivity.
    + intro Habsent.
      transitivity (A.t.code (readAccount (S.t.world_state next) query)).
      { exact (IH authorities next HnextWf Habsent). }
      exact Hcode.
Qed.

Theorem authorizations_preserve_noncontract auths authorities s query :
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20%Z) auths →
  stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
  @A.is_smart_contract (readAccount (S.t.world_state s) query) = false →
  let result := Result.List.fold_left
    (fun '(applied, state) auth ⇒
      match @E.process_authorization F state auth with
      | (None, next) ⇒ (applied, next)
      | (Some authority, next) ⇒ (authority :: applied, next)
      end) (authorities, s) auths in
  @A.is_smart_contract (readAccount (S.t.world_state (snd result)) query) = false.
Proof.
  intro Hwidth. revert authorities s.
  induction Hwidth as [|auth auths Hwidth Hrest IH]; intros authorities s Hwf Hbefore.
  - exact Hbefore.
  - change (@Result.List.fold_left) with (@OCamlList.fold_left).
    cbn [OCamlList.fold_left].
    pose proof (@authorization_preserves_wellformed F s auth Hwf) as HnextWf.
    pose proof (authorization_preserves_noncontract s auth query Hwf Hwidth Hbefore) as Hnext.
    destruct (@E.process_authorization F s auth) as [[authority|] next];
      cbn [snd] in HnextWf, Hnext; apply IH; assumption.
Qed.

End Authorization.

Section TransactionPrefix.
Context {F : E.FArgs}
  {UI0 : Basics.Unreachable Support.UintN.Impl.t}
  {UB : Basics.Unreachable Bloom.t}
  {U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation U64 := (Opaque.legacy_witness 0%N).
Local Abbreviation UF := (Opaque.legacy_witness 0%Z).
Local Abbreviation UI := (Opaque.legacy_witness 0%Z).
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation prefix := (@transactionPrefix F).

The upfront fee and sender-nonce update cannot install or clear code, including when the account becomes empty and its map entry is deleted.
Lemma fee_nonce_write_preserves_code world sender fee query :
  stringmaps.WellFormed (W.t.accounts world) →
  A.t.code (readAccount (State.op_pointcaretdollarlparenrparenltminus world
    (@W.account None sender)
    (fun account ⇒ A.t.with_balance (@Numeric.U256.op_minus (A.t.balance account) fee)
      (A.t.with_nonce (@Numeric.U64.op_plus (A.t.nonce account)
        (@Numeric.U64.one)) account))) query) = A.t.code (readAccount world query).
Proof.
  intro Hwf.
  change (A.t.code (readAccount (State.op_pointcaretlparenrparenltminus world
    (@W.account (Some false) sender)
    (A.t.with_balance (@Numeric.U256.op_minus (A.t.balance (readAccount world sender)) fee)
      (A.t.with_nonce (@Numeric.U64.op_plus (A.t.nonce (readAccount world sender))
        (@Numeric.U64.one)) (readAccount world sender)))) query) =
    A.t.code (readAccount world query)).
  cbv beta. rewrite code_after_account_write by exact Hwf.
  destruct (String.eqb sender query) eqn:Heq; [|reflexivity].
  apply String.eqb_eq in Heq. subst query. reflexivity.
Qed.

These are the actual pre-VM operations of process_transaction. The first result rules out obtaining a contract exemption through EIP-7702; the second identifies accounts whose delegation status is unchanged.
Theorem transaction_prefix_preserves_noncontract block tx validation query :
  stringmaps.WellFormed (W.t.accounts (State.BlockState.t.world_state block)) →
  Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20%Z)
    (T.authorization_list tx) →
  @A.is_smart_contract (readAccount (State.BlockState.t.world_state block) query) = false →
  @A.is_smart_contract
    (readAccount (S.t.world_state (snd (prefix block tx validation))) query) = false.
Proof.
  intros Hwf Hwidth Hbefore. unfold transactionPrefix.
  apply authorizations_preserve_noncontract; [exact Hwidth| |].
  - apply account_write_wellformed. exact Hwf.
  - pose proof (fee_nonce_write_preserves_code (State.BlockState.t.world_state block)
      (E.transaction_validation.sender validation)
      (E.transaction_validation.total_fee validation) query Hwf) as Hcode.
    cbv beta in Hcode.
    match type of Hcode with ?code = _ ⇒
      change (andb (Byte_string.Bytes.op_ltgt code Byte_string.Bytes.empty)
        (negb (@Delegation.is_valid_delegation code)) = false)
    end.
    rewrite Hcode. exact Hbefore.
Qed.

Theorem transaction_prefix_other_code block tx validation query :
  stringmaps.WellFormed (W.t.accounts (State.BlockState.t.world_state block)) →
  ¬ List.In query (fst (prefix block tx validation)) →
  A.t.code (readAccount (S.t.world_state (snd (prefix block tx validation))) query) =
    A.t.code (readAccount (State.BlockState.t.world_state block) query).
Proof.
  intro Hwf. unfold transactionPrefix.
  intro Habsent.
  eapply eq_trans; [apply authorizations_preserve_other_code; [|exact Habsent]|].
  - apply account_write_wellformed. exact Hwf.
  - apply fee_nonce_write_preserves_code. exact Hwf.
Qed.

An authorization that actually applies to the sender disables emptying, even if it clears delegation. If none applies, eligibility is exactly the pre-transaction delegation/counter test. Merely carrying an invalid signed authorization cannot change this decision. This characterizes the VM-entry check; preservation during the VM remains a separate proof obligation.
Theorem transaction_prefix_emptying block tx validation :
  stringmaps.WellFormed (W.t.accounts (State.BlockState.t.world_state block)) →
  stringmaps.WellFormed
    (W.t.next_emptying_transaction_block (State.BlockState.t.world_state block)) →
  let sender := E.transaction_validation.sender validation in
  let prepared := prefix block tx validation in
  fst (@Reserve_balance.is_emptying_tx
    (@E.bump_emptying_transaction_counters F (fst prepared) (snd prepared))) =
  andb (negb (@Delegation.is_valid_delegation
    (A.t.code (readAccount (State.BlockState.t.world_state block) sender))))
    (andb (N.leb (State.op_pointcaretlparenrparen (State.BlockState.t.world_state block)
      (@W.next_emptying_transaction_block_for sender))
      (Chain.Ethereum.Block.Header.t.number
        (Chain.Ethereum.Block.t.header (State.BlockState.t.current_block block))))
      (negb (existsb (fun addr ⇒ String.eqb addr sender) (fst prepared)))).
Proof.
  intros Haccounts Hcounters. cbn zeta.
  pose proof (@transaction_prefix_context F block tx validation) as Hcontext.
  pose proof (transaction_prefix_other_code block tx validation
    (E.transaction_validation.sender validation) Haccounts) as Hcode.
  destruct (prefix block tx validation) as [authorities prepared] eqn:Hprefix.
  cbn [fst snd] in Hcontext, Hcode |- ×.
  unfold hostframe.transactionContext in Hcontext.
  injection Hcontext as Hinitial Hblock Horigin Hgas Hprice Hhistory HcounterMap.
  assert (HpreparedWf : stringmaps.WellFormed
    (W.t.next_emptying_transaction_block (S.t.world_state prepared))).
  { rewrite HcounterMap. exact Hcounters. }
  destruct (existsb (fun addr ⇒ String.eqb addr (E.transaction_validation.sender validation))
    authorities) eqn:Hmember.
  - rewrite Bool.andb_false_r, Bool.andb_false_r.
    apply emptyinghistory.bump_blocks_emptying; [exact HpreparedWf|].
    rewrite Horigin. apply existsb_exists in Hmember.
    destruct Hmember as [addr [Hin Heq]]. apply String.eqb_eq in Heq. subst addr. exact Hin.
  - assert (Habsent : ¬ List.In (E.transaction_validation.sender validation) authorities).
    { intro Hin. assert (Hyes : existsb
        (fun addr ⇒ String.eqb addr (E.transaction_validation.sender validation)) authorities = true).
      { apply existsb_exists. ∃ (E.transaction_validation.sender validation).
        split; [exact Hin|apply String.eqb_refl]. }
      congruence. }
    specialize (Hcode Habsent).
    destruct (@emptyinghistory.bump_counters_exact F prepared authorities HpreparedWf)
      as [_ [HafterAccounts [_ HafterCounters]]].
    specialize (HafterCounters (E.transaction_validation.sender validation)).
    rewrite Hmember in HafterCounters.
    assert (HafterCode : A.t.code (readAccount (S.t.world_state
      (@E.bump_emptying_transaction_counters F authorities prepared))
        (E.transaction_validation.sender validation)) =
      A.t.code (readAccount (State.BlockState.t.world_state block)
        (E.transaction_validation.sender validation))).
    { match goal with |- _ = ?rhs ⇒
      change (A.t.code (match Chain.Ethereum.Address.Map.find_opt
        (E.transaction_validation.sender validation)
        (W.t.accounts (S.t.world_state (@E.bump_emptying_transaction_counters F authorities prepared))) with
      | Some account ⇒ account | None ⇒ @A.empty end) = rhs) end.
      rewrite HafterAccounts. exact Hcode. }
    match goal with |- _ = ?rhs ⇒
    change (andb (negb (@Delegation.is_valid_delegation
      (A.t.code (readAccount (S.t.world_state (@E.bump_emptying_transaction_counters F authorities prepared))
        (S.t.tx_origin prepared)))))
      (N.leb (State.op_pointcaretlparenrparen
        (S.t.world_state (@E.bump_emptying_transaction_counters F authorities prepared))
        (@W.next_emptying_transaction_block_for
          (S.t.tx_origin prepared)))
        (Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block prepared)))) = rhs) end.
    rewrite Horigin, Hblock. cbv beta in HafterCode |- ×.
    rewrite HafterCode, HafterCounters. rewrite Bool.andb_true_r.
    rewrite (read_counter_same_map _ _ _ HcounterMap). reflexivity.
Qed.
End TransactionPrefix.

Print Assumptions authorization_code_effect.
Print Assumptions authorization_preserves_noncontract.
Print Assumptions authorizations_preserve_other_code.
Print Assumptions authorizations_preserve_noncontract.
Print Assumptions transaction_prefix_preserves_noncontract.
Print Assumptions transaction_prefix_other_code.
Print Assumptions transaction_prefix_emptying.