test_prime.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # 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. """Tests prime functions."""
  15. import unittest
  16. import rsa.prime
  17. import rsa.randnum
  18. class PrimeTest(unittest.TestCase):
  19. def test_is_prime(self):
  20. """Test some common primes."""
  21. # Test some trivial numbers
  22. self.assertFalse(rsa.prime.is_prime(-1))
  23. self.assertFalse(rsa.prime.is_prime(0))
  24. self.assertFalse(rsa.prime.is_prime(1))
  25. self.assertTrue(rsa.prime.is_prime(2))
  26. self.assertFalse(rsa.prime.is_prime(42))
  27. self.assertTrue(rsa.prime.is_prime(41))
  28. # Test some slightly larger numbers
  29. self.assertEqual(
  30. [907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997],
  31. [x for x in range(901, 1000) if rsa.prime.is_prime(x)],
  32. )
  33. # Test around the 50th millionth known prime.
  34. self.assertTrue(rsa.prime.is_prime(982451653))
  35. self.assertFalse(rsa.prime.is_prime(982451653 * 961748941))
  36. def test_miller_rabin_primality_testing(self):
  37. """Uses monkeypatching to ensure certain random numbers.
  38. This allows us to predict/control the code path.
  39. """
  40. randints = []
  41. def fake_randint(maxvalue):
  42. return randints.pop(0)
  43. orig_randint = rsa.randnum.randint
  44. rsa.randnum.randint = fake_randint
  45. try:
  46. # 'n is composite'
  47. randints.append(2630484832) # causes the 'n is composite' case with n=3784949785
  48. self.assertEqual(False, rsa.prime.miller_rabin_primality_testing(2787998641, 7))
  49. self.assertEqual([], randints)
  50. # 'Exit inner loop and continue with next witness'
  51. randints.extend(
  52. [
  53. 2119139098, # causes 'Exit inner loop and continue with next witness'
  54. # the next witnesses for the above case:
  55. 3051067716,
  56. 3603501763,
  57. 3230895847,
  58. 3687808133,
  59. 3760099987,
  60. 4026931495,
  61. 3022471882,
  62. ]
  63. )
  64. self.assertEqual(
  65. True,
  66. rsa.prime.miller_rabin_primality_testing(2211417913, len(randints)),
  67. )
  68. self.assertEqual([], randints)
  69. finally:
  70. rsa.randnum.randint = orig_randint
  71. def test_mersenne_primes(self):
  72. """Tests first known Mersenne primes.
  73. Mersenne primes are prime numbers that can be written in the form
  74. `Mn = 2**n - 1` for some integer `n`. For the list of known Mersenne
  75. primes, see:
  76. https://en.wikipedia.org/wiki/Mersenne_prime#List_of_known_Mersenne_primes
  77. """
  78. # List of known Mersenne exponents.
  79. known_mersenne_exponents = [
  80. 2,
  81. 3,
  82. 5,
  83. 7,
  84. 13,
  85. 17,
  86. 19,
  87. 31,
  88. 61,
  89. 89,
  90. 107,
  91. 127,
  92. 521,
  93. 607,
  94. 1279,
  95. 2203,
  96. 2281,
  97. 4423,
  98. ]
  99. # Test Mersenne primes.
  100. for exp in known_mersenne_exponents:
  101. self.assertTrue(rsa.prime.is_prime(2 ** exp - 1))
  102. def test_get_primality_testing_rounds(self):
  103. """Test round calculation for primality testing."""
  104. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 63), 10)
  105. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 127), 10)
  106. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 255), 10)
  107. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 511), 7)
  108. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 767), 7)
  109. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 1023), 4)
  110. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 1279), 4)
  111. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 1535), 3)
  112. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 2047), 3)
  113. self.assertEqual(rsa.prime.get_primality_testing_rounds(1 << 4095), 3)