Library SetcodeConcrete.accountframe
Account code and nonce until creation or migration
From Stdlib Require Import List String ZArith Bool.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.hostnonce QedkConcrete.hostcode
QedkConcrete.accountcode QedkConcrete.accountnonces
QedkConcrete.worldaccounts QedkConcrete.recursiveassembly QedkConcrete.vmhostframe
SetcodeConcrete.codeframe SetcodeConcrete.nonceprecompile.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module H := Host.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Local Abbreviation account := (fun s a ⇒
State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None a)).
Definition accountFrame (query : Addr.t) (P : string → Numeric.U64.t → Prop) s :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
(List.In query (S.t.accounts_created_in_current_transaction s) ∨
P (A.t.code (account s query)) (A.t.nonce (account s query))).
Lemma account_frame_write query P s keep addr value :
accountFrame query P s →
(addr = query → List.In query (S.t.accounts_created_in_current_transaction s) ∨
P (A.t.code value) (A.t.nonce value)) →
accountFrame query P (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [Hmap Haccount] Hvalue. split.
- apply account_write_wellformed. exact Hmap.
- cbn beta iota zeta delta [S.t.with_world_state S.t.world_state
S.t.accounts_created_in_current_transaction].
rewrite code_after_account_write, nonce_after_account_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq; [apply Hvalue, String.eqb_eq, Heq|exact Haccount].
Qed.
Lemma account_frame_mark_creation query P s addr :
accountFrame query P s →
accountFrame query P (S.t.with_accounts_created_in_current_transaction
(Addr._Set.(OCamlSet.S.add) addr (S.t.accounts_created_in_current_transaction s)) s).
Proof.
intros [Hmap [Hin|Haccount]]; split; try exact Hmap.
- left. apply address_set_add_members. right. exact Hin.
- right. exact Haccount.
Qed.
Lemma account_frame_same query P before after :
accountFrame query P before →
S.t.world_state after = S.t.world_state before →
S.t.accounts_created_in_current_transaction after =
S.t.accounts_created_in_current_transaction before →
accountFrame query P after.
Proof. unfold accountFrame. intros H Hw Hc. rewrite Hw, Hc. exact H. Qed.
Lemma account_frame_callback {R : Set} query P (action : S.t → R × S.t) :
preservesCreation action →
(∀ nonce, preservesCallback (nonceInvariant query nonce) action) →
preservesCallback (accountFrame query P) action.
Proof.
intros Hcreation Hnonce s [Hmap Haccount].
destruct (Hcreation s Hmap) as [Hafter Hframe]. split; [exact Hafter|].
destruct Haccount as [Hin|Haccount].
- left. exact (creations_retained _ _ Hframe query Hin).
- destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction (snd (action s)))) as [Hin|Hnot].
+ left. exact Hin.
+ right.
pose proof (noncreated_code _ _ Hframe query Hnot) as Hcode.
pose proof (proj2 (Hnonce _ s (conj Hmap eq_refl))) as Hnumber.
change (A.t.code (account (snd (action s)) query) = A.t.code (account s query)) in Hcode.
change (A.t.nonce (account (snd (action s)) query) = A.t.nonce (account s query)) in Hnumber.
exact (eq_rect _ (fun Q : Prop ⇒ Q) Haccount _
(eq_sym (f_equal2 P Hcode Hnumber))).
Qed.
Section Host.
Context {F : H.FArgs}.
Lemma transfer_account_frame query P sender recipient value :
preservesCallback (accountFrame query P) (H.transfer_ether sender recipient value).
Proof.
apply account_frame_callback; [apply transfer_preserves_creation|].
intro nonce. apply transfer_preserves_nonce.
Qed.
Lemma storage_account_frame query P addr key value :
preservesCallback (accountFrame query P) (H.set_storage addr key value).
Proof.
apply account_frame_callback; [apply storage_preserves_creation|].
intro nonce. apply storage_preserves_nonce.
Qed.
Lemma selfdestruct_account_frame query P addr beneficiary :
preservesCallback (accountFrame query P) (H.selfdestruct addr beneficiary).
Proof.
apply account_frame_callback; [apply selfdestruct_preserves_creation|].
intro nonce. apply selfdestruct_preserves_nonce.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.hostnonce QedkConcrete.hostcode
QedkConcrete.accountcode QedkConcrete.accountnonces
QedkConcrete.worldaccounts QedkConcrete.recursiveassembly QedkConcrete.vmhostframe
SetcodeConcrete.codeframe SetcodeConcrete.nonceprecompile.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module H := Host.Make.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Local Abbreviation account := (fun s a ⇒
State.op_pointcaretlparenrparen (S.t.world_state s) (W.account None a)).
Definition accountFrame (query : Addr.t) (P : string → Numeric.U64.t → Prop) s :=
stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) ∧
(List.In query (S.t.accounts_created_in_current_transaction s) ∨
P (A.t.code (account s query)) (A.t.nonce (account s query))).
Lemma account_frame_write query P s keep addr value :
accountFrame query P s →
(addr = query → List.In query (S.t.accounts_created_in_current_transaction s) ∨
P (A.t.code value) (A.t.nonce value)) →
accountFrame query P (S.t.with_world_state
(State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [Hmap Haccount] Hvalue. split.
- apply account_write_wellformed. exact Hmap.
- cbn beta iota zeta delta [S.t.with_world_state S.t.world_state
S.t.accounts_created_in_current_transaction].
rewrite code_after_account_write, nonce_after_account_write by exact Hmap.
destruct (String.eqb addr query) eqn:Heq; [apply Hvalue, String.eqb_eq, Heq|exact Haccount].
Qed.
Lemma account_frame_mark_creation query P s addr :
accountFrame query P s →
accountFrame query P (S.t.with_accounts_created_in_current_transaction
(Addr._Set.(OCamlSet.S.add) addr (S.t.accounts_created_in_current_transaction s)) s).
Proof.
intros [Hmap [Hin|Haccount]]; split; try exact Hmap.
- left. apply address_set_add_members. right. exact Hin.
- right. exact Haccount.
Qed.
Lemma account_frame_same query P before after :
accountFrame query P before →
S.t.world_state after = S.t.world_state before →
S.t.accounts_created_in_current_transaction after =
S.t.accounts_created_in_current_transaction before →
accountFrame query P after.
Proof. unfold accountFrame. intros H Hw Hc. rewrite Hw, Hc. exact H. Qed.
Lemma account_frame_callback {R : Set} query P (action : S.t → R × S.t) :
preservesCreation action →
(∀ nonce, preservesCallback (nonceInvariant query nonce) action) →
preservesCallback (accountFrame query P) action.
Proof.
intros Hcreation Hnonce s [Hmap Haccount].
destruct (Hcreation s Hmap) as [Hafter Hframe]. split; [exact Hafter|].
destruct Haccount as [Hin|Haccount].
- left. exact (creations_retained _ _ Hframe query Hin).
- destruct (List.in_dec String.string_dec query
(S.t.accounts_created_in_current_transaction (snd (action s)))) as [Hin|Hnot].
+ left. exact Hin.
+ right.
pose proof (noncreated_code _ _ Hframe query Hnot) as Hcode.
pose proof (proj2 (Hnonce _ s (conj Hmap eq_refl))) as Hnumber.
change (A.t.code (account (snd (action s)) query) = A.t.code (account s query)) in Hcode.
change (A.t.nonce (account (snd (action s)) query) = A.t.nonce (account s query)) in Hnumber.
exact (eq_rect _ (fun Q : Prop ⇒ Q) Haccount _
(eq_sym (f_equal2 P Hcode Hnumber))).
Qed.
Section Host.
Context {F : H.FArgs}.
Lemma transfer_account_frame query P sender recipient value :
preservesCallback (accountFrame query P) (H.transfer_ether sender recipient value).
Proof.
apply account_frame_callback; [apply transfer_preserves_creation|].
intro nonce. apply transfer_preserves_nonce.
Qed.
Lemma storage_account_frame query P addr key value :
preservesCallback (accountFrame query P) (H.set_storage addr key value).
Proof.
apply account_frame_callback; [apply storage_preserves_creation|].
intro nonce. apply storage_preserves_nonce.
Qed.
Lemma selfdestruct_account_frame query P addr beneficiary :
preservesCallback (accountFrame query P) (H.selfdestruct addr beneficiary).
Proof.
apply account_frame_callback; [apply selfdestruct_preserves_creation|].
intro nonce. apply selfdestruct_preserves_nonce.
Qed.
Adoption writes only regular code and keeps the nonce. A property that
admits regular code therefore survives even when this address migrates.
Lemma setcode_account_frame query P current source static gas :
(∀ code nonce, Setcode_rules.regular_source code = true → P code nonce) →
preservesCallback (accountFrame query P) (H.setcodefrom current source static gas).
Proof.
intros Hregular s Hbefore. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hbefore.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] access] write] reserve] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact Hbefore.
all: apply account_frame_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current); [exact Hbefore|].
all: intro Heq; right; apply Hregular.
all: match goal with
Hvalid : orb (negb (Setcode_rules.regular_source ?code)) _ = false |- _ ⇒
apply Bool.orb_false_iff in Hvalid as [Hvalid _];
apply Bool.negb_false_iff in Hvalid; exact Hvalid
end.
Qed.
Lemma increment_account_frame query P addr :
(∀ code nonce, P code nonce → P code (Numeric.U64.op_plus nonce Numeric.U64.one)) →
preservesCallback (accountFrame query P) (H.try_increment_nonce addr).
Proof.
intros Hnext s Hbefore. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
apply account_frame_write with (s := s) (keep := false) (addr := addr); [exact Hbefore|].
intro Heq; subst addr. destruct (proj2 Hbefore) as [Hin|HP];
[left; exact Hin|right; apply Hnext; exact HP].
Qed.
Lemma increment_other_account_frame query P addr :
addr ≠ query → preservesCallback (accountFrame query P) (H.try_increment_nonce addr).
Proof.
intros Hneq s Hbefore. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
apply account_frame_write with (s := s) (keep := false) (addr := addr);
[exact Hbefore|contradiction].
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_account_frame vm query P :
PrimitiveHostFrame (HostF vm) (accountFrame query P).
Proof.
constructor; intros.
all: cbn -[H.account_exists H.get_balance H.access_account H.get_storage
H.set_storage H.access_storage H.get_transient_storage H.set_transient_storage
H.get_code_size H.get_code_hash H.copy_code H.get_block_hash H.emit_log H.selfdestruct].
all: try lazymatch goal with
| |- preservesCallback _ (H.set_storage _ _ _) ⇒ apply storage_account_frame
| |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_account_frame
end.
all: intros s Hinitial.
all: try (rewrite copy_code_read_only; exact Hinitial).
all: try exact Hinitial.
all: unfold H.access_account, H.access_storage; cbn -[W.account];
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
exact Hinitial.
Qed.
End Assembly.
Lemma reserve_precompile_account_frame {F : Reserve_balance_introspection.Make.FArgs}
query P msg :
query ≠ Reserve_balance.precompile_address →
preservesCallback (accountFrame query P) (Reserve_balance_introspection.Make.precompile msg).
Proof.
intros Hdifferent s [Hmap HP].
change (accountFrame query P (snd (@Reserve_balance_introspection.Make.precompile F msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; assumption.
- rewrite Hafter.
destruct (reserve_setting_preserves_code _ _ _ _ _ query Hmap Hwrite) as [Hmap' Hcode].
destruct (setter_other_nonce _ _ _ _ _ query Hdifferent Hmap Hwrite) as [_ Hnonce].
split; [exact Hmap'|]. destruct HP as [Hin|HP]; [left; exact Hin|right].
exact (eq_rect _ (fun Q : Prop ⇒ Q) HP _ (eq_sym (f_equal2 P Hcode Hnonce))).
Qed.
Lemma precompile_account_frame {F : Precompiles.Make.FArgs}
cold warm access write reserve query P msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
query ≠ Reserve_balance.precompile_address →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (accountFrame query P) action.
Proof.
intros Hrevision Hdifferent Hselected. unfold Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- destruct (Evmc.Message.t.delegated msg) in Hselected;
inversion Hselected; subst action; intros s HP; exact HP.
- unfold Precompiles.Make.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply nonceprecompile.find_singleton_value in Hnative. subst native.
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
+ inversion Hselected; subst action. intros s HP; exact HP.
+ inversion Hselected; subst action. apply reserve_precompile_account_frame. exact Hdifferent.
Qed.
Print Assumptions setcode_account_frame.
Print Assumptions concrete_primitives_account_frame.
(∀ code nonce, Setcode_rules.regular_source code = true → P code nonce) →
preservesCallback (accountFrame query P) (H.setcodefrom current source static gas).
Proof.
intros Hregular s Hbefore. unfold H.setcodefrom.
destruct H.ChainParams.(Chain.Monad.PARAMS.revision); try exact Hbefore.
match goal with prices : (_ × _ × _ × _ × _)%type |- _ ⇒
destruct prices as [[[[cold warm] access] write] reserve] end.
cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact Hbefore.
all: apply account_frame_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current); [exact Hbefore|].
all: intro Heq; right; apply Hregular.
all: match goal with
Hvalid : orb (negb (Setcode_rules.regular_source ?code)) _ = false |- _ ⇒
apply Bool.orb_false_iff in Hvalid as [Hvalid _];
apply Bool.negb_false_iff in Hvalid; exact Hvalid
end.
Qed.
Lemma increment_account_frame query P addr :
(∀ code nonce, P code nonce → P code (Numeric.U64.op_plus nonce Numeric.U64.one)) →
preservesCallback (accountFrame query P) (H.try_increment_nonce addr).
Proof.
intros Hnext s Hbefore. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
apply account_frame_write with (s := s) (keep := false) (addr := addr); [exact Hbefore|].
intro Heq; subst addr. destruct (proj2 Hbefore) as [Hin|HP];
[left; exact Hin|right; apply Hnext; exact HP].
Qed.
Lemma increment_other_account_frame query P addr :
addr ≠ query → preservesCallback (accountFrame query P) (H.try_increment_nonce addr).
Proof.
intros Hneq s Hbefore. unfold H.try_increment_nonce, H.M.
cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus].
match goal with |- context [if ?test then _ else _] ⇒ destruct test end; [exact Hbefore|].
apply account_frame_write with (s := s) (keep := false) (addr := addr);
[exact Hbefore|contradiction].
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_account_frame vm query P :
PrimitiveHostFrame (HostF vm) (accountFrame query P).
Proof.
constructor; intros.
all: cbn -[H.account_exists H.get_balance H.access_account H.get_storage
H.set_storage H.access_storage H.get_transient_storage H.set_transient_storage
H.get_code_size H.get_code_hash H.copy_code H.get_block_hash H.emit_log H.selfdestruct].
all: try lazymatch goal with
| |- preservesCallback _ (H.set_storage _ _ _) ⇒ apply storage_account_frame
| |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_account_frame
end.
all: intros s Hinitial.
all: try (rewrite copy_code_read_only; exact Hinitial).
all: try exact Hinitial.
all: unfold H.access_account, H.access_storage; cbn -[W.account];
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
exact Hinitial.
Qed.
End Assembly.
Lemma reserve_precompile_account_frame {F : Reserve_balance_introspection.Make.FArgs}
query P msg :
query ≠ Reserve_balance.precompile_address →
preservesCallback (accountFrame query P) (Reserve_balance_introspection.Make.precompile msg).
Proof.
intros Hdifferent s [Hmap HP].
change (accountFrame query P (snd (@Reserve_balance_introspection.Make.precompile F msg s))).
destruct (precompile_world_effect msg s) as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. split; assumption.
- rewrite Hafter.
destruct (reserve_setting_preserves_code _ _ _ _ _ query Hmap Hwrite) as [Hmap' Hcode].
destruct (setter_other_nonce _ _ _ _ _ query Hdifferent Hmap Hwrite) as [_ Hnonce].
split; [exact Hmap'|]. destruct HP as [Hin|HP]; [left; exact Hin|right].
exact (eq_rect _ (fun Q : Prop ⇒ Q) HP _ (eq_sym (f_equal2 P Hcode Hnonce))).
Qed.
Lemma precompile_account_frame {F : Precompiles.Make.FArgs}
cold warm access write reserve query P msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
query ≠ Reserve_balance.precompile_address →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (accountFrame query P) action.
Proof.
intros Hrevision Hdifferent Hselected. unfold Precompiles.Make.try_precompile in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [pure|] eqn:Hethereum in Hselected end.
- destruct (Evmc.Message.t.delegated msg) in Hselected;
inversion Hselected; subst action; intros s HP; exact HP.
- unfold Precompiles.Make.monad_precompiles in Hselected. rewrite Hrevision in Hselected.
match type of Hselected with context [@Addr.Map.find_opt ?T addr ?entries] ⇒
destruct (@Addr.Map.find_opt T addr entries) as [native|] eqn:Hnative in Hselected end.
2: discriminate.
apply nonceprecompile.find_singleton_value in Hnative. subst native.
match type of Hselected with context [if ?reject then _ else _] ⇒ destruct reject in Hselected end.
+ inversion Hselected; subst action. intros s HP; exact HP.
+ inversion Hselected; subst action. apply reserve_precompile_account_frame. exact Hdifferent.
Qed.
Print Assumptions setcode_account_frame.
Print Assumptions concrete_primitives_account_frame.