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