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 | |
| 25 | def hex_to_int(val: str) -> int: |
| 26 | return int(val, 16) if val else 0 |
| 27 | |
| 28 | def quote_str(val) -> str: |
| 29 | return "\"{}\"".format(val) |
| 30 | |
| 31 | def bound_mpi8(val: int) -> int: |
| 32 | """First number exceeding 8-byte limbs needed for given input value.""" |
| 33 | return bound_mpi8_limbs(limbs_mpi8(val)) |
| 34 | |
| 35 | def bound_mpi4(val: int) -> int: |
| 36 | """First number exceeding 4-byte limbs needed for given input value.""" |
| 37 | return bound_mpi4_limbs(limbs_mpi4(val)) |
| 38 | |
| 39 | def bound_mpi8_limbs(limbs: int) -> int: |
| 40 | """First number exceeding maximum of given 8-byte limbs.""" |
| 41 | bits = 64 * limbs |
| 42 | return 1 << bits |
| 43 | |
| 44 | def bound_mpi4_limbs(limbs: int) -> int: |
| 45 | """First number exceeding maximum of given 4-byte limbs.""" |
| 46 | bits = 32 * limbs |
| 47 | return 1 << bits |
| 48 | |
| 49 | def limbs_mpi8(val: int) -> int: |
| 50 | """Return the number of 8-byte limbs required to store value.""" |
| 51 | return (val.bit_length() + 63) // 64 |
| 52 | |
| 53 | def limbs_mpi4(val: int) -> int: |
| 54 | """Return the number of 4-byte limbs required to store value.""" |
| 55 | return (val.bit_length() + 31) // 32 |
| 56 | |
| 57 | def combination_pairs(values: List[T]) -> List[Tuple[T, T]]: |
| 58 | """Return all pair combinations from input values. |
| 59 | |
| 60 | The return value is cast, as older versions of mypy are unable to derive |
| 61 | the specific type returned by itertools.combinations_with_replacement. |
| 62 | """ |
| 63 | return typing.cast( |
| 64 | List[Tuple[T, T]], |
| 65 | list(itertools.combinations_with_replacement(values, 2)) |
| 66 | ) |
| 67 | |
| 68 | |
| 69 | class OperationCommon: |
| 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. |
| 80 | """ |
| 81 | symbol = "" |
| 82 | input_values = [] # type: List[str] |
| 83 | input_cases = [] # type: List[Tuple[str, str]] |
| 84 | |
| 85 | def __init__(self, val_a: str, val_b: str) -> None: |
| 86 | self.arg_a = val_a |
| 87 | self.arg_b = val_b |
| 88 | self.int_a = hex_to_int(val_a) |
| 89 | self.int_b = hex_to_int(val_b) |
| 90 | |
| 91 | def arguments(self) -> List[str]: |
| 92 | return [quote_str(self.arg_a), quote_str(self.arg_b), self.result()] |
| 93 | |
| 94 | @abstractmethod |
| 95 | def result(self) -> str: |
| 96 | """Get the result of the operation. |
| 97 | |
| 98 | This could be calculated during initialization and stored as `_result` |
| 99 | and then returned, or calculated when the method is called. |
| 100 | """ |
| 101 | raise NotImplementedError |
| 102 | |
| 103 | @classmethod |
| 104 | def get_value_pairs(cls) -> Iterator[Tuple[str, str]]: |
| 105 | """Generator to yield pairs of inputs. |
| 106 | |
| 107 | Combinations are first generated from all input values, and then |
| 108 | specific cases provided. |
| 109 | """ |
| 110 | yield from combination_pairs(cls.input_values) |
| 111 | yield from cls.input_cases |