Section 19.3.4

Serial-number arithmetic and the half-range ambiguity

Definition

Serial-number arithmetic is a set of rules, defined in RFC 1982, for adding to and comparing counters that wrap around: after their largest value they continue from 0. For a ww-bit counter, a value bb is newer than a value aa when the counter needs between 1 and 2w−1−12^{w-1} - 1 steps forward to go from aa to bb, less than half its range. The half-range ambiguity is the case of two values exactly 2w−12^{w-1} steps apart: each is that far ahead of the other, so neither is newer.

Where it fits

Main entry: Counter wraparound (§6.21).

The Counter wraparound entry shows that a ww-bit counter records the true count only modulo 2w2^{w}, that is, as the remainder after division by 2w2^{w}. Ordinary unsigned comparison §2.12 ranks values by their size, from 0 to 2w−12^{w} - 1, so it fails once the counter wraps: a newer value can be numerically smaller. A 4-bit counter has 16 values, 0 to 15. If it reads 14 and then advances three steps, to 15, 0 and 1, unsigned comparison says 1<141 < 14 and calls the newer value older.

Serial-number arithmetic compares forward distance instead. Let d=(b−a)mod2wd = (b - a) \bmod 2^{w}, the number of steps forward from aa to bb. Then bb is newer than aa when 1≤d<2w−11 \le d < 2^{w-1}, older when d>2w−1d > 2^{w-1}, equal when d=0d = 0, and undefined when d=2w−1d = 2^{w-1}. In the example, d=(1−14)mod16=−13+16=3d = (1 - 14) \bmod 16 = -13 + 16 = 3, and 3<83 < 8, so 1 is newer than 14. In the other direction, (14−1)mod16=13>8(14 - 1) \bmod 16 = 13 > 8, so 14 is older than 1, which agrees.

The rule is right only when the true number of steps between the two values is less than half the range. The counter cannot tell 3 steps from 3+16=193 + 16 = 19: starting from 14, both end at 1, because (14+19)mod16=33mod16=1(14 + 19) \bmod 16 = 33 \bmod 16 = 1. After 11 steps the counter reads (14+11)mod16=25mod16=9(14 + 11) \bmod 16 = 25 \bmod 16 = 9, and d=(9−14)mod16=11>8d = (9 - 14) \bmod 16 = 11 > 8, so the rule calls the newer value older. RFC 1982, written for the serial numbers of DNS (Domain Name System) zones, accordingly defines addition only for increments of at most 2w−1−12^{w-1} - 1.

At exactly half the range the rule has no answer. On the 4-bit counter, 0 and 8 are 8 steps apart in both directions. RFC 1982 leaves such pairs undefined: an implementation may return either result or report an error, and users must keep such pairs from existing together. The comparison is also not transitive, so it is not an order in the mathematical sense; for a 2-bit counter, RFC 1982 lists 1>01 > 0, 2>12 > 1, 3>23 > 2 and 0>30 > 3, a cycle. A common implementation computes b−ab - a with wrapping, reads the result as a signed ww-bit number and calls bb newer if that number is positive. It matches the rule except at d=2w−1d = 2^{w-1}, which reads as −2w−1-2^{w-1} in both directions, so the implementation silently picks an answer for a pair that has none.

Limits and common mistakes

The rule needs a bound on how far apart compared values can be. A replay floor, the last counter value a receiver accepted, compared under this rule fails in two ways once that bound is broken. If the sender moves more than half the range ahead of the floor, the receiver refuses genuine new messages as old. If the floor moves more than half the range past a recorded old message, that message looks newer than the floor and is accepted again.

The rule authenticates nothing. A larger serial number from an unauthenticated source is not evidence of a newer state.

When a protocol cannot bound the distance, it needs more state: a counter too wide to wrap during the system’s life, such as a 64-bit counter, or an epoch, a second authenticated counter that the sender raises each time the first one wraps, with the pair (epoch, counter) compared in lexicographic order §19.3.3.

Questions answered

Why cannot ordinary unsigned comparison determine newer after wrap?

Because after a counter wraps, newer values are numerically smaller, and the stored value no longer records how many times the counter has wrapped. Ordinary unsigned comparison ranks values by size, so on a 4-bit counter that went 14, 15, 0, 1 it calls 1 older than 14. The reading 1 is also the same after 3 steps and after 19 steps, so no comparison of the two readings alone can recover the true order.

Serial-number arithmetic, defined in RFC 1982, compares the forward distance instead: for a ww-bit counter, bb is newer than aa when (b−a)mod2w(b - a) \bmod 2^{w} lies between 1 and 2w−1−12^{w-1} - 1. Here (1−14)mod16=3(1 - 14) \bmod 16 = 3, so 1 is newer. That rule is correct only when the true distance is less than half the range, 2w−12^{w-1}. It is undefined at exactly half, such as 0 and 8 on a 4-bit counter, and wrong beyond. A protocol that cannot bound the distance needs a counter too wide to wrap, or an authenticated epoch that counts the wraps.

Related entries: Event epoch and sequence identity (§15.36), Version high-water mark (§8.27), Unsigned modular arithmetic (§4.16), Signed versus unsigned comparison (§2.12), Replay attack (§1.26), Lexicographic version order versus componentwise order (§19.3.3). Further reading: [154].