blob: 1465e3ed75056bf71041c511ed885d91db6265b7 [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
Minos Galanakise9c86a12022-11-09 11:46:47 +000029class BignumModRawOperation(bignum_common.OperationCommon, BignumModRawTarget, metaclass=ABCMeta):
30 #pylint: disable=abstract-method
Minos Galanakisa461ece2022-11-09 12:36:02 +000031 """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 Galanakis855c2282022-11-10 11:33:25 +000077 def r2(self) -> int: # pylint: disable=invalid-name
Minos Galanakisa461ece2022-11-09 12:36:02 +000078 return pow(self.r, 2)
Janos Follath1be322a2022-11-02 14:46:23 +000079
Minos Galanakise9c86a12022-11-09 11:46:47 +000080class BignumModRawOperationArchSplit(BignumModRawOperation):
81 #pylint: disable=abstract-method
Minos Galanakisa461ece2022-11-09 12:36:02 +000082 """Common features for bignum mod raw operations where the result depends on
Minos Galanakise9c86a12022-11-09 11:46:47 +000083 the limb size."""
84
Minos Galanakisa461ece2022-11-09 12:36:02 +000085 limb_sizes = [32, 64] # type: List[int]
Minos Galanakise9c86a12022-11-09 11:46:47 +000086
Minos Galanakisa461ece2022-11-09 12:36:02 +000087 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 Galanakise9c86a12022-11-09 11:46:47 +000094
95 @classmethod
96 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
97 for a_value, b_value in cls.get_value_pairs():
Minos Galanakisa461ece2022-11-09 12:36:02 +000098 for bil in cls.limb_sizes:
99 yield cls(a_value, b_value, bits_in_limb=bil).create_test_case()
Minos Galanakis855c2282022-11-10 11:33:25 +0000100# 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 Follath1be322a2022-11-02 14:46:23 +0000126# 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