Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The encoded target at most quadruples per period

Proved
BitcoinDifficulty.encoded_target_upper

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

bitcoinbitcoin-mainnetconsensusdifficultyformal-verificationproof-of-workretargettimewarp

For setCompact bits ≤ powLimit and any timestamps: setCompact (calculateNextWork bits lastTime firstTime) ≤ 4 · setCompact bits. The compact encoding rounds down (getCompact_le).

Preamble
import Definitions.Def_BitcoinDifficulty
import Definitions.Def_BitcoinPow
open BitcoinDifficulty BitcoinPow
Formal statement
theorem BitcoinDifficulty.encoded_target_upper (bits : BitVec 32) (hb : BitcoinPow.setCompact bits ≤ BitcoinPow.powLimit)
    (lastTime firstTime : Int) :
    BitcoinPow.setCompact (BitcoinPow.calculateNextWork bits lastTime firstTime) ≤ 4 * BitcoinPow.setCompact bits := by sorry
Source
Bitcoin Core a773e1e33b349dc819b5a0edc1c7a4839dc73b48: CalculateNextWorkRequired, GetNextWorkRequired, src/pow.cpp: https://github.com/bitcoin/bitcoin/blob/a773e1e33b349dc819b5a0edc1c7a4839dc73b48/src/pow.cpp; arith_uint256::SetCompact/GetCompact (arith_uint256.cpp); 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: theorem BitcoinDifficulty.encoded_target_upper

Statement

Binders (universally quantified).

  • ccc, a compact word (a 32-bit word, named bits);
  • ℓ\ellℓ and fff, integers (named lastTime, firstTime), unrelated to each other.

Hypothesis.

  • (hb) D(c)≤L=2224−1D(c) \le L = 2^{224} - 1D(c)≤L=2224−1.

Conclusion.

D(N(c,ℓ,f))  ≤  4⋅D(c),D\big( N(c, \ell, f) \big) \;\le\; 4 \cdot D(c),D(N(c,ℓ,f))≤4⋅D(c),

where 4⋅D(c)4 \cdot D(c)4⋅D(c) is an ordinary natural-number product (no reduction modulo anything, no cap at LLL on the right-hand side). Unfolding NNN: with τ=κ(ℓ−f)\tau = \kappa(\ell - f)τ=κ(ℓ−f), u=⌊((D(c)⋅τ) mod 2256)/T⌋u = \big\lfloor \big((D(c)\cdot\tau) \bmod 2^{256}\big)/T \big\rflooru=⌊((D(c)⋅τ)mod2256)/T⌋ and u′=min⁡(u,L)u' = \min(u, L)u′=min(u,L), the left-hand side is D(E(u′))D(E(u'))D(E(u′)) — the decoded value of the re-encoded, capped next target. The inequality is non-strict.

Definitions used

Constants (from the imported proof-of-work definitions).

  • W=2256W = 2^{256}W=2256 (the modulus called width).
  • L=2224−1L = 2^{224} - 1L=2224−1 (the value called powLimit).
  • T=14⋅24⋅60⋅60=1,209,600T = 14 \cdot 24 \cdot 60 \cdot 60 = 1{,}209{,}600T=14⋅24⋅60⋅60=1,209,600 (the value called targetTimespan); hence ⌊T/4⌋=302,400\lfloor T/4 \rfloor = 302{,}400⌊T/4⌋=302,400 and 4T=4,838,4004T = 4{,}838{,}4004T=4,838,400.

Decoding a compact word, D(c)D(c)D(c) (the function called setCompact). A compact word ccc is a 32-bit word, i.e. an integer with 0≤c<2320 \le c < 2^{32}0≤c<232. Put

  • e(c)=⌊c/224⌋e(c) = \lfloor c / 2^{24} \rfloore(c)=⌊c/224⌋, the top byte of ccc, an integer in [0,255][0, 255][0,255];
  • m(c)=c mod 223m(c) = c \bmod 2^{23}m(c)=cmod223, the low 23 bits of ccc (bit 23, worth 2232^{23}223, and the top byte are discarded).

Then D(c)D(c)D(c) is the natural number

D(c)={⌊m(c) / 2 8(3−e(c))⌋if e(c)≤3,(m(c)⋅2 8(e(c)−3)) mod 2256if e(c)≥4.D(c) = \begin{cases} \left\lfloor m(c) \,/\, 2^{\,8(3 - e(c))} \right\rfloor & \text{if } e(c) \le 3,\\[4pt] \left( m(c) \cdot 2^{\,8(e(c) - 3)} \right) \bmod 2^{256} & \text{if } e(c) \ge 4. \end{cases}D(c)={⌊m(c)/28(3−e(c))⌋(m(c)⋅28(e(c)−3))mod2256​if e(c)≤3,if e(c)≥4.​

Consequences of this definition that the quantifiers below silently include: D(c)<2256D(c) < 2^{256}D(c)<2256 always; D(c)=0D(c) = 0D(c)=0 whenever m(c)=0m(c) = 0m(c)=0, whenever e(c)=0e(c) = 0e(c)=0, and whenever e(c)≥35e(c) \ge 35e(c)≥35 (the shift then exceeds 256 bits and the reduction modulo 22562^{256}2256 leaves 000); for e(c)=33e(c) = 33e(c)=33 or 343434 only the low 16 or low 8 bits of m(c)m(c)m(c) survive the reduction; a word whose bit 23 ("sign" bit) is set decodes to the same value as the word with that bit cleared. DDD never consults the overflow or sign conditions of the original code; it is a total function on all 2322^{32}232 words.

Encoding a natural number, E(v)E(v)E(v) (the function called getCompact). For a natural number vvv let b(v)b(v)b(v) be its bit length (b(0)=0b(0) = 0b(0)=0, and b(v)=⌊log⁡2v⌋+1b(v) = \lfloor \log_2 v \rfloor + 1b(v)=⌊log2​v⌋+1 for v≥1v \ge 1v≥1) and let s=⌊(b(v)+7)/8⌋s = \lfloor (b(v) + 7)/8 \rfloors=⌊(b(v)+7)/8⌋ (the number of bytes of vvv). Put

n={((v mod 264)⋅2 8(3−s)) mod 232if s≤3,(⌊v/2 8(s−3)⌋ mod 264) mod 232if s≥4.n = \begin{cases} \left( (v \bmod 2^{64}) \cdot 2^{\,8(3-s)} \right) \bmod 2^{32} & \text{if } s \le 3,\\[4pt] \left( \left\lfloor v / 2^{\,8(s-3)} \right\rfloor \bmod 2^{64} \right) \bmod 2^{32} & \text{if } s \ge 4. \end{cases}n={((vmod264)⋅28(3−s))mod232(⌊v/28(s−3)⌋mod264)mod232​if s≤3,if s≥4.​

If bit 23 of nnn is set (i.e. n∧223≠0n \wedge 2^{23} \neq 0n∧223=0, bitwise and), let n′=⌊n/28⌋n' = \lfloor n / 2^{8} \rfloorn′=⌊n/28⌋ and s′=s+1s' = s + 1s′=s+1; otherwise n′=nn' = nn′=n and s′=ss' = ss′=s. Then E(v)E(v)E(v) is the 32-bit word (n′  OR  (s′⋅224)) mod 232\left( n' \;\mathrm{OR}\; (s' \cdot 2^{24}) \right) \bmod 2^{32}(n′OR(s′⋅224))mod232 (bitwise or, then reduction modulo 2322^{32}232).

Consequences: for every v<2256v < 2^{256}v<2256 one has s≤32s \le 32s≤32 and n<224n < 2^{24}n<224, so the reductions modulo 2642^{64}264 and 2322^{32}232 change nothing and the bitwise or is a plain sum n′+s′⋅224n' + s' \cdot 2^{24}n′+s′⋅224; the word E(v)E(v)E(v) then has top byte s′s's′ and low 23 bits n′n'n′ (bit 23 of n′n'n′ is clear). Composing with DDD: for v<2256v < 2^{256}v<2256, D(E(v))D(E(v))D(E(v)) equals vvv with its lowest 8(s−3)8(s-3)8(s−3) bits set to zero (lowest 8(s−2)8(s-2)8(s−2) bits when the bit-23 adjustment fired; nothing zeroed when s≤3s \le 3s≤3 and no adjustment), so in particular D(E(v))≤vD(E(v)) \le vD(E(v))≤v, and D(E(0))=0D(E(0)) = 0D(E(0))=0.

Clamping a timespan, κ(a)\kappa(a)κ(a) (the function called clampTimespan). For an integer aaa (positive, zero or negative),

κ(a)={302,400if a<302,400,4,838,400if a>4,838,400,aotherwise.\kappa(a) = \begin{cases} 302{,}400 & \text{if } a < 302{,}400,\\ 4{,}838{,}400 & \text{if } a > 4{,}838{,}400,\\ a & \text{otherwise.} \end{cases}κ(a)=⎩⎨⎧​302,4004,838,400a​if a<302,400,if a>4,838,400,otherwise.​

κ(a)\kappa(a)κ(a) always lies in [302,400,  4,838,400][302{,}400,\; 4{,}838{,}400][302,400,4,838,400], so it is positive and its conversion to a natural number (written κ(a)\kappa(a)κ(a) below as well) is lossless.

The next encoded target, N(c,ℓ,f)N(c, \ell, f)N(c,ℓ,f) (the function called calculateNextWork). For a compact word ccc and integers ℓ\ellℓ ("last time") and fff ("first time"), put τ=κ(ℓ−f)\tau = \kappa(\ell - f)τ=κ(ℓ−f) and

u=⌊(D(c)⋅τ) mod 2256T⌋,u′={Lif L<u,uotherwise,u = \left\lfloor \frac{\left( D(c) \cdot \tau \right) \bmod 2^{256}}{T} \right\rfloor, \qquad u' = \begin{cases} L & \text{if } L < u,\\ u & \text{otherwise,}\end{cases}u=⌊T(D(c)⋅τ)mod2256​⌋,u′={Lu​if L<u,otherwise,​

i.e. u′=min⁡(u,L)u' = \min(u, L)u′=min(u,L). Then N(c,ℓ,f)=E(u′)N(c, \ell, f) = E(u')N(c,ℓ,f)=E(u′), a compact word. The product D(c)⋅τD(c)\cdot\tauD(c)⋅τ is reduced modulo 22562^{256}2256 before the floor division by TTT; the cap at LLL is applied before re-encoding, and nothing caps or checks the decoded value of the encoded result afterwards.

Edge cases and what the quantifiers include

  • ℓ\ellℓ and fff range over all integers; ℓ−f\ell - fℓ−f may be zero or negative (clamped to 302,400302{,}400302,400) or arbitrarily large (clamped to 4,838,400=4T4{,}838{,}400 = 4T4,838,400=4T).
  • If D(c)=0D(c) = 0D(c)=0 the right-hand side is 000, so the statement asserts D(N(c,ℓ,f))=0D(N(c, \ell, f)) = 0D(N(c,ℓ,f))=0 for every ℓ,f\ell, fℓ,f in that case; the hypothesis admits all such words (zero mantissa, top byte 000, top byte ≥35\ge 35≥35, etc.).
  • The hypothesis constrains only the decoded value; the sign bit (bit 23) and the overflow conditions of the original code are not mentioned.
  • The cap at LLL inside NNN applies before encoding; the bound 4⋅D(c)4 \cdot D(c)4⋅D(c) on the right may exceed LLL (it does whenever D(c)>L/4D(c) > L/4D(c)>L/4), and the statement does not assert D(N(c,ℓ,f))≤LD(N(c,\ell,f)) \le LD(N(c,ℓ,f))≤L.
  • When D(c)≤LD(c) \le LD(c)≤L, D(c)⋅τ<2247<2256D(c) \cdot \tau < 2^{247} < 2^{256}D(c)⋅τ<2247<2256, so the modular reduction inside NNN is inactive for every admitted ccc; it is still part of the literal statement.
Human review
  • Endorsed by Shuze Chen · Oct 29, 2026

    Confirmed by the moderator at approval.

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