Skip to content

math: Int.ProbablyPrime is slow for even integers #9269

@ALTree

Description

@ALTree

The ProbablyPrime function in math/big filters out multiples of small odd primes before performing the Miller-Rabin rounds.. but it doesn't check for parity if the tested number is bigger than a word.

This leads to odd performances:

func BenchmarkPP2(b *testing.B) {
    // 521419622856657689423872613771 * 2
    n := new(big.Int)
    n.SetString("1042839245713315378847745227542", 10)

    for i := 0; i < b.N; i++ {
        dummy = n.ProbablyPrime(30)
    }
}

func BenchmarkPP3(b *testing.B) {
    // 521419622856657689423872613771 * 3
    n := new(big.Int)
    n.SetString("1564258868569973068271617841313", 10)

    for i := 0; i < b.N; i++ {
        dummy = n.ProbablyPrime(30)
    }
}

Gives

PASS
BenchmarkPP2        5000        373102 ns/op
BenchmarkPP3     1000000          1322 ns/op

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions