1"""Framework classes for generation of bignum mod test cases."""
2# Copyright The Mbed TLS Contributors
3# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
4#
5
6from typing import Dict, List
7
8from . import test_data_generation
9from . import bignum_common
10from .bignum_data import ONLY_PRIME_MODULI
11
12class BignumModTarget(test_data_generation.BaseTarget):
13    #pylint: disable=abstract-method, too-few-public-methods
14    """Target for bignum mod test case generation."""
15    target_basename = 'test_suite_bignum_mod.generated'
16
17
18class BignumModMul(bignum_common.ModOperationCommon,
19                   BignumModTarget):
20    # pylint:disable=duplicate-code
21    """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]:
29        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)
32               ] + self.result()
33
34    def result(self) -> List[str]:
35        result = (self.int_a * self.int_b) % self.int_n
36        return [self.format_result(self.to_montgomery(result))]
37
38
39class 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
47    def result(self) -> List[str]:
48        result = (self.int_a - self.int_b) % self.int_n
49        # To make negative tests easier, append 0 for success to the
50        # generated cases
51        return [self.format_result(result), "0"]
52
53class 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
62    disallow_zero_a = True
63
64    def result(self) -> List[str]:
65        result = bignum_common.invmod_positive(self.int_a, self.int_n)
66        # 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
79    disallow_zero_a = True
80    montgomery_form_a = True
81
82    def result(self) -> List[str]:
83        result = bignum_common.invmod_positive(self.int_a, self.int_n)
84        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
89
90class 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"]
103