test_number.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Util/test_number.py: Self-test for parts of the Cryptodome.Util.number module
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Self-tests for (some of) Cryptodome.Util.number"""
  25. import math
  26. import unittest
  27. from Cryptodome.Util.py3compat import *
  28. from Cryptodome.SelfTest.st_common import list_test_cases
  29. from Cryptodome.Util import number
  30. from Cryptodome.Util.number import long_to_bytes
  31. class MyError(Exception):
  32. """Dummy exception used for tests"""
  33. # NB: In some places, we compare tuples instead of just output values so that
  34. # if any inputs cause a test failure, we'll be able to tell which ones.
  35. class MiscTests(unittest.TestCase):
  36. def test_ceil_div(self):
  37. """Util.number.ceil_div"""
  38. self.assertRaises(TypeError, number.ceil_div, "1", 1)
  39. self.assertRaises(ZeroDivisionError, number.ceil_div, 1, 0)
  40. self.assertRaises(ZeroDivisionError, number.ceil_div, -1, 0)
  41. # b = 1
  42. self.assertEqual(0, number.ceil_div(0, 1))
  43. self.assertEqual(1, number.ceil_div(1, 1))
  44. self.assertEqual(2, number.ceil_div(2, 1))
  45. self.assertEqual(3, number.ceil_div(3, 1))
  46. # b = 2
  47. self.assertEqual(0, number.ceil_div(0, 2))
  48. self.assertEqual(1, number.ceil_div(1, 2))
  49. self.assertEqual(1, number.ceil_div(2, 2))
  50. self.assertEqual(2, number.ceil_div(3, 2))
  51. self.assertEqual(2, number.ceil_div(4, 2))
  52. self.assertEqual(3, number.ceil_div(5, 2))
  53. # b = 3
  54. self.assertEqual(0, number.ceil_div(0, 3))
  55. self.assertEqual(1, number.ceil_div(1, 3))
  56. self.assertEqual(1, number.ceil_div(2, 3))
  57. self.assertEqual(1, number.ceil_div(3, 3))
  58. self.assertEqual(2, number.ceil_div(4, 3))
  59. self.assertEqual(2, number.ceil_div(5, 3))
  60. self.assertEqual(2, number.ceil_div(6, 3))
  61. self.assertEqual(3, number.ceil_div(7, 3))
  62. # b = 4
  63. self.assertEqual(0, number.ceil_div(0, 4))
  64. self.assertEqual(1, number.ceil_div(1, 4))
  65. self.assertEqual(1, number.ceil_div(2, 4))
  66. self.assertEqual(1, number.ceil_div(3, 4))
  67. self.assertEqual(1, number.ceil_div(4, 4))
  68. self.assertEqual(2, number.ceil_div(5, 4))
  69. self.assertEqual(2, number.ceil_div(6, 4))
  70. self.assertEqual(2, number.ceil_div(7, 4))
  71. self.assertEqual(2, number.ceil_div(8, 4))
  72. self.assertEqual(3, number.ceil_div(9, 4))
  73. def test_getPrime(self):
  74. """Util.number.getPrime"""
  75. self.assertRaises(ValueError, number.getPrime, -100)
  76. self.assertRaises(ValueError, number.getPrime, 0)
  77. self.assertRaises(ValueError, number.getPrime, 1)
  78. bits = 4
  79. for i in range(100):
  80. x = number.getPrime(bits)
  81. self.assertEqual(x >= (1 << bits - 1), 1)
  82. self.assertEqual(x < (1 << bits), 1)
  83. bits = 512
  84. x = number.getPrime(bits)
  85. self.assertNotEqual(x % 2, 0)
  86. self.assertEqual(x >= (1 << bits - 1), 1)
  87. self.assertEqual(x < (1 << bits), 1)
  88. def test_getStrongPrime(self):
  89. """Util.number.getStrongPrime"""
  90. self.assertRaises(ValueError, number.getStrongPrime, 256)
  91. self.assertRaises(ValueError, number.getStrongPrime, 513)
  92. bits = 512
  93. x = number.getStrongPrime(bits)
  94. self.assertNotEqual(x % 2, 0)
  95. self.assertEqual(x > (1 << bits-1)-1, 1)
  96. self.assertEqual(x < (1 << bits), 1)
  97. e = 2**16+1
  98. x = number.getStrongPrime(bits, e)
  99. self.assertEqual(number.GCD(x-1, e), 1)
  100. self.assertNotEqual(x % 2, 0)
  101. self.assertEqual(x > (1 << bits-1)-1, 1)
  102. self.assertEqual(x < (1 << bits), 1)
  103. e = 2**16+2
  104. x = number.getStrongPrime(bits, e)
  105. self.assertEqual(number.GCD((x-1)>>1, e), 1)
  106. self.assertNotEqual(x % 2, 0)
  107. self.assertEqual(x > (1 << bits-1)-1, 1)
  108. self.assertEqual(x < (1 << bits), 1)
  109. def test_isPrime(self):
  110. """Util.number.isPrime"""
  111. self.assertEqual(number.isPrime(-3), False) # Regression test: negative numbers should not be prime
  112. self.assertEqual(number.isPrime(-2), False) # Regression test: negative numbers should not be prime
  113. self.assertEqual(number.isPrime(1), False) # Regression test: isPrime(1) caused some versions of PyCryptodome to crash.
  114. self.assertEqual(number.isPrime(2), True)
  115. self.assertEqual(number.isPrime(3), True)
  116. self.assertEqual(number.isPrime(4), False)
  117. self.assertEqual(number.isPrime(2**1279-1), True)
  118. self.assertEqual(number.isPrime(-(2**1279-1)), False) # Regression test: negative numbers should not be prime
  119. # test some known gmp pseudo-primes taken from
  120. # http://www.trnicely.net/misc/mpzspsp.html
  121. for composite in (43 * 127 * 211, 61 * 151 * 211, 15259 * 30517,
  122. 346141 * 692281, 1007119 * 2014237, 3589477 * 7178953,
  123. 4859419 * 9718837, 2730439 * 5460877,
  124. 245127919 * 490255837, 963939391 * 1927878781,
  125. 4186358431 * 8372716861, 1576820467 * 3153640933):
  126. self.assertEqual(number.isPrime(int(composite)), False)
  127. def test_size(self):
  128. self.assertEqual(number.size(2),2)
  129. self.assertEqual(number.size(3),2)
  130. self.assertEqual(number.size(0xa2),8)
  131. self.assertEqual(number.size(0xa2ba40),8*3)
  132. self.assertEqual(number.size(0xa2ba40ee07e3b2bd2f02ce227f36a195024486e49c19cb41bbbdfbba98b22b0e577c2eeaffa20d883a76e65e394c69d4b3c05a1e8fadda27edb2a42bc000fe888b9b32c22d15add0cd76b3e7936e19955b220dd17d4ea904b1ec102b2e4de7751222aa99151024c7cb41cc5ea21d00eeb41f7c800834d2c6e06bce3bce7ea9a5), 1024)
  133. self.assertRaises(ValueError, number.size, -1)
  134. class LongTests(unittest.TestCase):
  135. def test1(self):
  136. self.assertEqual(long_to_bytes(0), b'\x00')
  137. self.assertEqual(long_to_bytes(1), b'\x01')
  138. self.assertEqual(long_to_bytes(0x100), b'\x01\x00')
  139. self.assertEqual(long_to_bytes(0xFF00000000), b'\xFF\x00\x00\x00\x00')
  140. self.assertEqual(long_to_bytes(0xFF00000000), b'\xFF\x00\x00\x00\x00')
  141. self.assertEqual(long_to_bytes(0x1122334455667788), b'\x11\x22\x33\x44\x55\x66\x77\x88')
  142. self.assertEqual(long_to_bytes(0x112233445566778899), b'\x11\x22\x33\x44\x55\x66\x77\x88\x99')
  143. def test2(self):
  144. self.assertEqual(long_to_bytes(0, 1), b'\x00')
  145. self.assertEqual(long_to_bytes(0, 2), b'\x00\x00')
  146. self.assertEqual(long_to_bytes(1, 3), b'\x00\x00\x01')
  147. self.assertEqual(long_to_bytes(65535, 2), b'\xFF\xFF')
  148. self.assertEqual(long_to_bytes(65536, 2), b'\x00\x01\x00\x00')
  149. self.assertEqual(long_to_bytes(0x100, 1), b'\x01\x00')
  150. self.assertEqual(long_to_bytes(0xFF00000001, 6), b'\x00\xFF\x00\x00\x00\x01')
  151. self.assertEqual(long_to_bytes(0xFF00000001, 8), b'\x00\x00\x00\xFF\x00\x00\x00\x01')
  152. self.assertEqual(long_to_bytes(0xFF00000001, 10), b'\x00\x00\x00\x00\x00\xFF\x00\x00\x00\x01')
  153. self.assertEqual(long_to_bytes(0xFF00000001, 11), b'\x00\x00\x00\x00\x00\x00\xFF\x00\x00\x00\x01')
  154. def test_err1(self):
  155. self.assertRaises(ValueError, long_to_bytes, -1)
  156. def get_tests(config={}):
  157. tests = []
  158. tests += list_test_cases(MiscTests)
  159. tests += list_test_cases(LongTests)
  160. return tests
  161. if __name__ == '__main__':
  162. suite = lambda: unittest.TestSuite(get_tests())
  163. unittest.main(defaultTest='suite')
  164. # vim:set ts=4 sw=4 sts=4 expandtab: