blob: 6fc4c919bc07941b3201f71b42dd2529d071bcbd [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
Tom Cosgrove61292682022-12-08 09:44:10 +000021from .bignum_data import ONLY_PRIME_MODULI
Janos Follathdf8239b2022-11-02 14:40:58 +000022
Janos Follath0cd89672022-11-09 12:14:14 +000023class BignumModRawTarget(test_data_generation.BaseTarget):
24 #pylint: disable=abstract-method, too-few-public-methods
Janos Follathdf8239b2022-11-02 14:40:58 +000025 """Target for bignum mod_raw test case generation."""
26 target_basename = 'test_suite_bignum_mod_raw.generated'
27
Minos Galanakis855c2282022-11-10 11:33:25 +000028# BEGIN MERGE SLOT 1
29
30# END MERGE SLOT 1
31
32# BEGIN MERGE SLOT 2
33
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010034class BignumModRawSub(bignum_common.ModOperationCommon,
35 BignumModRawTarget):
36 """Test cases for bignum mpi_mod_raw_sub()."""
Gabor Mezeic426d9b2022-11-15 18:51:20 +010037 symbol = "-"
38 test_function = "mpi_mod_raw_sub"
39 test_name = "mbedtls_mpi_mod_raw_sub"
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010040 input_style = "fixed"
41 arity = 2
Gabor Mezeic426d9b2022-11-15 18:51:20 +010042
43 def arguments(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010044 return [bignum_common.quote_str(n) for n in [self.arg_a,
45 self.arg_b,
46 self.arg_n]
47 ] + self.result()
Gabor Mezeic426d9b2022-11-15 18:51:20 +010048
49 def result(self) -> List[str]:
Gabor Mezei6b3c0c52022-11-23 16:45:05 +010050 result = (self.int_a - self.int_b) % self.int_n
51 return [self.format_result(result)]
Gabor Mezeic426d9b2022-11-15 18:51:20 +010052
Minos Galanakis855c2282022-11-10 11:33:25 +000053# END MERGE SLOT 2
54
55# BEGIN MERGE SLOT 3
56
Tom Cosgrove61292682022-12-08 09:44:10 +000057class BignumModRawInvPrime(bignum_common.ModOperationCommon,
58 BignumModRawTarget):
59 """Test cases for bignum mpi_mod_raw_inv_prime()."""
60 moduli = ONLY_PRIME_MODULI
61 symbol = "^ -1"
62 test_function = "mpi_mod_raw_inv_prime"
63 test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
64 input_style = "fixed"
65 arity = 1
66 suffix = True
67
68 @property
69 def is_valid(self) -> bool:
70 return self.int_a > 0 and self.int_a < self.int_n
71
Tom Cosgrove9d8afd12022-12-09 10:58:46 +000072 @property
73 def arg_a(self) -> str:
Tom Cosgrove61292682022-12-08 09:44:10 +000074 # Input has to be given in Montgomery form
75 mont_a = self.to_montgomery(self.int_a)
Tom Cosgrove9d8afd12022-12-09 10:58:46 +000076 return self.format_arg('{:x}'.format(mont_a))
Tom Cosgrove61292682022-12-08 09:44:10 +000077
78 def result(self) -> List[str]:
79 result = bignum_common.invmod(self.int_a, self.int_n)
80 if result < 0:
81 result += self.int_n
82 mont_result = self.to_montgomery(result)
83 return [self.format_result(mont_result)]
84
Minos Galanakis855c2282022-11-10 11:33:25 +000085# END MERGE SLOT 3
86
87# BEGIN MERGE SLOT 4
88
89# END MERGE SLOT 4
90
91# BEGIN MERGE SLOT 5
92
Tom Cosgrove19230092022-11-24 15:56:53 +000093class BignumModRawAdd(bignum_common.ModOperationCommon,
94 BignumModRawTarget):
95 """Test cases for bignum mpi_mod_raw_add()."""
96 symbol = "+"
97 test_function = "mpi_mod_raw_add"
98 test_name = "mbedtls_mpi_mod_raw_add"
99 input_style = "fixed"
100 arity = 2
101
Tom Cosgrove19230092022-11-24 15:56:53 +0000102 def result(self) -> List[str]:
103 result = (self.int_a + self.int_b) % self.int_n
104 return [self.format_result(result)]
105
Minos Galanakis855c2282022-11-10 11:33:25 +0000106# END MERGE SLOT 5
107
108# BEGIN MERGE SLOT 6
109
110# END MERGE SLOT 6
111
112# BEGIN MERGE SLOT 7
Janos Follathf352c672022-11-20 13:40:25 +0000113
Janos Follath155ad8c2022-11-17 14:42:40 +0000114class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
Janos Follath948afce2022-11-17 13:38:56 +0000115 BignumModRawTarget):
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000116 """ Test cases for mpi_mod_raw_to_mont_rep(). """
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000117 test_function = "mpi_mod_raw_to_mont_rep"
118 test_name = "Convert into Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000119 symbol = "R *"
Janos Follath6fa3f062022-11-17 20:33:51 +0000120 input_style = "arch_split"
Janos Follath1921fd52022-11-18 17:51:02 +0000121 arity = 1
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000122
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000123 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000124 result = self.to_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000125 return [self.format_result(result)]
Minos Galanakisa252f6b2022-11-09 19:23:53 +0000126
Janos Follathf352c672022-11-20 13:40:25 +0000127class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
128 BignumModRawTarget):
Minos Galanakis50de0732022-11-09 19:36:16 +0000129 """ Test cases for mpi_mod_raw_from_mont_rep(). """
Minos Galanakis50de0732022-11-09 19:36:16 +0000130 test_function = "mpi_mod_raw_from_mont_rep"
131 test_name = "Convert from Mont: "
Janos Follath8ae7a652022-11-19 15:05:19 +0000132 symbol = "1/R *"
Janos Follathf352c672022-11-20 13:40:25 +0000133 input_style = "arch_split"
134 arity = 1
Minos Galanakis50de0732022-11-09 19:36:16 +0000135
Janos Follath1921fd52022-11-18 17:51:02 +0000136 def result(self) -> List[str]:
Tom Cosgrovec2406002022-12-06 12:20:43 +0000137 result = self.from_montgomery(self.int_a)
Janos Follath1921fd52022-11-18 17:51:02 +0000138 return [self.format_result(result)]
139
Minos Galanakis78665eb2022-12-07 18:10:46 +0000140class BignumModRawModNegate(bignum_common.ModOperationCommon,
141 BignumModRawTarget):
142 """ Test cases for mpi_mod_raw_neg(). """
143 test_function = "mpi_mod_raw_neg"
144 test_name = "Modular negation: "
Minos Galanakisf3abea62022-12-08 11:48:26 +0000145 symbol = "-"
Minos Galanakis78665eb2022-12-07 18:10:46 +0000146 input_style = "arch_split"
147 arity = 1
Janos Follath1921fd52022-11-18 17:51:02 +0000148
Minos Galanakis78665eb2022-12-07 18:10:46 +0000149 def result(self) -> List[str]:
150 result = (self.int_n - self.int_a) % self.int_n
151 return [self.format_result(result)]
Janos Follath1be322a2022-11-02 14:46:23 +0000152# END MERGE SLOT 7
153
154# BEGIN MERGE SLOT 8
155
156# END MERGE SLOT 8
157
158# BEGIN MERGE SLOT 9
159
160# END MERGE SLOT 9
161
162# BEGIN MERGE SLOT 10
163
164# END MERGE SLOT 10