test_prime.py 4.4 KB

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