// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Serial number arithmetic (RFC 1982): comparing two numbers that wrap. //! //! A zone's serial is 32 bits and a zone outlives 4294967296 changes, so the //! number has to be allowed to wrap and comparing it as an integer is wrong at //! exactly the moment it matters. RFC 1982 defines the comparison that is //! right: `s1` is less than `s2` when the distance from `s1` forwards to `s2` //! is less than half the space. So 1 is *newer* than 0xffffffff, which is the //! whole point and the bug a `<` ships. //! //! ### Where it is used //! //! `Soa.serial`, which decides whether a secondary needs the zone again, and //! which an IXFR client compares against what a primary offers. `Rrsig`'s //! `inception` and `expiration` are the same arithmetic against the clock //! (RFC 4034 §3.1.5), and `compareTo` takes them too. //! //! ### Where it stops being an order //! //! Two serials exactly half the space apart -- `0` and `0x80000000` -- are //! each "less than" the other under the definition, so §3.2 leaves the result //! undefined. That is not an edge case to paper over: it is what makes this a //! comparison and not an ordering, and it is why `compare` answers with an //! optional rather than with `std.math.Order`. A caller meeting `null` has two //! serials that cannot be told apart, and the only honest thing to do is to //! refuse the transfer rather than to guess. //! //! The values themselves are ordinary `u32`s; nothing here wraps them in a //! type, because the arithmetic belongs to the comparison rather than to the //! number, and a serial that has been added to is still a serial. const std = @import("std"); /// Whether `a` is strictly before `b`, as RFC 1982 §3.2 defines it. /// /// False when the two are equal and false when they are incomparable, which /// means `!lt(a, b) and !lt(b, a)` is exactly "these two cannot be ordered" -- /// see `compare` for the version that says which of those it was. pub fn lt(a: u32, b: u32) bool { return a != b and b -% a < half; } test lt { try std.testing.expect(lt(1, 2)); // The serial after the largest one is 0, not something four billion // smaller. try std.testing.expect(lt(0xffffffff, 0)); // And half the space apart is neither. try std.testing.expect(!lt(0, 0x80000000) and !lt(0x80000000, 0)); } /// Whether `a` is strictly after `b`. pub fn gt(a: u32, b: u32) bool { return lt(b, a); } test gt { try std.testing.expect(gt(2, 1)); try std.testing.expect(gt(0, 0xffffffff)); } /// Whether `b` is `a` or anything after it. pub fn lte(a: u32, b: u32) bool { return a == b or lt(a, b); } test lte { try std.testing.expect(lte(7, 7)); try std.testing.expect(lte(0xffffffff, 0)); } /// Whether `b` is `a` or anything before it. pub fn gte(a: u32, b: u32) bool { return a == b or gt(a, b); } test gte { try std.testing.expect(gte(7, 7)); try std.testing.expect(gte(0, 0xffffffff)); } /// The comparison, with the case RFC 1982 §3.2 leaves undefined reported as /// `null` rather than decided. pub fn compare(a: u32, b: u32) ?std.math.Order { if (a == b) return .eq; if (lt(a, b)) return .lt; if (lt(b, a)) return .gt; return null; } test compare { // A secondary holding serial 0xffffffff, offered 1 after the wrap. try std.testing.expectEqual(std.math.Order.lt, compare(0xffffffff, 1).?); // Two serials no comparison can order: refuse the transfer. try std.testing.expectEqual(@as(?std.math.Order, null), compare(0, 0x80000000)); } /// Adding to a serial, which §3.1 allows only for an increment smaller than /// half the space: adding more than that produces a number this same /// arithmetic would then call *older*, so the result is undefined and this /// refuses rather than returning it. pub fn add(serial: u32, increment: u32) error{OutOfRange}!u32 { if (increment >= half) return error.OutOfRange; return serial +% increment; } test add { // Bumping across the wrap is fine, and the result compares as newer. const bumped = try add(0xffffffff, 2); try std.testing.expectEqual(@as(u32, 1), bumped); try std.testing.expect(gt(bumped, 0xffffffff)); // Half the space or more would compare as older, so it is refused. try std.testing.expectError(error.OutOfRange, add(5, 0x80000000)); } /// Half the space, which is the distance at which "before" stops meaning /// anything. const half = 1 << 31; const testing = std.testing; test "the ordinary case, where it is just a number" { try testing.expect(lt(1, 2)); try testing.expect(gt(2, 1)); try testing.expect(!lt(2, 1)); try testing.expect(!lt(1, 1)); try testing.expect(!gt(1, 1)); try testing.expect(lte(1, 1)); try testing.expect(gte(1, 1)); try testing.expectEqual(std.math.Order.lt, compare(1, 2).?); try testing.expectEqual(std.math.Order.gt, compare(2, 1).?); try testing.expectEqual(std.math.Order.eq, compare(7, 7).?); } test "the wrap, which is the whole reason this file exists" { // A zone that has been bumped 4294967296 times starts again at 0, and the // secondary holding 0xffffffff has to see that as older rather than as // newer by four billion. A client comparing with `<` concludes it is ahead // of the primary and never transfers again. const last = std.math.maxInt(u32); try testing.expect(lt(last, 0)); try testing.expect(lt(last, 1)); try testing.expect(gt(1, last)); try testing.expect(!lt(0, last)); try testing.expectEqual(std.math.Order.lt, compare(last, 1).?); // And the same a few steps either side of the seam. try testing.expect(lt(last - 2, 2)); try testing.expect(gt(2, last - 2)); } test "the two serials RFC 1982 refuses to order" { // §3.2: with exactly half the space between them, each is "less than" the // other by the definition, so there is no answer. `compare` says so; `lt` // says no in both directions, which is the same statement seen from the // other side. try testing.expectEqual(@as(?std.math.Order, null), compare(0, 1 << 31)); try testing.expectEqual(@as(?std.math.Order, null), compare(1 << 31, 0)); try testing.expect(!lt(0, 1 << 31)); try testing.expect(!lt(1 << 31, 0)); try testing.expect(!gt(0, 1 << 31)); // One step to either side of that distance and it is an order again. try testing.expect(lt(0, (1 << 31) - 1)); try testing.expect(gt(0, (1 << 31) + 1)); } test "adding, and the increment that is too large to mean anything" { try testing.expectEqual(@as(u32, 11), try add(10, 1)); try testing.expectEqual(@as(u32, 0), try add(std.math.maxInt(u32), 1)); try testing.expectEqual(@as(u32, 5), try add(std.math.maxInt(u32), 6)); // §3.1: the largest legal increment is one less than half the space, and // adding it leaves a number this arithmetic still calls newer. const most = (1 << 31) - 1; try testing.expect(gt(try add(100, most), 100)); try testing.expectError(error.OutOfRange, add(100, 1 << 31)); try testing.expectError(error.OutOfRange, add(100, std.math.maxInt(u32))); } test "every pair of serials is ordered the way the definition says" { // A brute-force check over a small ring, against the definition written // out longhand: `a < b` exactly when going forwards from `a` reaches `b` // in fewer than half the space's steps. It catches a sign error that the // hand-picked cases above would not, because it tries every pair rather // than the interesting ones. var a: u32 = 0; while (a < 64) : (a += 1) { var b: u32 = 0; while (b < 64) : (b += 1) { const forwards = b -% a; const expected = a != b and forwards < half; try testing.expectEqual(expected, lt(a, b)); } } // And the same around the wrap, where the numbers are large and the // distances are small. const base = std.math.maxInt(u32) - 32; var i: u32 = 0; while (i < 64) : (i += 1) { var j: u32 = 0; while (j < 64) : (j += 1) { const x = base +% i; const y = base +% j; try testing.expectEqual(i < j, lt(x, y)); } } }