blob: a2336f95a658a0168f0a75f74fe27f2be2e2cf64 [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
Gilles Peskinead335b52022-12-20 22:39:15 +010017from typing import Iterator, List, Optional, Union
Janos Follathdf8239b2022-11-02 14:40:58 +000018
Gilles Peskine23636ac2022-12-20 19:30:47 +010019from . import test_case
Janos Follathdf8239b2022-11-02 14:40:58 +000020from . import test_data_generation
Minos Galanakise9c86a12022-11-09 11:46:47 +000021from . import bignum_common
Tom Cosgrove61292682022-12-08 09:44:10 +000022from .bignum_data import ONLY_PRIME_MODULI
Janos Follathdf8239b2022-11-02 14:40:58 +000023
Janos Follath0cd89672022-11-09 12:14:14 +000024class BignumModRawTarget(test_data_generation.BaseTarget):
25 #pylint: disable=abstract-method, too-few-public-methods
Janos Follathdf8239b2022-11-02 14:40:58 +000026 """Target for bignum mod_raw test case generation."""
27 target_basename = 'test_suite_bignum_mod_raw.generated'
28
Minos Galanakis855c2282022-11-10 11:33:25 +000029# BEGIN MERGE SLOT 1
30
31# END MERGE SLOT 1
32
33# BEGIN MERGE SLOT 2
34
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010035class BignumModRawSub(bignum_common.ModOperationCommon,
36 BignumModRawTarget):
37 """Test cases for bignum mpi_mod_raw_sub()."""
Gabor Mezeic426d9b2022-11-15 18:51:20 +010038 symbol = "-"
39 test_function = "mpi_mod_raw_sub"
40 test_name = "mbedtls_mpi_mod_raw_sub"
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010041 input_style = "fixed"
42 arity = 2
Gabor Mezeic426d9b2022-11-15 18:51:20 +010043
44 def arguments(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010045 return [bignum_common.quote_str(n) for n in [self.arg_a,
46 self.arg_b,
47 self.arg_n]
48 ] + self.result()
Gabor Mezeic426d9b2022-11-15 18:51:20 +010049
50 def result(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010051 result = (self.int_a - self.int_b) % self.int_n
52 return [self.format_result(result)]
Gabor Mezeic426d9b2022-11-15 18:51:20 +010053
Minos Galanakis855c2282022-11-10 11:33:25 +000054# END MERGE SLOT 2
55
56# BEGIN MERGE SLOT 3
57
Tom Cosgrove61292682022-12-08 09:44:10 +000058class BignumModRawInvPrime(bignum_common.ModOperationCommon,
59 BignumModRawTarget):
60 """Test cases for bignum mpi_mod_raw_inv_prime()."""
61 moduli = ONLY_PRIME_MODULI
62 symbol = "^ -1"
63 test_function = "mpi_mod_raw_inv_prime"
64 test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
65 input_style = "fixed"
66 arity = 1
67 suffix = True
68
69 @property
70 def is_valid(self) -> bool:
71 return self.int_a > 0 and self.int_a < self.int_n
72
Tom Cosgrove9d8afd12022-12-09 10:58:46 +000073 @property
74 def arg_a(self) -> str:
Tom Cosgrove61292682022-12-08 09:44:10 +000075 # Input has to be given in Montgomery form
76 mont_a = self.to_montgomery(self.int_a)
Tom Cosgrove9d8afd12022-12-09 10:58:46 +000077 return self.format_arg('{:x}'.format(mont_a))
Tom Cosgrove61292682022-12-08 09:44:10 +000078
79 def result(self) -> List[str]:
80 result = bignum_common.invmod(self.int_a, self.int_n)
81 if result < 0:
82 result += self.int_n
83 mont_result = self.to_montgomery(result)
84 return [self.format_result(mont_result)]
85
Minos Galanakis855c2282022-11-10 11:33:25 +000086# END MERGE SLOT 3
87
88# BEGIN MERGE SLOT 4
89
90# END MERGE SLOT 4
91
92# BEGIN MERGE SLOT 5
93
Tom Cosgrove19230092022-11-24 15:56:53 +000094class BignumModRawAdd(bignum_common.ModOperationCommon,
95 BignumModRawTarget):
96 """Test cases for bignum mpi_mod_raw_add()."""
97 symbol = "+"
98 test_function = "mpi_mod_raw_add"
99 test_name = "mbedtls_mpi_mod_raw_add"
100 input_style = "fixed"
101 arity = 2
102
Tom Cosgrove19230092022-11-24 15:56:53 +0000103 def result(self) -> List[str]:
104 result = (self.int_a + self.int_b) % self.int_n
105 return [self.format_result(result)]
106
Minos Galanakis855c2282022-11-10 11:33:25 +0000107# END MERGE SLOT 5
108
109# BEGIN MERGE SLOT 6
110
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100111class BignumModRawConvertRep(bignum_common.ModOperationCommon,
112 BignumModRawTarget):
113 # This is an abstract class, it's ok to have unimplemented methods.
114 #pylint: disable=abstract-method
115 """Test cases for representation conversion."""
Gilles Peskine23636ac2022-12-20 19:30:47 +0100116 arity = 1
117
Gilles Peskinead335b52022-12-20 22:39:15 +0100118 def __init__(self, val_n: str, val_a: str, bits_in_limb: Optional[int],
Gilles Peskine23636ac2022-12-20 19:30:47 +0100119 rep: bignum_common.ModulusRepresentation) -> None:
Gilles Peskinead335b52022-12-20 22:39:15 +0100120 if bits_in_limb is None:
121 super().__init__(val_n=val_n, val_a=val_a)
122 else:
123 self.input_style = "arch_split"
124 super().__init__(val_n=val_n, val_a=val_a, bits_in_limb=bits_in_limb)
Gilles Peskine23636ac2022-12-20 19:30:47 +0100125 self.rep = rep
126
Gilles Peskine23636ac2022-12-20 19:30:47 +0100127 def arguments(self) -> List[str]:
128 return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(),
129 bignum_common.quote_str(self.arg_a)] +
130 self.result())
131
Gilles Peskinead335b52022-12-20 22:39:15 +0100132 def description(self) -> str:
133 base = super().description()
134 mod_with_rep = 'mod({})'.format(self.rep.name)
135 return base.replace('mod', mod_with_rep, 1)
136
Gilles Peskine23636ac2022-12-20 19:30:47 +0100137 @classmethod
138 def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
139 representations = \
140 bignum_common.ModulusRepresentation.supported_representations()
141 for rep in representations:
Gilles Peskinead335b52022-12-20 22:39:15 +0100142 if rep is bignum_common.ModulusRepresentation.MONTGOMERY:
143 limb_sizes = cls.limb_sizes #type: Union[List[int], List[None]]
144 else:
145 limb_sizes = [None] # no dependency on limb size
Gilles Peskine23636ac2022-12-20 19:30:47 +0100146 for n in cls.moduli:
147 for a in cls.input_values:
Gilles Peskinead335b52022-12-20 22:39:15 +0100148 for bil in limb_sizes:
149 test_object = cls(n, a, bil, rep)
150 if test_object.is_valid:
151 yield test_object.create_test_case()
Gilles Peskine23636ac2022-12-20 19:30:47 +0100152
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100153class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep):
154 """Test cases for mpi_mod_raw_canonical_to_modulus_rep."""
155 test_function = "mpi_mod_raw_canonical_to_modulus_rep"
156 test_name = "Rep canon->mod"
157
158 def result(self) -> List[str]:
Minos Galanakis56894102022-12-21 17:31:56 +0000159 return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))]
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100160
161class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep):
162 """Test cases for mpi_mod_raw_modulus_to_canonical_rep."""
163 test_function = "mpi_mod_raw_modulus_to_canonical_rep"
164 test_name = "Rep mod->canon"
165
166 @property
167 def arg_a(self) -> str:
Minos Galanakis56894102022-12-21 17:31:56 +0000168 return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep)))
Gilles Peskinebe69c7d2022-12-20 19:51:22 +0100169
170 def result(self) -> List[str]:
171 return [self.format_result(self.int_a)]
172
Minos Galanakis855c2282022-11-10 11:33:25 +0000173# END MERGE SLOT 6
174
175# BEGIN MERGE SLOT 7
Janos Follathf352c672022-11-20 13:40:25 +0000176
Janos Follath155ad8c2022-11-17 14:42:40 +0000177class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
Janos Follath948afce2022-11-17 13:38:56 +0000178 BignumModRawTarget):
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000179 """ Test cases for mpi_mod_raw_to_mont_rep(). """
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000180 test_function = "mpi_mod_raw_to_mont_rep"
181 test_name = "Convert into Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000182 symbol = "R *"
Janos Follath6fa3f062022-11-17 20:33:51 +0000183 input_style = "arch_split"
Janos Follath1921fd52022-11-18 17:51:02 +0000184 arity = 1
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000185
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000186 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000187 result = self.to_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000188 return [self.format_result(result)]
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000189
Janos Follathf352c672022-11-20 13:40:25 +0000190class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
191 BignumModRawTarget):
Minos Galanakis50de0732022-11-09 19:36:16 +0000192 """ Test cases for mpi_mod_raw_from_mont_rep(). """
Minos Galanakis50de0732022-11-09 19:36:16 +0000193 test_function = "mpi_mod_raw_from_mont_rep"
194 test_name = "Convert from Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000195 symbol = "1/R *"
Janos Follathf352c672022-11-20 13:40:25 +0000196 input_style = "arch_split"
197 arity = 1
Minos Galanakis50de0732022-11-09 19:36:16 +0000198
Janos Follath1921fd52022-11-18 17:51:02 +0000199 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000200 result = self.from_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000201 return [self.format_result(result)]
202
Minos Galanakis78665eb2022-12-07 18:10:46 +0000203class BignumModRawModNegate(bignum_common.ModOperationCommon,
204 BignumModRawTarget):
205 """ Test cases for mpi_mod_raw_neg(). """
206 test_function = "mpi_mod_raw_neg"
207 test_name = "Modular negation: "
Minos Galanakisf3abea62022-12-08 11:48:26 +0000208 symbol = "-"
Minos Galanakis78665eb2022-12-07 18:10:46 +0000209 input_style = "arch_split"
210 arity = 1
Janos Follath1921fd52022-11-18 17:51:02 +0000211
Minos Galanakis78665eb2022-12-07 18:10:46 +0000212 def result(self) -> List[str]:
213 result = (self.int_n - self.int_a) % self.int_n
214 return [self.format_result(result)]
Janos Follath1be322a2022-11-02 14:46:23 +0000215# END MERGE SLOT 7
216
217# BEGIN MERGE SLOT 8
218
219# END MERGE SLOT 8
220
221# BEGIN MERGE SLOT 9
222
223# END MERGE SLOT 9
224
225# BEGIN MERGE SLOT 10
226
227# END MERGE SLOT 10