Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

CheckBlock characterized

Proved
BitcoinBlock.checkBlock_iff

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

bip30bip34bitcoinbitcoin-mainnetblock-validationconnectblockconsensusformal-verificationundo

checkBlock b = true iff the proof of work holds and BlockSpec b: non-empty, merkle root matching and unmutated, size limits, exactly the first transaction a coinbase, every transaction well formed, legacy sigops within 80,000.

Deprecated. Superseded by BitcoinBlockV2.checkBlock_iff (revision 2 of the block mission): the p2shSigOpCount of this revision counted every push of scriptSig instead of only the redeem script, over-counting P2SH sigops; the V2 declarations carry the corrected definition.

Preamble
import Definitions.Def_BitcoinBlock
import Definitions.Def_BitcoinSer
Formal statement
theorem BitcoinBlock.checkBlock_iff (b : BitcoinSer.Block) :
    BitcoinBlock.checkBlock b = true ↔ BitcoinBlock.checkHeaderPow b.header = true ∧ BitcoinBlock.BlockSpec b := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: AcceptBlockHeader, CheckBlockHeader, ContextualCheckBlockHeader, CheckBlock, CheckMerkleRoot, CheckWitnessMalleation, ContextualCheckBlock, ConnectBlock, GetBlockScriptFlags, IsBIP30Repeat, IsBIP30Unspendable, UpdateCoins, DisconnectBlock, ApplyTxInUndo, src/validation.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/validation.cpp; CheckTransaction (consensus/tx_check.cpp); IsFinalTx, GetLegacySigOpCount, GetP2SHSigOpCount, GetTransactionSigOpCost, CheckTxInputs (consensus/tx_verify.cpp); CScript::GetSigOpCount, IsUnspendable (script/script.cpp, script.h); CountWitnessSigOps (script/interpreter.cpp); AddCoin, SpendCoin, AddCoins (coins.cpp); GetBlockProof (chain.cpp); GetWitnessCommitmentIndex, GetBlockWeight (consensus/validation.h); CMainParams (kernel/chainparams.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: BitcoinBlock.checkBlock_iff

Binders

  • bbb : a serialized block: a header b.headerb.\mathrm{header}b.header (version ∈Z\in \mathbb{Z}∈Z; prevBlock, merkleRoot byte lists; time, bits, nonce ∈N\in\mathbb{N}∈N) and a transaction list b.vtxb.\mathrm{vtx}b.vtx (each transaction: version ∈N\in\mathbb{N}∈N, input list, output list, lock time ∈N\in \mathbb{N}∈N; an input has a prevout ⟨hash bytes,n∈N⟩\langle\text{hash bytes}, n\in\mathbb{N}\rangle⟨hash bytes,n∈N⟩, a scriptSig, a sequence ∈N\in\mathbb{N}∈N and a witness stack; an output has a value ∈Z\in \mathbb{Z}∈Z and a scriptPubKey).

There are no hypotheses.

Conclusion

checkBlock(b)=true  ⟺  checkHeaderPow(b.header)=true  ∧  BlockSpec(b).\mathrm{checkBlock}(b) = \mathrm{true} \iff \mathrm{checkHeaderPow}(b.\mathrm{header}) = \mathrm{true} \;\wedge\; \mathrm{BlockSpec}(b).checkBlock(b)=true⟺checkHeaderPow(b.header)=true∧BlockSpec(b).

Left side, unfolded

checkBlock(b)\mathrm{checkBlock}(b)checkBlock(b) is the Boolean conjunction (in source order) of:

  1. checkHeaderPow(b.header)\mathrm{checkHeaderPow}(b.\mathrm{header})checkHeaderPow(b.header): the little-endian value of the double-SHA256 of the serialized header is at most the target decoded (with truncation modulo 22562^{256}2256) from b.header.bits mod 232b.\mathrm{header.bits} \bmod 2^{32}b.header.bitsmod232, and that compact word is not negative, not zero-valued, not overflowing, and decodes to at most 2224−12^{224}-12224−1;
  2. the first component of blockMerkleRoot(b)\mathrm{blockMerkleRoot}(b)blockMerkleRoot(b) equals b.header.merkleRootb.\mathrm{header.merkleRoot}b.header.merkleRoot, where blockMerkleRoot(b)\mathrm{blockMerkleRoot}(b)blockMerkleRoot(b) runs the merkle loop on the list of txids (double-SHA256 of the witness-stripped serialization) of b.vtxb.\mathrm{vtx}b.vtx: while more than one hash remains, flag "mutated" if some aligned pair (positions 2k,2k+12k, 2k+12k,2k+1) is equal, then replace the list by the double-SHA256 of each concatenated aligned pair, an odd last element being concatenated with itself; the root is the single remaining hash, or 323232 zero bytes for an empty list;
  3. the second component (the mutated flag) of blockMerkleRoot(b)\mathrm{blockMerkleRoot}(b)blockMerkleRoot(b) is false;
  4. b.vtxb.\mathrm{vtx}b.vtx is non-empty;
  5. 4⋅∣b.vtx∣≤4 000 0004\cdot |b.\mathrm{vtx}| \le 4\,000\,0004⋅∣b.vtx∣≤4000000;
  6. 4⋅∣serBlock(false,b)∣≤4 000 0004\cdot|\mathrm{serBlock}(\mathrm{false}, b)| \le 4\,000\,0004⋅∣serBlock(false,b)∣≤4000000, where serBlock(false,b)\mathrm{serBlock}(\mathrm{false},b)serBlock(false,b) is the serialized header followed by the compact-size-prefixed list of witness-stripped transaction serializations;
  7. isCoinBase\mathrm{isCoinBase}isCoinBase of the head of b.vtxb.\mathrm{vtx}b.vtx, with the genesis coinbase as default for an empty list; isCoinBase(tx)\mathrm{isCoinBase}(tx)isCoinBase(tx) means tx.vintx.\mathrm{vin}tx.vin is a one-element list whose prevout has hash equal to 323232 zero bytes and index 232−12^{32}-1232−1;
  8. every transaction of b.vtxb.\mathrm{vtx}b.vtx after the first is not a coinbase;
  9. every transaction txtxtx of b.vtxb.\mathrm{vtx}b.vtx satisfies checkTransaction(tx)\mathrm{checkTransaction}(tx)checkTransaction(tx): tx.vin≠[ ]tx.\mathrm{vin}\ne[\,]tx.vin=[], tx.vout≠[ ]tx.\mathrm{vout}\ne[\,]tx.vout=[], 4⋅∣serTx(false,tx)∣≤4 000 0004\cdot|\mathrm{serTx}(\mathrm{false},tx)| \le 4\,000\,0004⋅∣serTx(false,tx)∣≤4000000, the output loop succeeds (each value ≥0\ge 0≥0 and ≤2 100 000 000 000 000\le 2\,100\,000\,000\,000\,000≤2100000000000000 and each running partial sum in [0,2.1⋅1015][0, 2.1\cdot 10^{15}][0,2.1⋅1015]), the prevouts of tx.vintx.\mathrm{vin}tx.vin are pairwise distinct, and if isCoinBase(tx)\mathrm{isCoinBase}(tx)isCoinBase(tx) then the scriptSig length of the head of tx.vintx.\mathrm{vin}tx.vin (default input: empty prevout hash, index 000, empty scriptSig, sequence 000, empty witness) lies in [2,100][2,100][2,100], otherwise no input of txtxtx has a null prevout;
  10. 4⋅∑tx∈b.vtxlegacySigOpCount(tx)≤80 0004 \cdot \sum_{tx \in b.\mathrm{vtx}} \mathrm{legacySigOpCount}(tx) \le 80\,0004⋅∑tx∈b.vtx​legacySigOpCount(tx)≤80000, where legacySigOpCount(tx)\mathrm{legacySigOpCount}(tx)legacySigOpCount(tx) sums, over every input scriptSig and every output scriptPubKey, the inaccurate sigop count: scan the script instruction by instruction (stopping at a parse failure or when the byte-length fuel runs out), adding 111 for each OP_CHECKSIG/OP_CHECKSIGVERIFY byte and 202020 for each OP_CHECKMULTISIG/OP_CHECKMULTISIGVERIFY byte.

Right side, unfolded

checkHeaderPow(b.header)=true\mathrm{checkHeaderPow}(b.\mathrm{header}) = \mathrm{true}checkHeaderPow(b.header)=true as in item 1, and BlockSpec(b)\mathrm{BlockSpec}(b)BlockSpec(b), the proposition:

  • b.vtx≠[ ]b.\mathrm{vtx} \ne [\,]b.vtx=[];
  • (blockMerkleRoot b).1=b.header.merkleRoot(\mathrm{blockMerkleRoot}\,b).1 = b.\mathrm{header.merkleRoot}(blockMerkleRootb).1=b.header.merkleRoot and (blockMerkleRoot b).2=false(\mathrm{blockMerkleRoot}\,b).2 = \mathrm{false}(blockMerkleRootb).2=false;
  • 4⋅∣b.vtx∣≤4 000 0004\cdot|b.\mathrm{vtx}| \le 4\,000\,0004⋅∣b.vtx∣≤4000000 and 4⋅∣serBlock(false,b)∣≤4 000 0004\cdot|\mathrm{serBlock}(\mathrm{false},b)| \le 4\,000\,0004⋅∣serBlock(false,b)∣≤4000000;
  • every transaction in the first one element of b.vtxb.\mathrm{vtx}b.vtx is a coinbase, and every transaction after the first is not;
  • every tx∈b.vtxtx \in b.\mathrm{vtx}tx∈b.vtx satisfies TxSpec(tx)\mathrm{TxSpec}(tx)TxSpec(tx): tx.vin≠[ ]tx.\mathrm{vin}\ne[\,]tx.vin=[], tx.vout≠[ ]tx.\mathrm{vout}\ne[\,]tx.vout=[], 4⋅∣serTx(false,tx)∣≤4 000 0004\cdot|\mathrm{serTx}(\mathrm{false},tx)|\le 4\,000\,0004⋅∣serTx(false,tx)∣≤4000000, every output value vvv satisfies 0≤v≤2.1⋅10150 \le v \le 2.1\cdot 10^{15}0≤v≤2.1⋅1015, the sum of all output values is ≤2.1⋅1015\le 2.1\cdot10^{15}≤2.1⋅1015, prevouts pairwise distinct, isCoinBase(tx)=true⇒\mathrm{isCoinBase}(tx) = \mathrm{true} \RightarrowisCoinBase(tx)=true⇒ every input's scriptSig length is in [2,100][2,100][2,100], and isCoinBase(tx)=false⇒\mathrm{isCoinBase}(tx) = \mathrm{false} \RightarrowisCoinBase(tx)=false⇒ no input has a null prevout;
  • 4⋅∑txlegacySigOpCount(tx)≤80 0004\cdot\sum_{tx}\mathrm{legacySigOpCount}(tx) \le 80\,0004⋅∑tx​legacySigOpCount(tx)≤80000.

Edge cases silently included

Empty b.vtxb.\mathrm{vtx}b.vtx (left: item 4 fails, while item 7 would consult the genesis coinbase; right: first clause fails); the coinbase scriptSig bound is checked on the head input on the left versus all inputs on the right (a coinbase has exactly one input); partial-sum range on the left versus total-sum range on the right; unbounded bits\mathrm{bits}bits reduced modulo 2322^{32}232; no well-formedness of byte lengths (hashes of length other than 32) is assumed.

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

    Confirmed by the moderator at approval.

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