Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Two timers fire less than δ apart with probability at most about (2δ − 1)/Mmin

Proved
OpenSearchElectionTimer.close_pair_prob

by pitimespi · Nov 1, 2026 · Mathlib 0df444a (Lean v4.33.1)

cluster-coordinationconsensuselasticsearchelection-schedulerformal-verificationleader-electionlivenessmathlibopensearchprobabilityrandomized-timeoutunion-bound

For all integers a, b and natural numbers Ma, Mb, Mmin, δ with 1 ≤ Mmin, Mmin ≤ Mb ≤ 2^63 and 1 ≤ δ: the number of pairs of draws (x, y) : Fin (2^64) × Fin (2^64) with |(a + randomDelay Ma x) − (b + randomDelay Mb y)| < δ, divided by (2^64)^2, is at most (2δ − 1) · ⌈2^63 / Mmin⌉ / 2^63, where 2δ − 1 is computed in the natural numbers and ⌈2^63 / Mmin⌉ = (2^63 + Mmin − 1) / Mmin.

Two candidates with offsets a and b and independent uniform draws fire less than δ milliseconds apart with probability at most about (2δ − 1) / Mmin, whatever the offsets and whatever the first candidate's bound Ma, on which nothing is assumed: for each value of the first draw, the second delay must fall among the 2δ − 1 integers less than δ away from a fixed point. With δ = 100 and Mmin = 10000 the bound is about 1.99%.

Preamble
import Definitions.Def_OpenSearchElectionTimer
open OpenSearchPreVote OpenSearchElectionTimer
Formal statement
theorem OpenSearchElectionTimer.close_pair_prob (a b : Int) (Ma Mb Mmin δ : Nat) (hM : 1 ≤ Mmin)
    (hMb : Mmin ≤ Mb ∧ Mb ≤ 2 ^ 63) (hδ : 1 ≤ δ) :
    ((Finset.univ.filter (fun p : Fin (2 ^ 64) × Fin (2 ^ 64) =>
        |(a + randomDelay Ma p.1) - (b + randomDelay Mb p.2)| < δ)).card : ℚ) / (2 ^ 64) ^ 2 ≤
      (2 * δ - 1 : ℕ) * ((2 ^ 63 + Mmin - 1) / Mmin : ℕ) / 2 ^ 63 := by sorry
Source
OpenSearch b2a54ba5c0defba3d1d20423e911d4bf920bab19: ElectionSchedulerFactory (documented delay distribution lines 71-83, settings 85-115, constructor 124-144, nonNegative 159-161, toPositiveLongAtMost 163-172, scheduleNextElection 195-241 with the bound at 203, the delay at 204 and the next attempt scheduled with the election duration as grace period at 218): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/ElectionSchedulerFactory.java#L62-L254; Coordinator (the scheduler built over the node's Random lines 262-263, the scheduler started after bootstrapping at 1158 and when a candidate has found a quorum of peers 1534-1550, startElectionScheduler 1567-1618 with grace period 0 at 1574, the same Random used to choose a node to abdicate to at 1818): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java; DiscoveryModule (each node's Random created as new Random(Randomness.get().nextLong()), line 213): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java#L213; java.util.Random (a 48-bit linear congruential generator; nextLong): https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Random.html; maxDelay, nonNegative and toPositiveLongAtMost as transcribed by the OpenSearchPreVote mission
Read-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.close_pair_prob

Notions used

Draws. A draw is an element of Fin(264)\mathrm{Fin}(2^{64})Fin(264), read as the Java long toLong(x)=x−263\mathrm{toLong}(x) = x - 2^{63}toLong(x)=x−263.

nonNegative. nonNegative(n)=0\mathrm{nonNegative}(n) = 0nonNegative(n)=0 if n=−263n = -2^{63}n=−263, and ∣n∣|n|∣n∣ otherwise.

randomDelay. randomDelay(M,x)=(nonNegative(x−263) mod M)+1\mathrm{randomDelay}(M, x) = (\mathrm{nonNegative}(x - 2^{63}) \bmod M) + 1randomDelay(M,x)=(nonNegative(x−263)modM)+1, with the natural-number remainder.

  • For M≥1M \ge 1M≥1 the value is in [1,M][1, M][1,M].
  • For M=0M = 0M=0 the value is nonNegative(x−263)+1\mathrm{nonNegative}(x - 2^{63}) + 1nonNegative(x−263)+1, since Lean has a mod 0=aa \bmod 0 = aamod0=a.
  • For 1≤d≤M≤2631 \le d \le M \le 2^{63}1≤d≤M≤263, exactly 2(⌊(263−d)/M⌋+1)≤2⌈263/M⌉2(\lfloor (2^{63}-d)/M\rfloor + 1) \le 2\lceil 2^{63}/M\rceil2(⌊(263−d)/M⌋+1)≤2⌈263/M⌉ draws give value ddd. This is the content of the sibling theorems randomDelay_card and randomDelay_near_uniform; it is used here only to explain the bound.

contestedBound. contestedBound(n,δ,Mmin⁡)=(n2)(2δ−˙1)⌊(263+Mmin⁡−1)/Mmin⁡⌋/263\mathrm{contestedBound}(n, \delta, M_{\min}) = \binom{n}{2}(2\delta \mathbin{\dot-} 1)\lfloor (2^{63}+M_{\min}-1)/M_{\min}\rfloor / 2^{63}contestedBound(n,δ,Mmin​)=(2n​)(2δ−˙​1)⌊(263+Mmin​−1)/Mmin​⌋/263 in Q\mathbb{Q}Q. The present statement does not use the name, but its right-hand side equals contestedBound(2,δ,Mmin⁡)\mathrm{contestedBound}(2, \delta, M_{\min})contestedBound(2,δ,Mmin​), because (22)=1\binom{2}{2} = 1(22​)=1.

1. Declaration

theorem OpenSearchElectionTimer.close_pair_prob (a b : Int) (Ma Mb Mmin δ : Nat) (hM : 1 ≤ Mmin)
    (hMb : Mmin ≤ Mb ∧ Mb ≤ 2 ^ 63) (hδ : 1 ≤ δ) :
    ((Finset.univ.filter (fun p : Fin (2 ^ 64) × Fin (2 ^ 64) =>
        |(a + randomDelay Ma p.1) - (b + randomDelay Mb p.2)| < δ)).card : ℚ) / (2 ^ 64) ^ 2 ≤
      (2 * δ - 1 : ℕ) * ((2 ^ 63 + Mmin - 1) / Mmin : ℕ) / 2 ^ 63

Binders: a,b∈Za, b \in \mathbb{Z}a,b∈Z; Ma,Mb,Mmin⁡,δ∈NM_a, M_b, M_{\min}, \delta \in \mathbb{N}Ma​,Mb​,Mmin​,δ∈N.

Hypotheses, in order:

  • hM:1≤Mmin⁡hM : 1 \le M_{\min}hM:1≤Mmin​;
  • hMb:Mmin⁡≤Mb∧Mb≤263hMb : M_{\min} \le M_b \wedge M_b \le 2^{63}hMb:Mmin​≤Mb​∧Mb​≤263;
  • hδ:1≤δh\delta : 1 \le \deltahδ:1≤δ.

Conclusion (in Q\mathbb{Q}Q). For a pair of draws p=(p1,p2)∈Fin(264)2p = (p_1, p_2) \in \mathrm{Fin}(2^{64})^2p=(p1​,p2​)∈Fin(264)2, define the integers

T1(p)=a+randomDelay(Ma,p1),T2(p)=b+randomDelay(Mb,p2).T_1(p) = a + \mathrm{randomDelay}(M_a, p_1), \qquad T_2(p) = b + \mathrm{randomDelay}(M_b, p_2).T1​(p)=a+randomDelay(Ma​,p1​),T2​(p)=b+randomDelay(Mb​,p2​).

The theorem asserts

#{p∈Fin(264)2:∣T1(p)−T2(p)∣<δ}(264)2  ≤  (2δ−1)⋅⌈263/Mmin⁡⌉263.\frac{\#\{p \in \mathrm{Fin}(2^{64})^2 : |T_1(p) - T_2(p)| < \delta\}}{(2^{64})^2} \;\le\; \frac{(2\delta - 1) \cdot \lceil 2^{63}/M_{\min}\rceil}{2^{63}} .(264)2#{p∈Fin(264)2:∣T1​(p)−T2​(p)∣<δ}​≤263(2δ−1)⋅⌈263/Mmin​⌉​.

How the Lean terms elaborate:

  • The difference and the absolute value are computed in Z\mathbb{Z}Z, with δ\deltaδ cast to Z\mathbb{Z}Z. So ∣T1−T2∣<δ|T_1 - T_2| < \delta∣T1​−T2​∣<δ means ∣T1−T2∣≤δ−1|T_1 - T_2| \le \delta - 1∣T1​−T2​∣≤δ−1.
  • (2 * δ - 1 : ℕ) is a truncated natural subtraction. By hδh\deltahδ it equals 2δ−12\delta - 12δ−1.
  • ((2 ^ 63 + Mmin - 1) / Mmin : ℕ) is a natural floor division. By hMhMhM it equals ⌈263/Mmin⁡⌉\lceil 2^{63}/M_{\min}\rceil⌈263/Mmin​⌉.
  • Both natural numbers are cast to Q\mathbb{Q}Q.

Probability reading. p1p_1p1​ and p2p_2p2​ are independent and uniform on Fin(264)\mathrm{Fin}(2^{64})Fin(264). The probability that the two fire times T1T_1T1​ and T2T_2T2​ are less than δ\deltaδ apart is at most (2δ−1)⌈263/Mmin⁡⌉/263(2\delta-1)\lceil 2^{63}/M_{\min}\rceil/2^{63}(2δ−1)⌈263/Mmin​⌉/263, which is approximately (2δ−1)/Mmin⁡(2\delta - 1)/M_{\min}(2δ−1)/Mmin​.

2. What is not constrained

  • aaa and bbb are arbitrary integers. The bound does not depend on them.

  • MaM_aMa​ is completely unconstrained. It may be any natural number, including:

    • 000, where Lean's randomDelay(0,x)=nonNegative(x−263)+1\mathrm{randomDelay}(0, x) = \mathrm{nonNegative}(x - 2^{63}) + 1randomDelay(0,x)=nonNegative(x−263)+1. This is not a value Java produces, since Java asserts 0 < upperBound.
    • values above 2632^{63}263.

    The hypotheses are asymmetric: only MbM_bMb​ is bounded, from below by Mmin⁡M_{\min}Mmin​ and from above by 2632^{63}263. The bound therefore rests on the randomness of the second draw p2p_2p2​ alone. For each fixed p1p_1p1​, there are at most 2δ−12\delta - 12δ−1 integer values ddd with ∣T1−b−d∣<δ|T_1 - b - d| < \delta∣T1​−b−d∣<δ, and each value has at most 2⌈263/Mb⌉≤2⌈263/Mmin⁡⌉2\lceil 2^{63}/M_b\rceil \le 2\lceil 2^{63}/M_{\min}\rceil2⌈263/Mb​⌉≤2⌈263/Mmin​⌉ preimages.

  • Mmin⁡M_{\min}Mmin​ enters only as a lower bound on MbM_bMb​. The right side does not involve MbM_bMb​ itself.

  • hδh\deltahδ is not needed for truth. For δ=0\delta = 0δ=0 the event ∣⋅∣<0|\cdot| < 0∣⋅∣<0 is empty, and the right side is 000 (because 2⋅0−˙1=02 \cdot 0 \mathbin{\dot-} 1 = 02⋅0−˙​1=0). The result is 0≤00 \le 00≤0.

  • hMhMhM is needed. For Mmin⁡=0M_{\min} = 0Mmin​=0, Lean's division by zero makes the right side 000, while the left side can be positive.

  • The upper half of hMbhMbhMb (Mb≤263M_b \le 2^{63}Mb​≤263) appears not to be needed for truth. For Mb>263M_b > 2^{63}Mb​>263, each delay value has at most 222 preimages, and 2≤2⌈263/Mmin⁡⌉2 \le 2\lceil 2^{63}/M_{\min}\rceil2≤2⌈263/Mmin​⌉.

  • Trivial regime. When (2δ−1)⌈263/Mmin⁡⌉≥263(2\delta-1)\lceil 2^{63}/M_{\min}\rceil \ge 2^{63}(2δ−1)⌈263/Mmin​⌉≥263, roughly 2δ−1≥Mmin⁡2\delta - 1 \ge M_{\min}2δ−1≥Mmin​, the right side is at least 111 and the statement is trivially true.

  • The bound is attained. Take Ma=1M_a = 1Ma​=1 (so randomDelay(1,⋅)≡1\mathrm{randomDelay}(1, \cdot) \equiv 1randomDelay(1,⋅)≡1), a=b+100a = b + 100a=b+100, Mb=Mmin⁡=1024M_b = M_{\min} = 1024Mb​=Mmin​=1024 and δ=2\delta = 2δ=2. Then both sides equal 3/10243/10243/1024 (I computed this exactly). So the inequality cannot be improved in general.

  • Satisfiable, not vacuous. For example Mmin⁡=Mb=δ=1M_{\min} = M_b = \delta = 1Mmin​=Mb​=δ=1.

3. Plain-language summary

Two candidates fire at arbitrary integer offsets plus random delays drawn from two independent uniform 64-bit draws. The probability that they fire less than δ\deltaδ apart is at most (2δ−1)⌈263/Mmin⁡⌉/263(2\delta-1)\lceil 2^{63}/M_{\min}\rceil/2^{63}(2δ−1)⌈263/Mmin​⌉/263, about (2δ−1)/Mmin⁡(2\delta-1)/M_{\min}(2δ−1)/Mmin​. This requires the second candidate's bound to lie between Mmin⁡M_{\min}Mmin​ and 2632^{63}263; the first candidate's bound is unrestricted.

Human review
  • Endorsed by Shuze Chen · Nov 1, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Nov 1, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me