Debian Codegraph @

lib/Crypto/SelfTest/Math/test_Primality.py

raw symbols SCIP
1 #
2 # SelfTest/Math/test_Primality.py: Self-test for Primality module
3 #
4 # ===================================================================
5 #
6 # Copyright (c) 2014, Legrandin <[email protected]>
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in
17 # the documentation and/or other materials provided with the
18 # distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 # ===================================================================
33
34 """Self-test for Math.Numbers"""
35
36 import unittest
37
38 from Crypto.SelfTest.st_common import list_test_cases
39
40 from Crypto.Util.py3compat import *
41
42 from Crypto.Math.Numbers import Integer
43 from Crypto.Math.Primality import (
44 PROBABLY_PRIME, COMPOSITE,
45 miller_rabin_test, lucas_test,
46 test_probable_prime,
47 generate_probable_prime,
48 generate_probable_safe_prime,
49 )
50
51
52 class TestPrimality(unittest.TestCase):
53
54 primes = (1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 2**127-1, 175637383534939453397801320455508570374088202376942372758907369518414308188137781042871856139027160010343454418881888953150175357127346872102307696660678617989191485418582475696230580407111841072614783095326672517315988762029036079794994990250662362650625650262324085116467511357592728695033227611029693067539)
55 composites = (0, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 7*23, (2**19-1)*(2**67-1), 9746347772161,)
56
57 def test_miller_rabin(self):
58 for prime in self.primes:
59 self.assertEqual(miller_rabin_test(prime, 3), PROBABLY_PRIME)
60 for composite in self.composites:
61 self.assertEqual(miller_rabin_test(composite, 3), COMPOSITE)
62 self.assertRaises(ValueError, miller_rabin_test, -1, 3)
63
64 def test_lucas(self):
65 for prime in self.primes:
66 res = lucas_test(prime)
67 self.assertEqual(res, PROBABLY_PRIME)
68 for composite in self.composites:
69 res = lucas_test(composite)
70 self.assertEqual(res, COMPOSITE)
71 self.assertRaises(ValueError, lucas_test, -1)
72
73 def test_is_prime(self):
74 primes = (170141183460469231731687303715884105727,
75 19175002942688032928599,
76 1363005552434666078217421284621279933627102780881053358473,
77 2 ** 521 - 1)
78 for p in primes:
79 self.assertEqual(test_probable_prime(p), PROBABLY_PRIME)
80
81 not_primes = (
82 4754868377601046732119933839981363081972014948522510826417784001,
83 1334733877147062382486934807105197899496002201113849920496510541601,
84 260849323075371835669784094383812120359260783810157225730623388382401,
85 )
86 for np in not_primes:
87 self.assertEqual(test_probable_prime(np), COMPOSITE)
88
89 from Crypto.Util.number import sieve_base
90 for p in sieve_base[:100]:
91 res = test_probable_prime(p)
92 self.assertEqual(res, PROBABLY_PRIME)
93
94 def test_generate_prime_bit_size(self):
95 p = generate_probable_prime(exact_bits=512)
96 self.assertEqual(p.size_in_bits(), 512)
97
98 def test_generate_prime_filter(self):
99 def ending_with_one(number):
100 return number % 10 == 1
101
102 for x in range(20):
103 q = generate_probable_prime(exact_bits=160,
104 prime_filter=ending_with_one)
105 self.assertEqual(q % 10, 1)
106
107 def test_generate_safe_prime(self):
108 p = generate_probable_safe_prime(exact_bits=161)
109 self.assertEqual(p.size_in_bits(), 161)
110
111 def get_tests(config={}):
112 tests = []
113 tests += list_test_cases(TestPrimality)
114 return tests
115
116 if __name__ == '__main__':
117 suite = lambda: unittest.TestSuite(get_tests())
118 unittest.main(defaultTest='suite')