Library QedkConcrete.nondebitcreate

Creation preserves the balance of an unrelated uncreated account

The creation address is recorded before initcode runs. Consequently the balance guarantee may be released at that address, but nowhere else. Signer exclusion is applied later to the completed transaction, not assumed for each internal CREATE/CREATE2.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.nondebitframe QedkConcrete.hostcode
  QedkConcrete.hostframe QedkConcrete.hostbalance QedkConcrete.hostsupply
  QedkConcrete.supply QedkConcrete.worldaccounts QedkConcrete.vmhostframe.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Helpers.
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)).
Local Abbreviation floor := (@uncreatedBalanceFloor).

Lemma uncreated_floor_frame query minimum before after :
  @creationFrame before after →
  balanceInvariant (S.t.world_state after) →
  (minimum ≤ Numeric.U256.to_z (A.t.balance (readAccount before query)) →
    minimum ≤ Numeric.U256.to_z (A.t.balance (readAccount after query))) →
  floor query minimum before → floor query minimum after.
Proof.
  intros Hframe Hinv Hbalance [_ Haccount]. split; [exact Hinv|].
  destruct Haccount as [Hin|[Hempty Hfloor]].
  - left. exact (creations_retained _ _ Hframe query Hin).
  - destruct (List.in_dec String.string_dec query
      (S.t.accounts_created_in_current_transaction after)) as [Hin|Habsent].
    + left. exact Hin.
    + right. split; [rewrite (noncreated_code _ _ Hframe query Habsent); exact Hempty|].
      apply Hbalance. exact Hfloor.
Qed.

Lemma mark_creation_uncreated_floor query minimum s addr :
  floor query minimum s →
  floor query minimum (S.t.with_accounts_created_in_current_transaction
    (Addr._Set.(OCamlSet.S.add) addr (S.t.accounts_created_in_current_transaction s)) s).
Proof.
  intro Hfloor. eapply uncreated_floor_frame; [apply mark_creation_frame|exact (proj1 Hfloor)| |exact Hfloor].
  intro Hbalance. exact Hbalance.
Qed.

Lemma account_write_uncreated_floor query minimum s keep addr value :
  floor query minimum s →
  Numeric.U256.to_z (A.t.balance value) = Numeric.U256.to_z (A.t.balance (readAccount s addr)) →
  (A.t.code value = A.t.code (readAccount s addr) ∨
    List.In addr (S.t.accounts_created_in_current_transaction s)) →
  floor query minimum (S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr) value) s).
Proof.
  intros Hfloor Hbalance Hcode.
  eapply uncreated_floor_frame.
  - exact (proj2 (world_write_code_frame s keep addr value (proj1 (proj1 Hfloor)) Hcode)).
  - exact (proj1 (account_write_preserves_supply keep (S.t.world_state s) addr value
      (proj1 Hfloor) Hbalance)).
  - intro Hlower. cbv beta. cbn [S.t.with_world_state S.t.world_state].
    rewrite account_write_preserves_balances by (first [exact (proj1 (proj1 Hfloor))|exact Hbalance]).
    exact Hlower.
  - exact Hfloor.
Qed.

Lemma transfer_uncreated_floor {F : H.FArgs} query minimum sender recipient amount :
  sender ≠ query → 0 ≤ Numeric.U256.to_z amount →
  preservesCallback (floor query minimum) (@H.transfer_ether F sender recipient amount).
Proof.
  intros Hdifferent Hvalue s Hfloor. eapply uncreated_floor_frame.
  - exact (proj2 (transfer_preserves_creation sender recipient amount s (proj1 (proj1 Hfloor)))).
  - exact (proj1 (transfer_preserves_supply sender recipient amount s (proj1 Hfloor) Hvalue)).
  - intro Hlower. etransitivity; [exact Hlower|].
    apply transfer_cannot_debit_other; [exact (proj1 Hfloor)|exact Hvalue|exact Hdifferent].
  - exact Hfloor.
Qed.
At the outer transaction call the protected account may be the sender. Charge its explicit value once; all nested execution uses the ordinary unchanged floor. The supply invariant rules out wrapping incoming credits.
Lemma uncreated_floor_weaken query low high s :
  low ≤ high → floor query high s → floor query low s.
Proof.
  intros Hle [Hinv [Hcreated|[Hcode Hbalance]]]; split; [exact Hinv| |exact Hinv|].
  - left. exact Hcreated.
  - right. split; [exact Hcode|lia].
Qed.

Lemma transfer_value_floor {F : H.FArgs} query minimum sender recipient amount s :
  0 ≤ Numeric.U256.to_z amount →
  floor query (minimum + Numeric.U256.to_z amount) s →
  floor query minimum (snd (@H.transfer_ether F sender recipient amount s)).
Proof.
  intros Hvalue Hfloor.
  pose proof (@transfer_balance_effects F sender recipient amount s
    (proj1 Hfloor) Hvalue) as [Hinv [Htotal Hbalances]].
  pose proof (@transfer_preserves_creation UF F sender recipient amount s
    (proj1 (proj1 Hfloor))) as [_ Hframe].
  split; [exact Hinv|].
  destruct Hfloor as [_ [Hcreated|[Hcode Hbalance]]].
  { left. exact (creations_retained _ _ Hframe query Hcreated). }
  destruct (List.in_dec String.string_dec query (S.t.accounts_created_in_current_transaction
    (snd (@H.transfer_ether F sender recipient amount s)))) as [Hin|Habsent].
  { left. exact Hin. }
  right. split.
  { rewrite (noncreated_code _ _ Hframe query Habsent). exact Hcode. }
  cbv beta in Hbalance |- ×. rewrite Hbalances.
  destruct (fst (@H.transfer_ether F sender recipient amount s)),
    (String.eqb recipient query), (String.eqb sender query); lia.
Qed.

End Helpers.

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 frame := (@creationFrame).
Local Abbreviation preserves := preservesCreation.

The recursive invocation must preserve the same relation; this is an induction step, not a new VM contract. CREATE records its target before running initcode, and cannot install code outside that recorded set.
Local Abbreviation floor := (@uncreatedBalanceFloor).

Lemma process_create_preserves_uncreated_balance query minimum msg :
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    preserves ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code)) →
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    0 ≤ Numeric.U256.to_z (Evmc.Message.t.value nested) →
    ∀ s, floor query minimum s →
    (Evmc.Message.t.recipient nested = query →
      code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction s)) →
    floor query minimum (snd ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code s))) →
  0 ≤ Evmc.Message.t.gas msg < 2^63 →
  0 ≤ Numeric.U256.to_z (Evmc.Message.t.value msg) →
  Evmc.Message.t.sender msg ≠ query →
  preservesCallback (floor query minimum)
    (@H.process_create F msg).
Proof.
  intros Hcreation Hvm Hgas Hvalue Hsender 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 uncreatedBalanceFloor].
  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.
  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 : floor query minimum marked).
  { apply mark_creation_uncreated_floor. exact Hinitial. }
  assert (Hstored : floor query minimum stored).
  { unfold stored. eapply account_write_uncreated_floor with (s := marked) (keep := false) (addr := target);
      [exact Hmarked|reflexivity|left; reflexivity]. }
  assert (Hprepared : floor query minimum prepared).
  { unfold prepared. eapply account_write_uncreated_floor with (s := stored) (keep := false) (addr := target);
      [exact Hstored|reflexivity|left; reflexivity]. }
  assert (HtargetPrepared : List.In target (S.t.accounts_created_in_current_transaction prepared)).
  { change (List.In target (Addr._Set.(OCamlSet.S.add) target
      (S.t.accounts_created_in_current_transaction s))).
    apply address_set_add_members. left. reflexivity. }
  pose proof (@transfer_uncreated_floor UF F query minimum (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) Hsender Hvalue prepared Hprepared) as Htransfer.
  pose proof (@transfer_preserves_creation UF F (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared (proj1 (proj1 Hprepared))) as HtransferFrame.
  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, HtransferFrame.
  destruct HtransferFrame as [HmiddleWf HmiddleFrame].
  assert (HtargetMiddle : List.In target (S.t.accounts_created_in_current_transaction middle)).
  { apply (creations_retained _ _ HmiddleFrame). exact HtargetPrepared. }
  cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedBalanceFloor].
  destruct ok.
  2: { cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedBalanceFloor].
    exact Htransfer. }
  cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedBalanceFloor].
  match goal with |- context [(@H.Vm F).(Evmc.Vm.SIG.execute) ?nested ?code middle] ⇒
    assert (Hcode : Evmc.Message.t.recipient nested = query →
      code = Byte_string.Bytes.empty ∨ List.In query (S.t.accounts_created_in_current_transaction middle)) by
      (intro Heq; right; change (target = query) in Heq; rewrite <- Heq; exact HtargetMiddle);
    pose proof (Hvm nested code Hgas Hvalue middle Htransfer Hcode) as Hrecursive;
    pose proof (Hcreation nested code Hgas middle HmiddleWf) as HrecursiveFrame;
    destruct ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code middle) as [result afterVm]
  end.
  cbn beta iota zeta delta [fst snd] in Hrecursive, HrecursiveFrame.
  destruct HrecursiveFrame as [HafterWf HafterFrame].
  assert (HtargetAfter : List.In target (S.t.accounts_created_in_current_transaction afterVm)).
  { apply (creations_retained _ _ HafterFrame). exact HtargetMiddle. }
  destruct result as [status gasleft refund output address]. destruct status;
    cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedBalanceFloor];
    try exact Hrecursive.
  match goal with |- context [match ?failure with Some _ ⇒ _ | None ⇒ _ end] ⇒
    destruct failure as [error|] end.
  - exact Hrecursive.
  - eapply account_write_uncreated_floor with (s := afterVm) (keep := false) (addr := target);
      [exact Hrecursive|reflexivity|right; exact HtargetAfter].
Qed.
CREATE's sender nonce increment occurs before its rollback snapshot; retaining it cannot invalidate a balance or empty-code guarantee.

Lemma increment_nonce_preserves_uncreated_balance query minimum addr :
  preservesCallback (floor query minimum) (@H.try_increment_nonce F addr).
Proof.
  intros s Hfloor. unfold H.try_increment_nonce, H.M.
  cbn -[W.account Numeric.U64.op_eq Numeric.U64.op_plus uncreatedBalanceFloor].
  match goal with |- context [if ?test then _ else _] ⇒ destruct test end.
  - exact Hfloor.
  - eapply account_write_uncreated_floor with (s := s) (keep := false) (addr := addr);
      [exact Hfloor|reflexivity|left; reflexivity].
Qed.
End Creation.

Print Assumptions process_create_preserves_uncreated_balance.