test_modmult.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #
  2. # SelfTest/Math/test_modmult.py: Self-test for custom modular multiplication
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2023, 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 modular multiplication"""
  34. import unittest
  35. from Crypto.SelfTest.st_common import list_test_cases
  36. from Crypto.Util.number import long_to_bytes, bytes_to_long
  37. from Crypto.Util._raw_api import (create_string_buffer,
  38. get_raw_buffer,
  39. c_size_t)
  40. from Crypto.Math._IntegerCustom import _raw_montgomery
  41. class ExceptionModulus(ValueError):
  42. pass
  43. def monty_mult(term1, term2, modulus):
  44. if term1 >= modulus:
  45. term1 %= modulus
  46. if term2 >= modulus:
  47. term2 %= modulus
  48. modulus_b = long_to_bytes(modulus)
  49. numbers_len = len(modulus_b)
  50. term1_b = long_to_bytes(term1, numbers_len)
  51. term2_b = long_to_bytes(term2, numbers_len)
  52. out = create_string_buffer(numbers_len)
  53. error = _raw_montgomery.monty_multiply(
  54. out,
  55. term1_b,
  56. term2_b,
  57. modulus_b,
  58. c_size_t(numbers_len)
  59. )
  60. if error == 17:
  61. raise ExceptionModulus()
  62. if error:
  63. raise ValueError("monty_multiply() failed with error: %d" % error)
  64. return get_raw_buffer(out)
  65. modulus1 = 0xd66691b20071be4d66d4b71032b37fa007cfabf579fcb91e50bfc2753b3f0ce7be74e216aef7e26d4ae180bc20d7bd3ea88a6cbf6f87380e613c8979b5b043b200a8ff8856a3b12875e36e98a7569f3852d028e967551000b02c19e9fa52e83115b89309aabb1e1cf1e2cb6369d637d46775ce4523ea31f64ad2794cbc365dd8a35e007ed3b57695877fbf102dbeb8b3212491398e494314e93726926e1383f8abb5889bea954eb8c0ca1c62c8e9d83f41888095c5e645ed6d32515fe0c58c1368cad84694e18da43668c6f43e61d7c9bca633ddcda7aef5b79bc396d4a9f48e2a9abe0836cc455e435305357228e93d25aaed46b952defae0f57339bf26f5a9
  66. class TestModMultiply(unittest.TestCase):
  67. def test_small(self):
  68. self.assertEqual(b"\x01", monty_mult(5, 6, 29))
  69. def test_large(self):
  70. numbers_len = (modulus1.bit_length() + 7) // 8
  71. t1 = modulus1 // 2
  72. t2 = modulus1 - 90
  73. expect = b'\x00' * (numbers_len - 1) + b'\x2d'
  74. self.assertEqual(expect, monty_mult(t1, t2, modulus1))
  75. def test_zero_term(self):
  76. numbers_len = (modulus1.bit_length() + 7) // 8
  77. expect = b'\x00' * numbers_len
  78. self.assertEqual(expect, monty_mult(0x100, 0, modulus1))
  79. self.assertEqual(expect, monty_mult(0, 0x100, modulus1))
  80. def test_larger_term(self):
  81. t1 = 2**2047
  82. expect_int = 0x8edf4071f78e3d7ba622cdbbbef74612e301d69186776ae6bf87ff38c320d9aebaa64889c2f67de2324e6bccd2b10ad89e91fd21ba4bb523904d033eff5e70e62f01a84f41fa90a4f248ef249b82e1d2729253fdfc2a3b5b740198123df8bfbf7057d03e15244ad5f26eb9a099763b5c5972121ec076b0bf899f59bd95f7cc129abddccf24217bce52ca0f3a44c9ccc504765dbb89734205f3ae6a8cc560494a60ea84b27d8e00fa24bdd5b4f1d4232edb61e47d3d984c1fa50a3820a2e580fbc3fc8bc11e99df53b9efadf5a40ac75d384e400905aa6f1d88950cd53b1c54dc2222115ad84a27260fa4d978155c1434c551de1ee7361a17a2f79d4388f78a5d
  83. res = bytes_to_long(monty_mult(t1, t1, modulus1))
  84. self.assertEqual(res, expect_int)
  85. def get_tests(config={}):
  86. tests = []
  87. tests += list_test_cases(TestModMultiply)
  88. return tests
  89. if __name__ == '__main__':
  90. def suite():
  91. return unittest.TestSuite(get_tests())
  92. unittest.main(defaultTest='suite')