Library SetcodeConcrete.guardedaccountframe
Positive reserves prevent a delegated nonsender from adopting code
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.protectedstoragenative QedkConcrete.reserveencoding
QedkConcrete.hoststoragewords QedkConcrete.reservecheck QedkConcrete.hostframe
QedkConcrete.hostcode QedkConcrete.recursiveassembly QedkConcrete.vmhostframe.
Require Import SetcodeConcrete.accountframe SetcodeConcrete.emptyaccount
SetcodeConcrete.executionconfig SetcodeConcrete.executionconfigstorage
SetcodeConcrete.contextframe SetcodeConcrete.adoptionguard.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Definition initialThreshold storage block query :=
Numeric.U256.of_uint_exn (Reserve_config.active
{| Reserve_config.t.settled := readConfigField storage query 0;
Reserve_config.t.pending := readConfigField storage query 1;
Reserve_config.t.valid_from := readConfigField storage query 2 |} block).
Definition guardedAccount query delegated storage block sender s : Prop :=
configStorage block storage s ∧ S.t.tx_origin s = sender ∧
accountFrame query (fun code _ ⇒ code = delegated) s.
Lemma guarded_account_write query delegated storage block sender s keep addr value :
guardedAccount query delegated storage block sender s →
addr ≠ Reserve_balance.precompile_address →
(addr = query → In query (S.t.accounts_created_in_current_transaction s) ∨
A.t.code value = delegated) →
guardedAccount query delegated storage block sender
(S.t.with_world_state (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [Hconfig [Hsender Haccount]] Hnative Hvalue. split.
- apply protected_storage_write; [exact Hconfig|contradiction].
- split; [exact Hsender|]. apply account_frame_write; assumption.
Qed.
Lemma guarded_account_mark_creation query delegated storage block sender s addr :
guardedAccount query delegated storage block sender s →
addr ≠ Reserve_balance.precompile_address →
guardedAccount query delegated storage block sender
(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 [Hconfig [Hsender Haccount]] Hnative. split.
- apply protected_storage_mark_creation; assumption.
- split; [exact Hsender|]. apply account_frame_mark_creation; exact Haccount.
Qed.
Lemma guarded_account_same query delegated storage block sender before after :
W.t.accounts (S.t.world_state after) = W.t.accounts (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 →
storageBlock after = storageBlock before →
S.t.tx_origin after = S.t.tx_origin before →
guardedAccount query delegated storage block sender before →
guardedAccount query delegated storage block sender after.
Proof.
intros Haccounts Hcreated Hdes Hblock Horigin [Hconfig [Hsender [Hmap Haccount]]].
split; [eapply protected_storage_same_fields; eassumption|].
split; [rewrite Horigin; exact Hsender|].
unfold accountFrame, State.op_pointcaretlparenrparen, W.account, W.account_opt in ×.
cbn in ×. rewrite Haccounts, Hcreated. split; assumption.
Qed.
Lemma configured_threshold_at_current storage block query s :
stringmaps.WellFormed storage → storageWords storage →
configStorage block storage s →
Reserve_balance.user_reserve_balance (S.t.world_state s) (storageBlock s) query =
initialThreshold storage block query.
Proof.
intros Hmap Hwords Hstate.
destruct (protected_storage_contents _ _ _ Hstate) as [Hblock [updates Hwrites]].
pose proof (reserve_storage_writes_threshold_any_account _ _ _ _ query block
Hmap Hwords Hwrites ltac:(lia)) as Hthreshold.
unfold Reserve_balance.user_reserve_balance, Reserve_balance.stored_config, initialThreshold.
rewrite Hblock. f_equal. exact Hthreshold.
Qed.
Lemma migration_context_origin before after :
migrationContext after = migrationContext before →
S.t.tx_origin after = S.t.tx_origin before.
Proof.
unfold migrationContext, transactionContext. intro Heq.
injection Heq as Hsnapshot Hblock Horigin Hlimit Hprice Hhistory Hcounter Hprior Hchain.
exact Horigin.
Qed.
Section Host.
Context {F : H.FArgs}.
Context (query : Addr.t) (delegated : string) (storage : list (string × string))
(block : N) (sender : Addr.t).
Local Abbreviation protected := (guardedAccount query delegated storage block sender).
Lemma transfer_guarded_account from to value :
preservesCallback protected (H.transfer_ether from to value).
Proof.
intros s [Hconfig [Hsender Haccount]]. split.
- apply transfer_preserves_protected_storage. exact Hconfig.
- split.
+ rewrite (migration_context_origin _ _ (transfer_preserves_migration_context from to value s)).
exact Hsender.
+ apply transfer_account_frame. exact Haccount.
Qed.
Lemma setcode_guarded_account cold warm access write reserve current source static gas :
H.ChainParams.(Chain.Monad.PARAMS.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
stringmaps.WellFormed storage → storageWords storage →
initialThreshold storage block query ≠ 0 →
Delegation.is_valid_delegation delegated = true →
query ≠ sender → current ≠ Reserve_balance.precompile_address →
preservesCallback protected (H.setcodefrom current source static gas).
Proof.
intros Hrevision Hmap Hwords Hpositive Hdelegated Hdifferent Hnative s
[Hconfig [Hsender Haccount]].
split; [apply setcode_other_storage; assumption|]. split.
- rewrite (migration_context_origin _ _ (setcode_preserves_migration_context current source static gas s)).
exact Hsender.
- destruct (String.string_dec current query) as [Heq|Hneq].
2: exact (setcode_other_account_frame query _ current source static gas Hneq s Haccount).
subst current. destruct Haccount as [Hwf [Hcreated|Hcode]].
+ unfold H.setcodefrom. rewrite Hrevision. cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact (conj Hwf (or_introl Hcreated)).
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 := query).
all: first [exact (conj Hwf (or_introl Hcreated))|intros _; left; exact Hcreated].
+ assert (Hguard : Setcode_rules.adoption_allowed
(Delegation.is_valid_delegation (A.t.code (State.op_pointcaretlparenrparen s (H.account None query))))
(Addr.op_eq query (S.t.tx_origin s))
(Reserve_balance.user_reserve_balance (S.t.world_state s)
(Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))) query) = false).
{ change (A.t.code (State.op_pointcaretlparenrparen s (H.account None query)) = delegated) in Hcode.
rewrite Hcode, Hdelegated, Hsender.
assert (Hneq : Addr.op_eq query sender = false).
{ change (Z.eqb (OCamlString.compare query sender) 0 = false).
apply Z.eqb_neq. intro Heq. apply Hdifferent, address_compare_eq, Heq. }
rewrite Hneq. fold (storageBlock s).
rewrite (configured_threshold_at_current storage block query s Hmap Hwords Hconfig).
apply Bool.not_true_is_false. intro Hallowed.
apply Hpositive, rules.relayed_delegated_adoption_requires_zero, Hallowed. }
unfold H.setcodefrom. rewrite Hrevision.
destruct static; [exact (conj Hwf (or_intror Hcode))|].
destruct (Setcode_rules.system_account query); [exact (conj Hwf (or_intror Hcode))|].
cbn beta iota zeta.
match goal with |- context [if ?lt then _ else _] ⇒
destruct lt; [exact (conj Hwf (or_intror Hcode))|] end.
rewrite Hguard. exact (conj Hwf (or_intror Hcode)).
Qed.
End Host.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.protectedstorage QedkConcrete.protectedstoragehost
QedkConcrete.protectedstoragenative QedkConcrete.reserveencoding
QedkConcrete.hoststoragewords QedkConcrete.reservecheck QedkConcrete.hostframe
QedkConcrete.hostcode QedkConcrete.recursiveassembly QedkConcrete.vmhostframe.
Require Import SetcodeConcrete.accountframe SetcodeConcrete.emptyaccount
SetcodeConcrete.executionconfig SetcodeConcrete.executionconfigstorage
SetcodeConcrete.contextframe SetcodeConcrete.adoptionguard.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Definition initialThreshold storage block query :=
Numeric.U256.of_uint_exn (Reserve_config.active
{| Reserve_config.t.settled := readConfigField storage query 0;
Reserve_config.t.pending := readConfigField storage query 1;
Reserve_config.t.valid_from := readConfigField storage query 2 |} block).
Definition guardedAccount query delegated storage block sender s : Prop :=
configStorage block storage s ∧ S.t.tx_origin s = sender ∧
accountFrame query (fun code _ ⇒ code = delegated) s.
Lemma guarded_account_write query delegated storage block sender s keep addr value :
guardedAccount query delegated storage block sender s →
addr ≠ Reserve_balance.precompile_address →
(addr = query → In query (S.t.accounts_created_in_current_transaction s) ∨
A.t.code value = delegated) →
guardedAccount query delegated storage block sender
(S.t.with_world_state (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
(W.account (Some keep) addr) value) s).
Proof.
intros [Hconfig [Hsender Haccount]] Hnative Hvalue. split.
- apply protected_storage_write; [exact Hconfig|contradiction].
- split; [exact Hsender|]. apply account_frame_write; assumption.
Qed.
Lemma guarded_account_mark_creation query delegated storage block sender s addr :
guardedAccount query delegated storage block sender s →
addr ≠ Reserve_balance.precompile_address →
guardedAccount query delegated storage block sender
(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 [Hconfig [Hsender Haccount]] Hnative. split.
- apply protected_storage_mark_creation; assumption.
- split; [exact Hsender|]. apply account_frame_mark_creation; exact Haccount.
Qed.
Lemma guarded_account_same query delegated storage block sender before after :
W.t.accounts (S.t.world_state after) = W.t.accounts (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 →
storageBlock after = storageBlock before →
S.t.tx_origin after = S.t.tx_origin before →
guardedAccount query delegated storage block sender before →
guardedAccount query delegated storage block sender after.
Proof.
intros Haccounts Hcreated Hdes Hblock Horigin [Hconfig [Hsender [Hmap Haccount]]].
split; [eapply protected_storage_same_fields; eassumption|].
split; [rewrite Horigin; exact Hsender|].
unfold accountFrame, State.op_pointcaretlparenrparen, W.account, W.account_opt in ×.
cbn in ×. rewrite Haccounts, Hcreated. split; assumption.
Qed.
Lemma configured_threshold_at_current storage block query s :
stringmaps.WellFormed storage → storageWords storage →
configStorage block storage s →
Reserve_balance.user_reserve_balance (S.t.world_state s) (storageBlock s) query =
initialThreshold storage block query.
Proof.
intros Hmap Hwords Hstate.
destruct (protected_storage_contents _ _ _ Hstate) as [Hblock [updates Hwrites]].
pose proof (reserve_storage_writes_threshold_any_account _ _ _ _ query block
Hmap Hwords Hwrites ltac:(lia)) as Hthreshold.
unfold Reserve_balance.user_reserve_balance, Reserve_balance.stored_config, initialThreshold.
rewrite Hblock. f_equal. exact Hthreshold.
Qed.
Lemma migration_context_origin before after :
migrationContext after = migrationContext before →
S.t.tx_origin after = S.t.tx_origin before.
Proof.
unfold migrationContext, transactionContext. intro Heq.
injection Heq as Hsnapshot Hblock Horigin Hlimit Hprice Hhistory Hcounter Hprior Hchain.
exact Horigin.
Qed.
Section Host.
Context {F : H.FArgs}.
Context (query : Addr.t) (delegated : string) (storage : list (string × string))
(block : N) (sender : Addr.t).
Local Abbreviation protected := (guardedAccount query delegated storage block sender).
Lemma transfer_guarded_account from to value :
preservesCallback protected (H.transfer_ether from to value).
Proof.
intros s [Hconfig [Hsender Haccount]]. split.
- apply transfer_preserves_protected_storage. exact Hconfig.
- split.
+ rewrite (migration_context_origin _ _ (transfer_preserves_migration_context from to value s)).
exact Hsender.
+ apply transfer_account_frame. exact Haccount.
Qed.
Lemma setcode_guarded_account cold warm access write reserve current source static gas :
H.ChainParams.(Chain.Monad.PARAMS.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
stringmaps.WellFormed storage → storageWords storage →
initialThreshold storage block query ≠ 0 →
Delegation.is_valid_delegation delegated = true →
query ≠ sender → current ≠ Reserve_balance.precompile_address →
preservesCallback protected (H.setcodefrom current source static gas).
Proof.
intros Hrevision Hmap Hwords Hpositive Hdelegated Hdifferent Hnative s
[Hconfig [Hsender Haccount]].
split; [apply setcode_other_storage; assumption|]. split.
- rewrite (migration_context_origin _ _ (setcode_preserves_migration_context current source static gas s)).
exact Hsender.
- destruct (String.string_dec current query) as [Heq|Hneq].
2: exact (setcode_other_account_frame query _ current source static gas Hneq s Haccount).
subst current. destruct Haccount as [Hwf [Hcreated|Hcode]].
+ unfold H.setcodefrom. rewrite Hrevision. cbn beta iota zeta.
repeat match goal with |- context [if ?test then _ else _] ⇒
destruct test eqn:?; cbn beta iota zeta end.
all: try exact (conj Hwf (or_introl Hcreated)).
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 := query).
all: first [exact (conj Hwf (or_introl Hcreated))|intros _; left; exact Hcreated].
+ assert (Hguard : Setcode_rules.adoption_allowed
(Delegation.is_valid_delegation (A.t.code (State.op_pointcaretlparenrparen s (H.account None query))))
(Addr.op_eq query (S.t.tx_origin s))
(Reserve_balance.user_reserve_balance (S.t.world_state s)
(Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s))) query) = false).
{ change (A.t.code (State.op_pointcaretlparenrparen s (H.account None query)) = delegated) in Hcode.
rewrite Hcode, Hdelegated, Hsender.
assert (Hneq : Addr.op_eq query sender = false).
{ change (Z.eqb (OCamlString.compare query sender) 0 = false).
apply Z.eqb_neq. intro Heq. apply Hdifferent, address_compare_eq, Heq. }
rewrite Hneq. fold (storageBlock s).
rewrite (configured_threshold_at_current storage block query s Hmap Hwords Hconfig).
apply Bool.not_true_is_false. intro Hallowed.
apply Hpositive, rules.relayed_delegated_adoption_requires_zero, Hallowed. }
unfold H.setcodefrom. rewrite Hrevision.
destruct static; [exact (conj Hwf (or_intror Hcode))|].
destruct (Setcode_rules.system_account query); [exact (conj Hwf (or_intror Hcode))|].
cbn beta iota zeta.
match goal with |- context [if ?lt then _ else _] ⇒
destruct lt; [exact (conj Hwf (or_intror Hcode))|] end.
rewrite Hguard. exact (conj Hwf (or_intror Hcode)).
Qed.
End Host.