Section 19.3.3

Lexicographic version order versus componentwise order

Definition

Lexicographic order and componentwise order are two rules for comparing tuples, fixed-length lists of numbers such as a version written (major, minor). Lexicographic order compares the first components and reads the next component only when they are equal, the way a dictionary orders words. Componentwise order, also called product order, puts one tuple at or below another only when every component is at or below the matching one. Lexicographic order ranks every pair; componentwise order leaves some pairs, such as (2,0)(2, 0) and (1,1000)(1, 1000), incomparable.

The idea

A device stores the version of its installed software as a pair of numbers and refuses updates that are older. Two teams read the pair differently. The release team means (major, minor): version 2.0 is the release after 1.1000, so (2,0)(2, 0) is newer than (1,1000)(1, 1000). The security team means two independent anti-rollback §8.26 counters, the first for the boot code and the second for the application, neither of which may ever go down. An update labeled (2,0)(2, 0) reaches a device at (1,1000)(1, 1000). The release team’s rule accepts it. The security team’s rule refuses it, because the application counter would fall from 1000 to 0. The bytes are the same, the decisions differ, and nothing in the pair says which rule applies.

Lexicographic order treats the components like the digits of a number: the leftmost one outranks everything after it, as the thousands digit alone decides between 2000 and 1999. Componentwise order treats the components as separate measurements that must all agree, like a rule that one candidate beats another only by being at least as good in every required skill. A candidate with more experience but less training is then neither better nor worse, and that missing answer is information, not a defect.

How it works

Lexicographic comparison of a=(a1,…,an)a = (a_1, \dots, a_n) and b=(b1,…,bn)b = (b_1, \dots, b_n):

  1. Compare a1a_1 with b1b_1. If they differ, the tuple with the smaller first component is the smaller tuple; stop.

  2. Otherwise compare a2a_2 with b2b_2 in the same way, and continue to the right.

  3. If every component is equal, the tuples are equal.

Componentwise comparison of the same tuples:

  1. If ai≤bia_i \le b_i at every position ii, then a≤ba \le b.

  2. If bi≤aib_i \le a_i at every position ii, then b≤ab \le a.

  3. If neither holds, aa and bb are incomparable.

The two orders agree in one direction. If a≤ba \le b componentwise, then a≤ba \le b lexicographically, because at the first position where the tuples differ, aa has the smaller component. The reverse fails. Lexicographic order thus extends componentwise order to a ranking of every pair.

Two habits produce lexicographic order, intended or not.

Each order has its standard uses. Semantic Versioning, a widely used convention for release numbers of the form major.minor.patch, compares the three numbers lexicographically, each as a number. Vector clocks, tuples with one counter per machine that distributed systems use to record which events could have influenced which, are compared componentwise. Two incomparable vector clocks belong to events of which neither could have influenced the other.

Worked example

Compare a=(2,0)a = (2, 0) with b=(1,1000)b = (1, 1000).

For contrast, compare (1,5)(1, 5) with (1,7)(1, 7). Lexicographically the first components tie at 1, and 5<75 < 7, so (1,5)(1, 5) is smaller. Componentwise 1≤11 \le 1 and 5≤75 \le 7, so again (1,5)≤(1,7)(1, 5) \le (1, 7). The two orders agree, as they must whenever one tuple is componentwise at or below the other.

Packing with a 16-bit minor field turns (1,1000)(1, 1000) into 1×65,536+1000=66,5361 \times 65{,}536 + 1000 = 66{,}536 and (2,0)(2, 0) into 2×65,536+0=131,0722 \times 65{,}536 + 0 = 131{,}072. Since 131,072>66,536131{,}072 > 66{,}536, the integers agree with lexicographic order. If the minor number outgrows its field, the packing breaks: (1,65,536)(1, 65{,}536) would become 65,536+65,536=131,07265{,}536 + 65{,}536 = 131{,}072, the same integer as (2,0)(2, 0).

Comparing the text 1.10 with the text 1.9: the first characters, both 1, match; the second characters, both full stops, match; the third characters are 1 and 9, and the character 1 comes first. So the text 1.10 sorts before 1.9, although minor version 10 is newer than minor version 9. Comparing the parts between full stops as numbers gives (1,10)>(1,9)(1, 10) > (1, 9), because 10>910 > 9.

Stated precisely

Let a=(a1,…,an)a = (a_1, \dots, a_n) and b=(b1,…,bn)b = (b_1, \dots, b_n) be tuples of nn integers.

Both are partial orders §19.2.6: each is reflexive (a≤aa \le a), antisymmetric (a≤ba \le b and b≤ab \le a together give a=ba = b) and transitive (a≤ba \le b and b≤cb \le c together give a≤ca \le c). Lexicographic order is also total: for all aa and bb, a≤lexba \le_{\mathrm{lex}} b or b≤lexab \le_{\mathrm{lex}} a. Componentwise order is not total once n≥2n \ge 2. Finally, a≤cba \le_{c} b implies a≤lexba \le_{\mathrm{lex}} b.

Packing: let ww be the width in bits of the second field. For pairs of non-negative integers with a2<2wa_2 < 2^{w} and b2<2wb_2 < 2^{w}, a<lexba <_{\mathrm{lex}} b holds exactly when a1⋅2w+a2<b1⋅2w+b2a_1 \cdot 2^{w} + a_2 < b_1 \cdot 2^{w} + b_2.

An anti-rollback rule built on either order keeps a stored floor ff, the tuple last accepted, and accepts a candidate tuple vv only if f≤vf \le v in the chosen order, or f<vf < v if reinstalling the same version must also be refused; it then sets f:=vf := v. It guarantees that the device never accepts a tuple below the floor in the chosen order. That guarantee rests on three assumptions: every party uses the same order; the tuple is authenticated, meaning it lies inside the bytes that a digital signature §5.14 covers; and every component stays inside its declared range.

Limits and common mistakes

The choice of order is part of the meaning of the field, not a detail of its encoding. Write it into the format specification. Two parties that compare the same tuple under different orders reach different decisions, as the two teams above do.

Componentwise anti-rollback refuses any update that lowers any component, so it cannot work with numbering that resets the minor number at each major release: (2,0)(2, 0) after (1,1000)(1, 1000) is refused. The smallest tuple at or above both, componentwise, is (max(2,1),max(0,1000))=(2,1000)(\max(2, 1), \max(0, 1000)) = (2, 1000), a tuple that neither team issued.

Lexicographic anti-rollback stops looking once an earlier component rises. If the second component versions the application, an update (2,0)(2, 0) carries an older application than an installed (1,1000)(1, 1000), and lexicographic order accepts it. If each component protects something on its own, lexicographic order is the wrong rule.

Tuples of different lengths need a stated rule. Dictionary order puts a prefix first, so (1,2)<(1,2,0)(1, 2) < (1, 2, 0), while release numbering usually treats a missing component as 0, which makes the two equal.

An order says which tuple is larger. It does not show that an authorized issuer produced the tuple, and it does not handle components that wrap around at a fixed width, which serial-number arithmetic §19.3.4 addresses.

Questions answered

Is (2,0)(2,0) newer than (1,1000)(1,1000)?

It depends on which order the format specifies. Under lexicographic order, which compares the first numbers and reads the second numbers only to break a tie, (2,0)(2,0) is newer because 2>12 > 1; the second numbers, 0 and 1000, are never compared. That is the right reading for release numbers of the form major.minor, where 2.0 follows 1.1000. Under componentwise order, which calls one tuple newer only when none of its numbers is smaller and at least one is larger, the two tuples are incomparable: the first number rose from 1 to 2, but the second fell from 1000 to 0. That is the right reading when the two numbers are independent anti-rollback counters that must each never decrease, and a device using it refuses (2,0)(2,0) after (1,1000)(1,1000).

The tuple cannot say which order applies, so the format specification must say it, and the tuple must be authenticated before either order is applied.

Related entries: Set inclusion and policy order (§15.40), Version high-water mark (§8.27), Counter wraparound (§6.21), Partial order, lattice, join, and meet (§19.2.6), Rollback versus anti-rollback (§8.26), Serial-number arithmetic and the half-range ambiguity (§19.3.4).