Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xstoicunicornx/d73805c930a1c71f5f766a3c6d525b5b to your computer and use it in GitHub Desktop.

Select an option

Save xstoicunicornx/d73805c930a1c71f5f766a3c6d525b5b to your computer and use it in GitHub Desktop.
bip54-timewarp-chain-length-summary

Bounds on chain length with BIP-54 timewarp fixes

Source: Bounds on chain length with BIP-54 timewarp fixes by Pieter Wuille (sipa), Delving Bitcoin, Sept 2026. Most of the replies are a back-and-forth with zawy.

Background

Bitcoin adjusts mining difficulty every 2016 blocks, using the timestamps miners write into the blocks. If miners lie about those timestamps in the right pattern, they can push difficulty down and produce blocks much faster than one every 10 minutes. That's a "timewarp" attack. Today the only timestamp rule that limits this caps the chain at roughly 6 blocks per second, which is no real limit.

BIP-54 (the "consensus cleanup" soft fork) adds two timestamp rules to stop the known attacks:

  1. The first block of a new difficulty period can't be more than 2 hours earlier than the block before it. This stops the original timewarp attack.
  2. The last block of a period can't be earlier than the first block of that period. This stops a newer variant found by Murch and zawy.

The open question was whether these two rules are enough, or whether some other timestamp trick still exists that nobody has found.

What sipa showed

The two rules are enough. With both in place, there's a hard ceiling on how many blocks a chain can have, given how much time has passed and how much mining work went into it. In plain terms:

  • Over the long run, a chain can't go faster than about one block every 9 minutes 56 seconds.
  • An attacker can get some extra blocks beyond that, but only by paying for them with real hashing, through difficulty increases.

The numbers for Sept 9, 2026:

Scenario Max blocks
Real chain 966,270
Longest attack chain anyone has built 1,007,326
Proven ceiling with both rules 1,012,794
Ceiling with only one rule, or neither over 3.34 billion

So with both rules, even a perfectly optimized attacker with the same total work only gets about 5% more blocks than the real chain. Drop either rule and the ceiling jumps by a factor of about 3,300. Both rules are needed, and together they're enough.

How he got there

He loosened the problem in steps. Each step lets attackers do more than they really could, which keeps the result a safe overestimate. Then he solved the simpler version and got a single formula. The formula also handles Bitcoin's real rounding quirks in how it computes difficulty and work.

The formula is machine-checked in Lean, a proof assistant. An LLM wrote the proof, and sipa says he didn't review it himself. That's less alarming than it sounds. Lean's checker verifies the proof, so a person only has to confirm that the theorem statement matches Bitcoin's rules. The best attack chain in the table was also found with an AI model (Claude Fable 5.1). It's a neat example of AI tools being useful in protocol research while a human keeps the part that needs judgment.

Why anyone cares

The practical motivation is a defense in Bitcoin Core against header spam. In 2024, attackers could cheaply flood nodes with fake low-difficulty block headers and fill up memory. The fix ("headers presync") downloads headers twice. The first pass only checks whether the chain has enough total work to be worth storing. That logic needs a cap on how many headers a chain with a given amount of work could have, and right now the cap has to assume the 6-blocks-per-second worst case. Once BIP-54 is active and buried, that cap could drop to roughly the length of the real chain. That makes the defense much tighter.

The discussion

  • Strict always-increasing timestamps. zawy suggested requiring every timestamp to be later than the one before it. sipa showed that would only shrink the ceiling by about 0.6%, which is the cost of the 2-hour grace window. Not worth the change.
  • Infinite blocks. zawy briefly argued that a weak enough chain could still get unlimited blocks in finite time. sipa pointed to the conditions the proof covers, and zawy withdrew the claim.
  • Flipping the question. zawy proposed asking it the other way around: how much work does an attacker need to build a chain of N blocks in time T? sipa liked this, worked out that formula, and proved it in Lean too. It's arguably the more useful version, since it tells you the minimum cost of a given fake chain.
  • Syncing lowest hashes first. zawy suggested fetching the lowest-hash blocks first to filter spam. sipa noted the idea dates back to 2012, but nobody built the P2P changes it needed. In 2024 presync fixed the problem without any protocol changes.

My read

This is a reassuring result for BIP-54. People worried the timewarp fixes might be patches for two known attacks that leave room for a third. This closes that worry with a proof. The ceiling sits within 5% of the real chain, so there's no meaningful timestamp trick left. The headers-spam benefit is a nice side effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment