Library QedkConcrete.authorizationcodecore
Code changes caused by concrete authorization processing
From Stdlib Require Import List String ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.authorization QedkConcrete.worldaccounts
.
Require Export QedkConcrete.accountcode.
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 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 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)).
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.authorization QedkConcrete.worldaccounts
.
Require Export QedkConcrete.accountcode.
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 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 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.
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.
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.
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.