blob: 44d8d715e719e82bfa47b73987d78bf0fd74718e [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'
62 randints.append(2630484831) # causes the 'n is composite' case with n=3784949785
63 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([
68 2119139097, # causes 'Exit inner loop and continue with next witness'
69 # the next witnesses for the above case:
70 3051067715, 3603501762, 3230895846, 3687808132, 3760099986, 4026931494, 3022471881,
71 ])
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
78 def test_get_primality_testing_rounds(self):
79 """Test round calculation for primality testing."""
80
81 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 63), 10)
82 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 127), 10)
83 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 255), 10)
84 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 511), 7)
85 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 767), 7)
86 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 1023), 4)
87 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 1279), 4)
88 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 1535), 3)
89 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 2047), 3)
90 self.assertEquals(rsa.prime.get_primality_testing_rounds(1 << 4095), 3)