1"""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
17from typing import Iterator, List
18
19from . import test_case
20from . import test_data_generation
21from . import bignum_common
22from .bignum_data import ONLY_PRIME_MODULI
23
24class BignumModRawTarget(test_data_generation.BaseTarget):
25    #pylint: disable=abstract-method, too-few-public-methods
26    """Target for bignum mod_raw test case generation."""
27    target_basename = 'test_suite_bignum_mod_raw.generated'
28
29# BEGIN MERGE SLOT 1
30
31# END MERGE SLOT 1
32
33# BEGIN MERGE SLOT 2
34
35class BignumModRawSub(bignum_common.ModOperationCommon,
36                      BignumModRawTarget):
37    """Test cases for bignum mpi_mod_raw_sub()."""
38    symbol = "-"
39    test_function = "mpi_mod_raw_sub"
40    test_name = "mbedtls_mpi_mod_raw_sub"
41    input_style = "fixed"
42    arity = 2
43
44    def arguments(self) -> List[str]:
45        return [bignum_common.quote_str(n) for n in [self.arg_a,
46                                                     self.arg_b,
47                                                     self.arg_n]
48               ] + self.result()
49
50    def result(self) -> List[str]:
51        result = (self.int_a - self.int_b) % self.int_n
52        return [self.format_result(result)]
53
54class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon,
55                                    BignumModRawTarget):
56    """Test cases for ecp quasi_reduction()."""
57    symbol = "-"
58    test_function = "mpi_mod_raw_fix_quasi_reduction"
59    test_name = "fix_quasi_reduction"
60    input_style = "fixed"
61    arity = 1
62
63    # Extend the default values with n < x < 2n
64    input_values = bignum_common.ModOperationCommon.input_values + [
65        "73",
66
67        # First number generated by random.getrandbits(1024) - seed(3,2)
68        "ea7b5bf55eb561a4216363698b529b4a97b750923ceb3ffd",
69
70        # First number generated by random.getrandbits(1024) - seed(1,2)
71        ("cd447e35b8b6d8fe442e3d437204e52db2221a58008a05a6c4647159c324c985"
72         "9b810e766ec9d28663ca828dd5f4b3b2e4b06ce60741c7a87ce42c8218072e8c"
73         "35bf992dc9e9c616612e7696a6cecc1b78e510617311d8a3c2ce6f447ed4d57b"
74         "1e2feb89414c343c1027c4d1c386bbc4cd613e30d8f16adf91b7584a2265b1f5")
75    ] # type: List[str]
76
77    def result(self) -> List[str]:
78        result = self.int_a % self.int_n
79        return [self.format_result(result)]
80
81    @property
82    def is_valid(self) -> bool:
83        return bool(self.int_a < 2 * self.int_n)
84
85class BignumModRawMul(bignum_common.ModOperationCommon,
86                      BignumModRawTarget):
87    """Test cases for bignum mpi_mod_raw_mul()."""
88    symbol = "*"
89    test_function = "mpi_mod_raw_mul"
90    test_name = "mbedtls_mpi_mod_raw_mul"
91    input_style = "arch_split"
92    arity = 2
93
94    def arguments(self) -> List[str]:
95        return [self.format_result(self.to_montgomery(self.int_a)),
96                self.format_result(self.to_montgomery(self.int_b)),
97                bignum_common.quote_str(self.arg_n)
98               ] + self.result()
99
100    def result(self) -> List[str]:
101        result = (self.int_a * self.int_b) % self.int_n
102        return [self.format_result(self.to_montgomery(result))]
103
104# END MERGE SLOT 2
105
106# BEGIN MERGE SLOT 3
107
108class BignumModRawInvPrime(bignum_common.ModOperationCommon,
109                           BignumModRawTarget):
110    """Test cases for bignum mpi_mod_raw_inv_prime()."""
111    moduli = ONLY_PRIME_MODULI
112    symbol = "^ -1"
113    test_function = "mpi_mod_raw_inv_prime"
114    test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
115    input_style = "arch_split"
116    arity = 1
117    suffix = True
118    montgomery_form_a = True
119    disallow_zero_a = True
120
121    def result(self) -> List[str]:
122        result = bignum_common.invmod_positive(self.int_a, self.int_n)
123        mont_result = self.to_montgomery(result)
124        return [self.format_result(mont_result)]
125
126# END MERGE SLOT 3
127
128# BEGIN MERGE SLOT 4
129
130# END MERGE SLOT 4
131
132# BEGIN MERGE SLOT 5
133
134class BignumModRawAdd(bignum_common.ModOperationCommon,
135                      BignumModRawTarget):
136    """Test cases for bignum mpi_mod_raw_add()."""
137    symbol = "+"
138    test_function = "mpi_mod_raw_add"
139    test_name = "mbedtls_mpi_mod_raw_add"
140    input_style = "fixed"
141    arity = 2
142
143    def result(self) -> List[str]:
144        result = (self.int_a + self.int_b) % self.int_n
145        return [self.format_result(result)]
146
147# END MERGE SLOT 5
148
149# BEGIN MERGE SLOT 6
150
151class BignumModRawConvertRep(bignum_common.ModOperationCommon,
152                             BignumModRawTarget):
153    # This is an abstract class, it's ok to have unimplemented methods.
154    #pylint: disable=abstract-method
155    """Test cases for representation conversion."""
156    symbol = ""
157    input_style = "arch_split"
158    arity = 1
159    rep = bignum_common.ModulusRepresentation.INVALID
160
161    def set_representation(self, r: bignum_common.ModulusRepresentation) -> None:
162        self.rep = r
163
164    def arguments(self) -> List[str]:
165        return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(),
166                 bignum_common.quote_str(self.arg_a)] +
167                self.result())
168
169    def description(self) -> str:
170        base = super().description()
171        mod_with_rep = 'mod({})'.format(self.rep.name)
172        return base.replace('mod', mod_with_rep, 1)
173
174    @classmethod
175    def test_cases_for_values(cls, rep: bignum_common.ModulusRepresentation,
176                              n: str, a: str) -> Iterator[test_case.TestCase]:
177        """Emit test cases for the given values (if any).
178
179        This may emit no test cases if a isn't valid for the modulus n,
180        or multiple test cases if rep requires different data depending
181        on the limb size.
182        """
183        for bil in cls.limb_sizes:
184            test_object = cls(n, a, bits_in_limb=bil)
185            test_object.set_representation(rep)
186            # The class is set to having separate test cases for each limb
187            # size, because the Montgomery representation requires it.
188            # But other representations don't require it. So for other
189            # representations, emit a single test case with no dependency
190            # on the limb size.
191            if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
192                test_object.dependencies = \
193                    [dep for dep in test_object.dependencies
194                     if not dep.startswith('MBEDTLS_HAVE_INT')]
195            if test_object.is_valid:
196                yield test_object.create_test_case()
197            if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
198                # A single test case (emitted, or skipped due to invalidity)
199                # is enough, since this test case doesn't depend on the
200                # limb size.
201                break
202
203    # The parent class doesn't support non-bignum parameters. So we override
204    # test generation, in order to have the representation as a parameter.
205    @classmethod
206    def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
207
208        for rep in bignum_common.ModulusRepresentation.supported_representations():
209            for n in cls.moduli:
210                for a in cls.input_values:
211                    yield from cls.test_cases_for_values(rep, n, a)
212
213class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep):
214    """Test cases for mpi_mod_raw_canonical_to_modulus_rep."""
215    test_function = "mpi_mod_raw_canonical_to_modulus_rep"
216    test_name = "Rep canon->mod"
217
218    def result(self) -> List[str]:
219        return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))]
220
221class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep):
222    """Test cases for mpi_mod_raw_modulus_to_canonical_rep."""
223    test_function = "mpi_mod_raw_modulus_to_canonical_rep"
224    test_name = "Rep mod->canon"
225
226    @property
227    def arg_a(self) -> str:
228        return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep)))
229
230    def result(self) -> List[str]:
231        return [self.format_result(self.int_a)]
232
233# END MERGE SLOT 6
234
235# BEGIN MERGE SLOT 7
236
237class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
238                                BignumModRawTarget):
239    """ Test cases for mpi_mod_raw_to_mont_rep(). """
240    test_function = "mpi_mod_raw_to_mont_rep"
241    test_name = "Convert into Mont: "
242    symbol = "R *"
243    input_style = "arch_split"
244    arity = 1
245
246    def result(self) -> List[str]:
247        result = self.to_montgomery(self.int_a)
248        return [self.format_result(result)]
249
250class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
251                                  BignumModRawTarget):
252    """ Test cases for mpi_mod_raw_from_mont_rep(). """
253    test_function = "mpi_mod_raw_from_mont_rep"
254    test_name = "Convert from Mont: "
255    symbol = "1/R *"
256    input_style = "arch_split"
257    arity = 1
258
259    def result(self) -> List[str]:
260        result = self.from_montgomery(self.int_a)
261        return [self.format_result(result)]
262
263class BignumModRawModNegate(bignum_common.ModOperationCommon,
264                            BignumModRawTarget):
265    """ Test cases for mpi_mod_raw_neg(). """
266    test_function = "mpi_mod_raw_neg"
267    test_name = "Modular negation: "
268    symbol = "-"
269    input_style = "arch_split"
270    arity = 1
271
272    def result(self) -> List[str]:
273        result = (self.int_n - self.int_a) % self.int_n
274        return [self.format_result(result)]
275# END MERGE SLOT 7
276
277# BEGIN MERGE SLOT 8
278
279# END MERGE SLOT 8
280
281# BEGIN MERGE SLOT 9
282
283# END MERGE SLOT 9
284
285# BEGIN MERGE SLOT 10
286
287# END MERGE SLOT 10
288