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