blob: 1127ced8d815deefb509c29c2409f4be460670eb [file] [log] [blame]
Janos Follathdf8239b2022-11-02 14:40:58 +00001"""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 Follathdf8239b2022-11-02 14:40:58 +000017from abc import ABCMeta
Minos Galanakise9c86a12022-11-09 11:46:47 +000018from typing import Dict, Iterator, List
Janos Follathdf8239b2022-11-02 14:40:58 +000019
Minos Galanakise9c86a12022-11-09 11:46:47 +000020from . import test_case
Janos Follathdf8239b2022-11-02 14:40:58 +000021from . import test_data_generation
Minos Galanakise9c86a12022-11-09 11:46:47 +000022from . import bignum_common
Janos Follathdf8239b2022-11-02 14:40:58 +000023
Janos Follathd820ca52022-11-03 08:42:54 +000024class BignumModRawTarget(test_data_generation.BaseTarget, metaclass=ABCMeta):
Janos Follathdf8239b2022-11-02 14:40:58 +000025 #pylint: disable=abstract-method
26 """Target for bignum mod_raw test case generation."""
27 target_basename = 'test_suite_bignum_mod_raw.generated'
28
Janos Follath1be322a2022-11-02 14:46:23 +000029# 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 Galanakise9c86a12022-11-09 11:46:47 +000054class BignumModRawOperation(bignum_common.OperationCommon, BignumModRawTarget, metaclass=ABCMeta):
55 #pylint: disable=abstract-method
56 pass
Janos Follath1be322a2022-11-02 14:46:23 +000057
Minos Galanakise9c86a12022-11-09 11:46:47 +000058class 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 Follath1be322a2022-11-02 14:46:23 +000090# 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