The Time-Warp Attack on Bitcoin’s Difficulty Adjustment: How Median Time Past Manipulation Lets a Majority Miner Strand Hashrate at Low Difficulty

The time-warp attack is often described as a timestamp bug. That framing is imprecise. The retarget function in Bitcoin Core is a two-point estimator: it reads exactly two block timestamps per 2016-block period and ignores the other 2014. Any analysis of time-warp resistance should therefore begin by asking which constraints apply to those two headers, not whether timestamps are “honest” on average. The Median Time Past (MTP) rule is a strong constraint on interior blocks and a weak one on the period boundary. That asymmetry is the attack.

What the retarget actually computes

In src/pow.cpp, GetNextWorkRequired returns the previous nBits unchanged unless the next height is a multiple of the difficulty adjustment interval. When it is, the function locates the first block of the window by walking back DifficultyAdjustmentInterval() - 1 blocks from the last block:

int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);

CalculateNextWorkRequired then computes the actual timespan as the difference between the last block’s time and that first block’s time, clamps it, and scales the previous target:

int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
if (nActualTimespan < params.nPowTargetTimespan / 4) nActualTimespan = params.nPowTargetTimespan / 4;
if (nActualTimespan > params.nPowTargetTimespan * 4) nActualTimespan = params.nPowTargetTimespan * 4;
...
bnNew *= nActualTimespan;
bnNew /= params.nPowTargetTimespan;
if (bnNew > bnPowLimit) bnNew = bnPowLimit;

The interval itself is derived from consensus parameters as nPowTargetTimespan / nPowTargetSpacing, which on mainnet is 1,209,600 seconds divided by 600 seconds, i.e. 2016 blocks. The target timespan is 14 days. The clamp bounds the per-period adjustment to a factor of four in either direction.

The structural point: nActualTimespan depends on pindexFirst->GetBlockTime() and pindexLast->GetBlockTime(). No other header in the period enters the calculation. A miner who controls both endpoints controls the estimator’s input, subject only to whatever rules constrain those two headers.

The MTP rule constrains a different quantity

Bitcoin’s header-time rule is expressed in terms of Median Time Past. BIP9 defines GetMedianTimePast as the median nTime of a block and its 10 predecessors, and treats it as a monotonic clock defined by the chain. The consensus requirement is that a new block’s timestamp exceed the MTP of its ancestors. Bitcoin Optech’s topic summary states the rule as requiring a header time greater than the median time of the previous 11 blocks, which prevents a single miner or small group from claiming a block was created far in the past.

That is a real constraint, and it is why the attack requires a majority of hashrate rather than a single block. But note what MTP bounds: each header relative to a rolling median of the preceding 11 headers. It does not bound the first block of a retarget period relative to the last block of the previous period. Those two headers are 2015 blocks apart, and the retarget compares exactly them. A strong interior rule and a two-point estimator are not the same guarantee.

Walking the arithmetic

Suppose a miner with a majority of hashrate wants difficulty to fall. The mechanism, as Optech describes it, is to set almost every header to the minimum one-second increase over the median, and to set the last block of the difficulty period to the current wall-clock time. The sequence of header times then looks like a small monotonic ramp punctuated by a jump into the apparent future, after which the ramp resumes from the low base. Optech’s illustrative series is [0, 1, 2, 1795594, 3, 4, 5, ...].

Feed that into the retarget. The first block of the period carries a timestamp near the MTP floor. The last block carries wall-clock time. The difference is large, so nActualTimespan is large, and the clamp permits it to reach four times the target timespan. bnNew is scaled by that ratio, so the target rises and difficulty falls. The next period starts from a lower difficulty, blocks are found faster, and the same endpoint manipulation can be repeated. Optech notes that miners who begin a time-warp attack cannot significantly affect difficulty for the first 2016-block epoch, because both the first and last blocks in that epoch need approximately accurate times; from the second epoch onward the manipulation bites.

BIP54 quantifies the consequence in its motivation section: the timewarp bug makes it possible for a majority-hashrate attacker to arbitrarily lower mining difficulty and therefore arbitrarily increase the block rate, and in the worst case an attacker can bring difficulty down to its minimum within 38 days of starting the attack. That is a specification-level statement about the mechanism, not an observation that anyone has done it on mainnet.

Why the endpoints are the whole attack surface

A reviewer’s heuristic follows directly from the code: for any proposed timestamp rule, ask which of the two retarget endpoints it constrains, and by how much. A rule that tightens interior blocks leaves the estimator’s inputs unchanged. A rule that constrains only the first block of a period, without reference to the previous period’s last block, can be satisfied by moving both endpoints together. The useful rule is one that couples the boundary.

This is also why the MTP rule alone does not close the gap. MTP is a rolling median over 11 headers; the retarget is a difference over 2015 blocks. The two rules constrain different quantities, and the existence of a strong rule for one does not imply a bound on the other.

What BIP54 proposes

BIP54, Consensus Cleanup, specifies two new timestamp constraints for blocks after activation. For a block at height N:

  • If N % 2016 == 0, the timestamp must be greater than or equal to the timestamp of block N-1 minus 7200 seconds: T_N >= T_{N-1} - 7200.
  • If N % 2016 == 2015, the timestamp must be greater than or equal to the timestamp of block N-2015: T_N >= T_{N-2015}.

The first rule is the one that targets the time-warp. It prevents the first block of a difficulty period from being timestamped earlier than the previous block by more than two hours. The rationale in the BIP states that the restrictions on the first and last blocks of a difficulty adjustment period fix the timewarp and Murch–Zawy vulnerabilities, and that the timewarp fix works by preventing the timestamp of the first block of a difficulty period from being lower than the previous block’s, with a two-hour grace period.

The grace-period choice is a design tradeoff worth surfacing rather than treating as settled. BIP54 notes that a previous proposal used a ten-minute grace period and that this approach was adopted for testnet4, while BIP54 uses two hours “out of an abundance of caution” and because it “only trivially worsens the block rate increase under attack.” The BIP’s footnote quantifies the triviality: using two hours instead of ten minutes reduces the expected block interval time under attack by roughly 2.2 seconds. The tradeoff is between attack mitigation strength and tolerance for imperfect pool software; the testnet4 difficulty exception, per the BIP, pushed block timestamps into the future when abused, revealing how some broken pool software may produce blocks that do not respect a ten-minute grace period.

Two implementation notes from the BIP are relevant to operators. Bitcoin Core 29.0 and later will not generate a block template that violates the timestamp restrictions, and miners should use the curtime or mintime field from the getblocktemplate result for the block timestamp. The BIP also states that using a timestamp lower than mintime already produces an invalid block under existing rules, so this is not a new requirement.

What the specs establish, and what they do not

Established by the retrieved sources: the retarget reads two timestamps; the clamp bounds adjustment to a factor of four; MTP is defined over a block and its 10 predecessors; a majority-hashrate miner can hold most headers near the MTP floor and set the period’s last block to wall-clock time; BIP54 specifies the two boundary constraints above and states a 38-day worst case to minimum difficulty.

Not established by those sources, and worth separating out: whether the attack has been executed on mainnet (no retrieved source says so); the current mainnet activation status of BIP54 (the BIP text carries a specification status, which does not establish consensus activation); and any specific sustained block rate as an observed outcome. Optech’s “six blocks per second” figure appears in an illustrative model under the MTP rule, not as a measurement. Treat it as model output.

The reviewer’s own inference, flagged as such: the attack’s economics are a separate question from its correctness. A fix that is specified is not a fix that is activated, and readers tracking this should follow the activation path independently of the specification text. The BIP’s own framing — that the bug “makes it harder to reason about miners’ incentives” and that accelerating the block rate “allows an attacker to steal block subsidy from future miners” — is an argument about incentive analysis, not a claim about observed behavior.

Open questions a spec reviewer should still ask

First, interaction with MTP-based timelocks. Since BIP113, timelocked contracts depend on MTP rather than header time. During a time-warp attack MTP advances more slowly than wall-clock time, which changes the effective lock duration for those contracts. The retrieved sources describe the mechanism but do not give a contract-level worked example; that analysis remains to be done against specific script templates.

Second, the testnet4 precedent. BIP54 references a ten-minute grace period adopted for testnet4 and a difficulty exception that pushed timestamps into the future when abused. A reviewer should read the testnet4 rule and the mainnet proposal side by side, because the grace-period difference is the kind of parameter that changes behavior at the margin.

Third, the activation path. BIP54 bundles the time-warp fix with signature-operation limits, 64-byte transaction invalidation, and coinbase nLockTime rules. Bundling amortizes deployment cost, as the BIP states, but it also means the time-warp fix’s activation is coupled to the acceptability of the other changes. That coupling is a governance question, not a cryptographic one, and it should be evaluated as such.

The narrow conclusion is the one the code supports: Bitcoin’s retarget is a two-point estimator, MTP constrains interior headers rather than the period boundary, and a majority miner can exploit that gap to hold difficulty down. BIP54’s boundary rules target exactly that gap. Whether they are activated, and what the grace period should be, are separate questions that the specification text does not settle.