Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Acceptance by the contextual header check, characterized

Proved
BitcoinHeader.contextualCheck_iff

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

bitcoinbitcoin-mainnetblock-headerconsensusformal-verificationmedian-time-past

For every header hhh, previous height ppp, chain of previous times, required nBits rrr and local time nownownow, the contextual check accepts exactly when

h.bits=r  ∧  MTP(prevTimes)<h.time  ∧  h.time≤now+7,200  ∧  ¬ outdated,h.\mathrm{bits} = r \;\wedge\; \mathrm{MTP}(\text{prevTimes}) < h.\mathrm{time} \;\wedge\; h.\mathrm{time} \le now + 7{,}200 \;\wedge\; \neg\,\mathrm{outdated},h.bits=r∧MTP(prevTimes)<h.time∧h.time≤now+7,200∧¬outdated,

where outdated is the disjunction of the three version-floor conditions. These are the bad-diffbits, time-too-old, time-too-new and bad-version rejections of ContextualCheckBlockHeader.

Preamble
import Definitions.Def_BitcoinHeader
Formal statement
theorem BitcoinHeader.contextualCheck_iff (h : BitcoinHeader.Header) (prevHeight : Nat) (prevTimes : List Int)
    (required : BitVec 32) (now : Int) :
    BitcoinHeader.contextualCheck h prevHeight prevTimes required now = true ↔
      h.bits = required ∧
      BitcoinHeader.medianTimePast prevTimes < h.time ∧
      h.time ≤ now + BitcoinHeader.MAX_FUTURE_BLOCK_TIME ∧
      ¬ ((h.version < 2 ∧ BitcoinHeader.BIP34Height ≤ prevHeight + 1) ∨
         (h.version < 3 ∧ BitcoinHeader.BIP66Height ≤ prevHeight + 1) ∨
         (h.version < 4 ∧ BitcoinHeader.BIP65Height ≤ prevHeight + 1)) := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: CBlockIndex::GetMedianTimePast and nMedianTimeSpan, MAX_FUTURE_BLOCK_TIME, src/chain.h: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/chain.h; ContextualCheckBlockHeader, src/validation.cpp (lines 4091-4128): https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/validation.cpp; DeploymentActiveAfter (buried), src/deploymentstatus.h; BIP34Height, BIP66Height, BIP65Height in CMainParams, src/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)

For every Header hhh — a record with an arbitrary integer field h.versionh.\mathrm{version}h.version, an arbitrary integer field h.timeh.\mathrm{time}h.time, and a 32-bit bit-vector field h.bitsh.\mathrm{bits}h.bits — every natural number ppp (named prevHeight), every finite list of integers PPP (named prevTimes, possibly empty), every 32-bit bit-vector rrr (named required; a free parameter, not computed from anything), and every integer ν\nuν (named now; a free integer parameter), the statement asserts a logical equivalence ("if and only if") between the Boolean-valued function contextualCheck(h,p,P,r,ν)\mathrm{contextualCheck}(h, p, P, r, \nu)contextualCheck(h,p,P,r,ν) evaluating to the Boolean true and the conjunction of the following four propositions:

  1. h.bits=rh.\mathrm{bits} = rh.bits=r (equality of 32-bit bit-vectors);
  2. medianTimePast(P)<h.time\mathrm{medianTimePast}(P) < h.\mathrm{time}medianTimePast(P)<h.time (strict integer inequality), where medianTimePast(P)\mathrm{medianTimePast}(P)medianTimePast(P) is obtained by taking the first n=min⁡(11,∣P∣)n = \min(11, |P|)n=min(11,∣P∣) entries of PPP, sorting them into non-decreasing order w0≤⋯≤wn−1w_0 \le \dots \le w_{n-1}w0​≤⋯≤wn−1​, and returning w⌊n/2⌋w_{\lfloor n/2 \rfloor}w⌊n/2⌋​ (zero-based position, natural-number division rounding down; the middle element when nnn is odd, the upper of the two middle elements when nnn is even), or returning the fallback value 000 when PPP is empty — so for P=[]P = []P=[] this conjunct is simply 0<h.time0 < h.\mathrm{time}0<h.time;
  3. h.time≤ν+7200h.\mathrm{time} \le \nu + 7200h.time≤ν+7200 (non-strict integer inequality; 720072007200 is the constant MAX_FUTURE_BLOCK_TIME);
  4. the negation
¬[(h.version<2∧227931≤p+1)∨(h.version<3∧363725≤p+1)∨(h.version<4∧388381≤p+1)],\neg\Bigl[\bigl(h.\mathrm{version} < 2 \wedge 227931 \le p + 1\bigr) \vee \bigl(h.\mathrm{version} < 3 \wedge 363725 \le p + 1\bigr) \vee \bigl(h.\mathrm{version} < 4 \wedge 388381 \le p + 1\bigr)\Bigr],¬[(h.version<2∧227931≤p+1)∨(h.version<3∧363725≤p+1)∨(h.version<4∧388381≤p+1)],

with 227931227931227931, 363725363725363725, 388381388381388381 the constants BIP34Height, BIP66Height, BIP65Height, integer comparisons on h.versionh.\mathrm{version}h.version, and natural-number addition p+1p + 1p+1.

Unfolding, contextualCheck(h,p,P,r,ν)\mathrm{contextualCheck}(h, p, P, r, \nu)contextualCheck(h,p,P,r,ν) is by definition the Boolean "and" of: the Boolean deciding h.bits=rh.\mathrm{bits} = rh.bits=r; the Boolean deciding medianTimePast(P)<h.time\mathrm{medianTimePast}(P) < h.\mathrm{time}medianTimePast(P)<h.time; the Boolean deciding h.time≤ν+7200h.\mathrm{time} \le \nu + 7200h.time≤ν+7200; and the Boolean negation of versionOutdated(h.version,p)\mathrm{versionOutdated}(h.\mathrm{version}, p)versionOutdated(h.version,p), where versionOutdated(v,p)\mathrm{versionOutdated}(v,p)versionOutdated(v,p) is the Boolean "or" of (v<2v<2v<2 and 227931≤p+1227931 \le p+1227931≤p+1), (v<3v<3v<3 and 363725≤p+1363725 \le p+1363725≤p+1), (v<4v<4v<4 and 388381≤p+1388381 \le p+1388381≤p+1). So the statement says the Boolean computation returns true exactly when the four propositions, with each Boolean test replaced by the relation it decides, all hold; by the "only if" direction it returns false exactly when at least one fails. There are no hypotheses; the equivalence is asserted for all values of the five parameters, including P=[]P = []P=[], any ν\nuν (so conjunct 3 can be made true or false by choice of ν\nuν independently of PPP), and any rrr (so conjunct 1 depends only on whether rrr happens to equal h.bitsh.\mathrm{bits}h.bits). Nothing relates ppp to ∣P∣|P|∣P∣.

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