Library SetcodeConcrete.codeactivity
Empty code cannot reappear without recorded authorization activity
From Stdlib Require Import List String ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.authorization QedkConcrete.authorizationcodecore
QedkConcrete.accountcode QedkConcrete.worldaccounts QedkConcrete.hostframe
QedkConcrete.transactionframes QedkConcrete.validation.
Require Import SetcodeConcrete.transactionentry SetcodeConcrete.transactionaccountfinalize
SetcodeConcrete.transactioncode SetcodeConcrete.transactionregular
SetcodeConcrete.codeevolution SetcodeConcrete.executionaccountframe
SetcodeConcrete.accountframe SetcodeConcrete.executiondestruction
SetcodeConcrete.consensuschecks SetcodeConcrete.transactionactivity.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (W.account None addr)).
Section Entry.
Context {F : E.FArgs}.
Lemma applied_authority_recovered state auth authority after :
E.process_authorization state auth = (Some authority, after) →
T.Authorization.authority auth = Some authority.
Proof.
unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [discriminate|].
destruct (T.Authorization.authority auth) as [signer|] eqn:Hs; [|discriminate].
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test; cbn beta iota zeta end.
all: intro Heq; try discriminate; inversion Heq; reflexivity.
Qed.
Lemma authorization_code_without_recovery state auth query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state state)) →
T.Authorization.authority auth ≠ Some query →
A.t.code (readAccount (S.t.world_state (snd (E.process_authorization state auth))) query) =
A.t.code (readAccount (S.t.world_state state) query).
Proof.
intros Hmap Hnot. pose proof (authorization_code_effect state auth query Hmap) as Hcode.
destruct (E.process_authorization state auth) as [[authority|] after] eqn:Hrun;
cbn beta iota zeta delta [fst snd] in Hcode |- *; [|exact Hcode].
assert (Hdifferent : String.eqb authority query = false).
{ apply String.eqb_neq. intro Heq. subst query.
apply Hnot. exact (applied_authority_recovered state auth authority after Hrun). }
rewrite Hdifferent in Hcode. exact Hcode.
Qed.
Lemma authorizations_code_without_recovery auths applied state query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state state)) →
Forall (fun auth ⇒ T.Authorization.authority auth ≠ Some query) auths →
let result := Result.List.fold_left
(fun '(applied, current) auth ⇒
match E.process_authorization current auth with
| (None, next) ⇒ (applied, next)
| (Some authority, next) ⇒ (authority :: applied, next)
end) (applied, state) auths in
A.t.code (readAccount (S.t.world_state (snd result)) query) =
A.t.code (readAccount (S.t.world_state state) query).
Proof.
revert applied state. induction auths as [|auth rest IH]; intros applied state Hmap Hnot;
[reflexivity|].
inversion Hnot as [|? ? Hauth Hrest]; subst.
pose proof (authorization_preserves_wellformed state auth Hmap) as HafterMap.
pose proof (authorization_code_without_recovery state auth query Hmap Hauth) as Hcode.
change (@Result.List.fold_left) with (@OCamlList.fold_left). cbn [OCamlList.fold_left].
destruct (E.process_authorization state auth) as [[authority|] after];
cbn [snd] in HafterMap, Hcode.
all: rewrite IH by assumption; exact Hcode.
Qed.
Lemma absent_recovered_authority tx query :
¬ In query (recoveredAuthorities tx) →
Forall (fun auth ⇒ T.Authorization.authority auth ≠ Some query) (T.authorization_list tx).
Proof.
unfold recoveredAuthorities. induction (T.authorization_list tx) as [|auth rest IH];
intro Hnot; [constructor|].
cbn [Stdlib.Lists.List.fold_right] in Hnot.
constructor.
- intro Hrecover. rewrite Hrecover in Hnot. apply Hnot. left. reflexivity.
- apply IH. intro Hin. apply Hnot.
destruct (T.Authorization.authority auth); [right|]; exact Hin.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.authorization QedkConcrete.authorizationcodecore
QedkConcrete.accountcode QedkConcrete.worldaccounts QedkConcrete.hostframe
QedkConcrete.transactionframes QedkConcrete.validation.
Require Import SetcodeConcrete.transactionentry SetcodeConcrete.transactionaccountfinalize
SetcodeConcrete.transactioncode SetcodeConcrete.transactionregular
SetcodeConcrete.codeevolution SetcodeConcrete.executionaccountframe
SetcodeConcrete.accountframe SetcodeConcrete.executiondestruction
SetcodeConcrete.consensuschecks SetcodeConcrete.transactionactivity.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Import ListNotations.
Local Open Scope Z_scope.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module T := Chain.Ethereum.Transaction.
Module Addr := Chain.Ethereum.Address.
Local Abbreviation readAccount := (fun world addr ⇒
State.op_pointcaretlparenrparen world (W.account None addr)).
Section Entry.
Context {F : E.FArgs}.
Lemma applied_authority_recovered state auth authority after :
E.process_authorization state auth = (Some authority, after) →
T.Authorization.authority auth = Some authority.
Proof.
unfold E.process_authorization.
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [discriminate|].
destruct (T.Authorization.authority auth) as [signer|] eqn:Hs; [|discriminate].
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test; cbn beta iota zeta end.
all: intro Heq; try discriminate; inversion Heq; reflexivity.
Qed.
Lemma authorization_code_without_recovery state auth query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state state)) →
T.Authorization.authority auth ≠ Some query →
A.t.code (readAccount (S.t.world_state (snd (E.process_authorization state auth))) query) =
A.t.code (readAccount (S.t.world_state state) query).
Proof.
intros Hmap Hnot. pose proof (authorization_code_effect state auth query Hmap) as Hcode.
destruct (E.process_authorization state auth) as [[authority|] after] eqn:Hrun;
cbn beta iota zeta delta [fst snd] in Hcode |- *; [|exact Hcode].
assert (Hdifferent : String.eqb authority query = false).
{ apply String.eqb_neq. intro Heq. subst query.
apply Hnot. exact (applied_authority_recovered state auth authority after Hrun). }
rewrite Hdifferent in Hcode. exact Hcode.
Qed.
Lemma authorizations_code_without_recovery auths applied state query :
stringmaps.WellFormed (W.t.accounts (S.t.world_state state)) →
Forall (fun auth ⇒ T.Authorization.authority auth ≠ Some query) auths →
let result := Result.List.fold_left
(fun '(applied, current) auth ⇒
match E.process_authorization current auth with
| (None, next) ⇒ (applied, next)
| (Some authority, next) ⇒ (authority :: applied, next)
end) (applied, state) auths in
A.t.code (readAccount (S.t.world_state (snd result)) query) =
A.t.code (readAccount (S.t.world_state state) query).
Proof.
revert applied state. induction auths as [|auth rest IH]; intros applied state Hmap Hnot;
[reflexivity|].
inversion Hnot as [|? ? Hauth Hrest]; subst.
pose proof (authorization_preserves_wellformed state auth Hmap) as HafterMap.
pose proof (authorization_code_without_recovery state auth query Hmap Hauth) as Hcode.
change (@Result.List.fold_left) with (@OCamlList.fold_left). cbn [OCamlList.fold_left].
destruct (E.process_authorization state auth) as [[authority|] after];
cbn [snd] in HafterMap, Hcode.
all: rewrite IH by assumption; exact Hcode.
Qed.
Lemma absent_recovered_authority tx query :
¬ In query (recoveredAuthorities tx) →
Forall (fun auth ⇒ T.Authorization.authority auth ≠ Some query) (T.authorization_list tx).
Proof.
unfold recoveredAuthorities. induction (T.authorization_list tx) as [|auth rest IH];
intro Hnot; [constructor|].
cbn [Stdlib.Lists.List.fold_right] in Hnot.
constructor.
- intro Hrecover. rewrite Hrecover in Hnot. apply Hnot. left. reflexivity.
- apply IH. intro Hin. apply Hnot.
destruct (T.Authorization.authority auth); [right|]; exact Hin.
Qed.
Even the sender's upfront fee/nonce write does not change its code.
Only a recovered authorization can change it before the VM call.
Lemma transaction_entry_code_without_recovery block tx validation query :
stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
¬ In query (recoveredAuthorities tx) →
A.t.code (readAccount (S.t.world_state (snd (transactionVmInput block tx validation))) query) =
A.t.code (readAccount (B.t.world_state block) query).
Proof.
intros Hmap Hnot. unfold transactionVmInput, transactionPrefix.
match goal with |- context [Result.List.fold_left ?step (nil, ?start) ?auths] ⇒
set (charged := start) in × end.
assert (Hcharged : stringmaps.WellFormed (W.t.accounts (S.t.world_state charged)))
by (apply account_write_wellformed; exact Hmap).
pose proof (authorizations_code_without_recovery (T.authorization_list tx) nil charged query
Hcharged (absent_recovered_authority tx query Hnot)) as Hcode.
match goal with |- context [Result.List.fold_left ?step (nil, charged) ?auths] ⇒
destruct (Result.List.fold_left step (nil, charged) auths) as [applied prepared] end.
cbn beta iota zeta delta [fst snd] in Hcode |- ×.
unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[E.bump_emptying_transaction_counters Addr.Map.find_opt].
rewrite bump_counters_accounts.
change (A.t.code (readAccount (S.t.world_state prepared) query) =
A.t.code (readAccount (B.t.world_state block) query)).
etransitivity; [exact Hcode|].
change (A.t.code (readAccount
(State.op_pointcaretlparenrparenltminus (B.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 (B.t.world_state block)
(E.transaction_validation.sender validation))) (E.transaction_validation.total_fee validation))
(A.t.with_nonce (Setcode_rules.advance_sender_nonce
(migratedSender block (E.transaction_validation.sender validation))
(A.t.nonce (readAccount (B.t.world_state block) (E.transaction_validation.sender validation)))
(T.nonce tx))
(readAccount (B.t.world_state block) (E.transaction_validation.sender validation))))) query) =
A.t.code (readAccount (B.t.world_state block) query)).
cbv beta. rewrite code_after_account_write by exact Hmap.
destruct (String.eqb (E.transaction_validation.sender validation) query) eqn:Heq;
[apply String.eqb_eq in Heq; subst query|]; reflexivity.
Qed.
End Entry.
Section Execution.
Context {F : E.FArgs}.
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
¬ In query (recoveredAuthorities tx) →
A.t.code (readAccount (S.t.world_state (snd (transactionVmInput block tx validation))) query) =
A.t.code (readAccount (B.t.world_state block) query).
Proof.
intros Hmap Hnot. unfold transactionVmInput, transactionPrefix.
match goal with |- context [Result.List.fold_left ?step (nil, ?start) ?auths] ⇒
set (charged := start) in × end.
assert (Hcharged : stringmaps.WellFormed (W.t.accounts (S.t.world_state charged)))
by (apply account_write_wellformed; exact Hmap).
pose proof (authorizations_code_without_recovery (T.authorization_list tx) nil charged query
Hcharged (absent_recovered_authority tx query Hnot)) as Hcode.
match goal with |- context [Result.List.fold_left ?step (nil, charged) ?auths] ⇒
destruct (Result.List.fold_left step (nil, charged) auths) as [applied prepared] end.
cbn beta iota zeta delta [fst snd] in Hcode |- ×.
unfold State.op_pointcaretlparenrparen, W.account, W.account_opt.
cbn -[E.bump_emptying_transaction_counters Addr.Map.find_opt].
rewrite bump_counters_accounts.
change (A.t.code (readAccount (S.t.world_state prepared) query) =
A.t.code (readAccount (B.t.world_state block) query)).
etransitivity; [exact Hcode|].
change (A.t.code (readAccount
(State.op_pointcaretlparenrparenltminus (B.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 (B.t.world_state block)
(E.transaction_validation.sender validation))) (E.transaction_validation.total_fee validation))
(A.t.with_nonce (Setcode_rules.advance_sender_nonce
(migratedSender block (E.transaction_validation.sender validation))
(A.t.nonce (readAccount (B.t.world_state block) (E.transaction_validation.sender validation)))
(T.nonce tx))
(readAccount (B.t.world_state block) (E.transaction_validation.sender validation))))) query) =
A.t.code (readAccount (B.t.world_state block) query)).
cbv beta. rewrite code_after_account_write by exact Hmap.
destruct (String.eqb (E.transaction_validation.sender validation) query) eqn:Heq;
[apply String.eqb_eq in Heq; subst query|]; reflexivity.
Qed.
End Entry.
Section Execution.
Context {F : E.FArgs}.
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
block and post are the actual states before and after tx. If query
did not authorize it, an empty final code implies an empty initial code.
CREATE exclusion is required only when the account was initially an EOA;
ordinary contract code is preserved without that cryptographic premise.
Theorem process_transaction_empty_code_requires_empty_before block tx validation post query :
query ≠ Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
¬ In query (recoveredAuthorities tx) →
E.validate_transaction block tx = inl validation →
E.process_transaction block tx = inl post →
(A.is_smart_contract (readAccount (B.t.world_state block) query) = false →
¬ In query (S.t.accounts_created_in_current_transaction (transactionFinished block tx validation))) →
A.t.code (readAccount (B.t.world_state post) query) = Byte_string.Bytes.empty →
A.t.code (readAccount (B.t.world_state block) query) = Byte_string.Bytes.empty.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hnative Hmap Hnot Hv Hrun Hcreation Hempty.
destruct (A.is_smart_contract (readAccount (B.t.world_state block) query)) eqn:Hcontract.
- pose proof (process_transaction_preserves_regular cold warm access write reserve
Hrevision Hcold Hwarm block tx validation post query Hv Hrun
(conj Hmap (proj2 (regular_code_contract _) Hcontract))) as [_ Hregular].
change (regularCode (A.t.code (readAccount (B.t.world_state post) query))) in Hregular.
assert (Hfalse : regularCode Byte_string.Bytes.empty)
by (exact (eq_rect _ regularCode Hregular _ Hempty)).
discriminate Hfalse.
- specialize (Hcreation eq_refl).
pose proof (transaction_entry_code_without_recovery block tx validation query Hmap Hnot) as Hcode.
pose proof (transaction_entry_destruction block tx validation) as Hdestruct.
assert (HentryMap : stringmaps.WellFormed (W.t.accounts
(S.t.world_state (snd (transactionVmInput block tx validation))))).
{ unfold transactionVmInput, transactionPrefix.
match goal with |- context [Result.List.fold_left ?step (nil, ?start) ?auths] ⇒
assert (Hcharged : stringmaps.WellFormed (W.t.accounts (S.t.world_state start))) by
(apply account_write_wellformed; exact Hmap);
pose proof (proj1 (authorizations_preserve_balances auths nil start Hcharged)) as Hafter;
destruct (Result.List.fold_left step (nil, start) auths) as [applied prepared] end.
cbn beta iota zeta delta [snd] in Hafter |- ×.
cbn -[E.bump_emptying_transaction_counters Addr.Map.find_opt].
rewrite bump_counters_accounts. exact Hafter. }
assert (Hgas : 0 ≤ Evmc.Message.t.gas (fst (transactionVmInput block tx validation)) < 2^63).
{ unfold transactionVmInput. destruct (transactionPrefix block tx validation).
apply (validated_message_gas block tx validation _ Hv). }
set (P := fun code (_ : Numeric.U64.t) ⇒ regularCode code ∨
code = A.t.code (readAccount (B.t.world_state block) query)).
assert (Hpost : worldAccount query P (B.t.world_state post)).
{ apply (process_transaction_retains_finished_account block tx validation post query P Hv Hrun).
all: unfold transactionFinished in Hcreation |- ×.
all: destruct (transactionVmInput block tx validation) as [msg entry].
all: cbn beta iota zeta delta [fst snd] in Hgas, HentryMap, Hcode, Hdestruct.
all: destruct (migratedSender block (E.transaction_validation.sender validation)).
- split; [exact HentryMap|right; exact Hcode].
- pose proof (@execution_call_from_eoa_preserves_account_frame F query P Hnative
(fun code nonce Hsource ⇒ or_introl (regular_source_is_regular code Hsource))
(fun code nonce HP ⇒ HP) cold warm access write reserve Hcold Hwarm
tx msg Hrevision Hgas entry (conj HentryMap (or_intror (or_intror Hcode)))) as [Hwf [Hin|HP]].
+ contradiction.
+ split; assumption.
- rewrite Hdestruct. tauto.
- exact (@uncreated_sender_not_destroyed F cold warm access write reserve
Hrevision Hcold Hwarm tx msg entry query Hgas Hdestruct Hcreation). }
destruct Hpost as [_ [Hregular|Hsame]].
+ change (regularCode (A.t.code (readAccount (B.t.world_state post) query))) in Hregular.
assert (Hfalse : regularCode Byte_string.Bytes.empty)
by (exact (eq_rect _ regularCode Hregular _ Hempty)).
discriminate Hfalse.
+ exact (eq_trans (eq_sym Hsame) Hempty).
Qed.
End Execution.
Print Assumptions process_transaction_empty_code_requires_empty_before.
query ≠ Reserve_balance.precompile_address →
stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
¬ In query (recoveredAuthorities tx) →
E.validate_transaction block tx = inl validation →
E.process_transaction block tx = inl post →
(A.is_smart_contract (readAccount (B.t.world_state block) query) = false →
¬ In query (S.t.accounts_created_in_current_transaction (transactionFinished block tx validation))) →
A.t.code (readAccount (B.t.world_state post) query) = Byte_string.Bytes.empty →
A.t.code (readAccount (B.t.world_state block) query) = Byte_string.Bytes.empty.
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hnative Hmap Hnot Hv Hrun Hcreation Hempty.
destruct (A.is_smart_contract (readAccount (B.t.world_state block) query)) eqn:Hcontract.
- pose proof (process_transaction_preserves_regular cold warm access write reserve
Hrevision Hcold Hwarm block tx validation post query Hv Hrun
(conj Hmap (proj2 (regular_code_contract _) Hcontract))) as [_ Hregular].
change (regularCode (A.t.code (readAccount (B.t.world_state post) query))) in Hregular.
assert (Hfalse : regularCode Byte_string.Bytes.empty)
by (exact (eq_rect _ regularCode Hregular _ Hempty)).
discriminate Hfalse.
- specialize (Hcreation eq_refl).
pose proof (transaction_entry_code_without_recovery block tx validation query Hmap Hnot) as Hcode.
pose proof (transaction_entry_destruction block tx validation) as Hdestruct.
assert (HentryMap : stringmaps.WellFormed (W.t.accounts
(S.t.world_state (snd (transactionVmInput block tx validation))))).
{ unfold transactionVmInput, transactionPrefix.
match goal with |- context [Result.List.fold_left ?step (nil, ?start) ?auths] ⇒
assert (Hcharged : stringmaps.WellFormed (W.t.accounts (S.t.world_state start))) by
(apply account_write_wellformed; exact Hmap);
pose proof (proj1 (authorizations_preserve_balances auths nil start Hcharged)) as Hafter;
destruct (Result.List.fold_left step (nil, start) auths) as [applied prepared] end.
cbn beta iota zeta delta [snd] in Hafter |- ×.
cbn -[E.bump_emptying_transaction_counters Addr.Map.find_opt].
rewrite bump_counters_accounts. exact Hafter. }
assert (Hgas : 0 ≤ Evmc.Message.t.gas (fst (transactionVmInput block tx validation)) < 2^63).
{ unfold transactionVmInput. destruct (transactionPrefix block tx validation).
apply (validated_message_gas block tx validation _ Hv). }
set (P := fun code (_ : Numeric.U64.t) ⇒ regularCode code ∨
code = A.t.code (readAccount (B.t.world_state block) query)).
assert (Hpost : worldAccount query P (B.t.world_state post)).
{ apply (process_transaction_retains_finished_account block tx validation post query P Hv Hrun).
all: unfold transactionFinished in Hcreation |- ×.
all: destruct (transactionVmInput block tx validation) as [msg entry].
all: cbn beta iota zeta delta [fst snd] in Hgas, HentryMap, Hcode, Hdestruct.
all: destruct (migratedSender block (E.transaction_validation.sender validation)).
- split; [exact HentryMap|right; exact Hcode].
- pose proof (@execution_call_from_eoa_preserves_account_frame F query P Hnative
(fun code nonce Hsource ⇒ or_introl (regular_source_is_regular code Hsource))
(fun code nonce HP ⇒ HP) cold warm access write reserve Hcold Hwarm
tx msg Hrevision Hgas entry (conj HentryMap (or_intror (or_intror Hcode)))) as [Hwf [Hin|HP]].
+ contradiction.
+ split; assumption.
- rewrite Hdestruct. tauto.
- exact (@uncreated_sender_not_destroyed F cold warm access write reserve
Hrevision Hcold Hwarm tx msg entry query Hgas Hdestruct Hcreation). }
destruct Hpost as [_ [Hregular|Hsame]].
+ change (regularCode (A.t.code (readAccount (B.t.world_state post) query))) in Hregular.
assert (Hfalse : regularCode Byte_string.Bytes.empty)
by (exact (eq_rect _ regularCode Hregular _ Hempty)).
discriminate Hfalse.
+ exact (eq_trans (eq_sym Hsame) Hempty).
Qed.
End Execution.
Print Assumptions process_transaction_empty_code_requires_empty_before.