|
| 1 | +package math |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cloudflare/circl/internal/test" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSafePrime(t *testing.T) { |
| 13 | + firstSafePrimes := []int64{ |
| 14 | + 5, 7, 11, 23, 47, 59, 83, 107, 167, 179, 227, 263, 347, 359, 383, 467, |
| 15 | + 479, 503, 563, 587, 719, 839, 863, 887, 983, 1019, 1187, 1283, 1307, |
| 16 | + 1319, 1367, 1439, 1487, 1523, 1619, 1823, 1907, 2027, 2039, 2063, 2099, |
| 17 | + 2207, 2447, 2459, 2579, 2819, 2879, 2903, 2963, 2999, 3023, 3119, 3167, |
| 18 | + 3203, 3467, 3623, 3779, 3803, 3863, 3947, 4007, 4079, 4127, 4139, 4259, |
| 19 | + 4283, 4547, 4679, 4703, 4787, 4799, 4919, 5087, 5099, 5387, 5399, 5483, |
| 20 | + 5507, 5639, 5807, 5879, 5927, 5939, 6047, 6599, 6659, 6719, 6779, 6827, |
| 21 | + 6899, 6983, 7079, 7187, 7247, 7523, 7559, 7607, 7643, 7703, 7727, |
| 22 | + } |
| 23 | + |
| 24 | + p := new(big.Int) |
| 25 | + for _, pi := range firstSafePrimes { |
| 26 | + p.SetInt64(pi) |
| 27 | + test.CheckOk(IsSafePrime(p), fmt.Sprintf("it should be a safe prime p=%v", p), t) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestIsSafePrime(t *testing.T) { |
| 32 | + for i := 1; i < 5; i++ { |
| 33 | + bits := 128 * i |
| 34 | + t.Run(fmt.Sprint(bits), func(t *testing.T) { |
| 35 | + p, err := SafePrime(rand.Reader, bits) |
| 36 | + test.CheckNoErr(t, err, "safeprime failed") |
| 37 | + test.CheckOk(IsSafePrime(p), fmt.Sprintf("it should be a safe prime p=%v", p), t) |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func BenchmarkSafePrime(b *testing.B) { |
| 43 | + for i := 0; i < b.N; i++ { |
| 44 | + _, _ = SafePrime(rand.Reader, 256) |
| 45 | + } |
| 46 | +} |
0 commit comments