...
1 package math
2
3 import (
4 "crypto/rand"
5 "io"
6 "math/big"
7 )
8
9
10
11
12
13 func IsSafePrime(p *big.Int) bool {
14 pdiv2 := new(big.Int).Rsh(p, 1)
15 return p.ProbablyPrime(20) && pdiv2.ProbablyPrime(20)
16 }
17
18
19
20
21 func SafePrime(random io.Reader, bits int) (*big.Int, error) {
22 one := big.NewInt(1)
23 p := new(big.Int)
24 for {
25 q, err := rand.Prime(random, bits-1)
26 if err != nil {
27 return nil, err
28 }
29 p.Lsh(q, 1).Add(p, one)
30 if p.ProbablyPrime(20) {
31 return p, nil
32 }
33 }
34 }
35
View as plain text