blob: 4f12d9a865c6aa154c2f10aa350e0cc96a75edd4 [file] [log] [blame]
Janos Follathdf8239b2022-11-02 14:40:58 +00001"""Framework classes for generation of bignum mod_raw test cases."""
2# Copyright The Mbed TLS Contributors
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Janos Follathdf8239b2022-11-02 14:40:58 +000017from abc import ABCMeta
Minos Galanakise9c86a12022-11-09 11:46:47 +000018from typing import Dict, Iterator, List
Janos Follathdf8239b2022-11-02 14:40:58 +000019
Minos Galanakise9c86a12022-11-09 11:46:47 +000020from . import test_case
Janos Follathdf8239b2022-11-02 14:40:58 +000021from . import test_data_generation
Minos Galanakise9c86a12022-11-09 11:46:47 +000022from . import bignum_common
Janos Follathdf8239b2022-11-02 14:40:58 +000023
Janos Follath0cd89672022-11-09 12:14:14 +000024class BignumModRawTarget(test_data_generation.BaseTarget):
25 #pylint: disable=abstract-method, too-few-public-methods
Janos Follathdf8239b2022-11-02 14:40:58 +000026 """Target for bignum mod_raw test case generation."""
27 target_basename = 'test_suite_bignum_mod_raw.generated'
28
Minos Galanakise9c86a12022-11-09 11:46:47 +000029class BignumModRawOperation(bignum_common.OperationCommon, BignumModRawTarget, metaclass=ABCMeta):
30 #pylint: disable=abstract-method
Minos Galanakisa461ece2022-11-09 12:36:02 +000031 """Target for bignum mod_raw test case generation."""
32
33 def __init__(self, val_n: str, val_a: str, val_b: str = "0", bits_in_limb: int = 64) -> None:
34 super().__init__(val_a=val_a, val_b=val_b)
35 self.val_n = val_n
36 self.bits_in_limb = bits_in_limb
37
38 @property
39 def int_n(self) -> int:
40 return bignum_common.hex_to_int(self.val_n)
41
42 @property
43 def boundary(self) -> int:
44 data_in = [self.int_a, self.int_b, self.int_n]
45 return max([n for n in data_in if n is not None])
46
47 @property
48 def limbs(self) -> int:
49 return bignum_common.limbs_mpi(self.boundary, self.bits_in_limb)
50
51 @property
52 def hex_digits(self) -> int:
53 return 2 * (self.limbs * self.bits_in_limb // 8)
54
55 @property
56 def hex_n(self) -> str:
57 return "{:x}".format(self.int_n).zfill(self.hex_digits)
58
59 @property
60 def hex_a(self) -> str:
61 return "{:x}".format(self.int_a).zfill(self.hex_digits)
62
63 @property
64 def hex_b(self) -> str:
65 return "{:x}".format(self.int_b).zfill(self.hex_digits)
66
67 @property
68 def r(self) -> int: # pylint: disable=invalid-name
69 l = bignum_common.limbs_mpi(self.int_n, self.bits_in_limb)
70 return bignum_common.bound_mpi_limbs(l, self.bits_in_limb)
71
72 @property
73 def r_inv(self) -> int:
74 return bignum_common.invmod(self.r, self.int_n)
75
76 @property
Minos Galanakis855c2282022-11-10 11:33:25 +000077 def r2(self) -> int: # pylint: disable=invalid-name
Minos Galanakisa461ece2022-11-09 12:36:02 +000078 return pow(self.r, 2)
Janos Follath1be322a2022-11-02 14:46:23 +000079
Minos Galanakise9c86a12022-11-09 11:46:47 +000080class BignumModRawOperationArchSplit(BignumModRawOperation):
81 #pylint: disable=abstract-method
Minos Galanakisa461ece2022-11-09 12:36:02 +000082 """Common features for bignum mod raw operations where the result depends on
Minos Galanakise9c86a12022-11-09 11:46:47 +000083 the limb size."""
84
Minos Galanakisa461ece2022-11-09 12:36:02 +000085 limb_sizes = [32, 64] # type: List[int]
Minos Galanakise9c86a12022-11-09 11:46:47 +000086
Minos Galanakisa461ece2022-11-09 12:36:02 +000087 def __init__(self, val_n: str, val_a: str, val_b: str = "0", bits_in_limb: int = 64) -> None:
88 super().__init__(val_n=val_n, val_a=val_a, val_b=val_b, bits_in_limb=bits_in_limb)
89
90 if bits_in_limb not in self.limb_sizes:
91 raise ValueError("Invalid number of bits in limb!")
92
93 self.dependencies = ["MBEDTLS_HAVE_INT{:d}".format(bits_in_limb)]
Minos Galanakise9c86a12022-11-09 11:46:47 +000094
95 @classmethod
96 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
97 for a_value, b_value in cls.get_value_pairs():
Minos Galanakisa461ece2022-11-09 12:36:02 +000098 for bil in cls.limb_sizes:
99 yield cls(a_value, b_value, bits_in_limb=bil).create_test_case()
Minos Galanakis855c2282022-11-10 11:33:25 +0000100# BEGIN MERGE SLOT 1
101
102# END MERGE SLOT 1
103
104# BEGIN MERGE SLOT 2
105
106# END MERGE SLOT 2
107
108# BEGIN MERGE SLOT 3
109
110# END MERGE SLOT 3
111
112# BEGIN MERGE SLOT 4
113
114# END MERGE SLOT 4
115
116# BEGIN MERGE SLOT 5
117
118# END MERGE SLOT 5
119
120# BEGIN MERGE SLOT 6
121
122# END MERGE SLOT 6
123
124# BEGIN MERGE SLOT 7
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000125class BignumModRawConvertToMont(BignumModRawOperationArchSplit):
126 """ Test cases for mpi_mod_raw_to_mont_rep(). """
Minos Galanakis855c2282022-11-10 11:33:25 +0000127
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000128 test_function = "mpi_mod_raw_to_mont_rep"
129 test_name = "Convert into Mont: "
130
131 test_data_moduli = ["b",
132 "fd",
133 "eeff99aa37",
134 "eeff99aa11",
135 "800000000005",
136 "7fffffffffffffff",
137 "80fe000a10000001",
138 "25a55a46e5da99c71c7",
139 "1058ad82120c3a10196bb36229c1",
140 "7e35b84cb19ea5bc57ec37f5e431462fa962d98c1e63738d4657f"
141 "18ad6532e6adc3eafe67f1e5fa262af94cee8d3e7268593942a2a"
142 "98df75154f8c914a282f8b",
143 "8335616aed761f1f7f44e6bd49e807b82e3bf2bf11bfa63",
144 "ffcece570f2f991013f26dd5b03c4c5b65f97be5905f36cb4664f"
145 "2c78ff80aa8135a4aaf57ccb8a0aca2f394909a74cef1ef6758a6"
146 "4d11e2c149c393659d124bfc94196f0ce88f7d7d567efa5a649e2"
147 "deefaa6e10fdc3deac60d606bf63fc540ac95294347031aefd73d"
148 "6a9ee10188aaeb7a90d920894553cb196881691cadc51808715a0"
149 "7e8b24fcb1a63df047c7cdf084dd177ba368c806f3d51ddb5d389"
150 "8c863e687ecaf7d649a57a46264a582f94d3c8f2edaf59f77a7f6"
151 "bdaf83c991e8f06abe220ec8507386fce8c3da84c6c3903ab8f3a"
152 "d4630a204196a7dbcbd9bcca4e40ec5cc5c09938d49f5e1e6181d"
153 "b8896f33bb12e6ef73f12ec5c5ea7a8a337"
154 ]
155
156 test_input_numbers = ["0",
157 "1",
158 "97",
159 "f5",
160 "6f5c3",
161 "745bfe50f7",
162 "ffa1f9924123",
163 "334a8b983c79bd",
164 "5b84f632b58f3461",
165 "19acd15bc38008e1",
166 "ffffffffffffffff",
167 "54ce6a6bb8247fa0427cfc75a6b0599",
168 "fecafe8eca052f154ce6a6bb8247fa019558bfeecce9bb9",
169 "a87d7a56fa4bfdc7da42ef798b9cf6843d4c54794698cb14d72"
170 "851dec9586a319f4bb6d5695acbd7c92e7a42a5ede6972adcbc"
171 "f68425265887f2d721f462b7f1b91531bac29fa648facb8e3c6"
172 "1bd5ae42d5a59ba1c89a95897bfe541a8ce1d633b98f379c481"
173 "6f25e21f6ac49286b261adb4b78274fe5f61c187581f213e84b"
174 "2a821e341ef956ecd5de89e6c1a35418cd74a549379d2d4594a"
175 "577543147f8e35b3514e62cf3e89d1156cdc91ab5f4c928fbd6"
176 "9148c35df5962fed381f4d8a62852a36823d5425f7487c13a12"
177 "523473fb823aa9d6ea5f42e794e15f2c1a8785cf6b7d51a4617"
178 "947fb3baf674f74a673cf1d38126983a19ed52c7439fab42c2185"
179 ]
180
181 descr_tpl = '{} #{} N: \"{}\" A: \"{}\".'
182
183 def result(self) -> List[str]:
184 return [self.hex_x]
185
186 def arguments(self) -> List[str]:
187 return [bignum_common.quote_str(n) for n in [self.hex_n,
188 self.hex_a,
189 self.hex_x]]
190
191 def description(self) -> str:
192 return self.descr_tpl.format(self.test_name,
193 self.count,
194 self.int_n,
195 self.int_a)
196
197 @classmethod
198 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
199 for bil in [32, 64]:
200 for n in cls.test_data_moduli:
201 for i in cls.test_input_numbers:
202 # Skip invalid combinations where A.limbs > N.limbs
203 if bignum_common.hex_to_int(i) > bignum_common.hex_to_int(n):
204 continue
205 yield cls(n, i, bits_in_limb=bil).create_test_case()
206
207 @property
208 def x(self) -> int: # pylint: disable=invalid-name
209 return (self.int_a * self.r) % self.int_n
210
211 @property
212 def hex_x(self) -> str:
213 return "{:x}".format(self.x).zfill(self.hex_digits)
Minos Galanakis50de0732022-11-09 19:36:16 +0000214
215class BignumModRawConvertFromMont(BignumModRawConvertToMont):
216 """ Test cases for mpi_mod_raw_from_mont_rep(). """
217
218 test_function = "mpi_mod_raw_from_mont_rep"
219 test_name = "Convert from Mont: "
220
221 test_input_numbers = ["0",
222 "1",
223 "3ca",
224 "539ed428",
225 "7dfe5c6beb35a2d6",
226 "dca8de1c2adfc6d7aafb9b48e",
227 "a7d17b6c4be72f3d5c16bf9c1af6fc933",
228 "2fec97beec546f9553142ed52f147845463f579",
229 "378dc83b8bc5a7b62cba495af4919578dce6d4f175cadc4f",
230 "b6415f2a1a8e48a518345db11f56db3829c8f2c6415ab4a395a"
231 "b3ac2ea4cbef4af86eb18a84eb6ded4c6ecbfc4b59c2879a675"
232 "487f687adea9d197a84a5242a5cf6125ce19a6ad2e7341f1c57"
233 "d43ea4f4c852a51cb63dabcd1c9de2b827a3146a3d175b35bea"
234 "41ae75d2a286a3e9d43623152ac513dcdea1d72a7da846a8ab3"
235 "58d9be4926c79cfb287cf1cf25b689de3b912176be5dcaf4d4c"
236 "6e7cb839a4a3243a6c47c1e2c99d65c59d6fa3672575c2f1ca8"
237 "de6a32e854ec9d8ec635c96af7679fce26d7d159e4a9da3bd74"
238 "e1272c376cd926d74fe3fb164a5935cff3d5cdb92b35fe2cea32"
239 "138a7e6bfbc319ebd1725dacb9a359cbf693f2ecb785efb9d627"
240 ]
241
242 @property
243 def x(self): # pylint: disable=invalid-name
244 return (self.int_a * self.r_inv) % self.int_n
Janos Follath1be322a2022-11-02 14:46:23 +0000245# END MERGE SLOT 7
246
247# BEGIN MERGE SLOT 8
248
249# END MERGE SLOT 8
250
251# BEGIN MERGE SLOT 9
252
253# END MERGE SLOT 9
254
255# BEGIN MERGE SLOT 10
256
257# END MERGE SLOT 10