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 |
| 56 | pass |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 57 | |
Minos Galanakis | e9c86a1 | 2022-11-09 11:46:47 +0000 | [diff] [blame^] | 58 | class BignumModRawOperationArchSplit(BignumModRawOperation): |
| 59 | #pylint: disable=abstract-method |
| 60 | """Common features for bignum core operations where the result depends on |
| 61 | the limb size.""" |
| 62 | |
| 63 | def __init__(self, val_a: str, val_b: str, bits_in_limb: int) -> None: |
| 64 | super().__init__(val_a, val_b) |
| 65 | bound_val = max(self.int_a, self.int_b) |
| 66 | self.bits_in_limb = bits_in_limb |
| 67 | self.bound = bignum_common.bound_mpi(bound_val, self.bits_in_limb) |
| 68 | limbs = bignum_common.limbs_mpi(bound_val, self.bits_in_limb) |
| 69 | byte_len = limbs * self.bits_in_limb // 8 |
| 70 | self.hex_digits = 2 * byte_len |
| 71 | if self.bits_in_limb == 32: |
| 72 | self.dependencies = ["MBEDTLS_HAVE_INT32"] |
| 73 | elif self.bits_in_limb == 64: |
| 74 | self.dependencies = ["MBEDTLS_HAVE_INT64"] |
| 75 | else: |
| 76 | raise ValueError("Invalid number of bits in limb!") |
| 77 | self.arg_a = self.arg_a.zfill(self.hex_digits) |
| 78 | self.arg_b = self.arg_b.zfill(self.hex_digits) |
| 79 | self.arg_a_int = bignum_common.hex_to_int(self.arg_a) |
| 80 | self.arg_b_int = bignum_common.hex_to_int(self.arg_b) |
| 81 | |
| 82 | def pad_to_limbs(self, val) -> str: |
| 83 | return "{:x}".format(val).zfill(self.hex_digits) |
| 84 | |
| 85 | @classmethod |
| 86 | def generate_function_tests(cls) -> Iterator[test_case.TestCase]: |
| 87 | for a_value, b_value in cls.get_value_pairs(): |
| 88 | yield cls(a_value, b_value, 32).create_test_case() |
| 89 | yield cls(a_value, b_value, 64).create_test_case() |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 90 | # END MERGE SLOT 7 |
| 91 | |
| 92 | # BEGIN MERGE SLOT 8 |
| 93 | |
| 94 | # END MERGE SLOT 8 |
| 95 | |
| 96 | # BEGIN MERGE SLOT 9 |
| 97 | |
| 98 | # END MERGE SLOT 9 |
| 99 | |
| 100 | # BEGIN MERGE SLOT 10 |
| 101 | |
| 102 | # END MERGE SLOT 10 |