Commit 4f2cfdc7 authored by Shenghou Ma's avatar Shenghou Ma

crypto/rand: support generation of 2-5 bit primes, also document the error return for Prime

Fixes #6849.
Fixes #6867.

R=golang-dev, agl
CC=golang-dev
https://golang.org/cl/35870043
parent aa0ae755
......@@ -27,9 +27,11 @@ var smallPrimesProduct = new(big.Int).SetUint64(16294579238595022365)
// Prime returns a number, p, of the given size, such that p is prime
// with high probability.
// Prime will return error for any error returned by rand.Read or if bits < 2.
func Prime(rand io.Reader, bits int) (p *big.Int, err error) {
if bits < 1 {
err = errors.New("crypto/rand: prime size must be positive")
if bits < 2 {
err = errors.New("crypto/rand: prime size must be at least 2-bit")
return
}
b := uint(bits % 8)
......@@ -79,7 +81,7 @@ func Prime(rand io.Reader, bits int) (p *big.Int, err error) {
for delta := uint64(0); delta < 1<<20; delta += 2 {
m := mod + delta
for _, prime := range smallPrimes {
if m%uint64(prime) == 0 {
if m%uint64(prime) == 0 && (bits > 6 || m != uint64(prime)) {
continue NextDelta
}
}
......
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rand_test
import (
"crypto/rand"
"testing"
)
// http://golang.org/issue/6849.
func TestPrimeSmall(t *testing.T) {
for n := 2; n < 10; n++ {
p, err := rand.Prime(rand.Reader, n)
if err != nil {
t.Fatalf("Can't generate %d-bit prime: %v", n, err)
}
if p.BitLen() != n {
t.Fatalf("%v is not %d-bit", p, n)
}
if !p.ProbablyPrime(32) {
t.Fatalf("%v is not prime", p)
}
}
}
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