<commit_before>"""Framework classes for generation of bignum core test cases."""
# 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.

import random

from abc import ABCMeta
from typing import Dict, Iterator, List, Tuple

from . import test_case
from . import test_data_generation
from . import bignum_common

class BignumCoreTarget(test_data_generation.BaseTarget, metaclass=ABCMeta):
    #pylint: disable=abstract-method
    """Target for bignum core test case generation."""
    target_basename = 'test_suite_bignum_core.generated'


class BignumCoreOperation(bignum_common.OperationCommon, BignumCoreTarget, metaclass=ABCMeta):
    #pylint: disable=abstract-method
    """Common features for bignum core operations."""
    input_values = [
        "0", "1", "3", "f", "fe", "ff", "100", "ff00", "fffe", "ffff", "10000",
        "fffffffe", "ffffffff", "100000000", "1f7f7f7f7f7f7f",
        "8000000000000000", "fefefefefefefefe", "fffffffffffffffe",
        "ffffffffffffffff", "10000000000000000", "1234567890abcdef0",
        "fffffffffffffffffefefefefefefefe", "fffffffffffffffffffffffffffffffe",
        "ffffffffffffffffffffffffffffffff", "100000000000000000000000000000000",
        "<KEY>",
        "fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe",
        "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
        "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        "10000000000000000000000000000000000000000000000000000000000000000",
        "<KEY>",
        (
            "<KEY>"
            "<KEY>"
            "c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0"
            "cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b"
        )
    ]

    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:
            self.case_description = "{} {} {}".format(
                self.arg_a, self.symbol, self.arg_b
            )
        return super().description()

    @classmethod
    def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
        for a_value, b_value in cls.get_value_pairs():
            yield cls(a_value, b_value).create_test_case()


class BignumCoreOperationArchSplit(BignumCoreOperation):
    #pylint: disable=abstract-method
    """Common features for bignum core operations where the result depends on
    the limb size."""

    def __init__(self, val_a: str, val_b: str, bits_in_limb: int) -> None:
        super().__init__(val_a, val_b)
        self.bits_in_limb = bits_in_limb
        if self.bits_in_limb == 32:
            self.dependencies = ["MBEDTLS_HAVE_INT32"]
        elif self.bits_in_limb == 64:
            self.dependencies = ["MBEDTLS_HAVE_INT64"]

    @classmethod
    def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
        for a_value, b_value in cls.get_value_pairs():
            yield cls(a_value, b_value, 32).create_test_case()
            yield cls(a_value, b_value, 64).create_test_case()

class BignumCoreAddIf(BignumCoreOperationArchSplit):
    """Test cases for bignum core add if."""
    count = 0
    symbol = "+"
    test_function = "mpi_core_add_if"
    test_name = "mbedtls_mpi_core_add_if"

    def result(self) -> List[str]:
        result = self.int_a + self.int_b
        bound_val = max(self.int_a, self.int_b)

        bound = bignum_common.bound_mpi(bound_val, self.bits_in_limb)
        carry, result = divmod(result, bound)

        return [
            "\"{:x}\"".format(result),
            str(carry)
        ]


class BignumCoreSub(BignumCoreOperation):
    """Test cases for bignum core sub."""
    count = 0<commit_msg>Bignum Core test: move bound to constructor<commit_after>"""Framework classes for generation of bignum core test cases."""
# 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.

import random

from abc import ABCMeta
from typing import Dict, Iterator, List, Tuple

from . import test_case
from . import test_data_generation
from . import bignum_common

class BignumCoreTarget(test_data_generation.BaseTarget, metaclass=ABCMeta):
    #pylint: disable=abstract-method
    """Target for bignum core test case generation."""
    target_basename = 'test_suite_bignum_core.generated'


class BignumCoreOperation(bignum_common.OperationCommon, BignumCoreTarget, metaclass=ABCMeta):
    #pylint: disable=abstract-method
    """Common features for bignum core operations."""
    input_values = [
        "0", "1", "3", "f", "fe", "ff", "100", "ff00", "fffe", "ffff", "10000",
        "fffffffe", "ffffffff", "100000000", "1f7f7f7f7f7f7f",
        "8000000000000000", "fefefefefefefefe", "fffffffffffffffe",
        "ffffffffffffffff", "10000000000000000", "1234567890abcdef0",
        "fffffffffffffffffefefefefefefefe", "fffffffffffffffffffffffffffffffe",
        "ffffffffffffffffffffffffffffffff", "100000000000000000000000000000000",
        "<KEY>",
        "fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe",
        "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
        "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
        "10000000000000000000000000000000000000000000000000000000000000000",
        "<KEY>",
        (
            "<KEY>"
            "643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947"
            "c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0"
            "cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b"
        )
    ]

    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:
            self.case_description = "{} {} {}".format(
                self.arg_a, self.symbol, self.arg_b
            )
        return super().description()

    @classmethod
    def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
        for a_value, b_value in cls.get_value_pairs():
            yield cls(a_value, b_value).create_test_case()


class BignumCoreOperationArchSplit(BignumCoreOperation):
    #pylint: disable=abstract-method
    """Common features for bignum core operations where the result depends on
    the limb size."""

    def __init__(self, val_a: str, val_b: str, bits_in_limb: int) -> None:
        super().__init__(val_a, val_b)
        bound_val = max(self.int_a, self.int_b)
        self.bits_in_limb = bits_in_limb
        self.bound = bignum_common.bound_mpi(bound_val, self.bits_in_limb)
        if self.bits_in_limb == 32:
            self.dependencies = ["MBEDTLS_HAVE_INT32"]
        elif self.bits_in_limb == 64:
            self.dependencies = ["MBDTLS_HAVE_INT64"]

    @classmethod
    def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
        for a_value, b_value in cls.get_value_pairs():
            yield cls(a_value, b_value, 32).create_test_case()
            yield cls(a_value, b_value, 64).create_test_case()

class BignumCoreAddIf(BignumCoreOperationArchSplit):
    """Test cases for bignum core add if."""
    count = 0
    symbol = "+"
    test_function = "mpi_core_add_if"
    test_name = "mbedtls_mpi_core_add_if"

    def result(self) -> List[str]:
        result = self.int_a + self.int_b

        carry, result = divmod(result, self.bound)

        return [
            "\"{:x}\"".format(result),
            str(carry)
        ]


class BignumCoreSub(BignumCoreOperation):
    """Test cases for bignum core sub."""
    count = 0
{'fraction_non_alphanumeric': 0.09584346672562459, 'fraction_numerical': 0.062237453017908465, 'mean_word_length': 3.4413352970054003, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 30, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4209290', 'n_tokens_mistral': 3223, 'n_tokens_neox': 2702, 'n_words': 695}