The exact distribution of a random delay
ProvedOpenSearchElectionTimer.randomDelay_cardFor all natural numbers M and d with 1 ≤ M ≤ 2^63 and 1 ≤ d ≤ M: the number of draws x : Fin (2^64) with randomDelay M x = d is exactly 2 · (⌊(2^63 − d) / M⌋ + 1).
Equivalently, writing 2^63 = qM + r with 0 ≤ r < M, each of the delays 1, …, r has probability (q + 1) / 2^63 and each of the delays r + 1, …, M has probability q / 2^63, for a draw uniform over the 2^64 longs: nonNegative maps the longs two-to-one onto [0, 2^63), and the residues modulo M of [0, 2^63) occur q + 1 or q times. The distribution is uniform exactly when M divides 2^63.
import Definitions.Def_OpenSearchElectionTimer open OpenSearchPreVote OpenSearchElectionTimer
theorem OpenSearchElectionTimer.randomDelay_card (M d : Nat) (hM : 1 ≤ M) (hM' : M ≤ 2 ^ 63)
(hd : 1 ≤ d) (hdM : d ≤ M) :
(Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card = 2 * ((2 ^ 63 - d) / M + 1) := by sorryRead-back
What the Lean code literally says, in plain math · claude-opus-5-5 (independent sub-agent, blind: Lean code + auditor instructions only)
Read-back: OpenSearchElectionTimer.randomDelay_card
Notions used
Draws. A draw is an element of , read as the Java long .
nonNegative. if , and otherwise.
randomDelay. , with the natural-number remainder (and ).
Notation. is the number of elements of a finite set . is natural-number floor division, and is truncated subtraction.
1. Declaration
theorem OpenSearchElectionTimer.randomDelay_card (M d : Nat) (hM : 1 ≤ M) (hM' : M ≤ 2 ^ 63)
(hd : 1 ≤ d) (hdM : d ≤ M) :
(Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card = 2 * ((2 ^ 63 - d) / M + 1)
Binders: .
Hypotheses, in order:
- ;
- ;
- ;
- .
Conclusion (an equation in ):
Because , the subtraction is never truncated.
Unfolded. The left side counts the draws with . Dividing by gives the probability of delay under a uniform 64-bit draw:
Equivalent form (my rewriting). Let and . The right side equals when , and when . Summing the right side over gives , as it must.
Examples:
- , : , i.e. every draw.
- : exactly draws for every .
- : here and . Each has draws, and each has .
2. What is not constrained
- is needed. For the true count is , but the right side would be .
- is needed. For example, with and the count is , but the right side is positive.
- is needed. If and , the count is (delays never exceed ), but the right side is .
- is redundant. It follows from and , since .
- is allowed. This is not a Java
longvalue (the maximum is ). The extension is harmless. - Idealization. The count is over all elements of , i.e. a uniform 64-bit draw. The definitions file's docstring acknowledges that Java's
nextLong()takes only distinct values. The theorem says nothing about the real generator. - Satisfiable, not vacuous. The hypotheses hold for example with .
3. Plain-language summary
Take a bound with and a value with . Exactly of the possible draws give random delay . So the delay is exactly uniform on when divides . Otherwise, the first values each have two more draws than the rest.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.