Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

nonNegative maps the Java longs exactly two-to-one onto [0, 2^63)

Proved
OpenSearchPreVote.nonNegative_two_to_one

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

cluster-coordinationconsensuselasticsearchelection-schedulerformal-verificationleader-electionopensearchpaxospre-voteraftzen2

For every natural number v<263v < 2^{63}v<263 and every integer nnn with −263≤n<263-2^{63} \le n < 2^{63}−263≤n<263 (a Java long): nonNegative n = v iff n=vn = vn=v, or n=−vn = -vn=−v when v>0v > 0v>0, respectively n=−263n = -2^{63}n=−263 (Long.MIN_VALUE) when v=0v = 0v=0. Every value in [0,263)[0, 2^{63})[0,263) has exactly two preimages among the 2642^{64}264 longs, so a uniformly distributed long gives a uniformly distributed nonNegative value. The theorem is the counting statement; no probability is formalized.

Preamble
import Definitions.Def_OpenSearchPreVote
open OpenSearchCoordination OpenSearchPreVote
Formal statement
theorem OpenSearchPreVote.nonNegative_two_to_one (v : Nat) (hv : v < 2 ^ 63) (n : Int)
    (hn : -2 ^ 63 ≤ n ∧ n < 2 ^ 63) :
    nonNegative n = v ↔ n = v ∨ n = (if v = 0 then -2 ^ 63 else -(v : Int)) := by sorry
Source
OpenSearch b2a54ba5c0defba3d1d20423e911d4bf920bab19: PreVoteCollector (state line 79, start 111-115, update 128-131, handlePreVoteRequest 133-163, PreVotingRound 175-300, handlePreVoteResponse 225-279, close 296-299): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/PreVoteCollector.java#L66-L301; Coordinator (updateMaxTermSeen lines 560-581, startElection 583-602, nodeMayWinElection 628-633, getPreVoteResponse 873-879, handleJoin 1267-1299, startElectionScheduler 1567-1618, the preVoteCollector.update calls 516, 653, 777, 801, 839, 1157): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java#L560-L633; ElectionSchedulerFactory (settings lines 85-115, constructor check 133-143, nonNegative 159-161, toPositiveLongAtMost 169-172, scheduleNextElection 195-241): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/ElectionSchedulerFactory.java#L62-L254; ElectionStrategy.isElectionQuorum (lines 68-88): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/ElectionStrategy.java#L68-L88; the messages PreVoteRequest.java and PreVoteResponse.java in the same directory; CoordinationState (handleStartJoin, handleJoin) as transcribed by the OpenSearchCoordination 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: OpenSearchPreVote.nonNegative_two_to_one

Notions used

nonNegative\mathrm{nonNegative}nonNegative. For any integer nnn,

nonNegative(n)={0if n=−263,∣n∣∈Notherwise.\mathrm{nonNegative}(n) = \begin{cases} 0 & \text{if } n = -2^{63},\\ |n| \in \mathbb{N} & \text{otherwise.}\end{cases}nonNegative(n)={0∣n∣∈N​if n=−263,otherwise.​

The Lean text -2 ^ 63 parses as −(263)=−9223372036854775808-(2^{63}) = -9223372036854775808−(263)=−9223372036854775808, which is Java's Long.MIN_VALUE. I confirmed the parse with pp.parens; (−2)63(-2)^{63}(−2)63 would give the same value anyway, since 636363 is odd.

1. Declaration

Binders and hypotheses.

  • v∈Nv \in \mathbb{N}v∈N;
  • hvhvhv: v<263v < 2^{63}v<263;
  • n∈Zn \in \mathbb{Z}n∈Z;
  • hnhnhn: −263≤n∧n<263-2^{63} \le n \wedge n < 2^{63}−263≤n∧n<263, that is, nnn is in Java's long range.

Conclusion.

nonNegative(n)=v  ⟺  n=v  ∨  n={−263if v=0,−vif v≠0.\mathrm{nonNegative}(n) = v \iff n = v \;\vee\; n = \begin{cases} -2^{63} & \text{if } v = 0,\\ -v & \text{if } v \neq 0.\end{cases}nonNegative(n)=v⟺n=v∨n={−263−v​if v=0,if v=0.​

Here vvv on the right is cast to an integer.

Unfolded:

  • For v=0v = 0v=0, the longs mapped to 000 are exactly 000 and −263-2^{63}−263.
  • For 0<v<2630 < v < 2^{63}0<v<263, the longs mapped to vvv are exactly vvv and −v-v−v.

In both cases the two candidates are distinct and lie in [−263,263)[-2^{63}, 2^{63})[−263,263). So every value in [0,263)[0, 2^{63})[0,263) has exactly two preimages among the longs, and nonNegative\mathrm{nonNegative}nonNegative is exactly two-to-one from the longs onto [0,263)[0, 2^{63})[0,263). That the candidates lie in range is elementary arithmetic and is not itself part of the statement.

2. What is not constrained

  • Integers outside the long range. hnhnhn excludes them, so nothing is said about them. There nonNegative\mathrm{nonNegative}nonNegative is just ∣n∣|n|∣n∣, so the preimage sets differ. For example, nonNegative(−263−1)=nonNegative(263+1)=263+1\mathrm{nonNegative}(-2^{63} - 1) = \mathrm{nonNegative}(2^{63} + 1) = 2^{63} + 1nonNegative(−263−1)=nonNegative(263+1)=263+1, which I checked by evaluation.
  • Values v≥263v \ge 2^{63}v≥263. hvhvhv excludes them. The theorem does not state that no long maps to such a value, although that is true by the definition.
  • What is not stated. Nothing is said about toPositiveLongAtMost\mathrm{toPositiveLongAtMost}toPositiveLongAtMost or about the uniformity of the derived delays, for example the bias introduced by  mod \bmodmod when the bound does not divide 2632^{63}263.
  • Vacuity. None. Both hypotheses are satisfiable, for example with v=0v = 0v=0 and n=0n = 0n=0.

3. Plain-language summary

Restricted to Java long values, nonNegative\mathrm{nonNegative}nonNegative sends exactly vvv and −v-v−v to each vvv with 0<v<2630 < v < 2^{63}0<v<263, and exactly 000 and Long.MIN_VALUE to 000. Each value in [0,263)[0, 2^{63})[0,263) therefore has exactly two long preimages.

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