Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The replacement mission's GetFee is EvaluateFeeUp

Proved
BitcoinFeerateDiagram.getFee_eq_evaluateFeeUp

by pitimespi · Oct 29, 2026 · Mathlib 0df444a (Lean v4.33.1)

bitcoincluster-mempoolfeefracfeerate-diagramformal-verificationmathlibmempoolpolicyrbf

r.getFee v = evaluateFeeUp ⟨r.fee, r.size⟩ v, linking the two missions' fee arithmetic.

Preamble
import Definitions.Def_BitcoinFeerateDiagram
open BitcoinFeerateDiagram BitcoinReplacement
Formal statement
theorem BitcoinFeerateDiagram.getFee_eq_evaluateFeeUp (r : FeeRate) (v : ℕ) :
    (r.getFee v : ℤ) = FeeFrac.evaluateFeeUp ⟨r.fee, r.size⟩ v := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: util/feefrac.h (FeeFrac :20, MulFallback :25-30, DivFallback :40-59, Mul/Div :64-82, operator+/- :120-131, EvaluateFee :147-175, ByRatio :217-240), util/overflow.h (CeilDiv :70-74), util/feefrac.cpp (CompareChunks :12-75), policy/rbf.cpp (ImprovesFeerateDiagram :127-140, PaysForRBF :100-125): https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/util/feefrac.cpp; test vectors from src/test/rbf_tests.cpp (feerate_chunks_utilities) and src/test/feefrac_tests.cpp
Read-back

What the Lean code literally says, in plain math · claude-fable-5-1 (independent sub-agent, blind: Lean code + auditor instructions only)

Read-back: BitcoinFeerateDiagram.getFee_eq_evaluateFeeUp

Statement. For every FeeRate rrr — natural numbers fee(r)≥0\mathrm{fee}(r) \ge 0fee(r)≥0 and size(r)\mathrm{size}(r)size(r) with the built-in condition size(r)>0\mathrm{size}(r) > 0size(r)>0 — and every natural number vvv,

getFee(r,v)=evaluateFeeUp((fee(r),size(r)), v),\mathrm{getFee}(r, v) = \mathrm{evaluateFeeUp}\big((\mathrm{fee}(r), \mathrm{size}(r)),\ v\big),getFee(r,v)=evaluateFeeUp((fee(r),size(r)), v),

where the left side, a natural number, is regarded as an integer, and on the right (fee(r),size(r))(\mathrm{fee}(r), \mathrm{size}(r))(fee(r),size(r)) is the FeeFrac whose fee and size are fee(r)\mathrm{fee}(r)fee(r) and size(r)\mathrm{size}(r)size(r) regarded as integers, and vvv is regarded as an integer.

Unfolded: the left side is (fee(r) v+size(r)−1)/size(r)\big(\mathrm{fee}(r)\,v + \mathrm{size}(r) - 1\big) \mathbin{/} \mathrm{size}(r)(fee(r)v+size(r)−1)/size(r) (floor division of natural numbers). Since fee(r)≥0\mathrm{fee}(r) \ge 0fee(r)≥0, the right side is

{ceilDiv(fee(r) v, size(r))=fee(r) v/size(r)+[ fee(r) v mod size(r)≠0 ]if fee(r)<233,div(fee(r) v, size(r), false)if fee(r)≥233.\begin{cases}\mathrm{ceilDiv}\big(\mathrm{fee}(r)\,v,\ \mathrm{size}(r)\big) = \mathrm{fee}(r)\,v \mathbin{/} \mathrm{size}(r) + [\,\mathrm{fee}(r)\,v \bmod \mathrm{size}(r) \ne 0\,] & \text{if } \mathrm{fee}(r) < 2^{33},\\ \mathrm{div}\big(\mathrm{fee}(r)\,v,\ \mathrm{size}(r),\ \text{false}\big) & \text{if } \mathrm{fee}(r) \ge 2^{33}.\end{cases}{ceilDiv(fee(r)v, size(r))=fee(r)v/size(r)+[fee(r)vmodsize(r)=0]div(fee(r)v, size(r), false)​if fee(r)<233,if fee(r)≥233.​

Binders and edge cases. There are no hypotheses beyond those built into the types: fee(r)\mathrm{fee}(r)fee(r) and vvv are arbitrary natural numbers (including 000, and arbitrarily large — no word-size bounds), and size(r)\mathrm{size}(r)size(r) is a positive natural number. Negative fees cannot occur on either side since the FeeRate fee is a natural number.

Definitions used, unfolded

Division conventions. Three kinds of division occur.

  • Default integer division n/dn \mathbin{/} dn/d with remainder n mod dn \bmod dnmodd (Euclidean convention). For d>0d > 0d>0: n/d=⌊n/d⌋n / d = \lfloor n/d \rfloorn/d=⌊n/d⌋ and n mod d=n−d⌊n/d⌋∈[0,d)n \bmod d = n - d\lfloor n/d \rfloor \in [0, d)nmodd=n−d⌊n/d⌋∈[0,d), also for negative nnn (e.g. −7/2=−4-7/2 = -4−7/2=−4, −7 mod 2=1-7 \bmod 2 = 1−7mod2=1). For d<0d < 0d<0: n/d=−⌊n/∣d∣⌋n / d = -\lfloor n/|d| \rfloorn/d=−⌊n/∣d∣⌋ and n mod d∈[0,∣d∣)n \bmod d \in [0, |d|)nmodd∈[0,∣d∣). For d=0d = 0d=0: n/0=0n / 0 = 0n/0=0 and n mod 0=nn \bmod 0 = nnmod0=n.
  • Truncated division tdiv⁡(n,d)\operatorname{tdiv}(n,d)tdiv(n,d) with remainder tmod⁡(n,d)=n−d⋅tdiv⁡(n,d)\operatorname{tmod}(n,d) = n - d\cdot\operatorname{tdiv}(n,d)tmod(n,d)=n−d⋅tdiv(n,d): the quotient is rounded toward zero, so the remainder is 000 or has the sign of nnn, with ∣tmod⁡(n,d)∣<∣d∣|\operatorname{tmod}(n,d)| < |d|∣tmod(n,d)∣<∣d∣ (e.g. tdiv⁡(−7,2)=−3\operatorname{tdiv}(-7,2) = -3tdiv(−7,2)=−3, tmod⁡(−7,2)=−1\operatorname{tmod}(-7,2) = -1tmod(−7,2)=−1). For d=0d = 0d=0: tdiv⁡(n,0)=0\operatorname{tdiv}(n,0) = 0tdiv(n,0)=0, tmod⁡(n,0)=n\operatorname{tmod}(n,0) = ntmod(n,0)=n.
  • Rational division in Q\mathbb QQ, with the convention q/0=0q / 0 = 0q/0=0. For q∈Qq \in \mathbb Qq∈Q, ⌊q⌋\lfloor q \rfloor⌊q⌋ is the greatest integer ≤q\le q≤q and ⌈q⌉\lceil q \rceil⌈q⌉ the least integer ≥q\ge q≥q.

Below, [P][P][P] denotes 111 if the condition PPP holds and 000 otherwise.

FeeRate and getFee (from the imported replacement file). A FeeRate rrr consists of natural numbers fee(r)≥0\mathrm{fee}(r) \ge 0fee(r)≥0 and size(r)\mathrm{size}(r)size(r) with the built-in requirement size(r)>0\mathrm{size}(r) > 0size(r)>0. For v∈Nv \in \mathbb Nv∈N,

getFee(r,v)=(fee(r)⋅v+(size(r)−1))/size(r)\mathrm{getFee}(r, v) = \big(\mathrm{fee}(r)\cdot v + (\mathrm{size}(r) - 1)\big) \mathbin{/} \mathrm{size}(r)getFee(r,v)=(fee(r)⋅v+(size(r)−1))/size(r)

computed in the natural numbers with floor division (size(r)−1≥0\mathrm{size}(r) - 1 \ge 0size(r)−1≥0 since size(r)>0\mathrm{size}(r) > 0size(r)>0).

FeeFrac. A FeeFrac is a pair X=(fee(X),size(X))X = (\mathrm{fee}(X), \mathrm{size}(X))X=(fee(X),size(X)) of two arbitrary integers. Nothing constrains them: either component may be zero or negative, and no machine-word bounds are imposed. Addition and subtraction are componentwise,

X±Y=(fee(X)±fee(Y), size(X)±size(Y)),X \pm Y = \big(\mathrm{fee}(X) \pm \mathrm{fee}(Y),\ \mathrm{size}(X) \pm \mathrm{size}(Y)\big),X±Y=(fee(X)±fee(Y), size(X)±size(Y)),

and the zero FeeFrac is (0,0)(0,0)(0,0). A chunk list is a finite list c=[c1,…,cn]c = [c_1, \dots, c_n]c=[c1​,…,cn​] of FeeFracs; the empty list (n=0n = 0n=0) is allowed.

mul. mul(a,b)=a⋅b\mathrm{mul}(a, b) = a \cdot bmul(a,b)=a⋅b, the exact product of two integers.

div. For integers n,dn, dn,d and a boolean ρ\rhoρ ("round down"), with q=tdiv⁡(n,d)q = \operatorname{tdiv}(n,d)q=tdiv(n,d) and r=tmod⁡(n,d)r = \operatorname{tmod}(n,d)r=tmod(n,d) (truncated division):

div(n,d,ρ)=q+[ r>0 ]−[ r≠0 and ρ ].\mathrm{div}(n, d, \rho) = q + [\,r > 0\,] - [\,r \neq 0 \text{ and } \rho\,].div(n,d,ρ)=q+[r>0]−[r=0 and ρ].

Case by case: if r=0r = 0r=0 the result is qqq; if r>0r > 0r>0 it is qqq when ρ\rhoρ is true and q+1q + 1q+1 when ρ\rhoρ is false; if r<0r < 0r<0 it is q−1q - 1q−1 when ρ\rhoρ is true and qqq when ρ\rhoρ is false. The definition places no condition on ddd: for d=0d = 0d=0 it gives q=0q = 0q=0, r=nr = nr=n, hence [n>0]−[n≠0 and ρ][n > 0] - [n \ne 0 \text{ and } \rho][n>0]−[n=0 and ρ]; for d<0d < 0d<0 the remainder still carries the sign of nnn.

ceilDiv. For integers n,dn, dn,d, with default (Euclidean) integer division:

ceilDiv(n,d)=n/d+[ n mod d≠0 ].\mathrm{ceilDiv}(n, d) = n / d + [\,n \bmod d \ne 0\,].ceilDiv(n,d)=n/d+[nmodd=0].

The definition is stated for all integers (not only nonnegative ones); for d=0d = 0d=0 it gives [ n≠0 ][\,n \ne 0\,][n=0].

evaluateFee, evaluateFeeDown, evaluateFeeUp. For a FeeFrac XXX, an integer ttt ("at size") and a boolean ρ\rhoρ:

evaluateFee(X,t,ρ)={(fee(X)⋅t)/size(X)if 0≤fee(X)<233 and ρ is true,ceilDiv(fee(X)⋅t, size(X))if 0≤fee(X)<233 and ρ is false,div(mul(fee(X),t), size(X), ρ)otherwise (fee(X)<0 or fee(X)≥233),\mathrm{evaluateFee}(X, t, \rho) = \begin{cases} \big(\mathrm{fee}(X)\cdot t\big) \mathbin{/} \mathrm{size}(X) & \text{if } 0 \le \mathrm{fee}(X) < 2^{33} \text{ and } \rho \text{ is true},\\ \mathrm{ceilDiv}\big(\mathrm{fee}(X)\cdot t,\ \mathrm{size}(X)\big) & \text{if } 0 \le \mathrm{fee}(X) < 2^{33} \text{ and } \rho \text{ is false},\\ \mathrm{div}\big(\mathrm{mul}(\mathrm{fee}(X), t),\ \mathrm{size}(X),\ \rho\big) & \text{otherwise (} \mathrm{fee}(X) < 0 \text{ or } \mathrm{fee}(X) \ge 2^{33}\text{)},\end{cases}evaluateFee(X,t,ρ)=⎩⎨⎧​(fee(X)⋅t)/size(X)ceilDiv(fee(X)⋅t, size(X))div(mul(fee(X),t), size(X), ρ)​if 0≤fee(X)<233 and ρ is true,if 0≤fee(X)<233 and ρ is false,otherwise (fee(X)<0 or fee(X)≥233),​

where 233=85899345922^{33} = 8589934592233=8589934592, the "///" in the first line is the default (Euclidean) integer division, and mul(a,b)=ab\mathrm{mul}(a,b) = abmul(a,b)=ab. Then evaluateFeeDown(X,t)=evaluateFee(X,t,true)\mathrm{evaluateFeeDown}(X, t) = \mathrm{evaluateFee}(X, t, \text{true})evaluateFeeDown(X,t)=evaluateFee(X,t,true) and evaluateFeeUp(X,t)=evaluateFee(X,t,false)\mathrm{evaluateFeeUp}(X, t) = \mathrm{evaluateFee}(X, t, \text{false})evaluateFeeUp(X,t)=evaluateFee(X,t,false). The definition itself imposes no condition on size(X)\mathrm{size}(X)size(X) or ttt.

Human review
  • Endorsed by Shuze Chen · Oct 30, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 30, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me