Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

CheckTxInputs returns the fee, in range, with all inputs present

Proved
BitcoinUtxo.checkTxInputs_some

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

bitcoinbitcoin-mainnetconsensusformal-verificationutxovalue-conservation

For every view UUU, transaction txtxtx, spend height hhh and amount fff with checkTxInputs(U,tx,h)=some(f)\mathrm{checkTxInputs}(U, tx, h) = \mathrm{some}(f)checkTxInputs(U,tx,h)=some(f):

f=valueIn(U,tx.vin)−valueOut(tx),0≤f≤MAX_MONEY,f = \mathrm{valueIn}(U, tx.\mathrm{vin}) - \mathrm{valueOut}(tx), \qquad 0 \le f \le \mathrm{MAX\_MONEY},f=valueIn(U,tx.vin)−valueOut(tx),0≤f≤MAX_MONEY,

and every input outpoint of txtxtx is present in UUU. The fee returned through txfee is exactly the difference of input and output values, and the input loop guarantees that every input it summed exists.

Preamble
import Definitions.Def_BitcoinUtxo
open BitcoinUtxo
Formal statement
theorem BitcoinUtxo.checkTxInputs_some {T : Type} [DecidableEq T] (u : UtxoSet T) (tx : Tx T) (h : Nat) (fee : Int)
    (hfee : checkTxInputs u tx h = some fee) :
    fee = valueIn u tx.vin - valueOut tx ∧ 0 ≤ fee ∧ fee ≤ MAX_MONEY ∧
    ∀ op ∈ tx.vin, haveCoin u op = true := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: CheckTransaction, src/consensus/tx_check.cpp lines 11-68: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/consensus/tx_check.cpp; Consensus::CheckTxInputs, src/consensus/tx_verify.cpp (lines 157-220): https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/consensus/tx_verify.cpp; CCoinsViewCache::AddCoin, AddCoins, SpendCoin, HaveInputs, src/coins.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/coins.cpp; UpdateCoins, BIP30 check (lines 2396-2475), fee accumulation and coinbase bound in Chainstate::ConnectBlock (lines 2518-2612), src/validation.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/validation.cpp; COIN, MAX_MONEY, MoneyRange, src/consensus/amount.h; COINBASE_MATURITY, src/consensus/consensus.h
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 of BitcoinUtxo.checkTxInputs_some.

Let TTT be any type with decidable equality. A UTXO set uuu over TTT is a finite ordered list of pairs (outpoint, coin), where an outpoint is ⟨txid,n⟩\langle \mathrm{txid}, n\rangle⟨txid,n⟩ with txid∈T\mathrm{txid} \in Ttxid∈T, n∈Nn \in \mathbb{N}n∈N, and a coin ccc is a record with an output c.outc.\mathrm{out}c.out (an integer c.out.valuec.\mathrm{out}.\mathrm{value}c.out.value and a Boolean c.out.unspendablec.\mathrm{out}.\mathrm{unspendable}c.out.unspendable), a height c.height∈Nc.\mathrm{height} \in \mathbb{N}c.height∈N and a Boolean c.isCoinbasec.\mathrm{isCoinbase}c.isCoinbase. Write u[p]u[p]u[p] for the coin of the first entry of uuu with outpoint ppp (or "none"), and haveCoin(u,p)\mathrm{haveCoin}(u, p)haveCoin(u,p) for the Boolean "u[p]u[p]u[p] is not none". A transaction tx\mathrm{tx}tx has fields txid∈T\mathrm{txid} \in Ttxid∈T, vin\mathrm{vin}vin (a list of outpoints), vout\mathrm{vout}vout (a list of outputs) and a Boolean isCoinbase\mathrm{isCoinbase}isCoinbase. Let MAX_MONEY=2.1×1015\mathrm{MAX\_MONEY} = 2.1 \times 10^{15}MAX_MONEY=2.1×1015 and moneyRange(v)  ⟺  0≤v≤MAX_MONEY\mathrm{moneyRange}(v) \iff 0 \le v \le \mathrm{MAX\_MONEY}moneyRange(v)⟺0≤v≤MAX_MONEY. Set

valueOut(tx)=∑o∈tx.vouto.value,valueIn(u,ops)=∑p∈ops{c.out.valueu[p]=some c0u[p]=none\mathrm{valueOut}(\mathrm{tx}) = \sum_{o \in \mathrm{tx}.\mathrm{vout}} o.\mathrm{value}, \qquad \mathrm{valueIn}(u, \mathrm{ops}) = \sum_{p \in \mathrm{ops}} \begin{cases} c.\mathrm{out}.\mathrm{value} & u[p] = \text{some } c\\ 0 & u[p] = \text{none}\end{cases}valueOut(tx)=o∈tx.vout∑​o.value,valueIn(u,ops)=p∈ops∑​{c.out.value0​u[p]=some cu[p]=none​

(both sums over the lists with multiplicity; 000 for empty lists).

The function checkTxInputs(u,tx,h)\mathrm{checkTxInputs}(u, \mathrm{tx}, h)checkTxInputs(u,tx,h), for a spend height h∈Nh \in \mathbb{N}h∈N, returns an optional integer as follows.

  • If haveInputs(u,tx)\mathrm{haveInputs}(u, \mathrm{tx})haveInputs(u,tx) is false, i.e. tx.isCoinbase=false\mathrm{tx}.\mathrm{isCoinbase} = \mathrm{false}tx.isCoinbase=false and some p∈tx.vinp \in \mathrm{tx}.\mathrm{vin}p∈tx.vin has haveCoin(u,p)=false\mathrm{haveCoin}(u, p) = \mathrm{false}haveCoin(u,p)=false: none.
  • Otherwise run the input loop over tx.vin=[p1,…,pm]\mathrm{tx}.\mathrm{vin} = [p_1, \dots, p_m]tx.vin=[p1​,…,pm​] with accumulator a0=0a_0 = 0a0​=0: at step iii, if u[pi]=u[p_i] = u[pi​]= none, fail; if u[pi]=u[p_i] = u[pi​]= some cic_ici​ and ci.isCoinbase=truec_i.\mathrm{isCoinbase} = \mathrm{true}ci​.isCoinbase=true and h−˙ci.height<100h \mathbin{\dot-} c_i.\mathrm{height} < 100h−˙​ci​.height<100 (truncated natural subtraction, so this holds in particular whenever ci.height≥hc_i.\mathrm{height} \ge hci​.height≥h), fail; else set ai=ai−1+ci.out.valuea_i = a_{i-1} + c_i.\mathrm{out}.\mathrm{value}ai​=ai−1​+ci​.out.value and fail if moneyRange(ci.out.value)\mathrm{moneyRange}(c_i.\mathrm{out}.\mathrm{value})moneyRange(ci​.out.value) is false or moneyRange(ai)\mathrm{moneyRange}(a_i)moneyRange(ai​) is false. If any step fails: none. Otherwise vin=amv_{\mathrm{in}} = a_mvin​=am​ (which equals valueIn(u,tx.vin)\mathrm{valueIn}(u, \mathrm{tx}.\mathrm{vin})valueIn(u,tx.vin), since every u[pi]u[p_i]u[pi​] is some coin).
  • Let vout=valueOut(tx)v_{\mathrm{out}} = \mathrm{valueOut}(\mathrm{tx})vout​=valueOut(tx). If vin<voutv_{\mathrm{in}} < v_{\mathrm{out}}vin​<vout​: none. Else let fee=vin−vout\mathrm{fee} = v_{\mathrm{in}} - v_{\mathrm{out}}fee=vin​−vout​; if moneyRange(fee)\mathrm{moneyRange}(\mathrm{fee})moneyRange(fee) is false: none; else some fee\mathrm{fee}fee.

The theorem states: for every type TTT with decidable equality, every UTXO set uuu over TTT, every transaction tx\mathrm{tx}tx over TTT, every height h∈Nh \in \mathbb{N}h∈N and every integer fee\mathrm{fee}fee, if checkTxInputs(u,tx,h)=some fee\mathrm{checkTxInputs}(u, \mathrm{tx}, h) = \text{some } \mathrm{fee}checkTxInputs(u,tx,h)=some fee, then all four of the following hold:

  1. fee=valueIn(u,tx.vin)−valueOut(tx)\mathrm{fee} = \mathrm{valueIn}(u, \mathrm{tx}.\mathrm{vin}) - \mathrm{valueOut}(\mathrm{tx})fee=valueIn(u,tx.vin)−valueOut(tx) (exact integer equality);
  2. 0≤fee0 \le \mathrm{fee}0≤fee;
  3. fee≤MAX_MONEY\mathrm{fee} \le \mathrm{MAX\_MONEY}fee≤MAX_MONEY;
  4. for every outpoint ppp occurring in tx.vin\mathrm{tx}.\mathrm{vin}tx.vin, haveCoin(u,p)=true\mathrm{haveCoin}(u, p) = \mathrm{true}haveCoin(u,p)=true.

There is no hypothesis that the outpoints of uuu are distinct, none that tx.vin\mathrm{tx}.\mathrm{vin}tx.vin has no duplicates (a repeated input contributes its coin's value once per occurrence both in the loop and in valueIn\mathrm{valueIn}valueIn), and no hypothesis on tx.isCoinbase\mathrm{tx}.\mathrm{isCoinbase}tx.isCoinbase: a coinbase-flagged transaction is covered too, and for it conclusion 4 still follows because the input loop itself requires every u[pi]u[p_i]u[pi​] to be present. If tx.vin\mathrm{tx}.\mathrm{vin}tx.vin is empty, conclusion 4 is vacuous and valueIn=0\mathrm{valueIn} = 0valueIn=0. The conclusion does not mention the maturity test or the individual coin-value range tests, though the hypothesis entails them.

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

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Oct 26, 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