Library QedkConcrete.stakingsupply

Remaining budgets for external supply credits

Credits are nonnegative amounts in wei. A list can include withdrawals and declared staking rewards, without identifying declarations with successful execution. The preservation rule consumes a prefix budget when a separately proved transition increases total supply by at most that prefix. It is arithmetic bookkeeping, not a staking or VM contract.
The concrete account-credit corollary reuses the generated account lens and the existing non-wrapping credit proofs. No result here establishes that a staking syscall succeeds or implements that account update.
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.worldaccounts QedkConcrete.supply QedkConcrete.credits.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.

Definition externalCreditTotal (amounts : list Z) : Z :=
  Stdlib.Lists.List.fold_right Z.add 0 amounts.

Definition supply_with_credits (world : W.t) (remaining : list Z) : Prop :=
  balanceInvariant world ∧
  Forall (fun amount ⇒ 0 ≤ amount) remaining ∧
  totalBalance world + externalCreditTotal remaining < 2^256.

Lemma external_credit_total_nonnegative amounts :
  Forall (fun amount ⇒ 0 ≤ amount) amounts →
  0 ≤ externalCreditTotal amounts.
Proof.
  unfold externalCreditTotal. intro Hamounts. induction Hamounts;
    cbn [Stdlib.Lists.List.fold_right]; lia.
Qed.

Lemma external_credit_total_app prefix suffix :
  externalCreditTotal (prefix ++ suffix) =
    externalCreditTotal prefix + externalCreditTotal suffix.
Proof.
  induction prefix as [|amount prefix IH].
  - reflexivity.
  - change (amount + externalCreditTotal (prefix ++ suffix) =
      amount + externalCreditTotal prefix + externalCreditTotal suffix).
    rewrite IH. lia.
Qed.

Lemma supply_with_credits_nil world :
  supply_with_credits world [] ↔ balanceInvariant world.
Proof.
  split.
  - intros [Hworld _]. exact Hworld.
  - intro Hworld. split; [exact Hworld|]. split; [constructor|].
    pose proof (proj2 (proj2 Hworld)).
    change (totalBalance world + 0 < 2^256). lia.
Qed.

Lemma supply_with_credits_prefix world prefix suffix :
  supply_with_credits world (prefix ++ suffix) →
  supply_with_credits world prefix.
Proof.
  intros [Hworld [Hamounts Hbound]].
  apply Forall_app in Hamounts as [Hprefix Hsuffix].
  pose proof (external_credit_total_nonnegative suffix Hsuffix).
  rewrite external_credit_total_app in Hbound.
  split; [exact Hworld|]. split; [exact Hprefix|lia].
Qed.

The increase bound may be strict: burns, reverts and unused declared rewards need not exhaust the prefix budget. This lemma takes a proved state invariant and total bound, not a hypothesis about VM behavior.
Theorem supply_with_credits_preserved world post prefix suffix :
  supply_with_credits world (prefix ++ suffix) →
  balanceInvariant post →
  totalBalance post ≤ totalBalance world + externalCreditTotal prefix →
  supply_with_credits post suffix.
Proof.
  intros [_ [Hamounts Hbound]] Hpost Hincrease.
  apply Forall_app in Hamounts as [_ Hsuffix].
  rewrite external_credit_total_app in Hbound.
  split; [exact Hpost|]. split; [exact Hsuffix|lia].
Qed.

Transactions and other supply-nonincreasing steps spend no external credit budget. Their concrete preservation proofs can be supplied here.
Corollary supply_with_credits_nonincreasing world post remaining :
  supply_with_credits world remaining →
  balanceInvariant post → totalBalance post ≤ totalBalance world →
  supply_with_credits post remaining.
Proof.
  intros Hworld Hpost Htotal.
  apply (supply_with_credits_preserved world post [] remaining).
  - exact Hworld.
  - exact Hpost.
  - change (totalBalance post ≤ totalBalance world + 0). lia.
Qed.

Lemma external_credit_total_withdrawals withdrawals :
  externalCreditTotal (Stdlib.Lists.List.map withdrawalWei withdrawals) =
    Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 withdrawals.
Proof.
  induction withdrawals as [|wd withdrawals IH]; [reflexivity|].
  change (withdrawalWei wd + externalCreditTotal (Stdlib.Lists.List.map withdrawalWei withdrawals) =
    withdrawalWei wd + Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 withdrawals).
  rewrite IH. reflexivity.
Qed.

A single initial bound covers both kinds of external credit. The reward amounts are declarations in wei; this does not decode or execute them. Rewards precede the end-of-block withdrawals in the remaining budget.
Lemma withdrawals_and_rewards_budget world withdrawals rewards :
  balanceInvariant world →
  Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (WD.t.amount wd) < 2^64) withdrawals →
  Forall (fun reward ⇒ 0 ≤ reward) rewards →
  totalBalance world +
    Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 withdrawals +
    externalCreditTotal rewards < 2^256 →
  supply_with_credits world (rewards ++ Stdlib.Lists.List.map withdrawalWei withdrawals).
Proof.
  intros Hworld Hwithdrawals Hrewards Hbound.
  split; [exact Hworld|]. split.
  - apply Forall_app. split; [exact Hrewards|].
    rewrite Forall_map. eapply Forall_impl; [|exact Hwithdrawals].
    intros wd Hamount. cbn beta in ×. unfold withdrawalWei. lia.
  - rewrite external_credit_total_app, external_credit_total_withdrawals. lia.
Qed.

Definition creditAccount (world : W.t) (beneficiary : Addr.t) (amount : U.t) : W.t :=
  State.op_pointcaretdollarlparenrparenltminus world
    (Lens.Infix.op_pipeminusminus (@W.account None beneficiary) A.balance)
    (fun balance ⇒ @U.op_plus balance amount).

A declared allowance can exceed the amount actually credited. The resulting balance invariant and every account's monotonicity are derived from the one aggregate budget, including for an empty recipient account.
Theorem credit_account_with_allowance world beneficiary amount allowance remaining :
  supply_with_credits world (allowance :: remaining) →
  0 ≤ U.to_z amount ≤ allowance →
  let post := creditAccount world beneficiary amount in
  supply_with_credits post remaining ∧
  totalBalance post = totalBalance world + U.to_z amount ∧
  ∀ query,
    U.to_z (A.t.balance (State.op_pointcaretlparenrparen world (@W.account None query))) ≤
    U.to_z (A.t.balance (State.op_pointcaretlparenrparen post (@W.account None query))).
Proof.
  intros Hbudget Hamount. cbn zeta.
  destruct Hbudget as [Hworld [Hcredits Hbound]].
  inversion Hcredits as [|? ? Hallowance Hremaining]; subst.
  pose proof (external_credit_total_nonnegative remaining Hremaining) as Hrest.
  change (totalBalance world + (allowance + externalCreditTotal remaining) < 2^256) in Hbound.
  assert (HcreditBound : totalBalance world + U.to_z amount < 2^256) by lia.
  pose proof (credit_preserves_invariant world beneficiary amount
    (proj1 Hworld) (proj1 (proj2 Hworld)) (proj1 Hamount) HcreditBound) as Hpost.
  pose proof (credit_total world beneficiary amount
    (proj1 (proj2 Hworld)) (proj1 Hamount) HcreditBound) as Htotal.
  change (balanceInvariant (creditAccount world beneficiary amount)) in Hpost.
  change (totalBalance (creditAccount world beneficiary amount) =
    totalBalance world + U.to_z amount) in Htotal.
  split.
  - apply (supply_with_credits_preserved world
      (creditAccount world beneficiary amount) [allowance] remaining).
    + split; [exact Hworld|]. split; [exact Hcredits|exact Hbound].
    + exact Hpost.
    + change (totalBalance (creditAccount world beneficiary amount) ≤
        totalBalance world + (allowance + 0)). lia.
  - split; [exact Htotal|]. intro query.
    apply credit_nondecreasing_from_supply; try exact (proj1 Hworld);
      try exact (proj1 (proj2 Hworld)); lia.
Qed.

Print Assumptions supply_with_credits_preserved.
Print Assumptions withdrawals_and_rewards_budget.
Print Assumptions credit_account_with_allowance.