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 | |
| 17 | import itertools |
| 18 | import typing |
| 19 | |
| 20 | from abc import abstractmethod |
| 21 | from typing import Iterator, List, Tuple, TypeVar |
| 22 | |
| 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: |
| 41 | return int(val, 16) if val else 0 |
| 42 | |
| 43 | def quote_str(val) -> str: |
| 44 | return "\"{}\"".format(val) |
| 45 | |
| 46 | def bound_mpi8(val: int) -> int: |
| 47 | """First number exceeding 8-byte limbs needed for given input value.""" |
| 48 | return bound_mpi8_limbs(limbs_mpi8(val)) |
| 49 | |
| 50 | def bound_mpi4(val: int) -> int: |
| 51 | """First number exceeding 4-byte limbs needed for given input value.""" |
| 52 | return bound_mpi4_limbs(limbs_mpi4(val)) |
| 53 | |
| 54 | def bound_mpi8_limbs(limbs: int) -> int: |
| 55 | """First number exceeding maximum of given 8-byte limbs.""" |
| 56 | bits = 64 * limbs |
| 57 | return 1 << bits |
| 58 | |
| 59 | def bound_mpi4_limbs(limbs: int) -> int: |
| 60 | """First number exceeding maximum of given 4-byte limbs.""" |
| 61 | bits = 32 * limbs |
| 62 | return 1 << bits |
| 63 | |
| 64 | def limbs_mpi8(val: int) -> int: |
| 65 | """Return the number of 8-byte limbs required to store value.""" |
| 66 | return (val.bit_length() + 63) // 64 |
| 67 | |
| 68 | def limbs_mpi4(val: int) -> int: |
| 69 | """Return the number of 4-byte limbs required to store value.""" |
| 70 | return (val.bit_length() + 31) // 32 |
| 71 | |
| 72 | def combination_pairs(values: List[T]) -> List[Tuple[T, T]]: |
| 73 | """Return all pair combinations from input values. |
| 74 | |
| 75 | The return value is cast, as older versions of mypy are unable to derive |
| 76 | the specific type returned by itertools.combinations_with_replacement. |
| 77 | """ |
| 78 | return typing.cast( |
| 79 | List[Tuple[T, T]], |
| 80 | list(itertools.combinations_with_replacement(values, 2)) |
| 81 | ) |
| 82 | |
| 83 | |
| 84 | class OperationCommon: |
| 85 | """Common features for bignum binary operations. |
| 86 | |
| 87 | This adds functionality common in binary operation tests. |
| 88 | |
| 89 | Attributes: |
| 90 | symbol: Symbol to use for the operation in case description. |
| 91 | input_values: List of values to use as test case inputs. These are |
| 92 | combined to produce pairs of values. |
| 93 | input_cases: List of tuples containing pairs of test case inputs. This |
| 94 | can be used to implement specific pairs of inputs. |
Werner Lewis | bbf0a32 | 2022-10-04 10:07:13 +0100 | [diff] [blame] | 95 | unique_combinations_only: Boolean to select if test case combinations |
| 96 | must be unique. If True, only A,B or B,A would be included as a test |
| 97 | case. If False, both A,B and B,A would be included. |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 98 | """ |
| 99 | symbol = "" |
| 100 | input_values = [] # type: List[str] |
| 101 | input_cases = [] # type: List[Tuple[str, str]] |
Werner Lewis | bbf0a32 | 2022-10-04 10:07:13 +0100 | [diff] [blame] | 102 | unique_combinations_only = True |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 103 | |
| 104 | def __init__(self, val_a: str, val_b: str) -> None: |
| 105 | self.arg_a = val_a |
| 106 | self.arg_b = val_b |
| 107 | self.int_a = hex_to_int(val_a) |
| 108 | self.int_b = hex_to_int(val_b) |
| 109 | |
| 110 | def arguments(self) -> List[str]: |
Werner Lewis | 1b20e7e | 2022-10-12 14:53:17 +0100 | [diff] [blame^] | 111 | return [ |
| 112 | quote_str(self.arg_a), quote_str(self.arg_b) |
| 113 | ] + self.result() |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 114 | |
| 115 | @abstractmethod |
Werner Lewis | 1b20e7e | 2022-10-12 14:53:17 +0100 | [diff] [blame^] | 116 | def result(self) -> List[str]: |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 117 | """Get the result of the operation. |
| 118 | |
| 119 | This could be calculated during initialization and stored as `_result` |
| 120 | and then returned, or calculated when the method is called. |
| 121 | """ |
| 122 | raise NotImplementedError |
| 123 | |
| 124 | @classmethod |
| 125 | def get_value_pairs(cls) -> Iterator[Tuple[str, str]]: |
| 126 | """Generator to yield pairs of inputs. |
| 127 | |
| 128 | Combinations are first generated from all input values, and then |
| 129 | specific cases provided. |
| 130 | """ |
Werner Lewis | bbf0a32 | 2022-10-04 10:07:13 +0100 | [diff] [blame] | 131 | if cls.unique_combinations_only: |
| 132 | yield from combination_pairs(cls.input_values) |
| 133 | else: |
| 134 | yield from ( |
| 135 | (a, b) |
| 136 | for a in cls.input_values |
| 137 | for b in cls.input_values |
| 138 | ) |
Werner Lewis | 99e8178 | 2022-09-30 16:28:43 +0100 | [diff] [blame] | 139 | yield from cls.input_cases |