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 | # 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 Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 38 | class 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 Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 46 | def result(self) -> List[str]: |
| 47 | result = (self.int_a - self.int_b) % self.int_n |
Tom Cosgrove | f51f972 | 2022-12-05 15:47:40 +0000 | [diff] [blame] | 48 | # To make negative tests easier, append 0 for success to the |
| 49 | # generated cases |
| 50 | return [self.format_result(result), "0"] |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 51 | |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 52 | class 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 |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 61 | disallow_zero_a = True |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 62 | |
| 63 | def result(self) -> List[str]: |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 64 | result = bignum_common.invmod_positive(self.int_a, self.int_n) |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 65 | # To make negative tests easier, append 0 for success to the |
| 66 | # generated cases |
| 67 | return [self.format_result(result), "0"] |
| 68 | |
| 69 | class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget): |
| 70 | """Test cases for bignum mpi_mod_inv() - Montgomery form.""" |
| 71 | moduli = ONLY_PRIME_MODULI # for now only prime moduli supported |
| 72 | symbol = "^ -1" |
| 73 | test_function = "mpi_mod_inv_mont" |
| 74 | test_name = "mbedtls_mpi_mod_inv Mont. form" |
| 75 | input_style = "arch_split" # Mont. form requires arch_split |
| 76 | arity = 1 |
| 77 | suffix = True |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 78 | disallow_zero_a = True |
| 79 | mongtomgery_form_a = True |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 80 | |
| 81 | def result(self) -> List[str]: |
Tom Cosgrove | 1133d23 | 2022-12-16 03:53:17 +0000 | [diff] [blame] | 82 | result = bignum_common.invmod_positive(self.int_a, self.int_n) |
Tom Cosgrove | dc19759 | 2022-12-15 16:59:40 +0000 | [diff] [blame] | 83 | mont_result = self.to_montgomery(result) |
| 84 | # To make negative tests easier, append 0 for success to the |
| 85 | # generated cases |
| 86 | return [self.format_result(mont_result), "0"] |
| 87 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 88 | # END MERGE SLOT 3 |
| 89 | |
| 90 | # BEGIN MERGE SLOT 4 |
| 91 | |
| 92 | # END MERGE SLOT 4 |
| 93 | |
| 94 | # BEGIN MERGE SLOT 5 |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 95 | class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget): |
| 96 | """Test cases for bignum mpi_mod_add().""" |
| 97 | count = 0 |
| 98 | symbol = "+" |
| 99 | test_function = "mpi_mod_add" |
| 100 | test_name = "mbedtls_mpi_mod_add" |
| 101 | input_style = "fixed" |
| 102 | |
| 103 | def result(self) -> List[str]: |
| 104 | result = (self.int_a + self.int_b) % self.int_n |
| 105 | # To make negative tests easier, append "0" for success to the |
| 106 | # generated cases |
| 107 | return [self.format_result(result), "0"] |
| 108 | |
Janos Follath | 1be322a | 2022-11-02 14:46:23 +0000 | [diff] [blame] | 109 | |
| 110 | # END MERGE SLOT 5 |
| 111 | |
| 112 | # BEGIN MERGE SLOT 6 |
| 113 | |
| 114 | # END MERGE SLOT 6 |
| 115 | |
| 116 | # BEGIN MERGE SLOT 7 |
| 117 | |
| 118 | # END MERGE SLOT 7 |
| 119 | |
| 120 | # BEGIN MERGE SLOT 8 |
| 121 | |
| 122 | # END MERGE SLOT 8 |
| 123 | |
| 124 | # BEGIN MERGE SLOT 9 |
| 125 | |
| 126 | # END MERGE SLOT 9 |
| 127 | |
| 128 | # BEGIN MERGE SLOT 10 |
| 129 | |
| 130 | # END MERGE SLOT 10 |