1
0
mirror of https://github.com/golang/go synced 2024-11-23 22:00:11 -07:00

math/big: correct documentation for ProbablyPrime.

As akalin points out in the bug, the comment previously claimed that the
probability that the input is prime given that the function returned
true is 1 - ¼ⁿ. But that's wrong: the correct statement is that the
probability of the function returning false given a composite input is
1 - ¼ⁿ.

This is not nearly as helpful, but at least it's truthful. A number of
other (correct) expressions are suggested on the bug, but I think that
the simplier one is preferable.

This change also notes that the function is not suitable for
adversarial inputs since it's deterministic.

Fixes #12274.

Change-Id: I6a0871d103b126ee5a5a922a8c6993055cb7b1ed
Reviewed-on: https://go-review.googlesource.com/14052
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Adam Langley 2015-08-30 09:21:35 -07:00
parent be16001187
commit 5d5889c4d9
2 changed files with 11 additions and 5 deletions

View File

@ -551,8 +551,11 @@ func (z *Int) binaryGCD(a, b *Int) *Int {
} }
// ProbablyPrime performs n Miller-Rabin tests to check whether x is prime. // ProbablyPrime performs n Miller-Rabin tests to check whether x is prime.
// If it returns true, x is prime with probability 1 - 1/4^n. // If x is prime, it returns true.
// If it returns false, x is not prime. n must be > 0. // If x is not prime, it returns false with probability at least 1 - ¼ⁿ.
//
// It is not suitable for judging primes that an adversary may have crafted
// to fool this test.
func (x *Int) ProbablyPrime(n int) bool { func (x *Int) ProbablyPrime(n int) bool {
if n <= 0 { if n <= 0 {
panic("non-positive n for ProbablyPrime") panic("non-positive n for ProbablyPrime")

View File

@ -1121,9 +1121,12 @@ func (z nat) expNNMontgomery(x, y, m nat) nat {
return zz.norm() return zz.norm()
} }
// probablyPrime performs reps Miller-Rabin tests to check whether n is prime. // probablyPrime performs n Miller-Rabin tests to check whether x is prime.
// If it returns true, n is prime with probability 1 - 1/4^reps. // If x is prime, it returns true.
// If it returns false, n is not prime. // If x is not prime, it returns false with probability at least 1 - ¼ⁿ.
//
// It is not suitable for judging primes that an adversary may have crafted
// to fool this test.
func (n nat) probablyPrime(reps int) bool { func (n nat) probablyPrime(reps int) bool {
if len(n) == 0 { if len(n) == 0 {
return false return false