Random delays are uniform up to a relative error of M / 2^63
ProvedOpenSearchElectionTimer.randomDelay_near_uniformFor all natural numbers M and d with 1 ≤ M ≤ 2^63 and 1 ≤ d ≤ M: 2 · ⌊2^63 / M⌋ ≤ #{x : Fin (2^64) | randomDelay M x = d} ≤ 2 · ⌈2^63 / M⌉, the ceiling being written (2^63 + M − 1) / M.
For a uniform draw, each delay d ∈ [1, M] has a probability between ⌊2^63 / M⌋ / 2^63 and ⌈2^63 / M⌉ / 2^63, that is, within a factor 1 ± M / 2^63 of 1 / M. For the default maximum timeout M = 10000 milliseconds, M / 2^63 is about 1.1 · 10^-15.
import Definitions.Def_OpenSearchElectionTimer open OpenSearchPreVote OpenSearchElectionTimer
theorem OpenSearchElectionTimer.randomDelay_near_uniform (M d : Nat) (hM : 1 ≤ M) (hM' : M ≤ 2 ^ 63)
(hd : 1 ≤ d) (hdM : d ≤ M) :
2 * (2 ^ 63 / M) ≤ (Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ∧
(Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ≤ 2 * ((2 ^ 63 + M - 1) / M) := 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_near_uniform
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. For , write . is natural-number floor division, which is when .
1. Declaration
theorem OpenSearchElectionTimer.randomDelay_near_uniform (M d : Nat) (hM : 1 ≤ M) (hM' : M ≤ 2 ^ 63)
(hd : 1 ≤ d) (hdM : d ≤ M) :
2 * (2 ^ 63 / M) ≤ (Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ∧
(Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ≤ 2 * ((2 ^ 63 + M - 1) / M)
Binders: .
Hypotheses, in order:
- ;
- ;
- ;
- .
Conclusion (in ):
The last equality uses .
Probability form. Dividing by :
This interval contains and has width at most . The width is exactly when divides , in which case the delay is exactly uniform.
2. What is not constrained
- Weaker than
randomDelay_card. That theorem gives exactly. Since , the present interval follows from it. - and are needed for the lower bound. For we have , while when .
- is not needed for truth. For :
- , so the lower bound is trivial;
- ;
- , so the upper bound still holds.
- is redundant. It follows from and .
- Idealization. As in the card theorem, the count is over all draws (a uniform 64-bit idealization), not over the values the real
java.util.Random.nextLong()can produce. - Satisfiable, not vacuous.
3. Plain-language summary
For a bound with , each delay value is produced by at least and at most of the draws. So under a uniform 64-bit draw, its probability is within of . Delays drawn this way are uniform on up to that rounding.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.