test_Primality.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #
  2. # SelfTest/Math/test_Primality.py: Self-test for Primality module
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  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. """Self-test for Math.Numbers"""
  34. import unittest
  35. from Crypto.SelfTest.st_common import list_test_cases
  36. from Crypto.Util.py3compat import *
  37. from Crypto.Math.Numbers import Integer
  38. from Crypto.Math.Primality import (
  39. PROBABLY_PRIME, COMPOSITE,
  40. miller_rabin_test, lucas_test,
  41. test_probable_prime,
  42. generate_probable_prime,
  43. generate_probable_safe_prime,
  44. )
  45. class TestPrimality(unittest.TestCase):
  46. primes = (13, 17, 19, 23, 2**127-1,)
  47. composites = (12, 7*23, (2**19-1)*(2**67-1), 9746347772161,)
  48. def test_miller_rabin(self):
  49. for prime in self.primes:
  50. self.assertEqual(miller_rabin_test(prime, 3), PROBABLY_PRIME)
  51. for composite in self.composites:
  52. self.assertEqual(miller_rabin_test(composite, 3), COMPOSITE)
  53. def test_lucas(self):
  54. for prime in self.primes:
  55. self.assertEqual(lucas_test(prime), PROBABLY_PRIME)
  56. for composite in self.composites:
  57. self.assertEqual(lucas_test(composite), COMPOSITE)
  58. def test_is_prime(self):
  59. primes = (170141183460469231731687303715884105727,
  60. 19175002942688032928599,
  61. 1363005552434666078217421284621279933627102780881053358473,
  62. 2 ** 521 - 1)
  63. for p in primes:
  64. self.assertEqual(test_probable_prime(p), PROBABLY_PRIME)
  65. not_primes = (
  66. 4754868377601046732119933839981363081972014948522510826417784001,
  67. 1334733877147062382486934807105197899496002201113849920496510541601,
  68. 260849323075371835669784094383812120359260783810157225730623388382401,
  69. )
  70. for np in not_primes:
  71. self.assertEqual(test_probable_prime(np), COMPOSITE)
  72. def test_generate_prime_bit_size(self):
  73. p = generate_probable_prime(exact_bits=512)
  74. self.assertEqual(p.size_in_bits(), 512)
  75. def test_generate_prime_filter(self):
  76. def ending_with_one(number):
  77. return number % 10 == 1
  78. for x in xrange(20):
  79. q = generate_probable_prime(exact_bits=160,
  80. prime_filter=ending_with_one)
  81. self.assertEqual(q % 10, 1)
  82. def test_generate_safe_prime(self):
  83. p = generate_probable_safe_prime(exact_bits=161)
  84. self.assertEqual(p.size_in_bits(), 161)
  85. def get_tests(config={}):
  86. tests = []
  87. tests += list_test_cases(TestPrimality)
  88. return tests
  89. if __name__ == '__main__':
  90. suite = lambda: unittest.TestSuite(get_tests())
  91. unittest.main(defaultTest='suite')