blob: 5d4bda2a7d7882776ea0249c07dd72e9935c07ea [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 Follath284672c2022-11-19 14:55:43 +000017from typing import Dict, List
Janos Follathdf8239b2022-11-02 14:40:58 +000018
19from . import test_data_generation
Minos Galanakise9c86a12022-11-09 11:46:47 +000020from . import bignum_common
Janos Follathdf8239b2022-11-02 14:40:58 +000021
Janos Follath0cd89672022-11-09 12:14:14 +000022class BignumModRawTarget(test_data_generation.BaseTarget):
23 #pylint: disable=abstract-method, too-few-public-methods
Janos Follathdf8239b2022-11-02 14:40:58 +000024 """Target for bignum mod_raw test case generation."""
25 target_basename = 'test_suite_bignum_mod_raw.generated'
26
Minos Galanakis855c2282022-11-10 11:33:25 +000027# BEGIN MERGE SLOT 1
28
29# END MERGE SLOT 1
30
31# BEGIN MERGE SLOT 2
32
Gabor Mezeic426d9b2022-11-15 18:51:20 +010033class BignumModRawSub(BignumModRawOperation):
34 """Test cases for bignum mod raw sub."""
35 count = 0
36 symbol = "-"
37 test_function = "mpi_mod_raw_sub"
38 test_name = "mbedtls_mpi_mod_raw_sub"
39 unique_combinations_only = False
40
41 input_values = [
42 "0", "1", "fe", "ff", "fffe", "ffff",
43 "fffffffffffffffe", "ffffffffffffffff",
44 "fffffffffffffffffffffffffffffffe",
45 "ffffffffffffffffffffffffffffffff",
46 "1234567890abcdef01234567890abcdef0",
47 "3653f8dd9b1f282e4067c3584ee207f8da94e3e8ab73738f",
48 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
49 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
50 "1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0",
51 (
52 "14c15c910b11ad28cc21ce88d0060cc54278c2614e1bcb383bb4a570294c4ea3"
53 "738d243a6e58d5ca49c7b59b995253fd6c79a3de69f85e3131f3b9238224b122"
54 "c3e4a892d9196ada4fcfa583e1df8af9b474c7e89286a1754abcb06ae8abb93f"
55 "01d89a024cdce7a6d7288ff68c320f89f1347e0cdd905ecfd160c5d0ef412ed6"
56 )
57 ]
58
59 modulus_values = [
60 "7", "ff",
61 "d1c127a667786703830500038ebaef20e5a3e2dc378fb75b"
62 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff43",
63 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff67",
64 (
65 "c93ba7ec74d96f411ba008bdb78e63ff11bb5df46a51e16b2c9d156f8e4e18ab"
66 "f5e052cb01f47d0d1925a77f60991577e128fb6f52f34a27950a594baadd3d80"
67 "57abeb222cf3cca962db16abf79f2ada5bd29ab2f51244bf295eff9f6aaba130"
68 "2efc449b128be75eeaca04bc3c1a155d11d14e8be32a2c8287b3996cf6ad5223"
69 ),
70 (
71 "5c083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4"
72 "fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac0"
73 "83126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf"
74 "3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac05d2"
75 )
76 ]
77
78 descr_tpl = '{} #{} \"{}\" - \"{}\" % \"{}\".'
79
80 BITS_IN_LIMB = 32
81
82 @property
83 def boundary(self) -> int:
84 return self.int_n
85
86 @property
87 def x(self): # pylint: disable=invalid-name
88 return (self.int_a - self.int_b) % self.int_n if self.int_n > 0 else 0
89
90 @property
91 def hex_x(self) -> str:
92 return format(self.x, 'x').zfill(self.hex_digits)
93
94 def description(self) -> str:
95 return self.descr_tpl.format(self.test_name,
96 self.count,
97 self.int_a,
98 self.int_b,
99 self.int_n)
100
101 def arguments(self) -> List[str]:
102 return [bignum_common.quote_str(n) for n in [self.hex_a,
103 self.hex_b,
104 self.hex_n,
105 self.hex_x]]
106
107 def result(self) -> List[str]:
108 return [self.hex_x]
109
110 @classmethod
111 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
112 for a_value, b_value in cls.get_value_pairs():
113 int_a = bignum_common.hex_to_int(a_value)
114 int_b = bignum_common.hex_to_int(b_value)
115 highest = max(int_a, int_b)
116
117 # Choose a modulus bigger then the arguments
118 for n_value in cls.modulus_values:
119 int_n = bignum_common.hex_to_int(n_value)
120 if highest < int_n:
121 yield cls(n_value, a_value, b_value, cls.BITS_IN_LIMB).create_test_case()
122
Minos Galanakis855c2282022-11-10 11:33:25 +0000123# END MERGE SLOT 2
124
125# BEGIN MERGE SLOT 3
126
127# END MERGE SLOT 3
128
129# BEGIN MERGE SLOT 4
130
131# END MERGE SLOT 4
132
133# BEGIN MERGE SLOT 5
134
135# END MERGE SLOT 5
136
137# BEGIN MERGE SLOT 6
138
139# END MERGE SLOT 6
140
141# BEGIN MERGE SLOT 7
Janos Follathf352c672022-11-20 13:40:25 +0000142
Janos Follath155ad8c2022-11-17 14:42:40 +0000143class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
Janos Follath948afce2022-11-17 13:38:56 +0000144 BignumModRawTarget):
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000145 """ Test cases for mpi_mod_raw_to_mont_rep(). """
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000146 test_function = "mpi_mod_raw_to_mont_rep"
147 test_name = "Convert into Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000148 symbol = "R *"
Janos Follath6fa3f062022-11-17 20:33:51 +0000149 input_style = "arch_split"
Janos Follath1921fd52022-11-18 17:51:02 +0000150 arity = 1
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000151
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000152 def result(self) -> List[str]:
Janos Follath1921fd52022-11-18 17:51:02 +0000153 result = (self.int_a * self.r) % self.int_n
154 return [self.format_result(result)]
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000155
Minos Galanakis50de0732022-11-09 19:36:16 +0000156
Janos Follathf352c672022-11-20 13:40:25 +0000157class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
158 BignumModRawTarget):
Minos Galanakis50de0732022-11-09 19:36:16 +0000159 """ Test cases for mpi_mod_raw_from_mont_rep(). """
Minos Galanakis50de0732022-11-09 19:36:16 +0000160 test_function = "mpi_mod_raw_from_mont_rep"
161 test_name = "Convert from Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000162 symbol = "1/R *"
Janos Follathf352c672022-11-20 13:40:25 +0000163 input_style = "arch_split"
164 arity = 1
Minos Galanakis50de0732022-11-09 19:36:16 +0000165
Janos Follath1921fd52022-11-18 17:51:02 +0000166 def result(self) -> List[str]:
167 result = (self.int_a * self.r_inv) % self.int_n
168 return [self.format_result(result)]
169
170
Janos Follath1be322a2022-11-02 14:46:23 +0000171# END MERGE SLOT 7
172
173# BEGIN MERGE SLOT 8
174
175# END MERGE SLOT 8
176
177# BEGIN MERGE SLOT 9
178
179# END MERGE SLOT 9
180
181# BEGIN MERGE SLOT 10
182
183# END MERGE SLOT 10