Library QedkConcrete.persistentcode

Existing contract code survives concrete execution

CREATE/CREATE2 must pass the actual collision check before recording a creation. Nonempty code therefore protects an existing contract without assuming cryptographic address separation. SELFDESTRUCT cannot mark that account for deletion because it was not created in this transaction.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.hostcode QedkConcrete.hostframe QedkConcrete.worldaccounts
  QedkConcrete.vmhostframe QedkConcrete.accountcode.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Persistent.
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 persistentCode (query : Addr.t) (code : string) (s : S.t) : Prop := {
  persistent_code_map : stringmaps.WellFormed (W.t.accounts (S.t.world_state s));
  persistent_code_value : A.t.code (readAccount s query) = code;
  persistent_code_uncreated : ¬ List.In query (S.t.accounts_created_in_current_transaction s);
  persistent_code_undestroyed : ¬ List.In query (S.t.self_destruct s)
}.

Lemma persistent_code_frame query code before after :
  persistentCode query code before →
  stringmaps.WellFormed (W.t.accounts (S.t.world_state after)) →
  @creationFrame before after →
  ¬ List.In query (S.t.accounts_created_in_current_transaction after) →
  persistentCode query code after.
Proof.
  intros Hbefore Hmap Hframe Hcreated. constructor; try assumption.
  - etransitivity; [apply (noncreated_code _ _ Hframe); exact Hcreated|
      exact (persistent_code_value _ _ _ Hbefore)].
  - intro Hin. destruct (destruction_covered _ _ Hframe query Hin) as [Hold|Hnew].
    + exact (persistent_code_undestroyed _ _ _ Hbefore Hold).
    + exact (Hcreated Hnew).
Qed.

Lemma persistent_code_write query code s keep addr value :
  persistentCode query code s →
  (addr = query → A.t.code value = code) →
  persistentCode query code (S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr) value) s).
Proof.
  intros Hbefore Hvalue. constructor.
  - apply account_write_wellformed. exact (persistent_code_map _ _ _ Hbefore).
  - cbv beta. cbn [S.t.with_world_state S.t.world_state].
    rewrite code_after_account_write by exact (persistent_code_map _ _ _ Hbefore).
    destruct (String.eqb addr query) eqn:Heq.
    + apply Hvalue, String.eqb_eq. exact Heq.
    + exact (persistent_code_value _ _ _ Hbefore).
  - exact (persistent_code_uncreated _ _ _ Hbefore).
  - exact (persistent_code_undestroyed _ _ _ Hbefore).
Qed.

Lemma persistent_code_mark_creation query code s addr :
  persistentCode query code s → addr ≠ query →
  persistentCode query code (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 [Hmap Hcode 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.

Context {F : H.FArgs}.
Lemma transfer_preserves_persistent_code query code sender recipient amount :
  preservesCallback (persistentCode query code) (@H.transfer_ether F sender recipient amount).
Proof.
  intros s Hbefore.
  destruct (@transfer_preserves_creation UF F sender recipient amount s
    (persistent_code_map _ _ _ Hbefore)) as [Hmap Hframe].
  eapply persistent_code_frame; [exact Hbefore|exact Hmap|exact Hframe|].
  pose proof (f_equal fst (@transfer_preserves_creation_tracking F sender recipient amount s)) as Hset.
  cbn beta iota zeta delta [fst] in Hset. rewrite Hset.
  exact (persistent_code_uncreated _ _ _ Hbefore).
Qed.

Lemma create_guard_excludes_existing_code query code s target other :
  persistentCode query code s → code ≠ Byte_string.Bytes.empty →
  orb other (Byte_string.Bytes.op_ltgt (A.t.code (readAccount s target)) Byte_string.Bytes.empty) = false →
  target ≠ query.
Proof.
  intros Hbefore Hnonempty Hguard Heq. subst target.
  apply Bool.orb_false_iff in Hguard as [_ Hcode].
  rewrite (persistent_code_value _ _ _ Hbefore) in Hcode.
  change (negb (Z.eqb (OCamlString.compare code Byte_string.Bytes.empty) 0) = false) in Hcode.
  apply Bool.negb_false_iff, Z.eqb_eq in Hcode.
  apply Hnonempty. apply reservecheck.address_compare_eq. exact Hcode.
Qed.

Lemma increment_nonce_preserves_persistent_code query code addr :
  preservesCallback (persistentCode query code) (@H.try_increment_nonce F addr).
Proof.
  intros 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 persistent_code_write with (s := s) (keep := false) (addr := addr);
      [exact Hinitial|]. intro Heq; subst addr. exact (persistent_code_value _ _ _ Hinitial).
Qed.
End Persistent.

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 := (@persistentCode).

Lemma process_create_preserves_existing_code query fixedCode msg :
  fixedCode ≠ Byte_string.Bytes.empty →
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    preservesCallback (protected query fixedCode) ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  preservesCallback (protected query fixedCode)
    (@H.process_create F msg).
Proof.
  intros Hnonempty 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_existing_code query fixedCode s target _ Hinitial Hnonempty 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 fixedCode marked).
  { exact (@persistent_code_mark_creation query fixedCode s target Hinitial Htarget). }
  assert (Hstored : protected query fixedCode stored).
  { unfold stored. eapply persistent_code_write with (s := marked) (keep := false) (addr := target);
      [exact Hmarked|intro Heq; contradiction]. }
  assert (Hprepared : protected query fixedCode prepared).
  { unfold prepared. eapply persistent_code_write with (s := stored) (keep := false) (addr := target);
      [exact Hstored|intro Heq; contradiction]. }
  pose proof (@transfer_preserves_persistent_code UF F query fixedCode
    (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 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.
  try match goal with |- context [if andb ?enabled ?adopted then _ else _] ⇒
    destruct (andb enabled adopted); [exact Hrecursive|] end.
  match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct failure as [error|] end.
  - exact Hrecursive.
  - eapply persistent_code_write with (s := afterVm) (keep := false) (addr := target);
      [exact Hrecursive|intro Heq; contradiction].
Qed.

End Creation.