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 -bit counter, a value is newer than a value when the counter needs between 1 and steps forward to go from to , less than half its range. The half-range ambiguity is the case of two values exactly 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 -bit counter records the true count only modulo , that is, as the remainder after division by . Ordinary unsigned comparison §2.12 ranks values by their size, from 0 to , 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 and calls the newer value older.
Serial-number arithmetic compares forward distance instead. Let , the number of steps forward from to . Then is newer than when , older when , equal when , and undefined when . In the example, , and , so 1 is newer than 14. In the other direction, , 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 : starting from 14, both end at 1, because . After 11 steps the counter reads , and , 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 .
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 , , and , a cycle. A common implementation computes with wrapping, reads the result as a signed -bit number and calls newer if that number is positive. It matches the rule except at , which reads as 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 -bit counter, is newer than when lies between 1 and . Here , so 1 is newer. That rule is correct only when the true distance is less than half the range, . 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].