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 | |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 17 | from abc import ABCMeta |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 18 | from typing import Dict, Iterator, List |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 19 | |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 20 | from . import test_case |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 21 | from . import test_data_generation |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 22 | from . import bignum_common |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 23 | |
Janos Follath | d820ca5 | 2022-11-03 08:42:54 +0000 | [diff] [blame] | 24 | class BignumModRawTarget(test_data_generation.BaseTarget, metaclass=ABCMeta): |
Janos Follath | df8239b | 2022-11-02 14:40:58 +0000 | [diff] [blame] | 25 | #pylint: disable=abstract-method |
| 26 | """Target for bignum mod_raw test case generation.""" |
| 27 | target_basename = 'test_suite_bignum_mod_raw.generated' |
| 28 | |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 29 | class BignumModRawOperation(bignum_common.OperationCommon, BignumModRawTarget, metaclass=ABCMeta): |
| 30 | #pylint: disable=abstract-method |
Minos Galanakis | a461ece | 2022-11-09 12:36:02 +0000 | [diff] [blame] | 31 | """Target for bignum mod_raw test case generation.""" |
| 32 | |
| 33 | def __init__(self, val_n: str, val_a: str, val_b: str = "0", bits_in_limb: int = 64) -> None: |
| 34 | super().__init__(val_a=val_a, val_b=val_b) |
| 35 | self.val_n = val_n |
| 36 | self.bits_in_limb = bits_in_limb |
| 37 | |
| 38 | @property |
| 39 | def int_n(self) -> int: |
| 40 | return bignum_common.hex_to_int(self.val_n) |
| 41 | |
| 42 | @property |
| 43 | def boundary(self) -> int: |
| 44 | data_in = [self.int_a, self.int_b, self.int_n] |
| 45 | return max([n for n in data_in if n is not None]) |
| 46 | |
| 47 | @property |
| 48 | def limbs(self) -> int: |
| 49 | return bignum_common.limbs_mpi(self.boundary, self.bits_in_limb) |
| 50 | |
| 51 | @property |
| 52 | def hex_digits(self) -> int: |
| 53 | return 2 * (self.limbs * self.bits_in_limb // 8) |
| 54 | |
| 55 | @property |
| 56 | def hex_n(self) -> str: |
| 57 | return "{:x}".format(self.int_n).zfill(self.hex_digits) |
| 58 | |
| 59 | @property |
| 60 | def hex_a(self) -> str: |
| 61 | return "{:x}".format(self.int_a).zfill(self.hex_digits) |
| 62 | |
| 63 | @property |
| 64 | def hex_b(self) -> str: |
| 65 | return "{:x}".format(self.int_b).zfill(self.hex_digits) |
| 66 | |
| 67 | @property |
| 68 | def r(self) -> int: # pylint: disable=invalid-name |
| 69 | l = bignum_common.limbs_mpi(self.int_n, self.bits_in_limb) |
| 70 | return bignum_common.bound_mpi_limbs(l, self.bits_in_limb) |
| 71 | |
| 72 | @property |
| 73 | def r_inv(self) -> int: |
| 74 | return bignum_common.invmod(self.r, self.int_n) |
| 75 | |
| 76 | @property |
Minos Galanakis | 855c228 | 2022-11-10 11:33:25 +0000 | [diff] [blame^] | 77 | def r2(self) -> int: # pylint: disable=invalid-name |
Minos Galanakis | a461ece | 2022-11-09 12:36:02 +0000 | [diff] [blame] | 78 | return pow(self.r, 2) |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 79 | |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 80 | class BignumModRawOperationArchSplit(BignumModRawOperation): |
| 81 | #pylint: disable=abstract-method |
Minos Galanakis | a461ece | 2022-11-09 12:36:02 +0000 | [diff] [blame] | 82 | """Common features for bignum mod raw operations where the result depends on |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 83 | the limb size.""" |
| 84 | |
Minos Galanakis | a461ece | 2022-11-09 12:36:02 +0000 | [diff] [blame] | 85 | limb_sizes = [32, 64] # type: List[int] |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 86 | |
Minos Galanakis | a461ece | 2022-11-09 12:36:02 +0000 | [diff] [blame] | 87 | def __init__(self, val_n: str, val_a: str, val_b: str = "0", bits_in_limb: int = 64) -> None: |
| 88 | super().__init__(val_n=val_n, val_a=val_a, val_b=val_b, bits_in_limb=bits_in_limb) |
| 89 | |
| 90 | if bits_in_limb not in self.limb_sizes: |
| 91 | raise ValueError("Invalid number of bits in limb!") |
| 92 | |
| 93 | self.dependencies = ["MBEDTLS_HAVE_INT{:d}".format(bits_in_limb)] |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame] | 94 | |
| 95 | @classmethod |
| 96 | def generate_function_tests(cls) -> Iterator[test_case.TestCase]: |
| 97 | for a_value, b_value in cls.get_value_pairs(): |
Minos Galanakis | a461ece | 2022-11-09 12:36:02 +0000 | [diff] [blame] | 98 | for bil in cls.limb_sizes: |
| 99 | yield cls(a_value, b_value, bits_in_limb=bil).create_test_case() |
Minos Galanakis | 855c228 | 2022-11-10 11:33:25 +0000 | [diff] [blame^] | 100 | # BEGIN MERGE SLOT 1 |
| 101 | |
| 102 | # END MERGE SLOT 1 |
| 103 | |
| 104 | # BEGIN MERGE SLOT 2 |
| 105 | |
| 106 | # END MERGE SLOT 2 |
| 107 | |
| 108 | # BEGIN MERGE SLOT 3 |
| 109 | |
| 110 | # END MERGE SLOT 3 |
| 111 | |
| 112 | # BEGIN MERGE SLOT 4 |
| 113 | |
| 114 | # END MERGE SLOT 4 |
| 115 | |
| 116 | # BEGIN MERGE SLOT 5 |
| 117 | |
| 118 | # END MERGE SLOT 5 |
| 119 | |
| 120 | # BEGIN MERGE SLOT 6 |
| 121 | |
| 122 | # END MERGE SLOT 6 |
| 123 | |
| 124 | # BEGIN MERGE SLOT 7 |
| 125 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 126 | # END MERGE SLOT 7 |
| 127 | |
| 128 | # BEGIN MERGE SLOT 8 |
| 129 | |
| 130 | # END MERGE SLOT 8 |
| 131 | |
| 132 | # BEGIN MERGE SLOT 9 |
| 133 | |
| 134 | # END MERGE SLOT 9 |
| 135 | |
| 136 | # BEGIN MERGE SLOT 10 |
| 137 | |
| 138 | # END MERGE SLOT 10 |