blob: 9f98131bd646d8e4197107fbb639a222052e4ef0 [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
34# END MERGE SLOT 2
35
36# BEGIN MERGE SLOT 3
37
Tom Cosgrove62b20482022-12-01 14:27:37 +000038class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget):
39 """Test cases for bignum mpi_mod_sub()."""
40 symbol = "-"
41 test_function = "mpi_mod_sub"
42 test_name = "mbedtls_mpi_mod_sub"
43 input_style = "fixed"
44 arity = 2
45
Tom Cosgrove62b20482022-12-01 14:27:37 +000046 def result(self) -> List[str]:
47 result = (self.int_a - self.int_b) % self.int_n
Tom Cosgrovef51f9722022-12-05 15:47:40 +000048 # To make negative tests easier, append 0 for success to the
49 # generated cases
50 return [self.format_result(result), "0"]
Tom Cosgrove62b20482022-12-01 14:27:37 +000051
Tom Cosgrovedc197592022-12-15 16:59:40 +000052class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
53 """Test cases for bignum mpi_mod_inv() - not in Montgomery form."""
54 moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
55 symbol = "^ -1"
56 test_function = "mpi_mod_inv_non_mont"
57 test_name = "mbedtls_mpi_mod_inv non-Mont. form"
58 input_style = "fixed"
59 arity = 1
60 suffix = True
61
62 @property
63 def is_valid(self) -> bool:
64 return self.int_a > 0 and self.int_a < self.int_n
65
66 def result(self) -> List[str]:
67 result = bignum_common.invmod(self.int_a, self.int_n)
68 if result < 0:
69 result += self.int_n
70 # To make negative tests easier, append 0 for success to the
71 # generated cases
72 return [self.format_result(result), "0"]
73
74class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget):
75 """Test cases for bignum mpi_mod_inv() - Montgomery form."""
76 moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
77 symbol = "^ -1"
78 test_function = "mpi_mod_inv_mont"
79 test_name = "mbedtls_mpi_mod_inv Mont. form"
80 input_style = "arch_split" # Mont. form requires arch_split
81 arity = 1
82 suffix = True
83
84 @property
85 def is_valid(self) -> bool:
86 return self.int_a > 0 and self.int_a < self.int_n
87
88 @property
89 def arg_a(self) -> str:
90 mont_a = self.to_montgomery(self.int_a)
91 return self.format_arg('{:x}'.format(mont_a))
92
93 def result(self) -> List[str]:
94 result = bignum_common.invmod(self.int_a, self.int_n)
95 if result < 0:
96 result += self.int_n
97 mont_result = self.to_montgomery(result)
98 # To make negative tests easier, append 0 for success to the
99 # generated cases
100 return [self.format_result(mont_result), "0"]
101
Janos Follath1be322a2022-11-02 14:46:23 +0000102# END MERGE SLOT 3
103
104# BEGIN MERGE SLOT 4
105
106# END MERGE SLOT 4
107
108# BEGIN MERGE SLOT 5
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000109class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget):
110 """Test cases for bignum mpi_mod_add()."""
111 count = 0
112 symbol = "+"
113 test_function = "mpi_mod_add"
114 test_name = "mbedtls_mpi_mod_add"
115 input_style = "fixed"
116
117 def result(self) -> List[str]:
118 result = (self.int_a + self.int_b) % self.int_n
119 # To make negative tests easier, append "0" for success to the
120 # generated cases
121 return [self.format_result(result), "0"]
122
Janos Follath1be322a2022-11-02 14:46:23 +0000123
124# END MERGE SLOT 5
125
126# BEGIN MERGE SLOT 6
127
128# END MERGE SLOT 6
129
130# BEGIN MERGE SLOT 7
131
132# END MERGE SLOT 7
133
134# BEGIN MERGE SLOT 8
135
136# END MERGE SLOT 8
137
138# BEGIN MERGE SLOT 9
139
140# END MERGE SLOT 9
141
142# BEGIN MERGE SLOT 10
143
144# END MERGE SLOT 10