Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

ContextualCheckBlock characterized

Proved
BitcoinBlock.contextualCheck_iff

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

bip30bip34bitcoinbitcoin-mainnetblock-validationconnectblockconsensusformal-verificationundo

For a non-empty block whose transactions have inputs, contextualCheckBlock S b = true iff ContextSpec S b: every transaction final against the lock-time cutoff (median time past once CSV is active, else the block time), the BIP34 coinbase height prefix when active, the witness commitment rule, and the weight limit.

Deprecated. Superseded by BitcoinBlockV2.contextualCheck_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.contextualCheck_iff (S : ChainState) (b : BitcoinSer.Block)
    (hne : b.vtx ≠ []) (hvin : ∀ tx ∈ b.vtx, tx.vin ≠ []) :
    BitcoinBlock.contextualCheckBlock S b = true ↔ BitcoinBlock.ContextSpec S 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.contextualCheck_iff

Binders and hypotheses

  • SSS : a chain state ⟨headers,utxo⟩\langle\mathit{headers}, \mathit{utxo}\rangle⟨headers,utxo⟩.
  • bbb : a serialized block (header, transaction list b.vtxb.\mathrm{vtx}b.vtx).
  • Hypothesis hneh_{ne}hne​ : b.vtx≠[ ]b.\mathrm{vtx} \ne [\,]b.vtx=[].
  • Hypothesis hvinh_{vin}hvin​ : every transaction tx∈b.vtxtx \in b.\mathrm{vtx}tx∈b.vtx has tx.vin≠[ ]tx.\mathrm{vin} \ne [\,]tx.vin=[].

Both hypotheses are carried by the statement and absent from the conclusion.

Conclusion

contextualCheckBlock(S,b)=true  ⟺  ContextSpec(S,b).\mathrm{contextualCheckBlock}(S,b) = \mathrm{true} \iff \mathrm{ContextSpec}(S,b).contextualCheckBlock(S,b)=true⟺ContextSpec(S,b).

Common notation: height=∣S.headers∣\mathit{height} = |S.\mathrm{headers}|height=∣S.headers∣; tipHeight=∣S.headers∣−1\mathrm{tipHeight} = |S.\mathrm{headers}| - 1tipHeight=∣S.headers∣−1 (natural subtraction); cutoff=mtp(S)\mathit{cutoff} = \mathrm{mtp}(S)cutoff=mtp(S) (sorted median of the first 111111 of the header times at heights 0..tipHeight0..\mathrm{tipHeight}0..tipHeight taken most recent first, default 000) if 419328≤tipHeight+1419328 \le \mathrm{tipHeight} + 1419328≤tipHeight+1, else b.header.timeb.\mathrm{header.time}b.header.time as an integer.

Left side, unfolded

Boolean conjunction of:

  1. every tx∈b.vtxtx \in b.\mathrm{vtx}tx∈b.vtx satisfies isFinalTx(tx,height,cutoff)\mathrm{isFinalTx}(tx, \mathit{height}, \mathit{cutoff})isFinalTx(tx,height,cutoff): true if tx.nLockTime=0tx.\mathrm{nLockTime} = 0tx.nLockTime=0; else true if tx.nLockTime<heighttx.\mathrm{nLockTime} < \mathit{height}tx.nLockTime<height when tx.nLockTime<500 000 000tx.\mathrm{nLockTime} < 500\,000\,000tx.nLockTime<500000000, respectively tx.nLockTime<cutofftx.\mathrm{nLockTime} < \mathit{cutoff}tx.nLockTime<cutoff otherwise; else true iff every input has nSequence=4294967295\mathrm{nSequence} = 4294967295nSequence=4294967295;
  2. 227931≤tipHeight+1227931 \le \mathrm{tipHeight}+1227931≤tipHeight+1 is false, or heightScript(height)\mathrm{heightScript}(\mathit{height})heightScript(height) is a prefix of the scriptSig of the head input (default input with empty scriptSig) of the head transaction of b.vtxb.\mathrm{vtx}b.vtx (default: the genesis coinbase); heightScript(n)=[0]\mathrm{heightScript}(n) = [0]heightScript(n)=[0] if n=0n = 0n=0, [0x50+n][0x50 + n][0x50+n] if 1≤n≤161 \le n \le 161≤n≤16, otherwise the canonical push (length byte or PUSHDATA prefix) of the little-endian minimal signed script-number encoding of nnn;
  3. checkWitnessMalleation(b,481824≤tipHeight+1)\mathrm{checkWitnessMalleation}(b, 481824 \le \mathrm{tipHeight}+1)checkWitnessMalleation(b,481824≤tipHeight+1): with noWitness\mathit{noWitness}noWitness = "no transaction of bbb has an input with a non-empty witness": if the expectation flag is false, noWitness\mathit{noWitness}noWitness; if true, find the commitment index — the largest output index ppp of the first transaction whose scriptPubKey has length ≥38\ge 38≥38 and starts with bytes 6a 24 aa 21 a9 ed6a\,24\,aa\,21\,a9\,ed6a24aa21a9ed (none if b.vtxb.\mathrm{vtx}b.vtx is empty): if none, noWitness\mathit{noWitness}noWitness; else the witness stack of the first input (default input) of the first transaction (default genesis coinbase) must be exactly one element nonce\mathit{nonce}nonce of length 323232 with hash256(blockWitnessMerkleRoot(b)+ ⁣ ⁣+nonce)\mathrm{hash256}(\mathrm{blockWitnessMerkleRoot}(b) \mathbin{+\!\!+} \mathit{nonce})hash256(blockWitnessMerkleRoot(b)++nonce) equal to bytes 666 through 373737 of output ppp's scriptPubKey, where blockWitnessMerkleRoot\mathrm{blockWitnessMerkleRoot}blockWitnessMerkleRoot is the merkle root of 323232 zero bytes followed by the wtxids of the transactions after the first;
  4. blockWeight(b)≤4 000 000\mathrm{blockWeight}(b) \le 4\,000\,000blockWeight(b)≤4000000, where blockWeight(b)=3⋅∣serBlock(false,b)∣+∣serBlock(true,b)∣\mathrm{blockWeight}(b) = 3\cdot|\mathrm{serBlock}(\mathrm{false},b)| + |\mathrm{serBlock}(\mathrm{true},b)|blockWeight(b)=3⋅∣serBlock(false,b)∣+∣serBlock(true,b)∣.

Right side, unfolded

ContextSpec(S,b)\mathrm{ContextSpec}(S,b)ContextSpec(S,b) is the conjunction of:

  1. ∀tx∈b.vtx, isFinalTx(tx,height,cutoff)=true\forall tx \in b.\mathrm{vtx},\ \mathrm{isFinalTx}(tx, \mathit{height}, \mathit{cutoff}) = \mathrm{true}∀tx∈b.vtx, isFinalTx(tx,height,cutoff)=true (same function);
  2. 227931≤tipHeight+1⇒∀cb∈take1(b.vtx), ∀i∈take1(cb.vin), heightScript(height)227931 \le \mathrm{tipHeight}+1 \Rightarrow \forall cb \in \mathrm{take}_1(b.\mathrm{vtx}),\ \forall i \in \mathrm{take}_1(cb.\mathrm{vin}),\ \mathrm{heightScript}(\mathit{height})227931≤tipHeight+1⇒∀cb∈take1​(b.vtx), ∀i∈take1​(cb.vin), heightScript(height) is a prefix of i.scriptSigi.\mathrm{scriptSig}i.scriptSig;
  3. checkWitnessMalleation(b,481824≤tipHeight+1)=true\mathrm{checkWitnessMalleation}(b, 481824 \le \mathrm{tipHeight}+1) = \mathrm{true}checkWitnessMalleation(b,481824≤tipHeight+1)=true (same function);
  4. blockWeight(b)≤4 000 000\mathrm{blockWeight}(b) \le 4\,000\,000blockWeight(b)≤4000000.

The two sides differ only in clause 2, where the executable side uses head-with-default and the specification quantifies over the (at most one) first transaction and its (at most one) first input.

Edge cases silently included

The hypotheses rule out exactly the cases where clause 2 differs (empty b.vtxb.\mathrm{vtx}b.vtx, or a first transaction with no inputs); empty header list (tipHeight=0\mathrm{tipHeight}=0tipHeight=0, median 000); b.header.timeb.\mathrm{header.time}b.header.time unbounded; tx.nLockTimetx.\mathrm{nLockTime}tx.nLockTime and nSequence\mathrm{nSequence}nSequence unbounded naturals compared exactly; no coinbase or checkBlock\mathrm{checkBlock}checkBlock assumption (the "first transaction" need not be a coinbase).

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