2022-09-30 17:28:43 +02:00
|
|
|
"""Common features for bignum in test generation framework."""
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from abc import abstractmethod
|
2022-12-20 19:19:18 +01:00
|
|
|
import enum
|
2022-11-19 13:48:17 +01:00
|
|
|
from typing import Iterator, List, Tuple, TypeVar, Any
|
2023-04-06 17:33:10 +02:00
|
|
|
from copy import deepcopy
|
2022-11-19 13:48:17 +01:00
|
|
|
from itertools import chain
|
2023-06-27 17:34:59 +02:00
|
|
|
from math import ceil
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-11-09 13:31:23 +01:00
|
|
|
from . import test_case
|
2022-11-09 13:14:14 +01:00
|
|
|
from . import test_data_generation
|
2022-11-20 12:58:12 +01:00
|
|
|
from .bignum_data import INPUTS_DEFAULT, MODULI_DEFAULT
|
2022-11-09 13:14:14 +01:00
|
|
|
|
2022-09-30 17:28:43 +02:00
|
|
|
T = TypeVar('T') #pylint: disable=invalid-name
|
|
|
|
|
2022-10-04 11:10:40 +02:00
|
|
|
def invmod(a: int, n: int) -> int:
|
|
|
|
"""Return inverse of a to modulo n.
|
|
|
|
|
|
|
|
Equivalent to pow(a, -1, n) in Python 3.8+. Implementation is equivalent
|
|
|
|
to long_invmod() in CPython.
|
|
|
|
"""
|
|
|
|
b, c = 1, 0
|
|
|
|
while n:
|
|
|
|
q, r = divmod(a, n)
|
|
|
|
a, b, c, n = n, c, b - q*c, r
|
|
|
|
# at this point a is the gcd of the original inputs
|
|
|
|
if a == 1:
|
|
|
|
return b
|
|
|
|
raise ValueError("Not invertible")
|
|
|
|
|
2022-12-16 04:53:17 +01:00
|
|
|
def invmod_positive(a: int, n: int) -> int:
|
|
|
|
"""Return a non-negative inverse of a to modulo n."""
|
|
|
|
inv = invmod(a, n)
|
|
|
|
return inv if inv >= 0 else inv + n
|
|
|
|
|
2022-09-30 17:28:43 +02:00
|
|
|
def hex_to_int(val: str) -> int:
|
2022-11-15 20:43:33 +01:00
|
|
|
"""Implement the syntax accepted by mbedtls_test_read_mpi().
|
|
|
|
|
|
|
|
This is a superset of what is accepted by mbedtls_test_read_mpi_core().
|
|
|
|
"""
|
2022-11-10 09:15:21 +01:00
|
|
|
if val in ['', '-']:
|
2022-11-15 20:43:33 +01:00
|
|
|
return 0
|
|
|
|
return int(val, 16)
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-12-20 19:12:22 +01:00
|
|
|
def quote_str(val: str) -> str:
|
2022-09-30 17:28:43 +02:00
|
|
|
return "\"{}\"".format(val)
|
|
|
|
|
2022-10-19 14:50:10 +02:00
|
|
|
def bound_mpi(val: int, bits_in_limb: int) -> int:
|
|
|
|
"""First number exceeding number of limbs needed for given input value."""
|
|
|
|
return bound_mpi_limbs(limbs_mpi(val, bits_in_limb), bits_in_limb)
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-10-19 14:50:10 +02:00
|
|
|
def bound_mpi_limbs(limbs: int, bits_in_limb: int) -> int:
|
|
|
|
"""First number exceeding maximum of given number of limbs."""
|
|
|
|
bits = bits_in_limb * limbs
|
2022-09-30 17:28:43 +02:00
|
|
|
return 1 << bits
|
|
|
|
|
2022-10-19 14:50:10 +02:00
|
|
|
def limbs_mpi(val: int, bits_in_limb: int) -> int:
|
|
|
|
"""Return the number of limbs required to store value."""
|
2023-03-31 16:03:14 +02:00
|
|
|
bit_length = max(val.bit_length(), 1)
|
|
|
|
return (bit_length + bits_in_limb - 1) // bits_in_limb
|
2022-09-30 17:28:43 +02:00
|
|
|
|
|
|
|
def combination_pairs(values: List[T]) -> List[Tuple[T, T]]:
|
2022-11-09 21:57:52 +01:00
|
|
|
"""Return all pair combinations from input values."""
|
|
|
|
return [(x, y) for x in values for y in values]
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2023-06-27 17:34:59 +02:00
|
|
|
def bits_to_limbs(bits: int, bits_in_limb: int) -> int:
|
|
|
|
""" Return the appropriate ammount of limbs needed to store
|
|
|
|
a number contained in input bits"""
|
|
|
|
return ceil(bits / bits_in_limb)
|
|
|
|
|
2023-02-14 18:25:23 +01:00
|
|
|
def hex_digits_for_limb(limbs: int, bits_in_limb: int) -> int:
|
2023-06-27 17:34:59 +02:00
|
|
|
""" Return the hex digits need for a number of limbs. """
|
|
|
|
return 2 * ((limbs * bits_in_limb) // 8)
|
2023-02-14 18:25:23 +01:00
|
|
|
|
2023-04-25 13:13:25 +02:00
|
|
|
def hex_digits_max_int(val: str, bits_in_limb: int) -> int:
|
|
|
|
""" Return the first number exceeding maximum the limb space
|
|
|
|
required to store the input hex-string value. This method
|
|
|
|
weights on the input str_len rather than numerical value
|
|
|
|
and works with zero-padded inputs"""
|
|
|
|
n = ((1 << (len(val) * 4)) - 1)
|
|
|
|
l = limbs_mpi(n, bits_in_limb)
|
|
|
|
return bound_mpi_limbs(l, bits_in_limb)
|
|
|
|
|
|
|
|
def zfill_match(reference: str, target: str) -> str:
|
2023-05-11 11:54:44 +02:00
|
|
|
""" Zero pad target hex-string to match the limb size of
|
|
|
|
the reference input """
|
2023-04-25 13:13:25 +02:00
|
|
|
lt = len(target)
|
|
|
|
lr = len(reference)
|
2023-05-11 11:54:44 +02:00
|
|
|
target_len = lr if lt < lr else lt
|
|
|
|
return "{:x}".format(int(target, 16)).zfill(target_len)
|
2023-04-25 13:13:25 +02:00
|
|
|
|
2022-11-09 13:14:14 +01:00
|
|
|
class OperationCommon(test_data_generation.BaseTest):
|
2022-09-30 17:28:43 +02:00
|
|
|
"""Common features for bignum binary operations.
|
|
|
|
|
|
|
|
This adds functionality common in binary operation tests.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
symbol: Symbol to use for the operation in case description.
|
|
|
|
input_values: List of values to use as test case inputs. These are
|
|
|
|
combined to produce pairs of values.
|
|
|
|
input_cases: List of tuples containing pairs of test case inputs. This
|
|
|
|
can be used to implement specific pairs of inputs.
|
2022-10-04 11:07:13 +02:00
|
|
|
unique_combinations_only: Boolean to select if test case combinations
|
|
|
|
must be unique. If True, only A,B or B,A would be included as a test
|
|
|
|
case. If False, both A,B and B,A would be included.
|
2022-11-17 21:33:51 +01:00
|
|
|
input_style: Controls the way how test data is passed to the functions
|
|
|
|
in the generated test cases. "variable" passes them as they are
|
|
|
|
defined in the python source. "arch_split" pads the values with
|
|
|
|
zeroes depending on the architecture/limb size. If this is set,
|
|
|
|
test cases are generated for all architectures.
|
2022-11-18 18:49:13 +01:00
|
|
|
arity: the number of operands for the operation. Currently supported
|
|
|
|
values are 1 and 2.
|
2022-09-30 17:28:43 +02:00
|
|
|
"""
|
|
|
|
symbol = ""
|
2022-11-20 12:58:12 +01:00
|
|
|
input_values = INPUTS_DEFAULT # type: List[str]
|
2022-11-19 13:48:17 +01:00
|
|
|
input_cases = [] # type: List[Any]
|
2023-04-06 17:33:10 +02:00
|
|
|
dependencies = [] # type: List[Any]
|
2022-11-20 14:32:54 +01:00
|
|
|
unique_combinations_only = False
|
2022-11-19 16:55:53 +01:00
|
|
|
input_styles = ["variable", "fixed", "arch_split"] # type: List[str]
|
2022-11-17 21:33:51 +01:00
|
|
|
input_style = "variable" # type: str
|
2022-11-17 15:42:40 +01:00
|
|
|
limb_sizes = [32, 64] # type: List[int]
|
2022-11-18 18:49:13 +01:00
|
|
|
arities = [1, 2]
|
|
|
|
arity = 2
|
2022-12-08 10:44:10 +01:00
|
|
|
suffix = False # for arity = 1, symbol can be prefix (default) or suffix
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-11-19 16:55:53 +01:00
|
|
|
def __init__(self, val_a: str, val_b: str = "0", bits_in_limb: int = 32) -> None:
|
2022-11-18 17:05:46 +01:00
|
|
|
self.val_a = val_a
|
|
|
|
self.val_b = val_b
|
2022-11-18 17:48:45 +01:00
|
|
|
# Setting the int versions here as opposed to making them @properties
|
|
|
|
# provides earlier/more robust input validation.
|
2022-09-30 17:28:43 +02:00
|
|
|
self.int_a = hex_to_int(val_a)
|
|
|
|
self.int_b = hex_to_int(val_b)
|
2023-04-06 17:33:10 +02:00
|
|
|
self.dependencies = deepcopy(self.dependencies)
|
2022-11-17 15:42:40 +01:00
|
|
|
if bits_in_limb not in self.limb_sizes:
|
|
|
|
raise ValueError("Invalid number of bits in limb!")
|
2022-11-17 21:33:51 +01:00
|
|
|
if self.input_style == "arch_split":
|
2023-04-06 17:33:10 +02:00
|
|
|
self.dependencies.append("MBEDTLS_HAVE_INT{:d}".format(bits_in_limb))
|
2022-11-17 15:42:40 +01:00
|
|
|
self.bits_in_limb = bits_in_limb
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-11-17 16:13:02 +01:00
|
|
|
@property
|
|
|
|
def boundary(self) -> int:
|
2022-11-18 18:49:13 +01:00
|
|
|
if self.arity == 1:
|
|
|
|
return self.int_a
|
|
|
|
elif self.arity == 2:
|
|
|
|
return max(self.int_a, self.int_b)
|
|
|
|
raise ValueError("Unsupported number of operands!")
|
2022-11-17 16:13:02 +01:00
|
|
|
|
2022-11-17 21:33:51 +01:00
|
|
|
@property
|
|
|
|
def limb_boundary(self) -> int:
|
|
|
|
return bound_mpi(self.boundary, self.bits_in_limb)
|
|
|
|
|
2022-11-17 16:13:02 +01:00
|
|
|
@property
|
|
|
|
def limbs(self) -> int:
|
|
|
|
return limbs_mpi(self.boundary, self.bits_in_limb)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hex_digits(self) -> int:
|
2023-02-14 18:25:23 +01:00
|
|
|
return hex_digits_for_limb(self.limbs, self.bits_in_limb)
|
2022-11-17 16:13:02 +01:00
|
|
|
|
2022-12-20 19:12:22 +01:00
|
|
|
def format_arg(self, val: str) -> str:
|
2022-11-18 17:05:46 +01:00
|
|
|
if self.input_style not in self.input_styles:
|
|
|
|
raise ValueError("Unknown input style!")
|
|
|
|
if self.input_style == "variable":
|
|
|
|
return val
|
|
|
|
else:
|
|
|
|
return val.zfill(self.hex_digits)
|
|
|
|
|
2022-12-20 19:12:22 +01:00
|
|
|
def format_result(self, res: int) -> str:
|
2022-11-18 17:05:46 +01:00
|
|
|
res_str = '{:x}'.format(res)
|
|
|
|
return quote_str(self.format_arg(res_str))
|
|
|
|
|
2022-11-17 16:13:02 +01:00
|
|
|
@property
|
2022-11-18 17:05:46 +01:00
|
|
|
def arg_a(self) -> str:
|
|
|
|
return self.format_arg(self.val_a)
|
2022-11-17 16:13:02 +01:00
|
|
|
|
|
|
|
@property
|
2022-11-18 17:05:46 +01:00
|
|
|
def arg_b(self) -> str:
|
2022-11-18 18:49:13 +01:00
|
|
|
if self.arity == 1:
|
|
|
|
raise AttributeError("Operation is unary and doesn't have arg_b!")
|
2022-11-18 17:05:46 +01:00
|
|
|
return self.format_arg(self.val_b)
|
2022-11-17 16:13:02 +01:00
|
|
|
|
2022-09-30 17:28:43 +02:00
|
|
|
def arguments(self) -> List[str]:
|
2022-11-18 18:49:13 +01:00
|
|
|
args = [quote_str(self.arg_a)]
|
|
|
|
if self.arity == 2:
|
|
|
|
args.append(quote_str(self.arg_b))
|
|
|
|
return args + self.result()
|
2022-09-30 17:28:43 +02:00
|
|
|
|
2022-11-09 14:24:46 +01:00
|
|
|
def description(self) -> str:
|
|
|
|
"""Generate a description for the test case.
|
|
|
|
|
|
|
|
If not set, case_description uses the form A `symbol` B, where symbol
|
|
|
|
is used to represent the operation. Descriptions of each value are
|
|
|
|
generated to provide some context to the test case.
|
|
|
|
"""
|
|
|
|
if not self.case_description:
|
2022-11-19 16:05:19 +01:00
|
|
|
if self.arity == 1:
|
2022-12-08 10:44:10 +01:00
|
|
|
format_string = "{1:x} {0}" if self.suffix else "{0} {1:x}"
|
|
|
|
self.case_description = format_string.format(
|
2022-11-19 16:05:19 +01:00
|
|
|
self.symbol, self.int_a
|
|
|
|
)
|
|
|
|
elif self.arity == 2:
|
|
|
|
self.case_description = "{:x} {} {:x}".format(
|
|
|
|
self.int_a, self.symbol, self.int_b
|
|
|
|
)
|
2022-11-09 14:24:46 +01:00
|
|
|
return super().description()
|
|
|
|
|
2022-11-18 19:15:24 +01:00
|
|
|
@property
|
|
|
|
def is_valid(self) -> bool:
|
|
|
|
return True
|
|
|
|
|
2022-09-30 17:28:43 +02:00
|
|
|
@abstractmethod
|
2022-10-12 15:53:17 +02:00
|
|
|
def result(self) -> List[str]:
|
2022-09-30 17:28:43 +02:00
|
|
|
"""Get the result of the operation.
|
|
|
|
|
|
|
|
This could be calculated during initialization and stored as `_result`
|
|
|
|
and then returned, or calculated when the method is called.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_value_pairs(cls) -> Iterator[Tuple[str, str]]:
|
|
|
|
"""Generator to yield pairs of inputs.
|
|
|
|
|
|
|
|
Combinations are first generated from all input values, and then
|
|
|
|
specific cases provided.
|
|
|
|
"""
|
2022-11-19 15:55:43 +01:00
|
|
|
if cls.arity == 1:
|
|
|
|
yield from ((a, "0") for a in cls.input_values)
|
|
|
|
elif cls.arity == 2:
|
|
|
|
if cls.unique_combinations_only:
|
|
|
|
yield from combination_pairs(cls.input_values)
|
|
|
|
else:
|
|
|
|
yield from (
|
|
|
|
(a, b)
|
|
|
|
for a in cls.input_values
|
|
|
|
for b in cls.input_values
|
|
|
|
)
|
2022-10-04 11:07:13 +02:00
|
|
|
else:
|
2022-11-19 15:55:43 +01:00
|
|
|
raise ValueError("Unsupported number of operands!")
|
2022-11-03 15:46:18 +01:00
|
|
|
|
2022-11-09 13:31:23 +01:00
|
|
|
@classmethod
|
|
|
|
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
2022-11-17 21:33:51 +01:00
|
|
|
if cls.input_style not in cls.input_styles:
|
|
|
|
raise ValueError("Unknown input style!")
|
2022-11-18 18:49:13 +01:00
|
|
|
if cls.arity not in cls.arities:
|
|
|
|
raise ValueError("Unsupported number of operands!")
|
2022-11-18 19:15:24 +01:00
|
|
|
if cls.input_style == "arch_split":
|
2022-11-19 11:42:20 +01:00
|
|
|
test_objects = (cls(a, b, bits_in_limb=bil)
|
|
|
|
for a, b in cls.get_value_pairs()
|
2022-11-18 19:15:24 +01:00
|
|
|
for bil in cls.limb_sizes)
|
2022-11-19 13:48:17 +01:00
|
|
|
special_cases = (cls(*args, bits_in_limb=bil) # type: ignore
|
|
|
|
for args in cls.input_cases
|
|
|
|
for bil in cls.limb_sizes)
|
2022-11-18 19:15:24 +01:00
|
|
|
else:
|
2022-11-19 11:42:20 +01:00
|
|
|
test_objects = (cls(a, b)
|
|
|
|
for a, b in cls.get_value_pairs())
|
2022-11-19 13:48:17 +01:00
|
|
|
special_cases = (cls(*args) for args in cls.input_cases)
|
2022-11-18 19:15:24 +01:00
|
|
|
yield from (valid_test_object.create_test_case()
|
|
|
|
for valid_test_object in filter(
|
|
|
|
lambda test_object: test_object.is_valid,
|
2022-11-19 13:48:17 +01:00
|
|
|
chain(test_objects, special_cases)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-11-09 13:31:23 +01:00
|
|
|
|
2022-12-20 19:19:18 +01:00
|
|
|
class ModulusRepresentation(enum.Enum):
|
|
|
|
"""Representation selector of a modulus."""
|
|
|
|
# Numerical values aligned with the type mbedtls_mpi_mod_rep_selector
|
|
|
|
INVALID = 0
|
|
|
|
MONTGOMERY = 2
|
|
|
|
OPT_RED = 3
|
|
|
|
|
|
|
|
def symbol(self) -> str:
|
|
|
|
"""The C symbol for this representation selector."""
|
|
|
|
return 'MBEDTLS_MPI_MOD_REP_' + self.name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def supported_representations(cls) -> List['ModulusRepresentation']:
|
|
|
|
"""Return all representations that are supported in positive test cases."""
|
|
|
|
return [cls.MONTGOMERY, cls.OPT_RED]
|
|
|
|
|
|
|
|
|
2022-11-17 14:32:43 +01:00
|
|
|
class ModOperationCommon(OperationCommon):
|
|
|
|
#pylint: disable=abstract-method
|
|
|
|
"""Target for bignum mod_raw test case generation."""
|
2022-11-20 12:58:12 +01:00
|
|
|
moduli = MODULI_DEFAULT # type: List[str]
|
2022-12-16 17:10:36 +01:00
|
|
|
montgomery_form_a = False
|
2022-12-16 04:53:17 +01:00
|
|
|
disallow_zero_a = False
|
2022-11-17 14:32:43 +01:00
|
|
|
|
2022-11-17 15:42:40 +01:00
|
|
|
def __init__(self, val_n: str, val_a: str, val_b: str = "0",
|
|
|
|
bits_in_limb: int = 64) -> None:
|
|
|
|
super().__init__(val_a=val_a, val_b=val_b, bits_in_limb=bits_in_limb)
|
2022-11-17 14:32:43 +01:00
|
|
|
self.val_n = val_n
|
2022-11-18 17:48:45 +01:00
|
|
|
# Setting the int versions here as opposed to making them @properties
|
|
|
|
# provides earlier/more robust input validation.
|
|
|
|
self.int_n = hex_to_int(val_n)
|
2022-11-17 14:32:43 +01:00
|
|
|
|
2022-12-06 13:36:00 +01:00
|
|
|
def to_montgomery(self, val: int) -> int:
|
2022-12-06 13:20:43 +01:00
|
|
|
return (val * self.r) % self.int_n
|
|
|
|
|
2022-12-06 13:36:00 +01:00
|
|
|
def from_montgomery(self, val: int) -> int:
|
2022-12-06 13:20:43 +01:00
|
|
|
return (val * self.r_inv) % self.int_n
|
|
|
|
|
2022-12-20 19:19:18 +01:00
|
|
|
def convert_from_canonical(self, canonical: int,
|
|
|
|
rep: ModulusRepresentation) -> int:
|
|
|
|
"""Convert values from canonical representation to the given representation."""
|
|
|
|
if rep is ModulusRepresentation.MONTGOMERY:
|
|
|
|
return self.to_montgomery(canonical)
|
|
|
|
elif rep is ModulusRepresentation.OPT_RED:
|
|
|
|
return canonical
|
|
|
|
else:
|
|
|
|
raise ValueError('Modulus representation not supported: {}'
|
|
|
|
.format(rep.name))
|
|
|
|
|
2022-11-17 14:32:43 +01:00
|
|
|
@property
|
|
|
|
def boundary(self) -> int:
|
2022-11-18 18:49:13 +01:00
|
|
|
return self.int_n
|
2022-11-17 14:32:43 +01:00
|
|
|
|
2022-12-16 04:53:17 +01:00
|
|
|
@property
|
|
|
|
def arg_a(self) -> str:
|
2022-12-16 17:10:36 +01:00
|
|
|
if self.montgomery_form_a:
|
2022-12-16 04:53:17 +01:00
|
|
|
value_a = self.to_montgomery(self.int_a)
|
|
|
|
else:
|
|
|
|
value_a = self.int_a
|
|
|
|
return self.format_arg('{:x}'.format(value_a))
|
|
|
|
|
2022-11-17 14:32:43 +01:00
|
|
|
@property
|
2022-11-18 17:05:46 +01:00
|
|
|
def arg_n(self) -> str:
|
|
|
|
return self.format_arg(self.val_n)
|
2022-11-17 14:32:43 +01:00
|
|
|
|
2022-12-20 19:16:54 +01:00
|
|
|
def format_arg(self, val: str) -> str:
|
2022-12-21 18:30:10 +01:00
|
|
|
return super().format_arg(val).zfill(self.hex_digits)
|
2022-12-20 19:16:54 +01:00
|
|
|
|
2022-11-18 18:49:13 +01:00
|
|
|
def arguments(self) -> List[str]:
|
|
|
|
return [quote_str(self.arg_n)] + super().arguments()
|
|
|
|
|
2022-11-17 14:32:43 +01:00
|
|
|
@property
|
|
|
|
def r(self) -> int: # pylint: disable=invalid-name
|
|
|
|
l = limbs_mpi(self.int_n, self.bits_in_limb)
|
|
|
|
return bound_mpi_limbs(l, self.bits_in_limb)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def r_inv(self) -> int:
|
|
|
|
return invmod(self.r, self.int_n)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def r2(self) -> int: # pylint: disable=invalid-name
|
|
|
|
return pow(self.r, 2)
|
|
|
|
|
2022-11-19 11:42:20 +01:00
|
|
|
@property
|
|
|
|
def is_valid(self) -> bool:
|
|
|
|
if self.int_a >= self.int_n:
|
|
|
|
return False
|
2022-12-16 04:53:17 +01:00
|
|
|
if self.disallow_zero_a and self.int_a == 0:
|
|
|
|
return False
|
2022-11-19 11:42:20 +01:00
|
|
|
if self.arity == 2 and self.int_b >= self.int_n:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2022-11-19 16:05:19 +01:00
|
|
|
def description(self) -> str:
|
|
|
|
"""Generate a description for the test case.
|
|
|
|
|
|
|
|
It uses the form A `symbol` B mod N, where symbol is used to represent
|
|
|
|
the operation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self.case_description:
|
|
|
|
return super().description() + " mod {:x}".format(self.int_n)
|
|
|
|
return super().description()
|
|
|
|
|
2022-11-19 15:18:02 +01:00
|
|
|
@classmethod
|
|
|
|
def input_cases_args(cls) -> Iterator[Tuple[Any, Any, Any]]:
|
|
|
|
if cls.arity == 1:
|
|
|
|
yield from ((n, a, "0") for a, n in cls.input_cases)
|
|
|
|
elif cls.arity == 2:
|
|
|
|
yield from ((n, a, b) for a, b, n in cls.input_cases)
|
|
|
|
else:
|
|
|
|
raise ValueError("Unsupported number of operands!")
|
|
|
|
|
2022-11-19 11:42:20 +01:00
|
|
|
@classmethod
|
|
|
|
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
|
|
|
if cls.input_style not in cls.input_styles:
|
|
|
|
raise ValueError("Unknown input style!")
|
|
|
|
if cls.arity not in cls.arities:
|
|
|
|
raise ValueError("Unsupported number of operands!")
|
|
|
|
if cls.input_style == "arch_split":
|
|
|
|
test_objects = (cls(n, a, b, bits_in_limb=bil)
|
|
|
|
for n in cls.moduli
|
|
|
|
for a, b in cls.get_value_pairs()
|
|
|
|
for bil in cls.limb_sizes)
|
2022-11-19 15:18:02 +01:00
|
|
|
special_cases = (cls(*args, bits_in_limb=bil)
|
|
|
|
for args in cls.input_cases_args()
|
|
|
|
for bil in cls.limb_sizes)
|
2022-11-19 11:42:20 +01:00
|
|
|
else:
|
|
|
|
test_objects = (cls(n, a, b)
|
|
|
|
for n in cls.moduli
|
|
|
|
for a, b in cls.get_value_pairs())
|
2022-11-19 15:18:02 +01:00
|
|
|
special_cases = (cls(*args) for args in cls.input_cases_args())
|
2022-11-19 11:42:20 +01:00
|
|
|
yield from (valid_test_object.create_test_case()
|
|
|
|
for valid_test_object in filter(
|
|
|
|
lambda test_object: test_object.is_valid,
|
2022-11-19 15:18:02 +01:00
|
|
|
chain(test_objects, special_cases)
|
2022-11-19 11:42:20 +01:00
|
|
|
))
|