test_Primality.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Cryptodome.SelfTest.st_common import list_test_cases
  36. from Cryptodome.Util.py3compat import *
  37. from Cryptodome.Math.Numbers import Integer
  38. from Cryptodome.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 = (1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 2**127-1, 175637383534939453397801320455508570374088202376942372758907369518414308188137781042871856139027160010343454418881888953150175357127346872102307696660678617989191485418582475696230580407111841072614783095326672517315988762029036079794994990250662362650625650262324085116467511357592728695033227611029693067539)
  47. composites = (0, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 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. self.assertRaises(ValueError, miller_rabin_test, -1, 3)
  54. def test_lucas(self):
  55. for prime in self.primes:
  56. res = lucas_test(prime)
  57. self.assertEqual(res, PROBABLY_PRIME)
  58. for composite in self.composites:
  59. res = lucas_test(composite)
  60. self.assertEqual(res, COMPOSITE)
  61. self.assertRaises(ValueError, lucas_test, -1)
  62. def test_is_prime(self):
  63. primes = (170141183460469231731687303715884105727,
  64. 19175002942688032928599,
  65. 1363005552434666078217421284621279933627102780881053358473,
  66. 2 ** 521 - 1)
  67. for p in primes:
  68. self.assertEqual(test_probable_prime(p), PROBABLY_PRIME)
  69. not_primes = (
  70. 4754868377601046732119933839981363081972014948522510826417784001,
  71. 1334733877147062382486934807105197899496002201113849920496510541601,
  72. 260849323075371835669784094383812120359260783810157225730623388382401,
  73. )
  74. for np in not_primes:
  75. self.assertEqual(test_probable_prime(np), COMPOSITE)
  76. from Cryptodome.Util.number import sieve_base
  77. for p in sieve_base[:100]:
  78. res = test_probable_prime(p)
  79. self.assertEqual(res, PROBABLY_PRIME)
  80. def test_generate_prime_bit_size(self):
  81. p = generate_probable_prime(exact_bits=512)
  82. self.assertEqual(p.size_in_bits(), 512)
  83. def test_generate_prime_filter(self):
  84. def ending_with_one(number):
  85. return number % 10 == 1
  86. for x in range(20):
  87. q = generate_probable_prime(exact_bits=160,
  88. prime_filter=ending_with_one)
  89. self.assertEqual(q % 10, 1)
  90. def test_generate_safe_prime(self):
  91. p = generate_probable_safe_prime(exact_bits=161)
  92. self.assertEqual(p.size_in_bits(), 161)
  93. def get_tests(config={}):
  94. tests = []
  95. tests += list_test_cases(TestPrimality)
  96. return tests
  97. if __name__ == '__main__':
  98. suite = lambda: unittest.TestSuite(get_tests())
  99. unittest.main(defaultTest='suite')