• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Framework classes for generation of bignum mod 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 Dict, List
18
19from . import test_data_generation
20from . import bignum_common
21from .bignum_data import ONLY_PRIME_MODULI
22
23class BignumModTarget(test_data_generation.BaseTarget):
24    #pylint: disable=abstract-method, too-few-public-methods
25    """Target for bignum mod test case generation."""
26    target_basename = 'test_suite_bignum_mod.generated'
27
28# BEGIN MERGE SLOT 1
29
30# END MERGE SLOT 1
31
32# BEGIN MERGE SLOT 2
33
34class BignumModMul(bignum_common.ModOperationCommon,
35                   BignumModTarget):
36    # pylint:disable=duplicate-code
37    """Test cases for bignum mpi_mod_mul()."""
38    symbol = "*"
39    test_function = "mpi_mod_mul"
40    test_name = "mbedtls_mpi_mod_mul"
41    input_style = "arch_split"
42    arity = 2
43
44    def arguments(self) -> List[str]:
45        return [self.format_result(self.to_montgomery(self.int_a)),
46                self.format_result(self.to_montgomery(self.int_b)),
47                bignum_common.quote_str(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(self.to_montgomery(result))]
53
54# END MERGE SLOT 2
55
56# BEGIN MERGE SLOT 3
57
58class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget):
59    """Test cases for bignum mpi_mod_sub()."""
60    symbol = "-"
61    test_function = "mpi_mod_sub"
62    test_name = "mbedtls_mpi_mod_sub"
63    input_style = "fixed"
64    arity = 2
65
66    def result(self) -> List[str]:
67        result = (self.int_a - self.int_b) % self.int_n
68        # To make negative tests easier, append 0 for success to the
69        # generated cases
70        return [self.format_result(result), "0"]
71
72class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
73    """Test cases for bignum mpi_mod_inv() - not in Montgomery form."""
74    moduli = ONLY_PRIME_MODULI  # for now only prime moduli supported
75    symbol = "^ -1"
76    test_function = "mpi_mod_inv_non_mont"
77    test_name = "mbedtls_mpi_mod_inv non-Mont. form"
78    input_style = "fixed"
79    arity = 1
80    suffix = True
81    disallow_zero_a = True
82
83    def result(self) -> List[str]:
84        result = bignum_common.invmod_positive(self.int_a, self.int_n)
85        # To make negative tests easier, append 0 for success to the
86        # generated cases
87        return [self.format_result(result), "0"]
88
89class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget):
90    """Test cases for bignum mpi_mod_inv() - Montgomery form."""
91    moduli = ONLY_PRIME_MODULI  # for now only prime moduli supported
92    symbol = "^ -1"
93    test_function = "mpi_mod_inv_mont"
94    test_name = "mbedtls_mpi_mod_inv Mont. form"
95    input_style = "arch_split"  # Mont. form requires arch_split
96    arity = 1
97    suffix = True
98    disallow_zero_a = True
99    montgomery_form_a = True
100
101    def result(self) -> List[str]:
102        result = bignum_common.invmod_positive(self.int_a, self.int_n)
103        mont_result = self.to_montgomery(result)
104        # To make negative tests easier, append 0 for success to the
105        # generated cases
106        return [self.format_result(mont_result), "0"]
107
108# END MERGE SLOT 3
109
110# BEGIN MERGE SLOT 4
111
112# END MERGE SLOT 4
113
114# BEGIN MERGE SLOT 5
115class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget):
116    """Test cases for bignum mpi_mod_add()."""
117    count = 0
118    symbol = "+"
119    test_function = "mpi_mod_add"
120    test_name = "mbedtls_mpi_mod_add"
121    input_style = "fixed"
122
123    def result(self) -> List[str]:
124        result = (self.int_a + self.int_b) % self.int_n
125        # To make negative tests easier, append "0" for success to the
126        # generated cases
127        return [self.format_result(result), "0"]
128
129
130# END MERGE SLOT 5
131
132# BEGIN MERGE SLOT 6
133
134# END MERGE SLOT 6
135
136# BEGIN MERGE SLOT 7
137
138# END MERGE SLOT 7
139
140# BEGIN MERGE SLOT 8
141
142# END MERGE SLOT 8
143
144# BEGIN MERGE SLOT 9
145
146# END MERGE SLOT 9
147
148# BEGIN MERGE SLOT 10
149
150# END MERGE SLOT 10
151