Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Every random delay lies between one millisecond and the bound

Proved
OpenSearchElectionTimer.randomDelay_range

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

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

For every natural number M with 1 ≤ M and every draw x : Fin (2^64): 1 ≤ randomDelay M x and randomDelay M x ≤ M. The random part of an attempt's delay lies in (0, M], the range stated by the scheduler's documentation (ElectionSchedulerFactory.java:71-83, 163-167).

Preamble
import Definitions.Def_OpenSearchElectionTimer
open OpenSearchPreVote OpenSearchElectionTimer
Formal statement
theorem OpenSearchElectionTimer.randomDelay_range (M : Nat) (hM : 1 ≤ M) (x : Fin (2 ^ 64)) :
    1 ≤ randomDelay M x ∧ randomDelay M x ≤ M := 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.randomDelay_range

Notions used

Draws. A draw is an element xxx of Fin(264)={0,…,264−1}\mathrm{Fin}(2^{64}) = \{0, \dots, 2^{64}-1\}Fin(264)={0,…,264−1}. The draw is read as the Java long toLong(x)=x−263\mathrm{toLong}(x) = x - 2^{63}toLong(x)=x−263, an order-preserving bijection onto [−263,263−1][-2^{63}, 2^{63}-1][−263,263−1].

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

randomDelay. For M∈NM \in \mathbb{N}M∈N:

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

The remainder is the natural-number remainder, with Lean's convention a mod 0=aa \bmod 0 = aamod0=a. This transcribes Java's toPositiveLongAtMost(randomNumber, upperBound) = nonNegative(randomNumber) % upperBound + 1 (ESF:169-172), applied to the draw.

1. Declaration

theorem OpenSearchElectionTimer.randomDelay_range (M : Nat) (hM : 1 ≤ M) (x : Fin (2 ^ 64)) :
    1 ≤ randomDelay M x ∧ randomDelay M x ≤ M

Binders and hypotheses, in order:

  • M∈NM \in \mathbb{N}M∈N;
  • hM:1≤MhM : 1 \le MhM:1≤M;
  • x∈Fin(264)x \in \mathrm{Fin}(2^{64})x∈Fin(264), any of the 2642^{64}264 draws.

Conclusion (in N\mathbb{N}N):

1≤randomDelay(M,x)  ∧  randomDelay(M,x)≤M,1 \le \mathrm{randomDelay}(M, x) \;\wedge\; \mathrm{randomDelay}(M, x) \le M,1≤randomDelay(M,x)∧randomDelay(M,x)≤M,

that is,

1≤(nonNegative(x−263) mod M)+1≤M.1 \le \big(\mathrm{nonNegative}(x - 2^{63}) \bmod M\big) + 1 \le M .1≤(nonNegative(x−263)modM)+1≤M.

2. What is not constrained

  • No upper bound on MMM. Values above 263−12^{63}-1263−1, beyond any Java long, are allowed. For M>263M > 2^{63}M>263 the conclusion still holds, but the values reach at most 2632^{63}263, so [1,M][1, M][1,M] is not filled.
  • Pointwise only. The statement covers each draw xxx separately. It says nothing about how often each value occurs (see randomDelay_card and randomDelay_near_uniform), and it does not claim that every value in [1,M][1, M][1,M] is attained.
  • hMhMhM is necessary. For M=0M = 0M=0, Lean gives randomDelay(0,x)=nonNegative(x−263)+1≥1>0\mathrm{randomDelay}(0, x) = \mathrm{nonNegative}(x - 2^{63}) + 1 \ge 1 > 0randomDelay(0,x)=nonNegative(x−263)+1≥1>0, so the second conjunct fails. Java excludes M=0M = 0M=0 with an assertion.
  • Satisfiable, not vacuous. The hypothesis holds for example with M=1M = 1M=1.
  • Covers every Java long. toLong is a bijection onto the long range, so "for every xxx" amounts to "for every value random.nextLong() could return". This is a statement about the Lean function. The link to Java is the transcription, which matches ESF:169-172 for M≥1M \ge 1M≥1.

3. Plain-language summary

For any bound M≥1M \ge 1M≥1 and every possible 64-bit draw, the random part of an election attempt's delay is at least 111 and at most MMM. This matches the range "(0, upperBound]" documented on the Java method toPositiveLongAtMost. The theorem says nothing about the distribution of the delay.

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