Commit 43178697 authored by Shenghou Ma's avatar Shenghou Ma Committed by Minux Ma

math/big: panic if n <= 0 for ProbablyPrime

Fixes #9509

Change-Id: I3b86745d38e09093fe2f4b918d774bd6608727d7
Reviewed-on: https://go-review.googlesource.com/2313Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent b70ddc0b
......@@ -736,8 +736,11 @@ func (z *Int) binaryGCD(a, b *Int) *Int {
// 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 it returns false, x is not prime.
// If it returns false, x is not prime. n must be >0.
func (x *Int) ProbablyPrime(n int) bool {
if n <= 0 {
panic("non-positive n for ProbablyPrime")
}
return !x.neg && x.abs.probablyPrime(n)
}
......
......@@ -989,6 +989,21 @@ func TestProbablyPrime(t *testing.T) {
break
}
}
// check that ProbablyPrime panics if n <= 0
c := NewInt(11) // a prime
for _, n := range []int{-1, 0, 1} {
func() {
defer func() {
if n <= 0 && recover() == nil {
t.Fatalf("expected panic from ProbablyPrime(%d)", n)
}
}()
if !c.ProbablyPrime(n) {
t.Fatalf("%v should be a prime", c)
}
}()
}
}
type intShiftTest struct {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment