Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Adding a coin at a fresh outpoint adds its value, or nothing if unspendable

Proved
BitcoinUtxo.totalValue_add_fresh

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

bitcoinbitcoin-mainnetconsensusformal-verificationutxovalue-conservation

For every view UUU, outpoint ppp absent from UUU, and coin ccc,

totalValue(addCoin(U,p,c))=totalValue(U)+{0c unspendablec.valueotherwise.\mathrm{totalValue}(\mathrm{addCoin}(U, p, c)) = \mathrm{totalValue}(U) + \begin{cases} 0 & c \text{ unspendable} \\ c.\mathrm{value} & \text{otherwise.}\end{cases}totalValue(addCoin(U,p,c))=totalValue(U)+{0c.value​c unspendableotherwise.​

This is AddCoin on a fresh outpoint: an OP_RETURN-style output is dropped, any other is stored.

Preamble
import Definitions.Def_BitcoinUtxo
open BitcoinUtxo
Formal statement
theorem BitcoinUtxo.totalValue_add_fresh {T : Type} [DecidableEq T] (u : UtxoSet T) (op : OutPoint T) (c : Coin)
    (h : getCoin u op = none) :
    totalValue (addCoin u op c) = totalValue u + (if c.out.unspendable then 0 else c.out.value) := 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_add_fresh.

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 p=⟨txid,n⟩p = \langle \mathrm{txid}, n\ranglep=⟨txid,n⟩ an outpoint (a transaction id in TTT and an index n∈Nn \in \mathbb{N}n∈N) and ccc a coin (a record with an output c.outc.\mathrm{out}c.out, itself a record with an integer c.out.value∈Zc.\mathrm{out}.\mathrm{value} \in \mathbb{Z}c.out.value∈Z and a Boolean c.out.unspendablec.\mathrm{out}.\mathrm{unspendable}c.out.unspendable, together with 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" if no entry has outpoint ppp. 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 (p,c)(p, c)(p,c) of uuu (every entry counted, no range restriction, tv([ ])=0\mathrm{tv}([\,]) = 0tv([])=0). Write spendCoin(u,p)\mathrm{spendCoin}(u, p)spendCoin(u,p) for uuu with every entry of outpoint ppp removed. The operation addCoin(u,p,c)\mathrm{addCoin}(u, p, c)addCoin(u,p,c) is defined as

addCoin(u,p,c)={uif c.out.unspendable=true,(p,c)::spendCoin(u,p)if c.out.unspendable=false,\mathrm{addCoin}(u, p, c) = \begin{cases} u & \text{if } c.\mathrm{out}.\mathrm{unspendable} = \mathrm{true},\\ (p, c) :: \mathrm{spendCoin}(u, p) & \text{if } c.\mathrm{out}.\mathrm{unspendable} = \mathrm{false},\end{cases}addCoin(u,p,c)={u(p,c)::spendCoin(u,p)​if c.out.unspendable=true,if c.out.unspendable=false,​

i.e. an unspendable coin leaves uuu untouched, while a spendable one first deletes all entries at ppp and then prepends (p,c)(p, c)(p,c).

The theorem states: for every type TTT with decidable equality, every UTXO set uuu over TTT, every outpoint ppp over TTT and every coin ccc, if u[p]=noneu[p] = \text{none}u[p]=none (no entry of uuu has outpoint ppp), then

tv(addCoin(u,p,c))=tv(u)+{0if c.out.unspendable=true,c.out.valueotherwise,\mathrm{tv}\big(\mathrm{addCoin}(u, p, c)\big) = \mathrm{tv}(u) + \begin{cases} 0 & \text{if } c.\mathrm{out}.\mathrm{unspendable} = \mathrm{true},\\ c.\mathrm{out}.\mathrm{value} & \text{otherwise,}\end{cases}tv(addCoin(u,p,c))=tv(u)+{0c.out.value​if c.out.unspendable=true,otherwise,​

an exact equality of integers.

There is no hypothesis that the outpoints of uuu are pairwise distinct (no keysNodup), and no restriction on the sign or size of c.out.valuec.\mathrm{out}.\mathrm{value}c.out.value or of the values in uuu; uuu may be empty. The hypothesis u[p]=noneu[p] = \text{none}u[p]=none guarantees that spendCoin(u,p)\mathrm{spendCoin}(u, p)spendCoin(u,p) removes nothing.

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