blob: 1960723f1d134143ce841b0e470f5e873e609ccb [file] [log] [blame]
Janos Follathb99b0562022-11-02 14:44:08 +00001"""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
Werner Lewis93a31c92022-12-06 10:14:57 +000017from typing import Dict, List
Werner Lewiscff75782022-11-30 16:34:07 +000018
Janos Follathb99b0562022-11-02 14:44:08 +000019from . import test_data_generation
Werner Lewis93a31c92022-12-06 10:14:57 +000020from . import bignum_common
Tom Cosgrovedc197592022-12-15 16:59:40 +000021from .bignum_data import ONLY_PRIME_MODULI
Janos Follathb99b0562022-11-02 14:44:08 +000022
Janos Follath0cd89672022-11-09 12:14:14 +000023class BignumModTarget(test_data_generation.BaseTarget):
24 #pylint: disable=abstract-method, too-few-public-methods
Janos Follathb99b0562022-11-02 14:44:08 +000025 """Target for bignum mod test case generation."""
26 target_basename = 'test_suite_bignum_mod.generated'
27
Janos Follath1be322a2022-11-02 14:46:23 +000028# BEGIN MERGE SLOT 1
29
30# END MERGE SLOT 1
31
32# BEGIN MERGE SLOT 2
33
Gabor Mezeieca74662022-12-13 10:53:50 +010034class BignumModMul(bignum_common.ModOperationCommon,
35 BignumModTarget):
36 """Test cases for bignum mpi_mod_mul()."""
37 symbol = "*"
38 test_function = "mpi_mod_mul"
39 test_name = "mbedtls_mpi_mod_mul"
40 input_style = "arch_split"
41 arity = 2
42
43 def arguments(self) -> List[str]:
Gabor Mezei77b877d2022-12-16 15:25:02 +010044 return [self.format_result(self.to_montgomery(self.int_a)),
45 self.format_result(self.to_montgomery(self.int_b)),
46 bignum_common.quote_str(self.arg_n)
Gabor Mezeieca74662022-12-13 10:53:50 +010047 ] + self.result()
48
49 def result(self) -> List[str]:
50 result = (self.int_a * self.int_b) % self.int_n
Gabor Mezei77b877d2022-12-16 15:25:02 +010051 return [self.format_result(self.to_montgomery(result))]
Gabor Mezeieca74662022-12-13 10:53:50 +010052
Janos Follath1be322a2022-11-02 14:46:23 +000053# END MERGE SLOT 2
54
55# BEGIN MERGE SLOT 3
56
Tom Cosgrove62b20482022-12-01 14:27:37 +000057class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget):
58 """Test cases for bignum mpi_mod_sub()."""
59 symbol = "-"
60 test_function = "mpi_mod_sub"
61 test_name = "mbedtls_mpi_mod_sub"
62 input_style = "fixed"
63 arity = 2
64
Tom Cosgrove62b20482022-12-01 14:27:37 +000065 def result(self) -> List[str]:
66 result = (self.int_a - self.int_b) % self.int_n
Tom Cosgrovef51f9722022-12-05 15:47:40 +000067 # To make negative tests easier, append 0 for success to the
68 # generated cases
69 return [self.format_result(result), "0"]
Tom Cosgrove62b20482022-12-01 14:27:37 +000070
Tom Cosgrovedc197592022-12-15 16:59:40 +000071class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
72 """Test cases for bignum mpi_mod_inv() - not in Montgomery form."""
73 moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
74 symbol = "^ -1"
75 test_function = "mpi_mod_inv_non_mont"
76 test_name = "mbedtls_mpi_mod_inv non-Mont. form"
77 input_style = "fixed"
78 arity = 1
79 suffix = True
Tom Cosgrove1133d232022-12-16 03:53:17 +000080 disallow_zero_a = True
Tom Cosgrovedc197592022-12-15 16:59:40 +000081
82 def result(self) -> List[str]:
Tom Cosgrove1133d232022-12-16 03:53:17 +000083 result = bignum_common.invmod_positive(self.int_a, self.int_n)
Tom Cosgrovedc197592022-12-15 16:59:40 +000084 # To make negative tests easier, append 0 for success to the
85 # generated cases
86 return [self.format_result(result), "0"]
87
88class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget):
89 """Test cases for bignum mpi_mod_inv() - Montgomery form."""
90 moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
91 symbol = "^ -1"
92 test_function = "mpi_mod_inv_mont"
93 test_name = "mbedtls_mpi_mod_inv Mont. form"
94 input_style = "arch_split" # Mont. form requires arch_split
95 arity = 1
96 suffix = True
Tom Cosgrove1133d232022-12-16 03:53:17 +000097 disallow_zero_a = True
Tom Cosgrovef7237542022-12-16 16:10:36 +000098 montgomery_form_a = True
Tom Cosgrovedc197592022-12-15 16:59:40 +000099
100 def result(self) -> List[str]:
Tom Cosgrove1133d232022-12-16 03:53:17 +0000101 result = bignum_common.invmod_positive(self.int_a, self.int_n)
Tom Cosgrovedc197592022-12-15 16:59:40 +0000102 mont_result = self.to_montgomery(result)
103 # To make negative tests easier, append 0 for success to the
104 # generated cases
105 return [self.format_result(mont_result), "0"]
106
Janos Follath1be322a2022-11-02 14:46:23 +0000107# END MERGE SLOT 3
108
109# BEGIN MERGE SLOT 4
110
111# END MERGE SLOT 4
112
113# BEGIN MERGE SLOT 5
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000114class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget):
115 """Test cases for bignum mpi_mod_add()."""
116 count = 0
117 symbol = "+"
118 test_function = "mpi_mod_add"
119 test_name = "mbedtls_mpi_mod_add"
120 input_style = "fixed"
121
122 def result(self) -> List[str]:
123 result = (self.int_a + self.int_b) % self.int_n
124 # To make negative tests easier, append "0" for success to the
125 # generated cases
126 return [self.format_result(result), "0"]
127
Janos Follath1be322a2022-11-02 14:46:23 +0000128
129# END MERGE SLOT 5
130
131# BEGIN MERGE SLOT 6
132
133# END MERGE SLOT 6
134
135# BEGIN MERGE SLOT 7
136
137# END MERGE SLOT 7
138
139# BEGIN MERGE SLOT 8
140
141# END MERGE SLOT 8
142
143# BEGIN MERGE SLOT 9
144
145# END MERGE SLOT 9
146
147# BEGIN MERGE SLOT 10
148
149# END MERGE SLOT 10