blob: f554001ec74feb19da5449f0a4a536695843d355 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00003# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Janos Follathb99b0562022-11-02 14:44:08 +00004#
Janos Follathb99b0562022-11-02 14:44:08 +00005
Werner Lewis93a31c92022-12-06 10:14:57 +00006from typing import Dict, List
Werner Lewiscff75782022-11-30 16:34:07 +00007
Janos Follathb99b0562022-11-02 14:44:08 +00008from . import test_data_generation
Werner Lewis93a31c92022-12-06 10:14:57 +00009from . import bignum_common
Tom Cosgrovedc197592022-12-15 16:59:40 +000010from .bignum_data import ONLY_PRIME_MODULI
Janos Follathb99b0562022-11-02 14:44:08 +000011
Janos Follath0cd89672022-11-09 12:14:14 +000012class BignumModTarget(test_data_generation.BaseTarget):
13 #pylint: disable=abstract-method, too-few-public-methods
Janos Follathb99b0562022-11-02 14:44:08 +000014 """Target for bignum mod test case generation."""
15 target_basename = 'test_suite_bignum_mod.generated'
16
Janos Follath1be322a2022-11-02 14:46:23 +000017
Gabor Mezeieca74662022-12-13 10:53:50 +010018class BignumModMul(bignum_common.ModOperationCommon,
19 BignumModTarget):
Gabor Mezei8a261642022-12-16 17:18:28 +010020 # pylint:disable=duplicate-code
Gabor Mezeieca74662022-12-13 10:53:50 +010021 """Test cases for bignum mpi_mod_mul()."""
22 symbol = "*"
23 test_function = "mpi_mod_mul"
24 test_name = "mbedtls_mpi_mod_mul"
25 input_style = "arch_split"
26 arity = 2
27
28 def arguments(self) -> List[str]:
Gabor Mezei77b877d2022-12-16 15:25:02 +010029 return [self.format_result(self.to_montgomery(self.int_a)),
30 self.format_result(self.to_montgomery(self.int_b)),
31 bignum_common.quote_str(self.arg_n)
Gabor Mezeieca74662022-12-13 10:53:50 +010032 ] + self.result()
33
34 def result(self) -> List[str]:
35 result = (self.int_a * self.int_b) % self.int_n
Gabor Mezei77b877d2022-12-16 15:25:02 +010036 return [self.format_result(self.to_montgomery(result))]
Gabor Mezeieca74662022-12-13 10:53:50 +010037
Janos Follath1be322a2022-11-02 14:46:23 +000038
Tom Cosgrove62b20482022-12-01 14:27:37 +000039class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget):
40 """Test cases for bignum mpi_mod_sub()."""
41 symbol = "-"
42 test_function = "mpi_mod_sub"
43 test_name = "mbedtls_mpi_mod_sub"
44 input_style = "fixed"
45 arity = 2
46
Tom Cosgrove62b20482022-12-01 14:27:37 +000047 def result(self) -> List[str]:
48 result = (self.int_a - self.int_b) % self.int_n
Tom Cosgrovef51f9722022-12-05 15:47:40 +000049 # To make negative tests easier, append 0 for success to the
50 # generated cases
51 return [self.format_result(result), "0"]
Tom Cosgrove62b20482022-12-01 14:27:37 +000052
Tom Cosgrovedc197592022-12-15 16:59:40 +000053class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
54 """Test cases for bignum mpi_mod_inv() - not in Montgomery form."""
55 moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
56 symbol = "^ -1"
57 test_function = "mpi_mod_inv_non_mont"
58 test_name = "mbedtls_mpi_mod_inv non-Mont. form"
59 input_style = "fixed"
60 arity = 1
61 suffix = True
Tom Cosgrove1133d232022-12-16 03:53:17 +000062 disallow_zero_a = True
Tom Cosgrovedc197592022-12-15 16:59:40 +000063
64 def result(self) -> List[str]:
Tom Cosgrove1133d232022-12-16 03:53:17 +000065 result = bignum_common.invmod_positive(self.int_a, self.int_n)
Tom Cosgrovedc197592022-12-15 16:59:40 +000066 # To make negative tests easier, append 0 for success to the
67 # generated cases
68 return [self.format_result(result), "0"]
69
70class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget):
71 """Test cases for bignum mpi_mod_inv() - Montgomery form."""
72 moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
73 symbol = "^ -1"
74 test_function = "mpi_mod_inv_mont"
75 test_name = "mbedtls_mpi_mod_inv Mont. form"
76 input_style = "arch_split" # Mont. form requires arch_split
77 arity = 1
78 suffix = True
Tom Cosgrove1133d232022-12-16 03:53:17 +000079 disallow_zero_a = True
Tom Cosgrovef7237542022-12-16 16:10:36 +000080 montgomery_form_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 mont_result = self.to_montgomery(result)
85 # To make negative tests easier, append 0 for success to the
86 # generated cases
87 return [self.format_result(mont_result), "0"]
88
Janos Follath1be322a2022-11-02 14:46:23 +000089
Werner Lewise1b6b7c2022-11-29 12:25:05 +000090class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget):
91 """Test cases for bignum mpi_mod_add()."""
92 count = 0
93 symbol = "+"
94 test_function = "mpi_mod_add"
95 test_name = "mbedtls_mpi_mod_add"
96 input_style = "fixed"
97
98 def result(self) -> List[str]:
99 result = (self.int_a + self.int_b) % self.int_n
100 # To make negative tests easier, append "0" for success to the
101 # generated cases
102 return [self.format_result(result), "0"]