blob: 5577f679b1db9ecbf75f66f1fe3051ceb528f2f7 [file] [log] [blame]
adamantiked0f19252016-01-28 14:06:13 -03001# Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Tests prime functions."""
16
17import unittest
18
19import rsa.prime
Sybren A. Stüvel9a9e08c2016-03-29 19:05:23 +020020import rsa.randnum
Sybren A. Stüvelcfff5bf2016-03-17 12:01:23 +010021
22
23class PrimeTest(unittest.TestCase):
24 def test_is_prime(self):
25 """Test some common primes."""
26
27 # Test some trivial numbers
28 self.assertFalse(rsa.prime.is_prime(-1))
29 self.assertFalse(rsa.prime.is_prime(0))
30 self.assertFalse(rsa.prime.is_prime(1))
31 self.assertTrue(rsa.prime.is_prime(2))
32 self.assertFalse(rsa.prime.is_prime(42))
33 self.assertTrue(rsa.prime.is_prime(41))
34
35 # Test some slightly larger numbers
36 self.assertEqual(
37 [907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997],
38 [x for x in range(901, 1000) if rsa.prime.is_prime(x)]
39 )
40
41 # Test around the 50th millionth known prime.
42 self.assertTrue(rsa.prime.is_prime(982451653))
43 self.assertFalse(rsa.prime.is_prime(982451653 * 961748941))
Sybren A. Stüvel9a9e08c2016-03-29 19:05:23 +020044
45 def test_miller_rabin_primality_testing(self):
46 """Uses monkeypatching to ensure certain random numbers.
47
48 This allows us to predict/control the code path.
49 """
50
51 randints = []
52
53 def fake_randint(maxvalue):
54 return randints.pop(0)
55
56 orig_randint = rsa.randnum.randint
57 rsa.randnum.randint = fake_randint
58 try:
59 # 'n is composite'
adamantikef5200142016-03-28 15:29:59 -030060 randints.append(2630484832) # causes the 'n is composite' case with n=3784949785
Sybren A. Stüvel9a9e08c2016-03-29 19:05:23 +020061 self.assertEqual(False, rsa.prime.miller_rabin_primality_testing(2787998641, 7))
62 self.assertEqual([], randints)
63
64 # 'Exit inner loop and continue with next witness'
65 randints.extend([
adamantikef5200142016-03-28 15:29:59 -030066 2119139098, # causes 'Exit inner loop and continue with next witness'
Sybren A. Stüvel9a9e08c2016-03-29 19:05:23 +020067 # the next witnesses for the above case:
adamantikef5200142016-03-28 15:29:59 -030068 3051067716, 3603501763, 3230895847, 3687808133, 3760099987, 4026931495, 3022471882,
Sybren A. Stüvel9a9e08c2016-03-29 19:05:23 +020069 ])
70 self.assertEqual(True, rsa.prime.miller_rabin_primality_testing(2211417913,
71 len(randints)))
72 self.assertEqual([], randints)
73 finally:
74 rsa.randnum.randint = orig_randint
adamantike38a72552016-03-28 13:12:12 -030075
adamantikea72efaa2016-04-23 10:35:26 +020076 def test_mersenne_primes(self):
77 """Tests first known Mersenne primes.
78
79 Mersenne primes are prime numbers that can be written in the form
80 `Mn = 2**n - 1` for some integer `n`. For the list of known Mersenne
81 primes, see:
82 https://en.wikipedia.org/wiki/Mersenne_prime#List_of_known_Mersenne_primes
83 """
84
85 # List of known Mersenne exponents.
86 known_mersenne_exponents = [
87 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279,
88 2203, 2281, 4423,
89 ]
90
91 # Test Mersenne primes.
92 for exp in known_mersenne_exponents:
93 self.assertTrue(rsa.prime.is_prime(2**exp - 1))
94
adamantike38a72552016-03-28 13:12:12 -030095 def test_get_primality_testing_rounds(self):
96 """Test round calculation for primality testing."""
97
Michael Manganiello7e5854d2016-05-08 08:18:29 -030098 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 63), 10)
99 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 127), 10)
100 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 255), 10)
101 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 511), 7)
102 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 767), 7)
103 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 1023), 4)
104 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 1279), 4)
105 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 1535), 3)
106 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 2047), 3)
107 self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 4095), 3)