Debian Codegraph @

lib/Crypto/SelfTest/Math/test_modexp.py

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