Library SetcodeConcrete.systempersistent
Existing system accounts cannot be replaced or destroyed
From Stdlib Require Import List String ZArith NArith.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.hostcode
QedkConcrete.hostframe QedkConcrete.vmhostframe QedkConcrete.recursiveassembly.
Require Import 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 Open Scope Z_scope.
Lemma persistent_unchanged query code before after :
persistentCode query code 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 →
S.t.self_destruct after = S.t.self_destruct before →
persistentCode query code after.
Proof.
intros Hbefore Hw Hc Hd. destruct Hbefore. constructor;
first [rewrite Hw; assumption|rewrite Hc; assumption|rewrite Hd; assumption].
Qed.
Section Host.
Context {F : H.FArgs}.
Lemma storage_persistent query code addr key value :
preservesCallback (persistentCode query code) (H.set_storage addr key value).
Proof.
intros s Hbefore. apply persistent_code_write with (s := s) (keep := true) (addr := addr);
[exact Hbefore|]. intro Heq; subst addr. exact (persistent_code_value _ _ _ Hbefore).
Qed.
Lemma selfdestruct_persistent query code addr beneficiary :
preservesCallback (persistentCode query code) (H.selfdestruct addr beneficiary).
Proof.
intros s Hbefore.
destruct (selfdestruct_preserves_creation (F := F) addr beneficiary s
(persistent_code_map _ _ _ Hbefore)) as [Hmap Hframe].
eapply persistent_code_frame; [exact Hbefore|exact Hmap|exact Hframe|].
unfold H.selfdestruct. cbn -[H.transfer_ether Addr._Set W.account].
match goal with |- context [H.transfer_ether ?from ?to ?value ?initial] ⇒
pose proof (transfer_preserves_creation_tracking from to value initial) as Htracking;
destruct (H.transfer_ether from to value initial) as [ok middle]
end.
cbn beta iota zeta delta [fst snd] in Htracking.
injection Htracking as Hcreated Hdestroyed.
cbn -[Addr._Set W.account].
destruct (Addr._Set.(OCamlSet.S.mem) addr
(S.t.accounts_created_in_current_transaction middle));
change (¬ List.In query (S.t.accounts_created_in_current_transaction middle));
rewrite Hcreated; exact (persistent_code_uncreated _ _ _ Hbefore).
Qed.
Theorem setcode_preserves_system_code query code current source static gas :
Setcode_rules.system_account query = true →
preservesCallback (persistentCode query code) (H.setcodefrom current source static gas).
Proof.
intros Hsystem 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: try (destruct Hbefore; constructor; assumption).
all: apply persistent_code_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current); [destruct Hbefore; constructor; assumption|].
all: intro Heq; subst current; congruence.
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_persistent vm query code :
PrimitiveHostFrame (HostF vm) (persistentCode query code).
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_persistent
| |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_persistent
end.
all: intros s Hbefore.
all: try (rewrite copy_code_read_only; exact Hbefore).
all: try (destruct Hbefore; constructor; assumption).
all: unfold H.access_account, H.access_storage; cbn -[W.account];
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
destruct Hbefore; constructor; assumption.
Qed.
End Assembly.
Lemma reserve_precompile_persistent {F : Reserve_balance_introspection.Make.FArgs} query code msg :
preservesCallback (persistentCode query code) (Reserve_balance_introspection.Make.precompile msg).
Proof.
intros s Hbefore.
change (persistentCode query code (snd (@Reserve_balance_introspection.Make.precompile F msg s))).
destruct (@nonceprecompile.precompile_world_effect F msg s)
as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. exact Hbefore.
- rewrite Hafter.
destruct (reserve_setting_preserves_code _ _ _ _ _ query
(persistent_code_map _ _ _ Hbefore) Hwrite) as [Hmap Hcode].
constructor; try exact (persistent_code_uncreated _ _ _ Hbefore);
try exact (persistent_code_undestroyed _ _ _ Hbefore); try exact Hmap.
etransitivity; [exact Hcode|exact (persistent_code_value _ _ _ Hbefore)].
Qed.
Lemma precompile_persistent {F : Precompiles.Make.FArgs}
cold warm access write reserve query code msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (persistentCode query code) action.
Proof.
intros Hrevision 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 Hinitial; exact Hinitial.
- 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 Hinitial. exact Hinitial.
+ inversion Hselected; subst action. apply reserve_precompile_persistent.
Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.persistentcode QedkConcrete.hostcode
QedkConcrete.hostframe QedkConcrete.vmhostframe QedkConcrete.recursiveassembly.
Require Import 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 Open Scope Z_scope.
Lemma persistent_unchanged query code before after :
persistentCode query code 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 →
S.t.self_destruct after = S.t.self_destruct before →
persistentCode query code after.
Proof.
intros Hbefore Hw Hc Hd. destruct Hbefore. constructor;
first [rewrite Hw; assumption|rewrite Hc; assumption|rewrite Hd; assumption].
Qed.
Section Host.
Context {F : H.FArgs}.
Lemma storage_persistent query code addr key value :
preservesCallback (persistentCode query code) (H.set_storage addr key value).
Proof.
intros s Hbefore. apply persistent_code_write with (s := s) (keep := true) (addr := addr);
[exact Hbefore|]. intro Heq; subst addr. exact (persistent_code_value _ _ _ Hbefore).
Qed.
Lemma selfdestruct_persistent query code addr beneficiary :
preservesCallback (persistentCode query code) (H.selfdestruct addr beneficiary).
Proof.
intros s Hbefore.
destruct (selfdestruct_preserves_creation (F := F) addr beneficiary s
(persistent_code_map _ _ _ Hbefore)) as [Hmap Hframe].
eapply persistent_code_frame; [exact Hbefore|exact Hmap|exact Hframe|].
unfold H.selfdestruct. cbn -[H.transfer_ether Addr._Set W.account].
match goal with |- context [H.transfer_ether ?from ?to ?value ?initial] ⇒
pose proof (transfer_preserves_creation_tracking from to value initial) as Htracking;
destruct (H.transfer_ether from to value initial) as [ok middle]
end.
cbn beta iota zeta delta [fst snd] in Htracking.
injection Htracking as Hcreated Hdestroyed.
cbn -[Addr._Set W.account].
destruct (Addr._Set.(OCamlSet.S.mem) addr
(S.t.accounts_created_in_current_transaction middle));
change (¬ List.In query (S.t.accounts_created_in_current_transaction middle));
rewrite Hcreated; exact (persistent_code_uncreated _ _ _ Hbefore).
Qed.
Theorem setcode_preserves_system_code query code current source static gas :
Setcode_rules.system_account query = true →
preservesCallback (persistentCode query code) (H.setcodefrom current source static gas).
Proof.
intros Hsystem 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: try (destruct Hbefore; constructor; assumption).
all: apply persistent_code_write with
(s := S.t.with_accessed_addresses
(Addr._Set.(OCamlSet.S.add) source (S.t.accessed_addresses s)) s)
(keep := false) (addr := current); [destruct Hbefore; constructor; assumption|].
all: intro Heq; subst current; congruence.
Qed.
End Host.
Section Assembly.
Context {F : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs F)).
Theorem concrete_primitives_persistent vm query code :
PrimitiveHostFrame (HostF vm) (persistentCode query code).
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_persistent
| |- preservesCallback _ (H.selfdestruct _ _) ⇒ apply selfdestruct_persistent
end.
all: intros s Hbefore.
all: try (rewrite copy_code_read_only; exact Hbefore).
all: try (destruct Hbefore; constructor; assumption).
all: unfold H.access_account, H.access_storage; cbn -[W.account];
repeat match goal with |- context [if ?test then _ else _] ⇒ destruct test end;
destruct Hbefore; constructor; assumption.
Qed.
End Assembly.
Lemma reserve_precompile_persistent {F : Reserve_balance_introspection.Make.FArgs} query code msg :
preservesCallback (persistentCode query code) (Reserve_balance_introspection.Make.precompile msg).
Proof.
intros s Hbefore.
change (persistentCode query code (snd (@Reserve_balance_introspection.Make.precompile F msg s))).
destruct (@nonceprecompile.precompile_world_effect F msg s)
as [Hsame|[value [world [Hwrite Hafter]]]].
- rewrite Hsame. exact Hbefore.
- rewrite Hafter.
destruct (reserve_setting_preserves_code _ _ _ _ _ query
(persistent_code_map _ _ _ Hbefore) Hwrite) as [Hmap Hcode].
constructor; try exact (persistent_code_uncreated _ _ _ Hbefore);
try exact (persistent_code_undestroyed _ _ _ Hbefore); try exact Hmap.
etransitivity; [exact Hcode|exact (persistent_code_value _ _ _ Hbefore)].
Qed.
Lemma precompile_persistent {F : Precompiles.Make.FArgs}
cold warm access write reserve query code msg addr action :
Precompiles.Make.Revision.(Precompiles.Make_Revision_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
Precompiles.Make.try_precompile msg addr = Some action →
preservesCallback (persistentCode query code) action.
Proof.
intros Hrevision 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 Hinitial; exact Hinitial.
- 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 Hinitial. exact Hinitial.
+ inversion Hselected; subst action. apply reserve_precompile_persistent.
Qed.