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