Skip to content

math/big: ProbablyPrime skips Miller–Rabin rounds when n is MaxInt #81491

Open

Description

@filippo-agent

(*Int).ProbablyPrime(math.MaxInt) overflows the Miller–Rabin round count and skips that test entirely. As a result, it reports the composite integer 10877 as probably prime, contrary to the documented accuracy for inputs below 2^64.

x := big.NewInt(10877) // 73 * 149
fmt.Println(x.ProbablyPrime(0), x.ProbablyPrime(1), x.ProbablyPrime(math.MaxInt)) // false false true
Details

In src/math/big/prime.go, ProbablyPrime passes n+1 to probablyPrimeMillerRabin. For n == math.MaxInt, this becomes a negative int; the helper’s for i := 0; i < reps; i++ loop executes no rounds and returns true. Only the Lucas test remains after the earlier filters.

This issue was found by zkao and was triaged by @FiloSottile as a valid bug but not a vulnerability. /cc @golang/security

Activity

  1. gopherbot commented on Sep 17, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/834124 mentions this issue: math/big: avoid overflow in ProbablyPrime round count

  2. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Sep 18, 2026
  3. added this to the Go1.28 milestone on Sep 18, 2026
  4. cherrymui commented on Sep 18, 2026

    @cherrymui
    Member

    cc @griesemer @thatnealpatel @golang/security

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions