blob: 37ad27a115e4c596092cf112b62fbfcc841b76a4 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00003# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Janos Follathdf8239b2022-11-02 14:40:58 +00004#
Janos Follathdf8239b2022-11-02 14:40:58 +00005
Gilles Peskine394da2d2022-12-21 20:20:44 +01006from typing import Iterator, List
Janos Follathdf8239b2022-11-02 14:40:58 +00007
Gilles Peskine23636ac2022-12-20 19:30:47 +01008from . import test_case
Janos Follathdf8239b2022-11-02 14:40:58 +00009from . import test_data_generation
Minos Galanakise9c86a12022-11-09 11:46:47 +000010from . import bignum_common
Tom Cosgrove61292682022-12-08 09:44:10 +000011from .bignum_data import ONLY_PRIME_MODULI
Janos Follathdf8239b2022-11-02 14:40:58 +000012
Janos Follath0cd89672022-11-09 12:14:14 +000013class BignumModRawTarget(test_data_generation.BaseTarget):
14 #pylint: disable=abstract-method, too-few-public-methods
Janos Follathdf8239b2022-11-02 14:40:58 +000015 """Target for bignum mod_raw test case generation."""
16 target_basename = 'test_suite_bignum_mod_raw.generated'
17
Minos Galanakis855c2282022-11-10 11:33:25 +000018
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010019class BignumModRawSub(bignum_common.ModOperationCommon,
20 BignumModRawTarget):
21 """Test cases for bignum mpi_mod_raw_sub()."""
Gabor Mezeic426d9b2022-11-15 18:51:20 +010022 symbol = "-"
23 test_function = "mpi_mod_raw_sub"
24 test_name = "mbedtls_mpi_mod_raw_sub"
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010025 input_style = "fixed"
26 arity = 2
Gabor Mezeic426d9b2022-11-15 18:51:20 +010027
28 def arguments(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010029 return [bignum_common.quote_str(n) for n in [self.arg_a,
30 self.arg_b,
31 self.arg_n]
32 ] + self.result()
Gabor Mezeic426d9b2022-11-15 18:51:20 +010033
34 def result(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010035 result = (self.int_a - self.int_b) % self.int_n
36 return [self.format_result(result)]
Gabor Mezeic426d9b2022-11-15 18:51:20 +010037
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +010038class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon,
39 BignumModRawTarget):
40 """Test cases for ecp quasi_reduction()."""
41 symbol = "-"
42 test_function = "mpi_mod_raw_fix_quasi_reduction"
Gabor Mezeib57c9082023-01-27 14:37:42 +010043 test_name = "fix_quasi_reduction"
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +010044 input_style = "fixed"
45 arity = 1
46
47 # Extend the default values with n < x < 2n
48 input_values = bignum_common.ModOperationCommon.input_values + [
Gabor Mezei6f96c892023-01-24 17:38:26 +010049 "73",
Gabor Mezei246d66b2023-01-24 18:02:52 +010050
51 # First number generated by random.getrandbits(1024) - seed(3,2)
52 "ea7b5bf55eb561a4216363698b529b4a97b750923ceb3ffd",
53
54 # First number generated by random.getrandbits(1024) - seed(1,2)
55 ("cd447e35b8b6d8fe442e3d437204e52db2221a58008a05a6c4647159c324c985"
56 "9b810e766ec9d28663ca828dd5f4b3b2e4b06ce60741c7a87ce42c8218072e8c"
57 "35bf992dc9e9c616612e7696a6cecc1b78e510617311d8a3c2ce6f447ed4d57b"
58 "1e2feb89414c343c1027c4d1c386bbc4cd613e30d8f16adf91b7584a2265b1f5")
Gabor Mezei6f96c892023-01-24 17:38:26 +010059 ] # type: List[str]
Gabor Mezeiaaa1d2a2023-01-23 16:13:43 +010060
61 def result(self) -> List[str]:
62 result = self.int_a % self.int_n
63 return [self.format_result(result)]
64
65 @property
66 def is_valid(self) -> bool:
67 return bool(self.int_a < 2 * self.int_n)
68
Gabor Mezei80a334a2022-12-07 16:04:15 +010069class BignumModRawMul(bignum_common.ModOperationCommon,
70 BignumModRawTarget):
71 """Test cases for bignum mpi_mod_raw_mul()."""
72 symbol = "*"
73 test_function = "mpi_mod_raw_mul"
74 test_name = "mbedtls_mpi_mod_raw_mul"
75 input_style = "arch_split"
76 arity = 2
77
78 def arguments(self) -> List[str]:
Gabor Mezeib31b2e62022-12-15 15:00:44 +010079 return [self.format_result(self.to_montgomery(self.int_a)),
80 self.format_result(self.to_montgomery(self.int_b)),
81 bignum_common.quote_str(self.arg_n)
Gabor Mezei80a334a2022-12-07 16:04:15 +010082 ] + self.result()
83
84 def result(self) -> List[str]:
85 result = (self.int_a * self.int_b) % self.int_n
Gabor Mezeib31b2e62022-12-15 15:00:44 +010086 return [self.format_result(self.to_montgomery(result))]
Gabor Mezei80a334a2022-12-07 16:04:15 +010087
Minos Galanakis855c2282022-11-10 11:33:25 +000088
Tom Cosgrove61292682022-12-08 09:44:10 +000089class BignumModRawInvPrime(bignum_common.ModOperationCommon,
90 BignumModRawTarget):
91 """Test cases for bignum mpi_mod_raw_inv_prime()."""
92 moduli = ONLY_PRIME_MODULI
93 symbol = "^ -1"
94 test_function = "mpi_mod_raw_inv_prime"
95 test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
Tom Cosgrovedbac6092022-12-14 08:27:18 +000096 input_style = "arch_split"
Tom Cosgrove61292682022-12-08 09:44:10 +000097 arity = 1
98 suffix = True
Tom Cosgrovef7237542022-12-16 16:10:36 +000099 montgomery_form_a = True
Tom Cosgrove1133d232022-12-16 03:53:17 +0000100 disallow_zero_a = True
Tom Cosgrove61292682022-12-08 09:44:10 +0000101
102 def result(self) -> List[str]:
Tom Cosgrove1133d232022-12-16 03:53:17 +0000103 result = bignum_common.invmod_positive(self.int_a, self.int_n)
Tom Cosgrove61292682022-12-08 09:44:10 +0000104 mont_result = self.to_montgomery(result)
105 return [self.format_result(mont_result)]
106
Minos Galanakis855c2282022-11-10 11:33:25 +0000107
Tom Cosgrove19230092022-11-24 15:56:53 +0000108class BignumModRawAdd(bignum_common.ModOperationCommon,
109 BignumModRawTarget):
110 """Test cases for bignum mpi_mod_raw_add()."""
111 symbol = "+"
112 test_function = "mpi_mod_raw_add"
113 test_name = "mbedtls_mpi_mod_raw_add"
114 input_style = "fixed"
115 arity = 2
116
Tom Cosgrove19230092022-11-24 15:56:53 +0000117 def result(self) -> List[str]:
118 result = (self.int_a + self.int_b) % self.int_n
119 return [self.format_result(result)]
120
Minos Galanakis855c2282022-11-10 11:33:25 +0000121
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100122class BignumModRawConvertRep(bignum_common.ModOperationCommon,
123 BignumModRawTarget):
124 # This is an abstract class, it's ok to have unimplemented methods.
125 #pylint: disable=abstract-method
126 """Test cases for representation conversion."""
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000127 symbol = ""
128 input_style = "arch_split"
Gilles Peskine23636ac2022-12-20 19:30:47 +0100129 arity = 1
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000130 rep = bignum_common.ModulusRepresentation.INVALID
Gilles Peskine23636ac2022-12-20 19:30:47 +0100131
Gilles Peskine636809f2022-12-21 20:12:31 +0100132 def set_representation(self, r: bignum_common.ModulusRepresentation) -> None:
Minos Galanakisae4d2cf2022-12-21 17:34:15 +0000133 self.rep = r
Gilles Peskine23636ac2022-12-20 19:30:47 +0100134
Gilles Peskine23636ac2022-12-20 19:30:47 +0100135 def arguments(self) -> List[str]:
136 return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(),
137 bignum_common.quote_str(self.arg_a)] +
138 self.result())
139
Gilles Peskinead335b52022-12-20 22:39:15 +0100140 def description(self) -> str:
141 base = super().description()
142 mod_with_rep = 'mod({})'.format(self.rep.name)
143 return base.replace('mod', mod_with_rep, 1)
144
Gilles Peskine23636ac2022-12-20 19:30:47 +0100145 @classmethod
Gilles Peskine6d40e542022-12-21 20:18:23 +0100146 def test_cases_for_values(cls, rep: bignum_common.ModulusRepresentation,
147 n: str, a: str) -> Iterator[test_case.TestCase]:
Gilles Peskinef2873662022-12-21 20:28:29 +0100148 """Emit test cases for the given values (if any).
149
150 This may emit no test cases if a isn't valid for the modulus n,
151 or multiple test cases if rep requires different data depending
152 on the limb size.
153 """
Gilles Peskine6d40e542022-12-21 20:18:23 +0100154 for bil in cls.limb_sizes:
155 test_object = cls(n, a, bits_in_limb=bil)
156 test_object.set_representation(rep)
Gilles Peskinef2873662022-12-21 20:28:29 +0100157 # The class is set to having separate test cases for each limb
158 # size, because the Montgomery representation requires it.
159 # But other representations don't require it. So for other
160 # representations, emit a single test case with no dependency
161 # on the limb size.
162 if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
Gilles Peskine5efe4492022-12-21 20:33:30 +0100163 test_object.dependencies = \
164 [dep for dep in test_object.dependencies
165 if not dep.startswith('MBEDTLS_HAVE_INT')]
Gilles Peskine6d40e542022-12-21 20:18:23 +0100166 if test_object.is_valid:
167 yield test_object.create_test_case()
Gilles Peskinef2873662022-12-21 20:28:29 +0100168 if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
169 # A single test case (emitted, or skipped due to invalidity)
170 # is enough, since this test case doesn't depend on the
171 # limb size.
172 break
Gilles Peskine6d40e542022-12-21 20:18:23 +0100173
Gilles Peskinef2873662022-12-21 20:28:29 +0100174 # The parent class doesn't support non-bignum parameters. So we override
175 # test generation, in order to have the representation as a parameter.
Gilles Peskine6d40e542022-12-21 20:18:23 +0100176 @classmethod
Gilles Peskine23636ac2022-12-20 19:30:47 +0100177 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
Minos Galanakisafa7c042022-12-21 17:38:16 +0000178
179 for rep in bignum_common.ModulusRepresentation.supported_representations():
Gilles Peskine23636ac2022-12-20 19:30:47 +0100180 for n in cls.moduli:
181 for a in cls.input_values:
Gilles Peskine6d40e542022-12-21 20:18:23 +0100182 yield from cls.test_cases_for_values(rep, n, a)
Gilles Peskine23636ac2022-12-20 19:30:47 +0100183
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100184class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep):
185 """Test cases for mpi_mod_raw_canonical_to_modulus_rep."""
186 test_function = "mpi_mod_raw_canonical_to_modulus_rep"
187 test_name = "Rep canon->mod"
188
189 def result(self) -> List[str]:
Minos Galanakis56894102022-12-21 17:31:56 +0000190 return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))]
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100191
192class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep):
193 """Test cases for mpi_mod_raw_modulus_to_canonical_rep."""
194 test_function = "mpi_mod_raw_modulus_to_canonical_rep"
195 test_name = "Rep mod->canon"
196
197 @property
198 def arg_a(self) -> str:
Minos Galanakis56894102022-12-21 17:31:56 +0000199 return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep)))
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100200
201 def result(self) -> List[str]:
202 return [self.format_result(self.int_a)]
203
Janos Follathf352c672022-11-20 13:40:25 +0000204
Janos Follath155ad8c2022-11-17 14:42:40 +0000205class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
Janos Follath948afce2022-11-17 13:38:56 +0000206 BignumModRawTarget):
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000207 """ Test cases for mpi_mod_raw_to_mont_rep(). """
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000208 test_function = "mpi_mod_raw_to_mont_rep"
209 test_name = "Convert into Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000210 symbol = "R *"
Janos Follath6fa3f062022-11-17 20:33:51 +0000211 input_style = "arch_split"
Janos Follath1921fd52022-11-18 17:51:02 +0000212 arity = 1
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000213
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000214 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000215 result = self.to_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000216 return [self.format_result(result)]
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000217
Janos Follathf352c672022-11-20 13:40:25 +0000218class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
219 BignumModRawTarget):
Minos Galanakis50de0732022-11-09 19:36:16 +0000220 """ Test cases for mpi_mod_raw_from_mont_rep(). """
Minos Galanakis50de0732022-11-09 19:36:16 +0000221 test_function = "mpi_mod_raw_from_mont_rep"
222 test_name = "Convert from Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000223 symbol = "1/R *"
Janos Follathf352c672022-11-20 13:40:25 +0000224 input_style = "arch_split"
225 arity = 1
Minos Galanakis50de0732022-11-09 19:36:16 +0000226
Janos Follath1921fd52022-11-18 17:51:02 +0000227 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000228 result = self.from_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000229 return [self.format_result(result)]
230
Minos Galanakis78665eb2022-12-07 18:10:46 +0000231class BignumModRawModNegate(bignum_common.ModOperationCommon,
232 BignumModRawTarget):
233 """ Test cases for mpi_mod_raw_neg(). """
234 test_function = "mpi_mod_raw_neg"
235 test_name = "Modular negation: "
Minos Galanakisf3abea62022-12-08 11:48:26 +0000236 symbol = "-"
Minos Galanakis78665eb2022-12-07 18:10:46 +0000237 input_style = "arch_split"
238 arity = 1
Janos Follath1921fd52022-11-18 17:51:02 +0000239
Minos Galanakis78665eb2022-12-07 18:10:46 +0000240 def result(self) -> List[str]:
241 result = (self.int_n - self.int_a) % self.int_n
242 return [self.format_result(result)]