blob: 19942ed1609747a7696e53e836e7c21466e651cb [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
Minos Galanakisa461ece2022-11-09 12:36:02 +000056 """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 Follath1be322a2022-11-02 14:46:23 +0000104
Minos Galanakise9c86a12022-11-09 11:46:47 +0000105class BignumModRawOperationArchSplit(BignumModRawOperation):
106 #pylint: disable=abstract-method
Minos Galanakisa461ece2022-11-09 12:36:02 +0000107 """Common features for bignum mod raw operations where the result depends on
Minos Galanakise9c86a12022-11-09 11:46:47 +0000108 the limb size."""
109
Minos Galanakisa461ece2022-11-09 12:36:02 +0000110 limb_sizes = [32, 64] # type: List[int]
Minos Galanakise9c86a12022-11-09 11:46:47 +0000111
Minos Galanakisa461ece2022-11-09 12:36:02 +0000112 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 Galanakise9c86a12022-11-09 11:46:47 +0000119
120 @classmethod
121 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
122 for a_value, b_value in cls.get_value_pairs():
Minos Galanakisa461ece2022-11-09 12:36:02 +0000123 for bil in cls.limb_sizes:
124 yield cls(a_value, b_value, bits_in_limb=bil).create_test_case()
Janos Follath1be322a2022-11-02 14:46:23 +0000125# 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