Debian Codegraph @

lib/Crypto/SelfTest/Math/test_modmult.py

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