blob: e1ff0cd98ff2eca70e389e82aba14185389d24b1 [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
Gilles Peskine394da2d2022-12-21 20:20:44 +010017from typing import Iterator, List
Janos Follathdf8239b2022-11-02 14:40:58 +000018
Gilles Peskine23636ac2022-12-20 19:30:47 +010019from . import test_case
Janos Follathdf8239b2022-11-02 14:40:58 +000020from . import test_data_generation
Minos Galanakise9c86a12022-11-09 11:46:47 +000021from . import bignum_common
Tom Cosgrove61292682022-12-08 09:44:10 +000022from .bignum_data import ONLY_PRIME_MODULI
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 Galanakis855c2282022-11-10 11:33:25 +000029# BEGIN MERGE SLOT 1
30
31# END MERGE SLOT 1
32
33# BEGIN MERGE SLOT 2
34
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010035class BignumModRawSub(bignum_common.ModOperationCommon,
36 BignumModRawTarget):
37 """Test cases for bignum mpi_mod_raw_sub()."""
Gabor Mezeic426d9b2022-11-15 18:51:20 +010038 symbol = "-"
39 test_function = "mpi_mod_raw_sub"
40 test_name = "mbedtls_mpi_mod_raw_sub"
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010041 input_style = "fixed"
42 arity = 2
Gabor Mezeic426d9b2022-11-15 18:51:20 +010043
44 def arguments(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010045 return [bignum_common.quote_str(n) for n in [self.arg_a,
46 self.arg_b,
47 self.arg_n]
48 ] + self.result()
Gabor Mezeic426d9b2022-11-15 18:51:20 +010049
50 def result(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010051 result = (self.int_a - self.int_b) % self.int_n
52 return [self.format_result(result)]
Gabor Mezeic426d9b2022-11-15 18:51:20 +010053
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +010054class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon,
55 BignumModRawTarget):
56 """Test cases for ecp quasi_reduction()."""
57 symbol = "-"
58 test_function = "mpi_mod_raw_fix_quasi_reduction"
59 test_name = "mbedtls_mpi_mod_raw_fix_quasi_reduction"
60 input_style = "fixed"
61 arity = 1
Gabor Mezei1e8c2102023-01-26 12:27:29 +010062 dependencies = ["MBEDTLS_TEST_HOOKS"]
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +010063
64 # Extend the default values with n < x < 2n
65 input_values = bignum_common.ModOperationCommon.input_values + [
Gabor Mezei6f96c892023-01-24 17:38:26 +010066 "73",
Gabor Mezei246d66b2023-01-24 18:02:52 +010067
68 # First number generated by random.getrandbits(1024) - seed(3,2)
69 "ea7b5bf55eb561a4216363698b529b4a97b750923ceb3ffd",
70
71 # First number generated by random.getrandbits(1024) - seed(1,2)
72 ("cd447e35b8b6d8fe442e3d437204e52db2221a58008a05a6c4647159c324c985"
73 "9b810e766ec9d28663ca828dd5f4b3b2e4b06ce60741c7a87ce42c8218072e8c"
74 "35bf992dc9e9c616612e7696a6cecc1b78e510617311d8a3c2ce6f447ed4d57b"
75 "1e2feb89414c343c1027c4d1c386bbc4cd613e30d8f16adf91b7584a2265b1f5")
Gabor Mezei6f96c892023-01-24 17:38:26 +010076 ] # type: List[str]
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +010077
78 def result(self) -> List[str]:
79 result = self.int_a % self.int_n
80 return [self.format_result(result)]
81
82 @property
83 def is_valid(self) -> bool:
84 return bool(self.int_a < 2 * self.int_n)
85
Gabor Mezei80a334a2022-12-07 16:04:15 +010086class BignumModRawMul(bignum_common.ModOperationCommon,
87 BignumModRawTarget):
88 """Test cases for bignum mpi_mod_raw_mul()."""
89 symbol = "*"
90 test_function = "mpi_mod_raw_mul"
91 test_name = "mbedtls_mpi_mod_raw_mul"
92 input_style = "arch_split"
93 arity = 2
94
95 def arguments(self) -> List[str]:
Gabor Mezeib31b2e62022-12-15 15:00:44 +010096 return [self.format_result(self.to_montgomery(self.int_a)),
97 self.format_result(self.to_montgomery(self.int_b)),
98 bignum_common.quote_str(self.arg_n)
Gabor Mezei80a334a2022-12-07 16:04:15 +010099 ] + self.result()
100
101 def result(self) -> List[str]:
102 result = (self.int_a * self.int_b) % self.int_n
Gabor Mezeib31b2e62022-12-15 15:00:44 +0100103 return [self.format_result(self.to_montgomery(result))]
Gabor Mezei80a334a2022-12-07 16:04:15 +0100104
Minos Galanakis855c2282022-11-10 11:33:25 +0000105# END MERGE SLOT 2
106
107# BEGIN MERGE SLOT 3
108
Tom Cosgrove61292682022-12-08 09:44:10 +0000109class BignumModRawInvPrime(bignum_common.ModOperationCommon,
110 BignumModRawTarget):
111 """Test cases for bignum mpi_mod_raw_inv_prime()."""
112 moduli = ONLY_PRIME_MODULI
113 symbol = "^ -1"
114 test_function = "mpi_mod_raw_inv_prime"
115 test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
Tom Cosgrovedbac6092022-12-14 08:27:18 +0000116 input_style = "arch_split"
Tom Cosgrove61292682022-12-08 09:44:10 +0000117 arity = 1
118 suffix = True
Tom Cosgrovef7237542022-12-16 16:10:36 +0000119 montgomery_form_a = True
Tom Cosgrove1133d232022-12-16 03:53:17 +0000120 disallow_zero_a = True
Tom Cosgrove61292682022-12-08 09:44:10 +0000121
122 def result(self) -> List[str]:
Tom Cosgrove1133d232022-12-16 03:53:17 +0000123 result = bignum_common.invmod_positive(self.int_a, self.int_n)
Tom Cosgrove61292682022-12-08 09:44:10 +0000124 mont_result = self.to_montgomery(result)
125 return [self.format_result(mont_result)]
126
Minos Galanakis855c2282022-11-10 11:33:25 +0000127# END MERGE SLOT 3
128
129# BEGIN MERGE SLOT 4
130
131# END MERGE SLOT 4
132
133# BEGIN MERGE SLOT 5
134
Tom Cosgrove19230092022-11-24 15:56:53 +0000135class BignumModRawAdd(bignum_common.ModOperationCommon,
136 BignumModRawTarget):
137 """Test cases for bignum mpi_mod_raw_add()."""
138 symbol = "+"
139 test_function = "mpi_mod_raw_add"
140 test_name = "mbedtls_mpi_mod_raw_add"
141 input_style = "fixed"
142 arity = 2
143
Tom Cosgrove19230092022-11-24 15:56:53 +0000144 def result(self) -> List[str]:
145 result = (self.int_a + self.int_b) % self.int_n
146 return [self.format_result(result)]
147
Minos Galanakis855c2282022-11-10 11:33:25 +0000148# END MERGE SLOT 5
149
150# BEGIN MERGE SLOT 6
151
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100152class BignumModRawConvertRep(bignum_common.ModOperationCommon,
153 BignumModRawTarget):
154 # This is an abstract class, it's ok to have unimplemented methods.
155 #pylint: disable=abstract-method
156 """Test cases for representation conversion."""
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000157 symbol = ""
158 input_style = "arch_split"
Gilles Peskine23636ac2022-12-20 19:30:47 +0100159 arity = 1
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000160 rep = bignum_common.ModulusRepresentation.INVALID
Gilles Peskine23636ac2022-12-20 19:30:47 +0100161
Gilles Peskine636809f2022-12-21 20:12:31 +0100162 def set_representation(self, r: bignum_common.ModulusRepresentation) -> None:
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000163 self.rep = r
Gilles Peskine23636ac2022-12-20 19:30:47 +0100164
Gilles Peskine23636ac2022-12-20 19:30:47 +0100165 def arguments(self) -> List[str]:
166 return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(),
167 bignum_common.quote_str(self.arg_a)] +
168 self.result())
169
Gilles Peskinead335b52022-12-20 22:39:15 +0100170 def description(self) -> str:
171 base = super().description()
172 mod_with_rep = 'mod({})'.format(self.rep.name)
173 return base.replace('mod', mod_with_rep, 1)
174
Gilles Peskine23636ac2022-12-20 19:30:47 +0100175 @classmethod
Gilles Peskine6d40e542022-12-21 20:18:23 +0100176 def test_cases_for_values(cls, rep: bignum_common.ModulusRepresentation,
177 n: str, a: str) -> Iterator[test_case.TestCase]:
Gilles Peskinef2873662022-12-21 20:28:29 +0100178 """Emit test cases for the given values (if any).
179
180 This may emit no test cases if a isn't valid for the modulus n,
181 or multiple test cases if rep requires different data depending
182 on the limb size.
183 """
Gilles Peskine6d40e542022-12-21 20:18:23 +0100184 for bil in cls.limb_sizes:
185 test_object = cls(n, a, bits_in_limb=bil)
186 test_object.set_representation(rep)
Gilles Peskinef2873662022-12-21 20:28:29 +0100187 # The class is set to having separate test cases for each limb
188 # size, because the Montgomery representation requires it.
189 # But other representations don't require it. So for other
190 # representations, emit a single test case with no dependency
191 # on the limb size.
192 if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
Gilles Peskine5efe4492022-12-21 20:33:30 +0100193 test_object.dependencies = \
194 [dep for dep in test_object.dependencies
195 if not dep.startswith('MBEDTLS_HAVE_INT')]
Gilles Peskine6d40e542022-12-21 20:18:23 +0100196 if test_object.is_valid:
197 yield test_object.create_test_case()
Gilles Peskinef2873662022-12-21 20:28:29 +0100198 if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
199 # A single test case (emitted, or skipped due to invalidity)
200 # is enough, since this test case doesn't depend on the
201 # limb size.
202 break
Gilles Peskine6d40e542022-12-21 20:18:23 +0100203
Gilles Peskinef2873662022-12-21 20:28:29 +0100204 # The parent class doesn't support non-bignum parameters. So we override
205 # test generation, in order to have the representation as a parameter.
Gilles Peskine6d40e542022-12-21 20:18:23 +0100206 @classmethod
Gilles Peskine23636ac2022-12-20 19:30:47 +0100207 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
Minos Galanakisafa7c042022-12-21 17:38:16 +0000208
209 for rep in bignum_common.ModulusRepresentation.supported_representations():
Gilles Peskine23636ac2022-12-20 19:30:47 +0100210 for n in cls.moduli:
211 for a in cls.input_values:
Gilles Peskine6d40e542022-12-21 20:18:23 +0100212 yield from cls.test_cases_for_values(rep, n, a)
Gilles Peskine23636ac2022-12-20 19:30:47 +0100213
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100214class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep):
215 """Test cases for mpi_mod_raw_canonical_to_modulus_rep."""
216 test_function = "mpi_mod_raw_canonical_to_modulus_rep"
217 test_name = "Rep canon->mod"
218
219 def result(self) -> List[str]:
Minos Galanakis56894102022-12-21 17:31:56 +0000220 return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))]
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100221
222class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep):
223 """Test cases for mpi_mod_raw_modulus_to_canonical_rep."""
224 test_function = "mpi_mod_raw_modulus_to_canonical_rep"
225 test_name = "Rep mod->canon"
226
227 @property
228 def arg_a(self) -> str:
Minos Galanakis56894102022-12-21 17:31:56 +0000229 return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep)))
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100230
231 def result(self) -> List[str]:
232 return [self.format_result(self.int_a)]
233
Minos Galanakis855c2282022-11-10 11:33:25 +0000234# END MERGE SLOT 6
235
236# BEGIN MERGE SLOT 7
Janos Follathf352c672022-11-20 13:40:25 +0000237
Janos Follath155ad8c2022-11-17 14:42:40 +0000238class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
Janos Follath948afce2022-11-17 13:38:56 +0000239 BignumModRawTarget):
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000240 """ Test cases for mpi_mod_raw_to_mont_rep(). """
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000241 test_function = "mpi_mod_raw_to_mont_rep"
242 test_name = "Convert into Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000243 symbol = "R *"
Janos Follath6fa3f062022-11-17 20:33:51 +0000244 input_style = "arch_split"
Janos Follath1921fd52022-11-18 17:51:02 +0000245 arity = 1
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000246
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000247 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000248 result = self.to_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000249 return [self.format_result(result)]
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000250
Janos Follathf352c672022-11-20 13:40:25 +0000251class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
252 BignumModRawTarget):
Minos Galanakis50de0732022-11-09 19:36:16 +0000253 """ Test cases for mpi_mod_raw_from_mont_rep(). """
Minos Galanakis50de0732022-11-09 19:36:16 +0000254 test_function = "mpi_mod_raw_from_mont_rep"
255 test_name = "Convert from Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000256 symbol = "1/R *"
Janos Follathf352c672022-11-20 13:40:25 +0000257 input_style = "arch_split"
258 arity = 1
Minos Galanakis50de0732022-11-09 19:36:16 +0000259
Janos Follath1921fd52022-11-18 17:51:02 +0000260 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000261 result = self.from_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000262 return [self.format_result(result)]
263
Minos Galanakis78665eb2022-12-07 18:10:46 +0000264class BignumModRawModNegate(bignum_common.ModOperationCommon,
265 BignumModRawTarget):
266 """ Test cases for mpi_mod_raw_neg(). """
267 test_function = "mpi_mod_raw_neg"
268 test_name = "Modular negation: "
Minos Galanakisf3abea62022-12-08 11:48:26 +0000269 symbol = "-"
Minos Galanakis78665eb2022-12-07 18:10:46 +0000270 input_style = "arch_split"
271 arity = 1
Janos Follath1921fd52022-11-18 17:51:02 +0000272
Minos Galanakis78665eb2022-12-07 18:10:46 +0000273 def result(self) -> List[str]:
274 result = (self.int_n - self.int_a) % self.int_n
275 return [self.format_result(result)]
Janos Follath1be322a2022-11-02 14:46:23 +0000276# END MERGE SLOT 7
277
278# BEGIN MERGE SLOT 8
279
280# END MERGE SLOT 8
281
282# BEGIN MERGE SLOT 9
283
284# END MERGE SLOT 9
285
286# BEGIN MERGE SLOT 10
287
288# END MERGE SLOT 10