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