Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 1 | """Common features for bignum in test generation framework.""" |
| 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 | |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 17 | from abc import abstractmethod |
Janos Follath | 98edf21 | 2022-11-19 12:48:17 +0000 | [diff] [blame] | 18 | from typing import Iterator, List, Tuple, TypeVar, Any |
| 19 | from itertools import chain |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 20 | |
Janos Follath | 87df373 | 2022-11-09 12:31:23 +0000 | [diff] [blame] | 21 | from . import test_case |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 22 | from . import test_data_generation |
Janos Follath | dac44e6 | 2022-11-20 11:58:12 +0000 | [diff] [blame] | 23 | from .bignum_data import INPUTS_DEFAULT, MODULI_DEFAULT |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 24 | |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 25 | T = TypeVar('T') #pylint: disable=invalid-name |
| 26 | |
Werner Lewis | a850312 | 2022-10-04 10:10:40 +0100 | [diff] [blame] | 27 | def invmod(a: int, n: int) -> int: |
| 28 | """Return inverse of a to modulo n. |
| 29 | |
| 30 | Equivalent to pow(a, -1, n) in Python 3.8+. Implementation is equivalent |
| 31 | to long_invmod() in CPython. |
| 32 | """ |
| 33 | b, c = 1, 0 |
| 34 | while n: |
| 35 | q, r = divmod(a, n) |
| 36 | a, b, c, n = n, c, b - q*c, r |
| 37 | # at this point a is the gcd of the original inputs |
| 38 | if a == 1: |
| 39 | return b |
| 40 | raise ValueError("Not invertible") |
| 41 | |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 42 | def hex_to_int(val: str) -> int: |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 43 | """Implement the syntax accepted by mbedtls_test_read_mpi(). |
| 44 | |
| 45 | This is a superset of what is accepted by mbedtls_test_read_mpi_core(). |
| 46 | """ |
Gilles Peskine | b9b9026 | 2022-11-10 09:15:21 +0100 | [diff] [blame] | 47 | if val in ['', '-']: |
Gilles Peskine | 35af021 | 2022-11-15 20:43:33 +0100 | [diff] [blame] | 48 | return 0 |
| 49 | return int(val, 16) |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | f8a4463 | 2022-12-20 19:12:22 +0100 | [diff] [blame] | 51 | def quote_str(val: str) -> str: |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 52 | return "\"{}\"".format(val) |
| 53 | |
Werner Lewis | f86c82e | 2022-10-19 13:50:10 +0100 | [diff] [blame] | 54 | def bound_mpi(val: int, bits_in_limb: int) -> int: |
| 55 | """First number exceeding number of limbs needed for given input value.""" |
| 56 | return bound_mpi_limbs(limbs_mpi(val, bits_in_limb), bits_in_limb) |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 57 | |
Werner Lewis | f86c82e | 2022-10-19 13:50:10 +0100 | [diff] [blame] | 58 | def bound_mpi_limbs(limbs: int, bits_in_limb: int) -> int: |
| 59 | """First number exceeding maximum of given number of limbs.""" |
| 60 | bits = bits_in_limb * limbs |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 61 | return 1 << bits |
| 62 | |
Werner Lewis | f86c82e | 2022-10-19 13:50:10 +0100 | [diff] [blame] | 63 | def limbs_mpi(val: int, bits_in_limb: int) -> int: |
| 64 | """Return the number of limbs required to store value.""" |
| 65 | return (val.bit_length() + bits_in_limb - 1) // bits_in_limb |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 66 | |
| 67 | def combination_pairs(values: List[T]) -> List[Tuple[T, T]]: |
Gilles Peskine | 4cbbfd8 | 2022-11-09 21:57:52 +0100 | [diff] [blame] | 68 | """Return all pair combinations from input values.""" |
| 69 | return [(x, y) for x in values for y in values] |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 70 | |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 71 | class OperationCommon(test_data_generation.BaseTest): |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 72 | """Common features for bignum binary operations. |
| 73 | |
| 74 | This adds functionality common in binary operation tests. |
| 75 | |
| 76 | Attributes: |
| 77 | symbol: Symbol to use for the operation in case description. |
| 78 | input_values: List of values to use as test case inputs. These are |
| 79 | combined to produce pairs of values. |
| 80 | input_cases: List of tuples containing pairs of test case inputs. This |
| 81 | can be used to implement specific pairs of inputs. |
Werner Lewis | bbf0a32 | 2022-10-04 10:07:13 +0100 | [diff] [blame] | 82 | unique_combinations_only: Boolean to select if test case combinations |
| 83 | must be unique. If True, only A,B or B,A would be included as a test |
| 84 | case. If False, both A,B and B,A would be included. |
Janos Follath | 6fa3f06 | 2022-11-17 20:33:51 +0000 | [diff] [blame] | 85 | input_style: Controls the way how test data is passed to the functions |
| 86 | in the generated test cases. "variable" passes them as they are |
| 87 | defined in the python source. "arch_split" pads the values with |
| 88 | zeroes depending on the architecture/limb size. If this is set, |
| 89 | test cases are generated for all architectures. |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 90 | arity: the number of operands for the operation. Currently supported |
| 91 | values are 1 and 2. |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 92 | """ |
| 93 | symbol = "" |
Janos Follath | dac44e6 | 2022-11-20 11:58:12 +0000 | [diff] [blame] | 94 | input_values = INPUTS_DEFAULT # type: List[str] |
Janos Follath | 98edf21 | 2022-11-19 12:48:17 +0000 | [diff] [blame] | 95 | input_cases = [] # type: List[Any] |
Janos Follath | f457976 | 2022-11-20 13:32:54 +0000 | [diff] [blame] | 96 | unique_combinations_only = False |
Janos Follath | a36e430 | 2022-11-19 15:55:53 +0000 | [diff] [blame] | 97 | input_styles = ["variable", "fixed", "arch_split"] # type: List[str] |
Janos Follath | 6fa3f06 | 2022-11-17 20:33:51 +0000 | [diff] [blame] | 98 | input_style = "variable" # type: str |
Janos Follath | 155ad8c | 2022-11-17 14:42:40 +0000 | [diff] [blame] | 99 | limb_sizes = [32, 64] # type: List[int] |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 100 | arities = [1, 2] |
| 101 | arity = 2 |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 102 | suffix = False # for arity = 1, symbol can be prefix (default) or suffix |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 103 | |
Janos Follath | a36e430 | 2022-11-19 15:55:53 +0000 | [diff] [blame] | 104 | def __init__(self, val_a: str, val_b: str = "0", bits_in_limb: int = 32) -> None: |
Janos Follath | 4c59d35 | 2022-11-18 16:05:46 +0000 | [diff] [blame] | 105 | self.val_a = val_a |
| 106 | self.val_b = val_b |
Janos Follath | abfca8f | 2022-11-18 16:48:45 +0000 | [diff] [blame] | 107 | # Setting the int versions here as opposed to making them @properties |
| 108 | # provides earlier/more robust input validation. |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 109 | self.int_a = hex_to_int(val_a) |
| 110 | self.int_b = hex_to_int(val_b) |
Janos Follath | 155ad8c | 2022-11-17 14:42:40 +0000 | [diff] [blame] | 111 | if bits_in_limb not in self.limb_sizes: |
| 112 | raise ValueError("Invalid number of bits in limb!") |
Janos Follath | 6fa3f06 | 2022-11-17 20:33:51 +0000 | [diff] [blame] | 113 | if self.input_style == "arch_split": |
Janos Follath | 155ad8c | 2022-11-17 14:42:40 +0000 | [diff] [blame] | 114 | self.dependencies = ["MBEDTLS_HAVE_INT{:d}".format(bits_in_limb)] |
| 115 | self.bits_in_limb = bits_in_limb |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 116 | |
Janos Follath | b41ab92 | 2022-11-17 15:13:02 +0000 | [diff] [blame] | 117 | @property |
| 118 | def boundary(self) -> int: |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 119 | if self.arity == 1: |
| 120 | return self.int_a |
| 121 | elif self.arity == 2: |
| 122 | return max(self.int_a, self.int_b) |
| 123 | raise ValueError("Unsupported number of operands!") |
Janos Follath | b41ab92 | 2022-11-17 15:13:02 +0000 | [diff] [blame] | 124 | |
| 125 | @property |
Janos Follath | 6fa3f06 | 2022-11-17 20:33:51 +0000 | [diff] [blame] | 126 | def limb_boundary(self) -> int: |
| 127 | return bound_mpi(self.boundary, self.bits_in_limb) |
| 128 | |
| 129 | @property |
Janos Follath | b41ab92 | 2022-11-17 15:13:02 +0000 | [diff] [blame] | 130 | def limbs(self) -> int: |
| 131 | return limbs_mpi(self.boundary, self.bits_in_limb) |
| 132 | |
| 133 | @property |
| 134 | def hex_digits(self) -> int: |
| 135 | return 2 * (self.limbs * self.bits_in_limb // 8) |
| 136 | |
Gilles Peskine | f8a4463 | 2022-12-20 19:12:22 +0100 | [diff] [blame] | 137 | def format_arg(self, val: str) -> str: |
Janos Follath | 4c59d35 | 2022-11-18 16:05:46 +0000 | [diff] [blame] | 138 | if self.input_style not in self.input_styles: |
| 139 | raise ValueError("Unknown input style!") |
| 140 | if self.input_style == "variable": |
| 141 | return val |
| 142 | else: |
| 143 | return val.zfill(self.hex_digits) |
| 144 | |
Gilles Peskine | f8a4463 | 2022-12-20 19:12:22 +0100 | [diff] [blame] | 145 | def format_result(self, res: int) -> str: |
Janos Follath | 4c59d35 | 2022-11-18 16:05:46 +0000 | [diff] [blame] | 146 | res_str = '{:x}'.format(res) |
| 147 | return quote_str(self.format_arg(res_str)) |
Janos Follath | b41ab92 | 2022-11-17 15:13:02 +0000 | [diff] [blame] | 148 | |
| 149 | @property |
Janos Follath | 4c59d35 | 2022-11-18 16:05:46 +0000 | [diff] [blame] | 150 | def arg_a(self) -> str: |
| 151 | return self.format_arg(self.val_a) |
| 152 | |
| 153 | @property |
| 154 | def arg_b(self) -> str: |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 155 | if self.arity == 1: |
| 156 | raise AttributeError("Operation is unary and doesn't have arg_b!") |
Janos Follath | 4c59d35 | 2022-11-18 16:05:46 +0000 | [diff] [blame] | 157 | return self.format_arg(self.val_b) |
Janos Follath | b41ab92 | 2022-11-17 15:13:02 +0000 | [diff] [blame] | 158 | |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 159 | def arguments(self) -> List[str]: |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 160 | args = [quote_str(self.arg_a)] |
| 161 | if self.arity == 2: |
| 162 | args.append(quote_str(self.arg_b)) |
| 163 | return args + self.result() |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 164 | |
Janos Follath | 3aeb60a | 2022-11-09 13:24:46 +0000 | [diff] [blame] | 165 | def description(self) -> str: |
| 166 | """Generate a description for the test case. |
| 167 | |
| 168 | If not set, case_description uses the form A `symbol` B, where symbol |
| 169 | is used to represent the operation. Descriptions of each value are |
| 170 | generated to provide some context to the test case. |
| 171 | """ |
| 172 | if not self.case_description: |
Janos Follath | 8ae7a65 | 2022-11-19 15:05:19 +0000 | [diff] [blame] | 173 | if self.arity == 1: |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 174 | format_string = "{1:x} {0}" if self.suffix else "{0} {1:x}" |
| 175 | self.case_description = format_string.format( |
Janos Follath | 8ae7a65 | 2022-11-19 15:05:19 +0000 | [diff] [blame] | 176 | self.symbol, self.int_a |
| 177 | ) |
| 178 | elif self.arity == 2: |
| 179 | self.case_description = "{:x} {} {:x}".format( |
| 180 | self.int_a, self.symbol, self.int_b |
| 181 | ) |
Janos Follath | 3aeb60a | 2022-11-09 13:24:46 +0000 | [diff] [blame] | 182 | return super().description() |
| 183 | |
Janos Follath | 939621f | 2022-11-18 18:15:24 +0000 | [diff] [blame] | 184 | @property |
| 185 | def is_valid(self) -> bool: |
| 186 | return True |
| 187 | |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 188 | @abstractmethod |
Werner Lewis | 1b20e7e | 2022-10-12 14:53:17 +0100 | [diff] [blame] | 189 | def result(self) -> List[str]: |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 190 | """Get the result of the operation. |
| 191 | |
| 192 | This could be calculated during initialization and stored as `_result` |
| 193 | and then returned, or calculated when the method is called. |
| 194 | """ |
| 195 | raise NotImplementedError |
| 196 | |
| 197 | @classmethod |
| 198 | def get_value_pairs(cls) -> Iterator[Tuple[str, str]]: |
| 199 | """Generator to yield pairs of inputs. |
| 200 | |
| 201 | Combinations are first generated from all input values, and then |
| 202 | specific cases provided. |
| 203 | """ |
Janos Follath | 284672c | 2022-11-19 14:55:43 +0000 | [diff] [blame] | 204 | if cls.arity == 1: |
| 205 | yield from ((a, "0") for a in cls.input_values) |
| 206 | elif cls.arity == 2: |
| 207 | if cls.unique_combinations_only: |
| 208 | yield from combination_pairs(cls.input_values) |
| 209 | else: |
| 210 | yield from ( |
| 211 | (a, b) |
| 212 | for a in cls.input_values |
| 213 | for b in cls.input_values |
| 214 | ) |
Werner Lewis | bbf0a32 | 2022-10-04 10:07:13 +0100 | [diff] [blame] | 215 | else: |
Janos Follath | 284672c | 2022-11-19 14:55:43 +0000 | [diff] [blame] | 216 | raise ValueError("Unsupported number of operands!") |
Janos Follath | f8b3b72 | 2022-11-03 14:46:18 +0000 | [diff] [blame] | 217 | |
Janos Follath | 87df373 | 2022-11-09 12:31:23 +0000 | [diff] [blame] | 218 | @classmethod |
| 219 | def generate_function_tests(cls) -> Iterator[test_case.TestCase]: |
Janos Follath | 6fa3f06 | 2022-11-17 20:33:51 +0000 | [diff] [blame] | 220 | if cls.input_style not in cls.input_styles: |
| 221 | raise ValueError("Unknown input style!") |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 222 | if cls.arity not in cls.arities: |
| 223 | raise ValueError("Unsupported number of operands!") |
Janos Follath | 939621f | 2022-11-18 18:15:24 +0000 | [diff] [blame] | 224 | if cls.input_style == "arch_split": |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 225 | test_objects = (cls(a, b, bits_in_limb=bil) |
| 226 | for a, b in cls.get_value_pairs() |
Janos Follath | 939621f | 2022-11-18 18:15:24 +0000 | [diff] [blame] | 227 | for bil in cls.limb_sizes) |
Janos Follath | 98edf21 | 2022-11-19 12:48:17 +0000 | [diff] [blame] | 228 | special_cases = (cls(*args, bits_in_limb=bil) # type: ignore |
| 229 | for args in cls.input_cases |
| 230 | for bil in cls.limb_sizes) |
Janos Follath | 939621f | 2022-11-18 18:15:24 +0000 | [diff] [blame] | 231 | else: |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 232 | test_objects = (cls(a, b) |
| 233 | for a, b in cls.get_value_pairs()) |
Janos Follath | 98edf21 | 2022-11-19 12:48:17 +0000 | [diff] [blame] | 234 | special_cases = (cls(*args) for args in cls.input_cases) |
Janos Follath | 939621f | 2022-11-18 18:15:24 +0000 | [diff] [blame] | 235 | yield from (valid_test_object.create_test_case() |
| 236 | for valid_test_object in filter( |
| 237 | lambda test_object: test_object.is_valid, |
Janos Follath | 98edf21 | 2022-11-19 12:48:17 +0000 | [diff] [blame] | 238 | chain(test_objects, special_cases) |
| 239 | ) |
| 240 | ) |
| 241 | |
Janos Follath | 87df373 | 2022-11-09 12:31:23 +0000 | [diff] [blame] | 242 | |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 243 | class ModOperationCommon(OperationCommon): |
| 244 | #pylint: disable=abstract-method |
| 245 | """Target for bignum mod_raw test case generation.""" |
Janos Follath | dac44e6 | 2022-11-20 11:58:12 +0000 | [diff] [blame] | 246 | moduli = MODULI_DEFAULT # type: List[str] |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 247 | |
Janos Follath | 155ad8c | 2022-11-17 14:42:40 +0000 | [diff] [blame] | 248 | def __init__(self, val_n: str, val_a: str, val_b: str = "0", |
| 249 | bits_in_limb: int = 64) -> None: |
| 250 | super().__init__(val_a=val_a, val_b=val_b, bits_in_limb=bits_in_limb) |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 251 | self.val_n = val_n |
Janos Follath | abfca8f | 2022-11-18 16:48:45 +0000 | [diff] [blame] | 252 | # Setting the int versions here as opposed to making them @properties |
| 253 | # provides earlier/more robust input validation. |
| 254 | self.int_n = hex_to_int(val_n) |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 255 | |
Tom Cosgrove | 21d459d | 2022-12-06 12:36:00 +0000 | [diff] [blame] | 256 | def to_montgomery(self, val: int) -> int: |
Tom Cosgrove | c240600 | 2022-12-06 12:20:43 +0000 | [diff] [blame] | 257 | return (val * self.r) % self.int_n |
| 258 | |
Tom Cosgrove | 21d459d | 2022-12-06 12:36:00 +0000 | [diff] [blame] | 259 | def from_montgomery(self, val: int) -> int: |
Tom Cosgrove | c240600 | 2022-12-06 12:20:43 +0000 | [diff] [blame] | 260 | return (val * self.r_inv) % self.int_n |
| 261 | |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 262 | @property |
| 263 | def boundary(self) -> int: |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 264 | return self.int_n |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 265 | |
| 266 | @property |
Janos Follath | 4c59d35 | 2022-11-18 16:05:46 +0000 | [diff] [blame] | 267 | def arg_n(self) -> str: |
| 268 | return self.format_arg(self.val_n) |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 269 | |
Gilles Peskine | 5623ecc | 2022-12-20 19:16:54 +0100 | [diff] [blame^] | 270 | def format_arg(self, val: str) -> str: |
| 271 | if self.input_style == "variable": |
| 272 | return val.zfill(len(hex(self.int_n)) - 2) |
| 273 | else: |
| 274 | return super().format_arg(val) |
| 275 | |
Janos Follath | a36a3d3 | 2022-11-18 17:49:13 +0000 | [diff] [blame] | 276 | def arguments(self) -> List[str]: |
| 277 | return [quote_str(self.arg_n)] + super().arguments() |
| 278 | |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 279 | @property |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 280 | def r(self) -> int: # pylint: disable=invalid-name |
| 281 | l = limbs_mpi(self.int_n, self.bits_in_limb) |
| 282 | return bound_mpi_limbs(l, self.bits_in_limb) |
| 283 | |
| 284 | @property |
| 285 | def r_inv(self) -> int: |
| 286 | return invmod(self.r, self.int_n) |
| 287 | |
| 288 | @property |
| 289 | def r2(self) -> int: # pylint: disable=invalid-name |
| 290 | return pow(self.r, 2) |
| 291 | |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 292 | @property |
| 293 | def is_valid(self) -> bool: |
| 294 | if self.int_a >= self.int_n: |
| 295 | return False |
| 296 | if self.arity == 2 and self.int_b >= self.int_n: |
| 297 | return False |
| 298 | return True |
| 299 | |
Janos Follath | 8ae7a65 | 2022-11-19 15:05:19 +0000 | [diff] [blame] | 300 | def description(self) -> str: |
| 301 | """Generate a description for the test case. |
| 302 | |
| 303 | It uses the form A `symbol` B mod N, where symbol is used to represent |
| 304 | the operation. |
| 305 | """ |
| 306 | |
| 307 | if not self.case_description: |
| 308 | return super().description() + " mod {:x}".format(self.int_n) |
| 309 | return super().description() |
| 310 | |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 311 | @classmethod |
Janos Follath | 435b305 | 2022-11-19 14:18:02 +0000 | [diff] [blame] | 312 | def input_cases_args(cls) -> Iterator[Tuple[Any, Any, Any]]: |
| 313 | if cls.arity == 1: |
| 314 | yield from ((n, a, "0") for a, n in cls.input_cases) |
| 315 | elif cls.arity == 2: |
| 316 | yield from ((n, a, b) for a, b, n in cls.input_cases) |
| 317 | else: |
| 318 | raise ValueError("Unsupported number of operands!") |
| 319 | |
| 320 | @classmethod |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 321 | def generate_function_tests(cls) -> Iterator[test_case.TestCase]: |
| 322 | if cls.input_style not in cls.input_styles: |
| 323 | raise ValueError("Unknown input style!") |
| 324 | if cls.arity not in cls.arities: |
| 325 | raise ValueError("Unsupported number of operands!") |
| 326 | if cls.input_style == "arch_split": |
| 327 | test_objects = (cls(n, a, b, bits_in_limb=bil) |
| 328 | for n in cls.moduli |
| 329 | for a, b in cls.get_value_pairs() |
| 330 | for bil in cls.limb_sizes) |
Janos Follath | 435b305 | 2022-11-19 14:18:02 +0000 | [diff] [blame] | 331 | special_cases = (cls(*args, bits_in_limb=bil) |
| 332 | for args in cls.input_cases_args() |
| 333 | for bil in cls.limb_sizes) |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 334 | else: |
| 335 | test_objects = (cls(n, a, b) |
| 336 | for n in cls.moduli |
| 337 | for a, b in cls.get_value_pairs()) |
Janos Follath | 435b305 | 2022-11-19 14:18:02 +0000 | [diff] [blame] | 338 | special_cases = (cls(*args) for args in cls.input_cases_args()) |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 339 | yield from (valid_test_object.create_test_case() |
| 340 | for valid_test_object in filter( |
| 341 | lambda test_object: test_object.is_valid, |
Janos Follath | 435b305 | 2022-11-19 14:18:02 +0000 | [diff] [blame] | 342 | chain(test_objects, special_cases) |
Janos Follath | c4fca5d | 2022-11-19 10:42:20 +0000 | [diff] [blame] | 343 | )) |
Janos Follath | 5b1dbb4 | 2022-11-17 13:32:43 +0000 | [diff] [blame] | 344 | |
Janos Follath | f8b3b72 | 2022-11-03 14:46:18 +0000 | [diff] [blame] | 345 | # BEGIN MERGE SLOT 1 |
| 346 | |
| 347 | # END MERGE SLOT 1 |
| 348 | |
| 349 | # BEGIN MERGE SLOT 2 |
| 350 | |
| 351 | # END MERGE SLOT 2 |
| 352 | |
| 353 | # BEGIN MERGE SLOT 3 |
| 354 | |
| 355 | # END MERGE SLOT 3 |
| 356 | |
| 357 | # BEGIN MERGE SLOT 4 |
| 358 | |
| 359 | # END MERGE SLOT 4 |
| 360 | |
| 361 | # BEGIN MERGE SLOT 5 |
| 362 | |
| 363 | # END MERGE SLOT 5 |
| 364 | |
| 365 | # BEGIN MERGE SLOT 6 |
| 366 | |
| 367 | # END MERGE SLOT 6 |
| 368 | |
| 369 | # BEGIN MERGE SLOT 7 |
| 370 | |
| 371 | # END MERGE SLOT 7 |
| 372 | |
| 373 | # BEGIN MERGE SLOT 8 |
| 374 | |
| 375 | # END MERGE SLOT 8 |
| 376 | |
| 377 | # BEGIN MERGE SLOT 9 |
| 378 | |
| 379 | # END MERGE SLOT 9 |
| 380 | |
| 381 | # BEGIN MERGE SLOT 10 |
| 382 | |
| 383 | # END MERGE SLOT 10 |