Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 1 | """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 Peskine | 394da2d | 2022-12-21 20:20:44 +0100 | [diff] [blame] | 17 | from typing import Iterator, List |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 18 | |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 19 | from . import test_case |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 20 | from . import test_data_generation |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 21 | from . import bignum_common |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 22 | from .bignum_data import ONLY_PRIME_MODULI |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 23 | |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 24 | class BignumModRawTarget(test_data_generation.BaseTarget): |
| 25 | #pylint: disable=abstract-method, too-few-public-methods |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 26 | """Target for bignum mod_raw test case generation.""" |
| 27 | target_basename = 'test_suite_bignum_mod_raw.generated' |
| 28 | |
Minos Galanakis | 855c228 | 2022-11-10 11:33:25 +0000 | [diff] [blame] | 29 | |
Gabor Mezei | 6b3c0c5 | 2022-11-23 16:45:05 +0100 | [diff] [blame] | 30 | class BignumModRawSub(bignum_common.ModOperationCommon, |
| 31 | BignumModRawTarget): |
| 32 | """Test cases for bignum mpi_mod_raw_sub().""" |
Gabor Mezei | c426d9b | 2022-11-15 18:51:20 +0100 | [diff] [blame] | 33 | symbol = "-" |
| 34 | test_function = "mpi_mod_raw_sub" |
| 35 | test_name = "mbedtls_mpi_mod_raw_sub" |
Gabor Mezei | 6b3c0c5 | 2022-11-23 16:45:05 +0100 | [diff] [blame] | 36 | input_style = "fixed" |
| 37 | arity = 2 |
Gabor Mezei | c426d9b | 2022-11-15 18:51:20 +0100 | [diff] [blame] | 38 | |
| 39 | def arguments(self) -> List[str]: |
Gabor Mezei | 6b3c0c5 | 2022-11-23 16:45:05 +0100 | [diff] [blame] | 40 | return [bignum_common.quote_str(n) for n in [self.arg_a, |
| 41 | self.arg_b, |
| 42 | self.arg_n] |
| 43 | ] + self.result() |
Gabor Mezei | c426d9b | 2022-11-15 18:51:20 +0100 | [diff] [blame] | 44 | |
| 45 | def result(self) -> List[str]: |
Gabor Mezei | 6b3c0c5 | 2022-11-23 16:45:05 +0100 | [diff] [blame] | 46 | result = (self.int_a - self.int_b) % self.int_n |
| 47 | return [self.format_result(result)] |
Gabor Mezei | c426d9b | 2022-11-15 18:51:20 +0100 | [diff] [blame] | 48 | |
Gabor Mezei | aaa1d2a | 2023-01-23 16:13:43 +0100 | [diff] [blame] | 49 | class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon, |
| 50 | BignumModRawTarget): |
| 51 | """Test cases for ecp quasi_reduction().""" |
| 52 | symbol = "-" |
| 53 | test_function = "mpi_mod_raw_fix_quasi_reduction" |
Gabor Mezei | b57c908 | 2023-01-27 14:37:42 +0100 | [diff] [blame] | 54 | test_name = "fix_quasi_reduction" |
Gabor Mezei | aaa1d2a | 2023-01-23 16:13:43 +0100 | [diff] [blame] | 55 | input_style = "fixed" |
| 56 | arity = 1 |
| 57 | |
| 58 | # Extend the default values with n < x < 2n |
| 59 | input_values = bignum_common.ModOperationCommon.input_values + [ |
Gabor Mezei | 6f96c89 | 2023-01-24 17:38:26 +0100 | [diff] [blame] | 60 | "73", |
Gabor Mezei | 246d66b | 2023-01-24 18:02:52 +0100 | [diff] [blame] | 61 | |
| 62 | # First number generated by random.getrandbits(1024) - seed(3,2) |
| 63 | "ea7b5bf55eb561a4216363698b529b4a97b750923ceb3ffd", |
| 64 | |
| 65 | # First number generated by random.getrandbits(1024) - seed(1,2) |
| 66 | ("cd447e35b8b6d8fe442e3d437204e52db2221a58008a05a6c4647159c324c985" |
| 67 | "9b810e766ec9d28663ca828dd5f4b3b2e4b06ce60741c7a87ce42c8218072e8c" |
| 68 | "35bf992dc9e9c616612e7696a6cecc1b78e510617311d8a3c2ce6f447ed4d57b" |
| 69 | "1e2feb89414c343c1027c4d1c386bbc4cd613e30d8f16adf91b7584a2265b1f5") |
Gabor Mezei | 6f96c89 | 2023-01-24 17:38:26 +0100 | [diff] [blame] | 70 | ] # type: List[str] |
Gabor Mezei | aaa1d2a | 2023-01-23 16:13:43 +0100 | [diff] [blame] | 71 | |
| 72 | def result(self) -> List[str]: |
| 73 | result = self.int_a % self.int_n |
| 74 | return [self.format_result(result)] |
| 75 | |
| 76 | @property |
| 77 | def is_valid(self) -> bool: |
| 78 | return bool(self.int_a < 2 * self.int_n) |
| 79 | |
Gabor Mezei | 80a334a | 2022-12-07 16:04:15 +0100 | [diff] [blame] | 80 | class BignumModRawMul(bignum_common.ModOperationCommon, |
| 81 | BignumModRawTarget): |
| 82 | """Test cases for bignum mpi_mod_raw_mul().""" |
| 83 | symbol = "*" |
| 84 | test_function = "mpi_mod_raw_mul" |
| 85 | test_name = "mbedtls_mpi_mod_raw_mul" |
| 86 | input_style = "arch_split" |
| 87 | arity = 2 |
| 88 | |
| 89 | def arguments(self) -> List[str]: |
Gabor Mezei | b31b2e6 | 2022-12-15 15:00:44 +0100 | [diff] [blame] | 90 | return [self.format_result(self.to_montgomery(self.int_a)), |
| 91 | self.format_result(self.to_montgomery(self.int_b)), |
| 92 | bignum_common.quote_str(self.arg_n) |
Gabor Mezei | 80a334a | 2022-12-07 16:04:15 +0100 | [diff] [blame] | 93 | ] + self.result() |
| 94 | |
| 95 | def result(self) -> List[str]: |
| 96 | result = (self.int_a * self.int_b) % self.int_n |
Gabor Mezei | b31b2e6 | 2022-12-15 15:00:44 +0100 | [diff] [blame] | 97 | return [self.format_result(self.to_montgomery(result))] |
Gabor Mezei | 80a334a | 2022-12-07 16:04:15 +0100 | [diff] [blame] | 98 | |
Minos Galanakis | 855c228 | 2022-11-10 11:33:25 +0000 | [diff] [blame] | 99 | |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 100 | class BignumModRawInvPrime(bignum_common.ModOperationCommon, |
| 101 | BignumModRawTarget): |
| 102 | """Test cases for bignum mpi_mod_raw_inv_prime().""" |
| 103 | moduli = ONLY_PRIME_MODULI |
| 104 | symbol = "^ -1" |
| 105 | test_function = "mpi_mod_raw_inv_prime" |
| 106 | test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)" |
Tom Cosgrove | dbac609 | 2022-12-14 08:27:18 +0000 | [diff] [blame] | 107 | input_style = "arch_split" |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 108 | arity = 1 |
| 109 | suffix = True |
Tom Cosgrove | f723754 | 2022-12-16 16:10:36 +0000 | [diff] [blame] | 110 | montgomery_form_a = True |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 111 | disallow_zero_a = True |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 112 | |
| 113 | def result(self) -> List[str]: |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 114 | result = bignum_common.invmod_positive(self.int_a, self.int_n) |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 115 | mont_result = self.to_montgomery(result) |
| 116 | return [self.format_result(mont_result)] |
| 117 | |
Minos Galanakis | 855c228 | 2022-11-10 11:33:25 +0000 | [diff] [blame] | 118 | |
Tom Cosgrove | 1923009 | 2022-11-24 15:56:53 +0000 | [diff] [blame] | 119 | class BignumModRawAdd(bignum_common.ModOperationCommon, |
| 120 | BignumModRawTarget): |
| 121 | """Test cases for bignum mpi_mod_raw_add().""" |
| 122 | symbol = "+" |
| 123 | test_function = "mpi_mod_raw_add" |
| 124 | test_name = "mbedtls_mpi_mod_raw_add" |
| 125 | input_style = "fixed" |
| 126 | arity = 2 |
| 127 | |
Tom Cosgrove | 1923009 | 2022-11-24 15:56:53 +0000 | [diff] [blame] | 128 | def result(self) -> List[str]: |
| 129 | result = (self.int_a + self.int_b) % self.int_n |
| 130 | return [self.format_result(result)] |
| 131 | |
Minos Galanakis | 855c228 | 2022-11-10 11:33:25 +0000 | [diff] [blame] | 132 | |
Gilles Peskine | be69c7d | 2022-12-20 19:51:22 +0100 | [diff] [blame] | 133 | class BignumModRawConvertRep(bignum_common.ModOperationCommon, |
| 134 | BignumModRawTarget): |
| 135 | # This is an abstract class, it's ok to have unimplemented methods. |
| 136 | #pylint: disable=abstract-method |
| 137 | """Test cases for representation conversion.""" |
Minos Galanakis | ae4d2cf | 2022-12-21 17:34:15 +0000 | [diff] [blame] | 138 | symbol = "" |
| 139 | input_style = "arch_split" |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 140 | arity = 1 |
Minos Galanakis | ae4d2cf | 2022-12-21 17:34:15 +0000 | [diff] [blame] | 141 | rep = bignum_common.ModulusRepresentation.INVALID |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 142 | |
Gilles Peskine | 636809f | 2022-12-21 20:12:31 +0100 | [diff] [blame] | 143 | def set_representation(self, r: bignum_common.ModulusRepresentation) -> None: |
Minos Galanakis | ae4d2cf | 2022-12-21 17:34:15 +0000 | [diff] [blame] | 144 | self.rep = r |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 145 | |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 146 | def arguments(self) -> List[str]: |
| 147 | return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(), |
| 148 | bignum_common.quote_str(self.arg_a)] + |
| 149 | self.result()) |
| 150 | |
Gilles Peskine | ad335b5 | 2022-12-20 22:39:15 +0100 | [diff] [blame] | 151 | def description(self) -> str: |
| 152 | base = super().description() |
| 153 | mod_with_rep = 'mod({})'.format(self.rep.name) |
| 154 | return base.replace('mod', mod_with_rep, 1) |
| 155 | |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 156 | @classmethod |
Gilles Peskine | 6d40e54 | 2022-12-21 20:18:23 +0100 | [diff] [blame] | 157 | def test_cases_for_values(cls, rep: bignum_common.ModulusRepresentation, |
| 158 | n: str, a: str) -> Iterator[test_case.TestCase]: |
Gilles Peskine | f287366 | 2022-12-21 20:28:29 +0100 | [diff] [blame] | 159 | """Emit test cases for the given values (if any). |
| 160 | |
| 161 | This may emit no test cases if a isn't valid for the modulus n, |
| 162 | or multiple test cases if rep requires different data depending |
| 163 | on the limb size. |
| 164 | """ |
Gilles Peskine | 6d40e54 | 2022-12-21 20:18:23 +0100 | [diff] [blame] | 165 | for bil in cls.limb_sizes: |
| 166 | test_object = cls(n, a, bits_in_limb=bil) |
| 167 | test_object.set_representation(rep) |
Gilles Peskine | f287366 | 2022-12-21 20:28:29 +0100 | [diff] [blame] | 168 | # The class is set to having separate test cases for each limb |
| 169 | # size, because the Montgomery representation requires it. |
| 170 | # But other representations don't require it. So for other |
| 171 | # representations, emit a single test case with no dependency |
| 172 | # on the limb size. |
| 173 | if rep is not bignum_common.ModulusRepresentation.MONTGOMERY: |
Gilles Peskine | 5efe449 | 2022-12-21 20:33:30 +0100 | [diff] [blame] | 174 | test_object.dependencies = \ |
| 175 | [dep for dep in test_object.dependencies |
| 176 | if not dep.startswith('MBEDTLS_HAVE_INT')] |
Gilles Peskine | 6d40e54 | 2022-12-21 20:18:23 +0100 | [diff] [blame] | 177 | if test_object.is_valid: |
| 178 | yield test_object.create_test_case() |
Gilles Peskine | f287366 | 2022-12-21 20:28:29 +0100 | [diff] [blame] | 179 | if rep is not bignum_common.ModulusRepresentation.MONTGOMERY: |
| 180 | # A single test case (emitted, or skipped due to invalidity) |
| 181 | # is enough, since this test case doesn't depend on the |
| 182 | # limb size. |
| 183 | break |
Gilles Peskine | 6d40e54 | 2022-12-21 20:18:23 +0100 | [diff] [blame] | 184 | |
Gilles Peskine | f287366 | 2022-12-21 20:28:29 +0100 | [diff] [blame] | 185 | # The parent class doesn't support non-bignum parameters. So we override |
| 186 | # test generation, in order to have the representation as a parameter. |
Gilles Peskine | 6d40e54 | 2022-12-21 20:18:23 +0100 | [diff] [blame] | 187 | @classmethod |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 188 | def generate_function_tests(cls) -> Iterator[test_case.TestCase]: |
Minos Galanakis | afa7c04 | 2022-12-21 17:38:16 +0000 | [diff] [blame] | 189 | |
| 190 | for rep in bignum_common.ModulusRepresentation.supported_representations(): |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 191 | for n in cls.moduli: |
| 192 | for a in cls.input_values: |
Gilles Peskine | 6d40e54 | 2022-12-21 20:18:23 +0100 | [diff] [blame] | 193 | yield from cls.test_cases_for_values(rep, n, a) |
Gilles Peskine | 23636ac | 2022-12-20 19:30:47 +0100 | [diff] [blame] | 194 | |
Gilles Peskine | be69c7d | 2022-12-20 19:51:22 +0100 | [diff] [blame] | 195 | class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep): |
| 196 | """Test cases for mpi_mod_raw_canonical_to_modulus_rep.""" |
| 197 | test_function = "mpi_mod_raw_canonical_to_modulus_rep" |
| 198 | test_name = "Rep canon->mod" |
| 199 | |
| 200 | def result(self) -> List[str]: |
Minos Galanakis | 5689410 | 2022-12-21 17:31:56 +0000 | [diff] [blame] | 201 | return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))] |
Gilles Peskine | be69c7d | 2022-12-20 19:51:22 +0100 | [diff] [blame] | 202 | |
| 203 | class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep): |
| 204 | """Test cases for mpi_mod_raw_modulus_to_canonical_rep.""" |
| 205 | test_function = "mpi_mod_raw_modulus_to_canonical_rep" |
| 206 | test_name = "Rep mod->canon" |
| 207 | |
| 208 | @property |
| 209 | def arg_a(self) -> str: |
Minos Galanakis | 5689410 | 2022-12-21 17:31:56 +0000 | [diff] [blame] | 210 | return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep))) |
Gilles Peskine | be69c7d | 2022-12-20 19:51:22 +0100 | [diff] [blame] | 211 | |
| 212 | def result(self) -> List[str]: |
| 213 | return [self.format_result(self.int_a)] |
| 214 | |
Janos Follath | f352c67 | 2022-11-20 13:40:25 +0000 | [diff] [blame] | 215 | |
Janos Follath | 155ad8c | 2022-11-17 14:42:40 +0000 | [diff] [blame] | 216 | class BignumModRawConvertToMont(bignum_common.ModOperationCommon, |
Janos Follath | 948afce | 2022-11-17 13:38:56 +0000 | [diff] [blame] | 217 | BignumModRawTarget): |
Minos Galanakis | a252f6b | 2022-11-09 19:23:53 +0000 | [diff] [blame] | 218 | """ Test cases for mpi_mod_raw_to_mont_rep(). """ |
Minos Galanakis | a252f6b | 2022-11-09 19:23:53 +0000 | [diff] [blame] | 219 | test_function = "mpi_mod_raw_to_mont_rep" |
| 220 | test_name = "Convert into Mont: " |
Janos Follath | 8ae7a65 | 2022-11-19 15:05:19 +0000 | [diff] [blame] | 221 | symbol = "R *" |
Janos Follath | 6fa3f06 | 2022-11-17 20:33:51 +0000 | [diff] [blame] | 222 | input_style = "arch_split" |
Janos Follath | 1921fd5 | 2022-11-18 17:51:02 +0000 | [diff] [blame] | 223 | arity = 1 |
Minos Galanakis | a252f6b | 2022-11-09 19:23:53 +0000 | [diff] [blame] | 224 | |
Minos Galanakis | a252f6b | 2022-11-09 19:23:53 +0000 | [diff] [blame] | 225 | def result(self) -> List[str]: |
Tom Cosgrove | c240600 | 2022-12-06 12:20:43 +0000 | [diff] [blame] | 226 | result = self.to_montgomery(self.int_a) |
Janos Follath | 1921fd5 | 2022-11-18 17:51:02 +0000 | [diff] [blame] | 227 | return [self.format_result(result)] |
Minos Galanakis | a252f6b | 2022-11-09 19:23:53 +0000 | [diff] [blame] | 228 | |
Janos Follath | f352c67 | 2022-11-20 13:40:25 +0000 | [diff] [blame] | 229 | class BignumModRawConvertFromMont(bignum_common.ModOperationCommon, |
| 230 | BignumModRawTarget): |
Minos Galanakis | 50de073 | 2022-11-09 19:36:16 +0000 | [diff] [blame] | 231 | """ Test cases for mpi_mod_raw_from_mont_rep(). """ |
Minos Galanakis | 50de073 | 2022-11-09 19:36:16 +0000 | [diff] [blame] | 232 | test_function = "mpi_mod_raw_from_mont_rep" |
| 233 | test_name = "Convert from Mont: " |
Janos Follath | 8ae7a65 | 2022-11-19 15:05:19 +0000 | [diff] [blame] | 234 | symbol = "1/R *" |
Janos Follath | f352c67 | 2022-11-20 13:40:25 +0000 | [diff] [blame] | 235 | input_style = "arch_split" |
| 236 | arity = 1 |
Minos Galanakis | 50de073 | 2022-11-09 19:36:16 +0000 | [diff] [blame] | 237 | |
Janos Follath | 1921fd5 | 2022-11-18 17:51:02 +0000 | [diff] [blame] | 238 | def result(self) -> List[str]: |
Tom Cosgrove | c240600 | 2022-12-06 12:20:43 +0000 | [diff] [blame] | 239 | result = self.from_montgomery(self.int_a) |
Janos Follath | 1921fd5 | 2022-11-18 17:51:02 +0000 | [diff] [blame] | 240 | return [self.format_result(result)] |
| 241 | |
Minos Galanakis | 78665eb | 2022-12-07 18:10:46 +0000 | [diff] [blame] | 242 | class BignumModRawModNegate(bignum_common.ModOperationCommon, |
| 243 | BignumModRawTarget): |
| 244 | """ Test cases for mpi_mod_raw_neg(). """ |
| 245 | test_function = "mpi_mod_raw_neg" |
| 246 | test_name = "Modular negation: " |
Minos Galanakis | f3abea6 | 2022-12-08 11:48:26 +0000 | [diff] [blame] | 247 | symbol = "-" |
Minos Galanakis | 78665eb | 2022-12-07 18:10:46 +0000 | [diff] [blame] | 248 | input_style = "arch_split" |
| 249 | arity = 1 |
Janos Follath | 1921fd5 | 2022-11-18 17:51:02 +0000 | [diff] [blame] | 250 | |
Minos Galanakis | 78665eb | 2022-12-07 18:10:46 +0000 | [diff] [blame] | 251 | def result(self) -> List[str]: |
| 252 | result = (self.int_n - self.int_a) % self.int_n |
| 253 | return [self.format_result(result)] |