test_modexp.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #
  2. # SelfTest/Math/test_modexp.py: Self-test for module exponentiation
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2017, Helder Eijs <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 the custom module exponentiation"""
  34. import unittest
  35. from Cryptodome.SelfTest.st_common import list_test_cases
  36. from Cryptodome.Util.number import long_to_bytes, bytes_to_long
  37. from Cryptodome.Util.py3compat import *
  38. from Cryptodome.Util._raw_api import (load_pycryptodome_raw_lib,
  39. create_string_buffer,
  40. get_raw_buffer,
  41. c_size_t,
  42. c_ulonglong)
  43. from Cryptodome.Hash import SHAKE128
  44. from Cryptodome.Math.Numbers import Integer
  45. from Cryptodome.Math._IntegerCustom import _raw_montgomery
  46. from Cryptodome.Random.random import StrongRandom
  47. def create_rng(tag):
  48. rng = StrongRandom(SHAKE128.new(data=tag))
  49. return rng
  50. class ExceptionModulus(ValueError):
  51. pass
  52. def monty_pow(base, exp, modulus):
  53. max_len = len(long_to_bytes(max(base, exp, modulus)))
  54. base_b, exp_b, modulus_b = [ long_to_bytes(x, max_len) for x in
  55. (base, exp, modulus) ]
  56. out = create_string_buffer(max_len)
  57. error = _raw_montgomery.monty_pow(
  58. out,
  59. base_b,
  60. exp_b,
  61. modulus_b,
  62. c_size_t(max_len),
  63. c_ulonglong(32)
  64. )
  65. if error == 17:
  66. raise ExceptionModulus()
  67. if error:
  68. raise ValueError("monty_pow failed with error: %d" % error)
  69. result = bytes_to_long(get_raw_buffer(out))
  70. return result
  71. exponent1 = 0x2ce0af628901460a419a08ef950d498b9fd6f271a1a52ac293b86fe5c60efe8e8ba93fa1ebe1eb3d614d2e7b328cb60a2591440e163441a190ecf101ceec245f600fffdcf3f5b3a17a7baeacb96a424db1d7ec985e8ec998bb479fecfffed6a75f9a90fc97062fd973303bce855ad7b8d8272a94025e8532be9aabd54a183f303538d2a7e621b4131d59e823a4625f39bd7d518d7784f7c3a8f19061da74974ff42fa1c063dec2db97d461e291a7d6e721708a5229de166c1246363372854e27f3f08ae274bc16bfd205b028a4d81386494433d516dfbb35f495acba5e4e1d1843cb3c3129b6642a85fc7244ce5845fac071c7f622e4ee12ac43fabeeaa0cd01
  72. modulus1 = 0xd66691b20071be4d66d4b71032b37fa007cfabf579fcb91e50bfc2753b3f0ce7be74e216aef7e26d4ae180bc20d7bd3ea88a6cbf6f87380e613c8979b5b043b200a8ff8856a3b12875e36e98a7569f3852d028e967551000b02c19e9fa52e83115b89309aabb1e1cf1e2cb6369d637d46775ce4523ea31f64ad2794cbc365dd8a35e007ed3b57695877fbf102dbeb8b3212491398e494314e93726926e1383f8abb5889bea954eb8c0ca1c62c8e9d83f41888095c5e645ed6d32515fe0c58c1368cad84694e18da43668c6f43e61d7c9bca633ddcda7aef5b79bc396d4a9f48e2a9abe0836cc455e435305357228e93d25aaed46b952defae0f57339bf26f5a9
  73. class TestModExp(unittest.TestCase):
  74. def test_small(self):
  75. self.assertEqual(1, monty_pow(11,12,19))
  76. def test_large_1(self):
  77. base = 0xfffffffffffffffffffffffffffffffffffffffffffffffffff
  78. expected = pow(base, exponent1, modulus1)
  79. result = monty_pow(base, exponent1, modulus1)
  80. self.assertEqual(result, expected)
  81. def test_zero_exp(self):
  82. base = 0xfffffffffffffffffffffffffffffffffffffffffffffffffff
  83. result = monty_pow(base, 0, modulus1)
  84. self.assertEqual(result, 1)
  85. def test_zero_base(self):
  86. result = monty_pow(0, exponent1, modulus1)
  87. self.assertEqual(result, 0)
  88. def test_zero_modulus(self):
  89. base = 0xfffffffffffffffffffffffffffffffffffffffffffffffff
  90. self.assertRaises(ExceptionModulus, monty_pow, base, exponent1, 0)
  91. self.assertRaises(ExceptionModulus, monty_pow, 0, 0, 0)
  92. def test_larger_exponent(self):
  93. base = modulus1 - 0xFFFFFFF
  94. expected = pow(base, modulus1<<64, modulus1)
  95. result = monty_pow(base, modulus1<<64, modulus1)
  96. self.assertEqual(result, expected)
  97. def test_even_modulus(self):
  98. base = modulus1 >> 4
  99. self.assertRaises(ExceptionModulus, monty_pow, base, exponent1, modulus1-1)
  100. def test_several_lengths(self):
  101. prng = SHAKE128.new().update(b('Test'))
  102. for length in range(1, 100):
  103. modulus2 = Integer.from_bytes(prng.read(length)) | 1
  104. base = Integer.from_bytes(prng.read(length)) % modulus2
  105. exponent2 = Integer.from_bytes(prng.read(length))
  106. expected = pow(base, exponent2, modulus2)
  107. result = monty_pow(base, exponent2, modulus2)
  108. self.assertEqual(result, expected)
  109. def test_variable_exponent(self):
  110. prng = create_rng(b('Test variable exponent'))
  111. for i in range(20):
  112. for j in range(7):
  113. modulus = prng.getrandbits(8*30) | 1
  114. base = prng.getrandbits(8*30) % modulus
  115. exponent = prng.getrandbits(i*8+j)
  116. expected = pow(base, exponent, modulus)
  117. result = monty_pow(base, exponent, modulus)
  118. self.assertEqual(result, expected)
  119. exponent ^= (1 << (i*8+j)) - 1
  120. expected = pow(base, exponent, modulus)
  121. result = monty_pow(base, exponent, modulus)
  122. self.assertEqual(result, expected)
  123. def test_stress_63(self):
  124. prng = create_rng(b('Test 63'))
  125. length = 63
  126. for _ in range(2000):
  127. modulus = prng.getrandbits(8*length) | 1
  128. base = prng.getrandbits(8*length) % modulus
  129. exponent = prng.getrandbits(8*length)
  130. expected = pow(base, exponent, modulus)
  131. result = monty_pow(base, exponent, modulus)
  132. self.assertEqual(result, expected)
  133. def test_stress_64(self):
  134. prng = create_rng(b('Test 64'))
  135. length = 64
  136. for _ in range(2000):
  137. modulus = prng.getrandbits(8*length) | 1
  138. base = prng.getrandbits(8*length) % modulus
  139. exponent = prng.getrandbits(8*length)
  140. expected = pow(base, exponent, modulus)
  141. result = monty_pow(base, exponent, modulus)
  142. self.assertEqual(result, expected)
  143. def test_stress_65(self):
  144. prng = create_rng(b('Test 65'))
  145. length = 65
  146. for _ in range(2000):
  147. modulus = prng.getrandbits(8*length) | 1
  148. base = prng.getrandbits(8*length) % modulus
  149. exponent = prng.getrandbits(8*length)
  150. expected = pow(base, exponent, modulus)
  151. result = monty_pow(base, exponent, modulus)
  152. self.assertEqual(result, expected)
  153. def get_tests(config={}):
  154. tests = []
  155. tests += list_test_cases(TestModExp)
  156. return tests
  157. if __name__ == '__main__':
  158. suite = lambda: unittest.TestSuite(get_tests())
  159. unittest.main(defaultTest='suite')