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 |
| 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 Lewis | 93a31c9 | 2022-12-06 10:14:57 +0000 | [diff] [blame] | 17 | from typing import Dict, List |
Werner Lewis | cff7578 | 2022-11-30 16:34:07 +0000 | [diff] [blame] | 18 | |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 19 | from . import test_data_generation |
Werner Lewis | 93a31c9 | 2022-12-06 10:14:57 +0000 | [diff] [blame] | 20 | from . import bignum_common |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 21 | from .bignum_data import ONLY_PRIME_MODULI |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 22 | |
Janos Follath | 0cd8967 | 2022-11-09 12:14:14 +0000 | [diff] [blame] | 23 | class BignumModTarget(test_data_generation.BaseTarget): |
| 24 | #pylint: disable=abstract-method, too-few-public-methods |
Janos Follath | b99b056 | 2022-11-02 14:44:08 +0000 | [diff] [blame] | 25 | """Target for bignum mod test case generation.""" |
| 26 | target_basename = 'test_suite_bignum_mod.generated' |
| 27 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 28 | |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 29 | class BignumModMul(bignum_common.ModOperationCommon, |
| 30 | BignumModTarget): |
Gabor Mezei | 8a26164 | 2022-12-16 17:18:28 +0100 | [diff] [blame] | 31 | # pylint:disable=duplicate-code |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 32 | """Test cases for bignum mpi_mod_mul().""" |
| 33 | symbol = "*" |
| 34 | test_function = "mpi_mod_mul" |
| 35 | test_name = "mbedtls_mpi_mod_mul" |
| 36 | input_style = "arch_split" |
| 37 | arity = 2 |
| 38 | |
| 39 | def arguments(self) -> List[str]: |
Gabor Mezei | 77b877d | 2022-12-16 15:25:02 +0100 | [diff] [blame] | 40 | return [self.format_result(self.to_montgomery(self.int_a)), |
| 41 | self.format_result(self.to_montgomery(self.int_b)), |
| 42 | bignum_common.quote_str(self.arg_n) |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 43 | ] + self.result() |
| 44 | |
| 45 | def result(self) -> List[str]: |
| 46 | result = (self.int_a * self.int_b) % self.int_n |
Gabor Mezei | 77b877d | 2022-12-16 15:25:02 +0100 | [diff] [blame] | 47 | return [self.format_result(self.to_montgomery(result))] |
Gabor Mezei | eca7466 | 2022-12-13 10:53:50 +0100 | [diff] [blame] | 48 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 49 | |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 50 | class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget): |
| 51 | """Test cases for bignum mpi_mod_sub().""" |
| 52 | symbol = "-" |
| 53 | test_function = "mpi_mod_sub" |
| 54 | test_name = "mbedtls_mpi_mod_sub" |
| 55 | input_style = "fixed" |
| 56 | arity = 2 |
| 57 | |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 58 | def result(self) -> List[str]: |
| 59 | result = (self.int_a - self.int_b) % self.int_n |
Tom Cosgrove | f51f972 | 2022-12-05 15:47:40 +0000 | [diff] [blame] | 60 | # To make negative tests easier, append 0 for success to the |
| 61 | # generated cases |
| 62 | return [self.format_result(result), "0"] |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 63 | |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 64 | class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget): |
| 65 | """Test cases for bignum mpi_mod_inv() - not in Montgomery form.""" |
| 66 | moduli = ONLY_PRIME_MODULI # for now only prime moduli supported |
| 67 | symbol = "^ -1" |
| 68 | test_function = "mpi_mod_inv_non_mont" |
| 69 | test_name = "mbedtls_mpi_mod_inv non-Mont. form" |
| 70 | input_style = "fixed" |
| 71 | arity = 1 |
| 72 | suffix = True |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 73 | disallow_zero_a = True |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 74 | |
| 75 | def result(self) -> List[str]: |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 76 | result = bignum_common.invmod_positive(self.int_a, self.int_n) |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 77 | # To make negative tests easier, append 0 for success to the |
| 78 | # generated cases |
| 79 | return [self.format_result(result), "0"] |
| 80 | |
| 81 | class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget): |
| 82 | """Test cases for bignum mpi_mod_inv() - Montgomery form.""" |
| 83 | moduli = ONLY_PRIME_MODULI # for now only prime moduli supported |
| 84 | symbol = "^ -1" |
| 85 | test_function = "mpi_mod_inv_mont" |
| 86 | test_name = "mbedtls_mpi_mod_inv Mont. form" |
| 87 | input_style = "arch_split" # Mont. form requires arch_split |
| 88 | arity = 1 |
| 89 | suffix = True |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 90 | disallow_zero_a = True |
Tom Cosgrove | f723754 | 2022-12-16 16:10:36 +0000 | [diff] [blame] | 91 | montgomery_form_a = True |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 92 | |
| 93 | def result(self) -> List[str]: |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 94 | result = bignum_common.invmod_positive(self.int_a, self.int_n) |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 95 | mont_result = self.to_montgomery(result) |
| 96 | # To make negative tests easier, append 0 for success to the |
| 97 | # generated cases |
| 98 | return [self.format_result(mont_result), "0"] |
| 99 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 100 | |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 101 | class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget): |
| 102 | """Test cases for bignum mpi_mod_add().""" |
| 103 | count = 0 |
| 104 | symbol = "+" |
| 105 | test_function = "mpi_mod_add" |
| 106 | test_name = "mbedtls_mpi_mod_add" |
| 107 | input_style = "fixed" |
| 108 | |
| 109 | def result(self) -> List[str]: |
| 110 | result = (self.int_a + self.int_b) % self.int_n |
| 111 | # To make negative tests easier, append "0" for success to the |
| 112 | # generated cases |
| 113 | return [self.format_result(result), "0"] |