Library QedkConcrete.stakingretention

Account retention from positive balance, without a nonce premise

These lemmas concern the concrete account update used by staking storage writes and payouts. A positive updated balance prevents empty-account deletion, including when nonce and code are both zero/empty.
Deriving a positive remaining balance for staking execution uses the staking accounting and withdrawal-schedule invariant in stakingaccounting: any payout leaves the active stake locked.
From Stdlib Require Import ZArith Lia.
Require Import QedkConcrete.worldaccounts.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).
Local Abbreviation writeAccount := (fun world addr account ⇒
  State.op_pointcaretlparenrparenltminus world (@W.account None addr) account).

Lemma positive_balance_not_empty account :
  0 < U.to_z (A.t.balance account) → A.is_empty account = false.
Proof.
  intro Hpositive. destruct (A.is_empty account) eqn:Hempty; [|reflexivity].
  pose proof (account_empty_balance account Hempty). lia.
Qed.

The entire stored account survives, not just its balance. No condition on its nonce or code is needed.
Theorem positive_balance_retains_account world addr account :
  stringmaps.WellFormed (W.t.accounts world) →
  0 < U.to_z (A.t.balance account) →
  readAccount (writeAccount world addr account) addr = account.
Proof.
  intros Hworld Hpositive.
  change (readAccount
    (State.op_pointcaretlparenrparenltminus world (@W.account (Some false) addr) account)
    addr = account).
  cbn beta. rewrite account_read_after_write by exact Hworld.
  rewrite String.eqb_refl, (positive_balance_not_empty account Hpositive).
  reflexivity.
Qed.

locked is principal that must remain after this payout. The caller must derive locked ≤ balance - amount from the staking accounting invariant; this lemma proves the concrete U256 debit and account normalization retain that principal and the account's other fields. It is not a VM assumption.
Theorem debit_retains_locked_account world addr amount locked :
  stringmaps.WellFormed (W.t.accounts world) →
  0 ≤ U.to_z amount →
  0 < locked ≤ U.to_z (A.t.balance (readAccount world addr)) - U.to_z amount →
  U.to_z (A.t.balance (readAccount world addr)) < 2^256 →
  let before := readAccount world addr in
  let debit := A.t.with_balance (U.op_minus (A.t.balance before) amount) before in
  let post := writeAccount world addr debit in
  readAccount post addr = debit ∧
  locked ≤ U.to_z (A.t.balance (readAccount post addr)) ∧
  A.t.storage (readAccount post addr) = A.t.storage before ∧
  A.t.nonce (readAccount post addr) = A.t.nonce before ∧
  A.t.code (readAccount post addr) = A.t.code before.
Proof.
  intros Hworld Hamount Hlocked Hbound. cbn zeta.
  assert (Hsub : U.to_z (U.op_minus
    (A.t.balance (readAccount world addr)) amount) =
    U.to_z (A.t.balance (readAccount world addr)) - U.to_z amount).
  { apply reservecheck.u256_sub. lia. }
  cbn beta in ×.
  rewrite positive_balance_retains_account by
    (try exact Hworld; cbn [A.t.with_balance A.t.balance]; rewrite Hsub; lia).
  split; [reflexivity|]. cbn [A.t.with_balance A.t.balance A.t.storage A.t.nonce A.t.code]. rewrite Hsub.
  split; [lia|]. repeat split; reflexivity.
Qed.

Print Assumptions positive_balance_retains_account.
Print Assumptions debit_retains_locked_account.