Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 1 | """Framework classes for generation of bignum mod test cases.""" |
| 2 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 3 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 4 | # |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 5 | |
Werner Lewis | 93a31c9 | 2022-12-06 10:14:57 +0000 | [diff] [blame] | 6 | from typing import Dict, List |
Werner Lewis | cff7578 | 2022-11-30 16:34:07 +0000 | [diff] [blame] | 7 | |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 8 | from . import test_data_generation |
Werner Lewis | 93a31c9 | 2022-12-06 10:14:57 +0000 | [diff] [blame] | 9 | from . import bignum_common |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 10 | from .bignum_data import ONLY_PRIME_MODULI |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 11 | |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 12 | class BignumModTarget(test_data_generation.BaseTarget): |
| 13 | #pylint: disable=abstract-method, too-few-public-methods |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 14 | """Target for bignum mod test case generation.""" |
| 15 | target_basename = 'test_suite_bignum_mod.generated' |
| 16 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 17 | |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 18 | class BignumModMul(bignum_common.ModOperationCommon, |
| 19 | BignumModTarget): |
Gabor Mezei | 8a26164 | 2022-12-16 17:18:28 +0100 | [diff] [blame] | 20 | # pylint:disable=duplicate-code |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 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]: |
Gabor Mezei | 77b877d | 2022-12-16 15:25:02 +0100 | [diff] [blame] | 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) |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 32 | ] + self.result() |
| 33 | |
| 34 | def result(self) -> List[str]: |
| 35 | result = (self.int_a * self.int_b) % self.int_n |
Gabor Mezei | 77b877d | 2022-12-16 15:25:02 +0100 | [diff] [blame] | 36 | return [self.format_result(self.to_montgomery(result))] |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 37 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 38 | |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 39 | class 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 Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 47 | def result(self) -> List[str]: |
| 48 | result = (self.int_a - self.int_b) % self.int_n |
Tom Cosgrove | f51f972 | 2022-12-05 15:47:40 +0000 | [diff] [blame] | 49 | # To make negative tests easier, append 0 for success to the |
| 50 | # generated cases |
| 51 | return [self.format_result(result), "0"] |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 52 | |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 53 | class 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 Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 62 | disallow_zero_a = True |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 63 | |
| 64 | def result(self) -> List[str]: |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 65 | result = bignum_common.invmod_positive(self.int_a, self.int_n) |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 66 | # To make negative tests easier, append 0 for success to the |
| 67 | # generated cases |
| 68 | return [self.format_result(result), "0"] |
| 69 | |
| 70 | class 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 Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 79 | disallow_zero_a = True |
Tom Cosgrove | f723754 | 2022-12-16 16:10:36 +0000 | [diff] [blame] | 80 | montgomery_form_a = True |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 81 | |
| 82 | def result(self) -> List[str]: |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 83 | result = bignum_common.invmod_positive(self.int_a, self.int_n) |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 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 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 89 | |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 90 | class 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"] |