blob: 5645685a5f3839d442166861285e90bc7fd0d5f1 [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
62
63 # Extend the default values with n < x < 2n
64 input_values = bignum_common.ModOperationCommon.input_values + [
65 "73",
66 "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021",
67 ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1"
68 "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63"
69 "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929"
70 "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599")
71 ] # type: List[str]
72
73 def result(self) -> List[str]:
74 result = self.int_a % self.int_n
75 return [self.format_result(result)]
76
77 @property
78 def is_valid(self) -> bool:
79 return bool(self.int_a < 2 * self.int_n)
80
Gabor Mezei80a334a2022-12-07 16:04:15 +010081class BignumModRawMul(bignum_common.ModOperationCommon,
82 BignumModRawTarget):
83 """Test cases for bignum mpi_mod_raw_mul()."""
84 symbol = "*"
85 test_function = "mpi_mod_raw_mul"
86 test_name = "mbedtls_mpi_mod_raw_mul"
87 input_style = "arch_split"
88 arity = 2
89
90 def arguments(self) -> List[str]:
Gabor Mezeib31b2e62022-12-15 15:00:44 +010091 return [self.format_result(self.to_montgomery(self.int_a)),
92 self.format_result(self.to_montgomery(self.int_b)),
93 bignum_common.quote_str(self.arg_n)
Gabor Mezei80a334a2022-12-07 16:04:15 +010094 ] + self.result()
95
96 def result(self) -> List[str]:
97 result = (self.int_a * self.int_b) % self.int_n
Gabor Mezeib31b2e62022-12-15 15:00:44 +010098 return [self.format_result(self.to_montgomery(result))]
Gabor Mezei80a334a2022-12-07 16:04:15 +010099
Minos Galanakis855c2282022-11-10 11:33:25 +0000100# END MERGE SLOT 2
101
102# BEGIN MERGE SLOT 3
103
Tom Cosgrove61292682022-12-08 09:44:10 +0000104class BignumModRawInvPrime(bignum_common.ModOperationCommon,
105 BignumModRawTarget):
106 """Test cases for bignum mpi_mod_raw_inv_prime()."""
107 moduli = ONLY_PRIME_MODULI
108 symbol = "^ -1"
109 test_function = "mpi_mod_raw_inv_prime"
110 test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
Tom Cosgrovedbac6092022-12-14 08:27:18 +0000111 input_style = "arch_split"
Tom Cosgrove61292682022-12-08 09:44:10 +0000112 arity = 1
113 suffix = True
Tom Cosgrovef7237542022-12-16 16:10:36 +0000114 montgomery_form_a = True
Tom Cosgrove1133d232022-12-16 03:53:17 +0000115 disallow_zero_a = True
Tom Cosgrove61292682022-12-08 09:44:10 +0000116
117 def result(self) -> List[str]:
Tom Cosgrove1133d232022-12-16 03:53:17 +0000118 result = bignum_common.invmod_positive(self.int_a, self.int_n)
Tom Cosgrove61292682022-12-08 09:44:10 +0000119 mont_result = self.to_montgomery(result)
120 return [self.format_result(mont_result)]
121
Minos Galanakis855c2282022-11-10 11:33:25 +0000122# END MERGE SLOT 3
123
124# BEGIN MERGE SLOT 4
125
126# END MERGE SLOT 4
127
128# BEGIN MERGE SLOT 5
129
Tom Cosgrove19230092022-11-24 15:56:53 +0000130class BignumModRawAdd(bignum_common.ModOperationCommon,
131 BignumModRawTarget):
132 """Test cases for bignum mpi_mod_raw_add()."""
133 symbol = "+"
134 test_function = "mpi_mod_raw_add"
135 test_name = "mbedtls_mpi_mod_raw_add"
136 input_style = "fixed"
137 arity = 2
138
Tom Cosgrove19230092022-11-24 15:56:53 +0000139 def result(self) -> List[str]:
140 result = (self.int_a + self.int_b) % self.int_n
141 return [self.format_result(result)]
142
Minos Galanakis855c2282022-11-10 11:33:25 +0000143# END MERGE SLOT 5
144
145# BEGIN MERGE SLOT 6
146
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100147class BignumModRawConvertRep(bignum_common.ModOperationCommon,
148 BignumModRawTarget):
149 # This is an abstract class, it's ok to have unimplemented methods.
150 #pylint: disable=abstract-method
151 """Test cases for representation conversion."""
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000152 symbol = ""
153 input_style = "arch_split"
Gilles Peskine23636ac2022-12-20 19:30:47 +0100154 arity = 1
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000155 rep = bignum_common.ModulusRepresentation.INVALID
Gilles Peskine23636ac2022-12-20 19:30:47 +0100156
Gilles Peskine636809f2022-12-21 20:12:31 +0100157 def set_representation(self, r: bignum_common.ModulusRepresentation) -> None:
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000158 self.rep = r
Gilles Peskine23636ac2022-12-20 19:30:47 +0100159
Gilles Peskine23636ac2022-12-20 19:30:47 +0100160 def arguments(self) -> List[str]:
161 return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(),
162 bignum_common.quote_str(self.arg_a)] +
163 self.result())
164
Gilles Peskinead335b52022-12-20 22:39:15 +0100165 def description(self) -> str:
166 base = super().description()
167 mod_with_rep = 'mod({})'.format(self.rep.name)
168 return base.replace('mod', mod_with_rep, 1)
169
Gilles Peskine23636ac2022-12-20 19:30:47 +0100170 @classmethod
Gilles Peskine6d40e542022-12-21 20:18:23 +0100171 def test_cases_for_values(cls, rep: bignum_common.ModulusRepresentation,
172 n: str, a: str) -> Iterator[test_case.TestCase]:
Gilles Peskinef2873662022-12-21 20:28:29 +0100173 """Emit test cases for the given values (if any).
174
175 This may emit no test cases if a isn't valid for the modulus n,
176 or multiple test cases if rep requires different data depending
177 on the limb size.
178 """
Gilles Peskine6d40e542022-12-21 20:18:23 +0100179 for bil in cls.limb_sizes:
180 test_object = cls(n, a, bits_in_limb=bil)
181 test_object.set_representation(rep)
Gilles Peskinef2873662022-12-21 20:28:29 +0100182 # The class is set to having separate test cases for each limb
183 # size, because the Montgomery representation requires it.
184 # But other representations don't require it. So for other
185 # representations, emit a single test case with no dependency
186 # on the limb size.
187 if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
Gilles Peskine5efe4492022-12-21 20:33:30 +0100188 test_object.dependencies = \
189 [dep for dep in test_object.dependencies
190 if not dep.startswith('MBEDTLS_HAVE_INT')]
Gilles Peskine6d40e542022-12-21 20:18:23 +0100191 if test_object.is_valid:
192 yield test_object.create_test_case()
Gilles Peskinef2873662022-12-21 20:28:29 +0100193 if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
194 # A single test case (emitted, or skipped due to invalidity)
195 # is enough, since this test case doesn't depend on the
196 # limb size.
197 break
Gilles Peskine6d40e542022-12-21 20:18:23 +0100198
Gilles Peskinef2873662022-12-21 20:28:29 +0100199 # The parent class doesn't support non-bignum parameters. So we override
200 # test generation, in order to have the representation as a parameter.
Gilles Peskine6d40e542022-12-21 20:18:23 +0100201 @classmethod
Gilles Peskine23636ac2022-12-20 19:30:47 +0100202 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
Minos Galanakisafa7c042022-12-21 17:38:16 +0000203
204 for rep in bignum_common.ModulusRepresentation.supported_representations():
Gilles Peskine23636ac2022-12-20 19:30:47 +0100205 for n in cls.moduli:
206 for a in cls.input_values:
Gilles Peskine6d40e542022-12-21 20:18:23 +0100207 yield from cls.test_cases_for_values(rep, n, a)
Gilles Peskine23636ac2022-12-20 19:30:47 +0100208
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100209class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep):
210 """Test cases for mpi_mod_raw_canonical_to_modulus_rep."""
211 test_function = "mpi_mod_raw_canonical_to_modulus_rep"
212 test_name = "Rep canon->mod"
213
214 def result(self) -> List[str]:
Minos Galanakis56894102022-12-21 17:31:56 +0000215 return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))]
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100216
217class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep):
218 """Test cases for mpi_mod_raw_modulus_to_canonical_rep."""
219 test_function = "mpi_mod_raw_modulus_to_canonical_rep"
220 test_name = "Rep mod->canon"
221
222 @property
223 def arg_a(self) -> str:
Minos Galanakis56894102022-12-21 17:31:56 +0000224 return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep)))
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100225
226 def result(self) -> List[str]:
227 return [self.format_result(self.int_a)]
228
Minos Galanakis855c2282022-11-10 11:33:25 +0000229# END MERGE SLOT 6
230
231# BEGIN MERGE SLOT 7
Janos Follathf352c672022-11-20 13:40:25 +0000232
Janos Follath155ad8c2022-11-17 14:42:40 +0000233class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
Janos Follath948afce2022-11-17 13:38:56 +0000234 BignumModRawTarget):
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000235 """ Test cases for mpi_mod_raw_to_mont_rep(). """
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000236 test_function = "mpi_mod_raw_to_mont_rep"
237 test_name = "Convert into Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000238 symbol = "R *"
Janos Follath6fa3f062022-11-17 20:33:51 +0000239 input_style = "arch_split"
Janos Follath1921fd52022-11-18 17:51:02 +0000240 arity = 1
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000241
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000242 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000243 result = self.to_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000244 return [self.format_result(result)]
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000245
Janos Follathf352c672022-11-20 13:40:25 +0000246class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
247 BignumModRawTarget):
Minos Galanakis50de0732022-11-09 19:36:16 +0000248 """ Test cases for mpi_mod_raw_from_mont_rep(). """
Minos Galanakis50de0732022-11-09 19:36:16 +0000249 test_function = "mpi_mod_raw_from_mont_rep"
250 test_name = "Convert from Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000251 symbol = "1/R *"
Janos Follathf352c672022-11-20 13:40:25 +0000252 input_style = "arch_split"
253 arity = 1
Minos Galanakis50de0732022-11-09 19:36:16 +0000254
Janos Follath1921fd52022-11-18 17:51:02 +0000255 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000256 result = self.from_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000257 return [self.format_result(result)]
258
Minos Galanakis78665eb2022-12-07 18:10:46 +0000259class BignumModRawModNegate(bignum_common.ModOperationCommon,
260 BignumModRawTarget):
261 """ Test cases for mpi_mod_raw_neg(). """
262 test_function = "mpi_mod_raw_neg"
263 test_name = "Modular negation: "
Minos Galanakisf3abea62022-12-08 11:48:26 +0000264 symbol = "-"
Minos Galanakis78665eb2022-12-07 18:10:46 +0000265 input_style = "arch_split"
266 arity = 1
Janos Follath1921fd52022-11-18 17:51:02 +0000267
Minos Galanakis78665eb2022-12-07 18:10:46 +0000268 def result(self) -> List[str]:
269 result = (self.int_n - self.int_a) % self.int_n
270 return [self.format_result(result)]
Janos Follath1be322a2022-11-02 14:46:23 +0000271# END MERGE SLOT 7
272
273# BEGIN MERGE SLOT 8
274
275# END MERGE SLOT 8
276
277# BEGIN MERGE SLOT 9
278
279# END MERGE SLOT 9
280
281# BEGIN MERGE SLOT 10
282
283# END MERGE SLOT 10