Library QedkConcrete.nondebitcreatetracking

Bounded VM premises at concrete CREATE entry

The Host establishes the representation and context bounds required by the checked VM proof before invoking initcode. Creation tracking and the guarded balance floor are carried alongside those bounds.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.nondebitcreate QedkConcrete.nondebitframe
  QedkConcrete.hostcode QedkConcrete.hostframe QedkConcrete.hostbalance
  QedkConcrete.hostsupply QedkConcrete.supply QedkConcrete.worldaccounts
  QedkConcrete.vmhostframe QedkConcrete.hostaccountformat QedkConcrete.hostchecked
  QedkConcrete.vmsupplywords QedkConcrete.hostcontextwords QedkConcrete.vmcallchecked
  QedkConcrete.wordrange QedkConcrete.hoststoragewords.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Lemma account_write_supply_context {U64 : Basics.Unreachable Numeric.U64.t}
    {UF : Basics.Unreachable Numeric.U256.t} params limit s keep addr value :
  hostSupplyContext params limit s →
  Numeric.U256.to_z (A.t.balance value) = Numeric.U256.to_z
    (A.t.balance (State.op_pointcaretlparenrparen (S.t.world_state s) (@W.account None addr))) →
  accountFormat value →
  hostSupplyContext params limit (S.t.with_world_state
    (State.op_pointcaretlparenrparenltminus (S.t.world_state s)
      (@W.account (Some keep) addr) value) s).
Proof.
  intros [[[Hbalance Hlimit] [Hformat Htransient]] Hcontext] Hsame Hvalue.
  destruct (@account_write_preserves_supply keep (S.t.world_state s) addr value
    Hbalance Hsame) as [Hafter Htotal].
  split.
  - split.
    + split; [exact Hafter|]. cbn [S.t.with_world_state S.t.world_state]. rewrite Htotal. exact Hlimit.
    + split; [apply write_account_format; assumption|exact Htransient].
  - eapply host_context_words_preserved; [reflexivity|exact Hcontext].
Qed.
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.

Context (limit : Z).
Local Abbreviation invariant := (hostSupplyContext F.(H.ChainParams) limit).
Local Abbreviation floor := (@uncreatedBalanceFloor).

Lemma process_create_preserves_uncreated_balance_tracking query minimum msg :
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    ∀ s, stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
    ∀ a, List.In a (S.t.accounts_created_in_current_transaction s) →
      List.In a (S.t.accounts_created_in_current_transaction
        (snd ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code s)))) →
  (∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
    ∀ s, invariant 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))) →
  @vmMessageBounds msg →
  Evmc.Message.t.sender msg ≠ query →
  ∀ s, invariant s → floor query minimum s →
    floor query minimum (snd (@H.process_create F msg s)).
Proof.
  intros Hcreation Hvm [Hgas [Hword [Hdata Hcapacity]]] Hsender s Hcontext Hinitial.
  pose proof (proj1 Hword) as Hvalue.
  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 (HmarkedContext : invariant marked) by exact Hcontext.
  assert (HstoredContext : invariant stored).
  { unfold stored. eapply account_write_supply_context with (s := marked) (keep := false) (addr := target);
      [exact HmarkedContext|reflexivity|].
    apply storage_update_format.
    - apply read_account_format. exact (proj1 (proj2 (proj1 HmarkedContext))).
    - change (storageWords nil). constructor. }
  assert (HpreparedContext : invariant prepared).
  { unfold prepared. eapply account_write_supply_context with (s := stored) (keep := false) (addr := target);
      [exact HstoredContext|reflexivity|].
    apply nonce_update_format, read_account_format. exact (proj1 (proj2 (proj1 HstoredContext))). }
  assert (HtransferContext : invariant (snd (@H.transfer_ether F
    (Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared))).
  { eapply host_supply_context_frame; [apply supply_transfer; exact Hvalue|
      intro; apply transfer_preserves_format|apply transfer_preserves_context|exact HpreparedContext]. }
  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, HtransferContext.
  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);
    assert (Hnested : @vmMessageBounds nested) by
      (unfold vmMessageBounds; split; [exact Hgas|]; split; [exact Hword|];
       split; [change (0 < 2^62); lia|exact Hcapacity]);
    pose proof (Hvm nested code Hnested ltac:(lia) middle HtransferContext 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.
  assert (HtargetAfter : List.In target (S.t.accounts_created_in_current_transaction afterVm)).
  { exact (HrecursiveFrame target 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.
  try (match goal with |- context [if andb ?enabled ?test then _ else _] ⇒
    destruct (andb enabled test);
      cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedBalanceFloor] end);
    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.
Top-level creation may debit the sender by msg.value, but initcode cannot spend more of an undelegated signer's balance.
Lemma process_create_value_floor_tracking query minimum msg :
  (∀ nested code, 0 ≤ Evmc.Message.t.gas nested < 2^63 →
    ∀ s, stringmaps.WellFormed (W.t.accounts (S.t.world_state s)) →
    ∀ a, List.In a (S.t.accounts_created_in_current_transaction s) →
      List.In a (S.t.accounts_created_in_current_transaction
        (snd ((@H.Vm F).(Evmc.Vm.SIG.execute) nested code s)))) →
  (∀ nested code, @vmMessageBounds nested → OCamlString.length code < 2^256 →
    ∀ s, invariant 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))) →
  @vmMessageBounds msg →
  ∀ s, invariant s → floor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) s →
    floor query minimum (snd (@H.process_create F msg s)).
Proof.
  intros Hcreation Hvm [Hgas [Hword [Hdata Hcapacity]]] s Hcontext Hinitial.
  pose proof (proj1 Hword) as Hvalue.
  assert (Hlow : floor query minimum s) by
    (eapply uncreated_floor_weaken; [|exact Hinitial]; lia).
  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 Hlow. }
  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 + Numeric.U256.to_z (Evmc.Message.t.value msg)) marked).
  { apply mark_creation_uncreated_floor. exact Hinitial. }
  assert (Hstored : floor query (minimum + Numeric.U256.to_z (Evmc.Message.t.value msg)) 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 + Numeric.U256.to_z (Evmc.Message.t.value msg)) prepared).
  { unfold prepared. eapply account_write_uncreated_floor with (s := stored) (keep := false) (addr := target);
      [exact Hstored|reflexivity|left; reflexivity]. }
  assert (HmarkedContext : invariant marked) by exact Hcontext.
  assert (HstoredContext : invariant stored).
  { unfold stored. eapply account_write_supply_context with (s := marked) (keep := false) (addr := target);
      [exact HmarkedContext|reflexivity|].
    apply storage_update_format.
    - apply read_account_format. exact (proj1 (proj2 (proj1 HmarkedContext))).
    - change (storageWords nil). constructor. }
  assert (HpreparedContext : invariant prepared).
  { unfold prepared. eapply account_write_supply_context with (s := stored) (keep := false) (addr := target);
      [exact HstoredContext|reflexivity|].
    apply nonce_update_format, read_account_format. exact (proj1 (proj2 (proj1 HstoredContext))). }
  assert (HtransferContext : invariant (snd (@H.transfer_ether F
    (Evmc.Message.t.sender msg) target (Evmc.Message.t.value msg) prepared))).
  { eapply host_supply_context_frame; [apply supply_transfer; exact Hvalue|
      intro; apply transfer_preserves_format|apply transfer_preserves_context|exact HpreparedContext]. }
  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_value_floor UF F query minimum (Evmc.Message.t.sender msg)
    target (Evmc.Message.t.value msg) prepared Hvalue 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, HtransferContext.
  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);
    assert (Hnested : @vmMessageBounds nested) by
      (unfold vmMessageBounds; split; [exact Hgas|]; split; [exact Hword|];
       split; [change (0 < 2^62); lia|exact Hcapacity]);
    pose proof (Hvm nested code Hnested ltac:(lia) middle HtransferContext 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.
  assert (HtargetAfter : List.In target (S.t.accounts_created_in_current_transaction afterVm)).
  { exact (HrecursiveFrame target 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.
  try (match goal with |- context [if andb ?enabled ?test then _ else _] ⇒
    destruct (andb enabled test);
      cbn -[W.account Addr.of_contract_creation target stored prepared marked uncreatedBalanceFloor] end);
    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.
End Creation.

Print Assumptions process_create_preserves_uncreated_balance_tracking.