Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Spending distinct present inputs removes exactly valueIn

Proved
BitcoinUtxo.totalValue_spendInputs

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

bitcoinbitcoin-mainnetconsensusformal-verificationutxovalue-conservation

For every view UUU with pairwise distinct outpoints and list of outpoints III that is itself duplicate-free and all of whose members are present in UUU,

totalValue(spendInputs(U,I))=totalValue(U)−valueIn(U,I).\mathrm{totalValue}(\mathrm{spendInputs}(U, I)) = \mathrm{totalValue}(U) - \mathrm{valueIn}(U, I).totalValue(spendInputs(U,I))=totalValue(U)−valueIn(U,I).

Without distinctness of III the identity fails: a repeated outpoint is counted twice in valueIn but removed once.

Preamble
import Definitions.Def_BitcoinUtxo
open BitcoinUtxo
Formal statement
theorem BitcoinUtxo.totalValue_spendInputs {T : Type} [DecidableEq T] (u : UtxoSet T) :
    ∀ (ops : List (OutPoint T)), keysNodup u → ops.Nodup → (∀ op ∈ ops, haveCoin u op = true) →
      totalValue (spendInputs u ops) = totalValue u - valueIn u ops := 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.totalValue_spendInputs.

Let TTT be any type with decidable equality. A UTXO set uuu over TTT is a finite ordered list of pairs (p,c)(p, c)(p,c) with ppp an outpoint over TTT (a transaction id in TTT and an index in N\mathbb{N}N) and ccc a coin (whose output value is the integer c.out.valuec.\mathrm{out}.\mathrm{value}c.out.value). Write u[p]u[p]u[p] for the coin of the first entry of uuu with outpoint ppp, or "none" if there is none; haveCoin(u,p)\mathrm{haveCoin}(u, p)haveCoin(u,p) is the Boolean "u[p]u[p]u[p] is not none". Write tv(u)\mathrm{tv}(u)tv(u) for the integer sum of c.out.valuec.\mathrm{out}.\mathrm{value}c.out.value over all entries of uuu (with multiplicity, no range restriction, tv([ ])=0\mathrm{tv}([\,]) = 0tv([])=0). keysNodup(u)\mathrm{keysNodup}(u)keysNodup(u) is the proposition that the outpoints of uuu (first components, in list order) have no duplicates. spendCoin(u,p)\mathrm{spendCoin}(u, p)spendCoin(u,p) removes every entry of outpoint ppp from uuu, and for a list of outpoints ops=[p1,…,pm]\mathrm{ops} = [p_1, \dots, p_m]ops=[p1​,…,pm​],

spendInputs(u,ops)=spendCoin(⋯spendCoin(spendCoin(u,p1),p2)⋯ ,pm)\mathrm{spendInputs}(u, \mathrm{ops}) = \mathrm{spendCoin}(\cdots \mathrm{spendCoin}(\mathrm{spendCoin}(u, p_1), p_2) \cdots, p_m)spendInputs(u,ops)=spendCoin(⋯spendCoin(spendCoin(u,p1​),p2​)⋯,pm​)

(sequential removal, left to right; spendInputs(u,[ ])=u\mathrm{spendInputs}(u, [\,]) = uspendInputs(u,[])=u). Finally,

valueIn(u,ops)=∑i=1m{ci.out.valueif u[pi]=some ci,0if u[pi]=none,\mathrm{valueIn}(u, \mathrm{ops}) = \sum_{i=1}^{m} \begin{cases} c_i.\mathrm{out}.\mathrm{value} & \text{if } u[p_i] = \text{some } c_i,\\ 0 & \text{if } u[p_i] = \text{none},\end{cases}valueIn(u,ops)=i=1∑m​{ci​.out.value0​if u[pi​]=some ci​,if u[pi​]=none,​

the sum being over the list with multiplicity and equal to 000 for the empty list.

The theorem states: for every type TTT with decidable equality and every UTXO set uuu over TTT, for every finite list ops\mathrm{ops}ops of outpoints over TTT, if

  • keysNodup(u)\mathrm{keysNodup}(u)keysNodup(u) (the outpoints of uuu are pairwise distinct), and
  • the list ops\mathrm{ops}ops has no duplicate outpoints (as a list over the outpoint type, i.e. no two positions hold the same pair ⟨txid,n⟩\langle \mathrm{txid}, n\rangle⟨txid,n⟩), and
  • for every outpoint ppp occurring in ops\mathrm{ops}ops, haveCoin(u,p)=true\mathrm{haveCoin}(u, p) = \mathrm{true}haveCoin(u,p)=true (every listed outpoint has an entry in uuu),

then

tv(spendInputs(u,ops))=tv(u)−valueIn(u,ops),\mathrm{tv}\big(\mathrm{spendInputs}(u, \mathrm{ops})\big) = \mathrm{tv}(u) - \mathrm{valueIn}(u, \mathrm{ops}),tv(spendInputs(u,ops))=tv(u)−valueIn(u,ops),

an exact equality in Z\mathbb{Z}Z.

The quantifier over ops\mathrm{ops}ops is inside the statement, after uuu is fixed; ops\mathrm{ops}ops may be empty, in which case the third hypothesis is vacuous and the conclusion reads tv(u)=tv(u)−0\mathrm{tv}(u) = \mathrm{tv}(u) - 0tv(u)=tv(u)−0. No sign or range condition is imposed on any coin value.

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