Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Calculated lock thresholds fit their signed destinations

Proved
BitcoinBIP68.calculated_bounds

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

bip68bitcoinbitcoin-mainnetconsensusformal-verification

Let version and flags be arbitrary unsigned 32-bit words. Let a context and finite input list satisfy the admissible-domain conditions: 0<H≤231−10<H\le2^{31}-10<H≤231−1, every input height at most HHH, and nonnegative supplied ancestor clocks at most 263−12^{63}-1263−1. Assume additionally that every actually enforced, non-disabled height sum c+dc+dc+d fits 231−12^{31}-1231−1, and every actually enforced, non-disabled time sum M(max⁡(c−1,0))+512dM(\max(c-1,0))+512dM(max(c−1,0))+512d fits 263−12^{63}-1263−1. These are bounds before subtraction of one, selected according to bit 22.

If calculation returns thresholds a,ba,ba,b, then

−1≤a≤231−1∧−1≤b≤263−1.-1\le a\le2^{31}-1\quad\land\quad-1\le b\le2^{63}-1.−1≤a≤231−1∧−1≤b≤263−1.

Disabled inputs and disabled enforcement require no delay-sum bounds. Empty lists and zero delays are included. The result supplies range guarantees for the mathematical-integer model on the explicitly stated domain; it is not a theorem that the entire C++ caller always establishes that domain.

Preamble
import Definitions.Def_BIP68_Model
import Definitions.Def_BIP68_Core
Formal statement
theorem BitcoinBIP68.calculated_bounds (version flags : BitVec 32)
    (ctx : BitcoinBIP68.Context) (inputs : List BitcoinBIP68.Input)
    (hctx : BitcoinBIP68.admissible ctx inputs)
    (hsafe : BitcoinBIP68.noOverflow version flags ctx inputs) :
    let result := BitcoinBIP68.calculate version flags ctx inputs
    (-1 ≤ result.minHeight ∧ result.minHeight ≤ (BitcoinBIP68.int32Max : Int)) ∧
    (-1 ≤ result.minTime ∧ result.minTime ≤ (BitcoinBIP68.int64Max : Int)) := by sorry
Source
BIP68, Specification and Implementation: https://github.com/bitcoin/bips/blob/100ba872c808946155b1eeb8c4568058f2eea0a7/bip-0068.mediawiki; Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48, CalculateSequenceLocks (lines 45–101), EvaluateSequenceLocks (103–111), SequenceLocks (113–116): https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/consensus/tx_verify.cpp#L45-L116. Functional contract extracted from these routines; the BIP does not number these claims as mathematical theorems.
Read-back

What the Lean code literally says, in plain math · GPT-6 (Codex independent sub-agent)

For every pair of 32-bit words representing unsigned integers v,f∈{0,…,232−1}v,f\in\{0,\ldots,2^{32}-1\}v,f∈{0,…,232−1}, every context consisting of a natural-number height HHH and a function T:N→NT:\mathbb{N}\to\mathbb{N}T:N→N, and every finite ordered list of inputs, each consisting of a 32-bit sequence word with unsigned value sss and a natural-number coin height ccc, assume that 0<H≤21474836470<H\le 21474836470<H≤2147483647, that T(h)≤9223372036854775807T(h)\le 9223372036854775807T(h)≤9223372036854775807 for every natural number h≤Hh\le Hh≤H, and that c≤Hc\le Hc≤H for every input in the list. Set EEE to mean v≥2v\ge 2v≥2 and fff odd. For each input, set DDD to mean ⌊s/2147483648⌋=1\lfloor s/2147483648\rfloor=1⌊s/2147483648⌋=1 (bit 31 is set), set BBB to mean ⌊s/4194304⌋ mod 2=1\lfloor s/4194304\rfloor\bmod 2=1⌊s/4194304⌋mod2=1 (bit 22 is set), and put d=s mod 65536d=s\bmod 65536d=smod65536. Also assume that, for every input, if EEE holds and DDD does not hold, then T(max⁡(c−1,0))+512d≤9223372036854775807T(\max(c-1,0))+512d\le 9223372036854775807T(max(c−1,0))+512d≤9223372036854775807 when BBB holds, and c+d≤2147483647c+d\le 2147483647c+d≤2147483647 when BBB does not hold; these sums and inequalities are over natural numbers. Define the calculated result as follows: start with integer accumulators a=b=−1a=b=-1a=b=−1 and an empty auxiliary list; if EEE holds, process the input list from left to right, leaving a,ba,ba,b unchanged and prepending 000 to the auxiliary list whenever bit 31 of the sequence word is set; otherwise, if bit 22 is set, replace bbb by max⁡{b,T(max⁡(c−1,0))+512d−1}\max\{b,T(\max(c-1,0))+512d-1\}max{b,T(max(c−1,0))+512d−1} and leave aaa unchanged; otherwise replace aaa by max⁡{a,c+d−1}\max\{a,c+d-1\}max{a,c+d−1} and leave bbb unchanged; in either of these latter two cases prepend ccc to the auxiliary list. The additions and final subtraction by 111 in these accumulator updates are integer operations; the value 512d512d512d is equivalently obtained by masking the sequence word to its lowest 16 bits and shifting that 32-bit word left by nine bits. If EEE does not hold, leave a=b=−1a=b=-1a=b=−1 and set the auxiliary list to the reverse of the input coin-height list. The assertion is that the resulting two integer accumulators satisfy (−1≤a≤2147483647)  ∧  (−1≤b≤9223372036854775807)(-1\le a\le 2147483647)\;\land\;(-1\le b\le 9223372036854775807)(−1≤a≤2147483647)∧(−1≤b≤9223372036854775807). The list may be empty or contain repetitions, coin heights and delays may be zero, and max⁡(c−1,0)\max(c-1,0)max(c−1,0) makes the time lookup use T(0)T(0)T(0) when c=0c=0c=0; a zero sum before the integer subtraction produces −1-1−1. The extra sum-bound assumptions impose no conditions when EEE is false or for inputs whose bit 31 is set, but the context and coin-height assumptions still apply. An empty input list, or an enabled calculation in which every input has bit 31 set, leaves both integer accumulators equal to −1-1−1; no monotonicity of TTT is assumed.

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