Library QedkConcrete.protectedstorage

Isolation of an activated native account's storage

A nonzero nonce prevents CREATE from replacing the native account and prevents balance writes from deleting its storage through EIP-161. Empty code prevents ordinary execution at that address. P describes the storage property at the current block number, so a delayed-setting invariant can refer to its activation window. Native setter effects are handled separately from ordinary account writes.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostcode QedkConcrete.hostframe
  QedkConcrete.worldaccounts QedkConcrete.reservestorage
  QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Definition storageBlock (s : S.t) : Support.UintN.t :=
  Chain.Ethereum.Block.Header.t.number (Chain.Ethereum.Block.t.header (S.t.current_block s)).

Section Protected.
Context {U64 : Basics.Unreachable Numeric.U64.t} {UF : Basics.Unreachable Numeric.U256.t}.
Local Abbreviation readAccount := (fun s addr ⇒ State.op_pointcaretlparenrparen
  (S.t.world_state s) (@W.account None addr)).

Record protectedStorage (query : Addr.t) (P : Support.UintN.t → list (string × string) → Prop)
    (s : S.t) : Prop := {
  protected_storage_map : stringmaps.WellFormed (W.t.accounts (S.t.world_state s));
  protected_storage_nonce : A.t.nonce (readAccount s query) ≠ 0;
  protected_storage_code : A.t.code (readAccount s query) = Byte_string.Bytes.empty;
  protected_storage_contents : P (storageBlock s) (A.t.storage (readAccount s query));
  protected_storage_uncreated : ¬ List.In query (S.t.accounts_created_in_current_transaction s);
  protected_storage_undestroyed : ¬ List.In query (S.t.self_destruct s)
}.

Lemma protected_storage_same_fields query P 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 →
  protectedStorage query P before → protectedStorage query P after.
Proof.
  intros Haccounts Hcreated Hdestroyed Hblock Hbefore.
  assert (Haccount : readAccount after query = readAccount before query).
  { change ((match Addr.Map.find_opt query (W.t.accounts (S.t.world_state after)) with
      | Some ac ⇒ ac | None ⇒ @A.empty end) = readAccount before query).
    rewrite Haccounts. reflexivity. }
  destruct Hbefore. constructor; try rewrite Haccount; try rewrite Haccounts; try rewrite Hblock;
    try rewrite Hcreated; try rewrite Hdestroyed; assumption.
Qed.

Lemma protected_storage_write query P s keep addr value :
  protectedStorage query P s →
  (addr = query → A.t.nonce value ≠ 0 ∧
    A.t.code value = Byte_string.Bytes.empty ∧ P (storageBlock s) (A.t.storage value)) →
  protectedStorage query P (S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr) value) s).
Proof.
  intros Hbefore Hvalue.
  set (after := S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr) value) s).
  assert (Haccount : A.t.nonce (readAccount after query) ≠ 0 ∧
    A.t.code (readAccount after query) = Byte_string.Bytes.empty ∧
    P (storageBlock s) (A.t.storage (readAccount after query))).
  { unfold after. cbv beta. cbn [S.t.with_world_state S.t.world_state].
    rewrite account_read_after_write by exact (protected_storage_map _ _ _ Hbefore).
    destruct (String.eqb addr query) eqn:Heq.
    - apply String.eqb_eq in Heq. destruct (Hvalue Heq) as [Hnonce [Hcode Hstorage]].
      destruct keep; [repeat split; assumption|].
      rewrite nonzero_nonce_not_empty by exact Hnonce. repeat split; assumption.
    - split; [exact (protected_storage_nonce _ _ _ Hbefore)|].
      split; [exact (protected_storage_code _ _ _ Hbefore)|exact (protected_storage_contents _ _ _ Hbefore)]. }
  destruct Haccount as [Hnonce [Hcode Hstorage]]. constructor; try assumption.
  - apply account_write_wellformed. exact (protected_storage_map _ _ _ Hbefore).
  - exact (protected_storage_uncreated _ _ _ Hbefore).
  - exact (protected_storage_undestroyed _ _ _ Hbefore).
Qed.

Lemma protected_storage_balance query P s keep addr balance :
  protectedStorage query P s →
  protectedStorage query P (S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr)
      (A.t.with_balance balance (readAccount s addr))) s).
Proof.
  intro Hbefore. apply protected_storage_write; [exact Hbefore|].
  intro Heq. subst addr. split; [exact (protected_storage_nonce _ _ _ Hbefore)|].
  split; [exact (protected_storage_code _ _ _ Hbefore)|exact (protected_storage_contents _ _ _ Hbefore)].
Qed.

Lemma protected_storage_mark_creation query P s addr :
  protectedStorage query P s → addr ≠ query →
  protectedStorage 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 Hbefore Hdifferent. destruct Hbefore as [Hwf Hnonce Hcode Hstorage Hcreated Hdestroyed].
  constructor; try assumption.
  intro Hin. cbn [S.t.accounts_created_in_current_transaction S.t.with_accounts_created_in_current_transaction] in Hin.
  destruct (proj1 (address_set_add_members addr (S.t.accounts_created_in_current_transaction s) query) Hin)
    as [Heq|Hmember].
  - apply Hdifferent. symmetry. exact Heq.
  - exact (Hcreated Hmember).
Qed.

Lemma protected_storage_mark_destruction query P s addr :
  protectedStorage query P s → addr ≠ query →
  protectedStorage query P (S.t.with_self_destruct
    (Addr._Set.(OCamlSet.S.add) addr (S.t.self_destruct s)) s).
Proof.
  intros Hbefore Hdifferent. destruct Hbefore as [Hwf Hnonce Hcode Hstorage Hcreated Hdestroyed].
  constructor; try assumption.
  intro Hin. cbn [S.t.self_destruct S.t.with_self_destruct] in Hin.
  destruct (proj1 (address_set_add_members addr (S.t.self_destruct s) query) Hin) as [Heq|Hmember].
  - apply Hdifferent. symmetry. exact Heq.
  - exact (Hdestroyed Hmember).
Qed.

Context {F : H.FArgs}.

Lemma transfer_preserves_protected_storage query P sender recipient amount :
  preservesCallback (protectedStorage query P) (@H.transfer_ether F sender recipient amount).
Proof.
  intros s Hbefore. unfold H.transfer_ether.
  cbn -[Numeric.U256.op_gteq Numeric.U256.op_plus Numeric.U256.op_minus W.account].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  - cbn beta iota zeta delta [Monad.Identity.Impl._return fst snd].
    match goal with |- protectedStorage _ _ (S.t.with_world_state _ ?middle) ⇒
      set (debited := middle) in × end.
    assert (Hdebit : protectedStorage query P debited).
    { unfold debited. apply (protected_storage_balance query P s false sender). exact Hbefore. }
    exact (protected_storage_balance query P debited false recipient _ Hdebit).
  - exact Hbefore.
Qed.

Context {UU : Basics.Unreachable unit} {UI : Basics.Unreachable int}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma sstore_preserves_protected_storage query P addr key value :
  addr ≠ query →
  preservesCallback (protectedStorage query P) (@H.set_storage F addr key value).
Proof.
  intros Hdifferent s Hbefore.
  eapply protected_storage_write with (s := s) (keep := true) (addr := addr); [exact Hbefore|].
  intro Heq. contradiction.
Qed.

Lemma selfdestruct_preserves_protected_storage query P addr beneficiary :
  preservesCallback (protectedStorage query P) (@H.selfdestruct F addr beneficiary).
Proof.
  intros s Hbefore. unfold H.selfdestruct.
  cbn -[H.transfer_ether Addr._Set W.account].
  match goal with |- context [H.transfer_ether ?from ?to ?amount ?initial] ⇒
    pose proof (transfer_preserves_protected_storage query P from to amount initial Hbefore) as Htransfer;
    destruct (H.transfer_ether from to amount initial) as [ok middle] end.
  cbn [snd] in Htransfer. cbn -[Addr._Set W.account].
  destruct (Addr._Set.(OCamlSet.S.mem) addr
    (S.t.accounts_created_in_current_transaction middle)) eqn:Hcreated.
  - set (marked := S.t.with_self_destruct
      (Addr._Set.(OCamlSet.S.add) addr (S.t.self_destruct middle)) middle).
    apply (protected_storage_balance query P marked false addr).
    apply protected_storage_mark_destruction; [exact Htransfer|].
    intro Heq. subst addr. apply (protected_storage_uncreated _ _ _ Htransfer).
    apply address_set_mem_member. exact Hcreated.
  - exact Htransfer.
Qed.

CREATE's actual collision check rejects a protected address regardless of its balance or storage. No cryptographic CREATE exclusion is needed.
Lemma create_guard_excludes_protected query P s target other :
  protectedStorage query P s →
  orb (Numeric.U64.op_ltgt (A.t.nonce (readAccount s target)) (@Numeric.U64.zero))
    other = false →
  target ≠ query.
Proof.
  intros Hbefore Hguard Heq. subst target.
  apply Bool.orb_false_iff in Hguard as [Hnonce _].
  apply (protected_storage_nonce _ _ _ Hbefore).
  change (negb (Z.eqb (OCamlZ.compare (A.t.nonce (readAccount s query)) 0) 0)
    = false) in Hnonce.
  unfold OCamlZ.compare in Hnonce.
  destruct (Z.compare_spec (A.t.nonce (readAccount s query)) 0);
    [assumption|discriminate|discriminate].
Qed.
End Protected.

Section Creation.
Context {F : H.FArgs}
  {UN : Basics.Unreachable Support.UintN.t} {U64 : Basics.Unreachable Numeric.U64.t}
  {UF : Basics.Unreachable Numeric.U256.t} {UG : Basics.Unreachable Gas.t}
  {UA : Basics.Unreachable Addr.t} {UB : Basics.Unreachable Byte_string.Bytes.t}
  {UU : Basics.Unreachable unit} {US : Basics.Unreachable string}
  {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
  {U64i : Basics.Unreachable int64}
  {U32 : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation protected := (@protectedStorage).

This is the CREATE induction step. Its collision check excludes the protected address before storage is reset or initcode is executed. The recursive VM premise is to be discharged by the VM/Host induction.
Lemma process_create_preserves_protected_storage query P msg :
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    Evmc.Message.t.recipient nested ≠ query →
    preservesCallback (protected query P) ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  preservesCallback (protected query P)
    (@H.process_create F msg).
Proof.
  intros Hvm Hgas s Hinitial.
  unfold H.process_create, H.contract_creation_address, H.M.
  cbn -[Addr.of_contract_creation W.account Numeric.U64.op_ltgt
    Byte_string.Bytes.op_ltgt H.transfer_ether].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hcollision end.
  { exact Hinitial. }
  match goal with |- context [H.transfer_ether _ ?addr _ _] ⇒ set (target := addr) in × end.
  assert (Htarget : target ≠ query).
  { exact (@create_guard_excludes_protected query P s target _ Hinitial Hcollision). }
  match goal with |- context [H.transfer_ether _ _ _
    (S.t.with_world_state ?nonceWorld (S.t.with_world_state ?storageWorld ?created))] ⇒
    set (marked := created) in *;
    set (stored := S.t.with_world_state storageWorld marked) in *;
    set (prepared := S.t.with_world_state nonceWorld stored) in ×
  end.
  assert (Hmarked : protected query P marked).
  { exact (@protected_storage_mark_creation query P s target Hinitial Htarget). }
  assert (Hstored : protected query P stored).
  { unfold stored. eapply protected_storage_write with (s := marked) (keep := false) (addr := target);
      [exact Hmarked|intro Heq; contradiction]. }
  assert (Hprepared : protected query P prepared).
  { unfold prepared. eapply protected_storage_write with (s := stored) (keep := false) (addr := target);
      [exact Hstored|intro Heq; contradiction]. }
  pose proof (@transfer_preserves_protected_storage F query P
    (Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared Hprepared) as Htransfer.
  destruct (@H.transfer_ether F (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared) as [ok middle] eqn:HtransferRun.
  cbn beta iota zeta delta [fst snd] in Htransfer.
  cbn -[W.account Addr.of_contract_creation target stored prepared marked].
  destruct ok.
  2: { cbn -[W.account Addr.of_contract_creation target stored prepared marked].
    exact Htransfer. }
  cbn -[W.account Addr.of_contract_creation target stored prepared marked].
  match goal with |- context [(@H.Vm F).(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
    pose proof (Hvm nested code Hgas Htarget middle Htransfer) as Hrecursive;
    destruct ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
  end.
  cbn beta iota zeta delta [fst snd] in Hrecursive.
  destruct result as [status gasleft refund output address]. destruct status;
    cbn -[W.account Addr.of_contract_creation target stored prepared marked];
    try exact Hrecursive.
  match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct failure as [error|] end.
  - exact Hrecursive.
  - eapply protected_storage_write with (s := afterVm) (keep := false) (addr := target);
      [exact Hrecursive|intro Heq; contradiction].
Qed.

Lemma increment_nonce_preserves_protected_storage query P addr :
  addr ≠ query →
  preservesCallback (protected query P) (@H.try_increment_nonce F addr).
Proof.
  intros Hdifferent s Hinitial. 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 Hinitial.
  - eapply protected_storage_write with (s := s) (keep := false) (addr := addr);
      [exact Hinitial|intro Heq; contradiction].
Qed.
End Creation.

Print Assumptions transfer_preserves_protected_storage.
Print Assumptions selfdestruct_preserves_protected_storage.
Print Assumptions create_guard_excludes_protected.
Print Assumptions process_create_preserves_protected_storage.