Library QedkConcrete.credits

External credits in concrete block execution

Withdrawals are paid after transactions. The supply bound must include these credits, not just balances before the block. Under that bound their U256 addition is exact and cannot harm another account's fee solvency.
From Stdlib Require Import ZArith Lia List.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.worldaccounts QedkConcrete.supply.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Section Credit.
Context {U64 : Basics.Unreachable Numeric.U64.t}.
Context {U256 : Basics.Unreachable U.t}.

Lemma credit_preserves_invariant world addr amount :
  stringmaps.WellFormed (W.t.accounts world) →
  nonnegativeBalances world →
  0 ≤ U.to_z amount → totalBalance world + U.to_z amount < 2^256 →
  balanceInvariant (State.op_pointcaretdollarlparenrparenltminus world
    (Lens.Infix.op_pipeminusminus (@W.account None addr) A.balance)
    (fun balance ⇒ @U.op_plus balance amount)).
Proof.
  intros Hwf Hnonnegative Hamount Htotal.
  pose proof (@account_balance_bounded_by_supply world addr Hnonnegative) as Haccount.
  cbn beta in Haccount.
  split.
  - apply (@account_write_wellformed false). exact Hwf.
  - split.
    + apply (@account_write_nonnegative false); [exact Hnonnegative|].
      change (0 ≤ U.to_z (@U.op_plus
        (A.t.balance (State.op_pointcaretlparenrparen world (@W.account None addr))) amount)).
      rewrite u256_add.
      2: { split.
        - apply Z.add_nonneg_nonneg; [exact (proj1 Haccount)|exact Hamount].
        - eapply Z.le_lt_trans; [apply Z.add_le_mono_r; exact (proj2 Haccount)|exact Htotal]. }
      apply Z.add_nonneg_nonneg; [exact (proj1 Haccount)|exact Hamount].
    + rewrite credit_total by assumption. exact Htotal.
Qed.

End Credit.

Module E := MonadVM.Execution.Make.
Module B := MonadVM.State.BlockState.
Module WD := MonadVM.Chain.Ethereum.Withdrawal.

Definition withdrawalWei (wd : WD.t) : Z := Numeric.U64.to_z (WD.t.amount wd) × 1000000000.

Section Withdrawals.
Context {F : E.FArgs}.
Local Abbreviation readAccount := (fun world addr ⇒
  State.op_pointcaretlparenrparen world (@W.account None addr)).

Local Lemma withdrawal_amount amount :
  0 ≤ Numeric.U64.to_z amount < 2^64 →
  U.to_z (@U.op_star
    (@U.of_uint_exn (@Numeric.U64.to_uint amount))
    (@U.exp
      (@U.op_tildedollar 10) (@U.op_tildedollar 9))) =
    Numeric.U64.to_z amount × 1000000000.
Proof.
  intro Hrange.
  assert (Huint : @Numeric.U64.to_uint amount = Z.to_N (Numeric.U64.to_z amount)).
  { apply reservecheck.u256_to_uint. exact (proj1 Hrange). }
  rewrite Huint.
  assert (Hconvert : @U.of_uint_exn (Z.to_N (Numeric.U64.to_z amount)) = Numeric.U64.to_z amount).
  { change (U.to_z (@U.of_uint_exn (Z.to_N (Numeric.U64.to_z amount))) = Numeric.U64.to_z amount).
    rewrite reservecheck.u256_of_uint by (rewrite Z2N.id by lia; lia).
    rewrite Z2N.id by lia. reflexivity. }
  rewrite Hconvert.
  change (Z.land (Z.shiftl (Numeric.U64.to_z amount × 1000000000) 0) (Z.ones 256) =
    Numeric.U64.to_z amount × 1000000000).
  rewrite Z.shiftl_0_r, Z.land_ones by lia. apply Z.mod_small. lia.
Qed.

Lemma withdrawal_world block wd :
  0 ≤ Numeric.U64.to_z (WD.t.amount wd) < 2^64 →
  B.t.world_state (@E.process_withdrawal F block wd) =
  State.op_pointcaretdollarlparenrparenltminus (B.t.world_state block)
    (Lens.Infix.op_pipeminusminus (@W.account None (WD.t.recipient wd)) A.balance)
    (fun balance ⇒ @U.op_plus balance (withdrawalWei wd)).
Proof.
  intro Hrange. unfold E.process_withdrawal.
  pose proof (withdrawal_amount (WD.t.amount wd) Hrange) as Hamount.
  match type of Hamount with U.to_z ?value = ?wei ⇒ change (value = wei) in Hamount end.
  rewrite Hamount. reflexivity.
Qed.

Lemma withdrawal_effects block wd :
  stringmaps.WellFormed (W.t.accounts (B.t.world_state block)) →
  nonnegativeBalances (B.t.world_state block) →
  0 ≤ Numeric.U64.to_z (WD.t.amount wd) < 2^64 →
  totalBalance (B.t.world_state block) + withdrawalWei wd < 2^256 →
  let after := B.t.world_state (@E.process_withdrawal F block wd) in
  balanceInvariant after ∧
  totalBalance after = totalBalance (B.t.world_state block) + withdrawalWei wd ∧
  ∀ query, U.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
    U.to_z (A.t.balance (readAccount after query)).
Proof.
  intros Hwf Hnonnegative Hamount Htotal. cbn zeta.
  rewrite withdrawal_world by exact Hamount.
  assert (Hwei : 0 ≤ U.to_z (withdrawalWei wd)).
  { change (0 ≤ Numeric.U64.to_z (WD.t.amount wd) × 1000000000). lia. }
  split.
  - apply credit_preserves_invariant; assumption.
  - split.
    + apply credit_total; assumption.
    + intro query. apply credit_nondecreasing_from_supply; assumption.
Qed.

Lemma withdrawal_sum_nonnegative withdrawals :
  Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (WD.t.amount wd) < 2^64) withdrawals →
  0 ≤ Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 withdrawals.
Proof.
  intro Hvalid. induction Hvalid as [|wd tail Hamount Htail IH].
  - cbn. lia.
  - change (0 ≤ withdrawalWei wd +
      Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 tail).
    unfold withdrawalWei at 1. lia.
Qed.

One budget for all withdrawals suffices: paying a prefix leaves exactly the unused credit budget for the suffix, including repeated recipients.
Lemma withdrawals_preserve_supply_bound block withdrawals :
  balanceInvariant (B.t.world_state block) →
  Forall (fun wd ⇒ 0 ≤ Numeric.U64.to_z (WD.t.amount wd) < 2^64) withdrawals →
  let credits := Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 withdrawals in
  totalBalance (B.t.world_state block) + credits < 2^256 →
  let after := B.t.world_state (OCamlList.fold_left (@E.process_withdrawal F) block withdrawals) in
  balanceInvariant after ∧
  totalBalance after = totalBalance (B.t.world_state block) + credits ∧
  ∀ query, U.to_z (A.t.balance (readAccount (B.t.world_state block) query)) ≤
    U.to_z (A.t.balance (readAccount after query)).
Proof.
  revert block. induction withdrawals as [|wd tail IH];
    intros block Hinvariant Hvalid; cbn zeta; intro Htotal;
    cbn [OCamlList.fold_left Stdlib.Lists.List.fold_right] in ×.
  - split; [exact Hinvariant|]. split; [lia|]. intro query. apply Z.le_refl.
  - inversion Hvalid as [|? ? Hamount Htail]; subst.
    pose proof (withdrawal_sum_nonnegative tail Htail) as Hremaining.
    assert (HnextBudget : totalBalance (B.t.world_state block) + withdrawalWei wd < 2^256) by lia.
    destruct (withdrawal_effects block wd (proj1 Hinvariant) (proj1 (proj2 Hinvariant))
      Hamount HnextBudget) as [Hnext [HnextTotal HnextBalances]].
    assert (HtailBudget : totalBalance (B.t.world_state (@E.process_withdrawal F block wd)) +
      Stdlib.Lists.List.fold_right (fun wd total ⇒ withdrawalWei wd + total) 0 tail < 2^256).
    { rewrite HnextTotal. lia. }
    destruct (IH (@E.process_withdrawal F block wd) Hnext Htail HtailBudget)
      as [Hafter [HafterTotal HafterBalances]].
    split; [exact Hafter|]. split.
    + rewrite HafterTotal, HnextTotal. lia.
    + intro query. eapply Z.le_trans; [apply HnextBalances|apply HafterBalances].
Qed.

End Withdrawals.

Print Assumptions withdrawal_effects.
Print Assumptions withdrawals_preserve_supply_bound.