2022-11-02 15:44:08 +01:00
|
|
|
"""Framework classes for generation of bignum mod test cases."""
|
|
|
|
# Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2022-11-02 15:44:08 +01:00
|
|
|
#
|
|
|
|
|
2022-12-06 11:14:57 +01:00
|
|
|
from typing import Dict, List
|
2022-11-30 17:34:07 +01:00
|
|
|
|
2022-11-02 15:44:08 +01:00
|
|
|
from . import test_data_generation
|
2022-12-06 11:14:57 +01:00
|
|
|
from . import bignum_common
|
2022-12-15 17:59:40 +01:00
|
|
|
from .bignum_data import ONLY_PRIME_MODULI
|
2022-11-02 15:44:08 +01:00
|
|
|
|
2022-11-09 13:14:14 +01:00
|
|
|
class BignumModTarget(test_data_generation.BaseTarget):
|
|
|
|
#pylint: disable=abstract-method, too-few-public-methods
|
2022-11-02 15:44:08 +01:00
|
|
|
"""Target for bignum mod test case generation."""
|
|
|
|
target_basename = 'test_suite_bignum_mod.generated'
|
|
|
|
|
2022-11-02 15:46:23 +01:00
|
|
|
|
2022-12-13 10:53:50 +01:00
|
|
|
class BignumModMul(bignum_common.ModOperationCommon,
|
|
|
|
BignumModTarget):
|
2022-12-16 17:18:28 +01:00
|
|
|
# pylint:disable=duplicate-code
|
2022-12-13 10:53:50 +01:00
|
|
|
"""Test cases for bignum mpi_mod_mul()."""
|
|
|
|
symbol = "*"
|
|
|
|
test_function = "mpi_mod_mul"
|
|
|
|
test_name = "mbedtls_mpi_mod_mul"
|
|
|
|
input_style = "arch_split"
|
|
|
|
arity = 2
|
|
|
|
|
|
|
|
def arguments(self) -> List[str]:
|
2022-12-16 15:25:02 +01:00
|
|
|
return [self.format_result(self.to_montgomery(self.int_a)),
|
|
|
|
self.format_result(self.to_montgomery(self.int_b)),
|
|
|
|
bignum_common.quote_str(self.arg_n)
|
2022-12-13 10:53:50 +01:00
|
|
|
] + self.result()
|
|
|
|
|
|
|
|
def result(self) -> List[str]:
|
|
|
|
result = (self.int_a * self.int_b) % self.int_n
|
2022-12-16 15:25:02 +01:00
|
|
|
return [self.format_result(self.to_montgomery(result))]
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2022-11-02 15:46:23 +01:00
|
|
|
|
2022-12-01 15:27:37 +01:00
|
|
|
class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget):
|
|
|
|
"""Test cases for bignum mpi_mod_sub()."""
|
|
|
|
symbol = "-"
|
|
|
|
test_function = "mpi_mod_sub"
|
|
|
|
test_name = "mbedtls_mpi_mod_sub"
|
|
|
|
input_style = "fixed"
|
|
|
|
arity = 2
|
|
|
|
|
|
|
|
def result(self) -> List[str]:
|
|
|
|
result = (self.int_a - self.int_b) % self.int_n
|
2022-12-05 16:47:40 +01:00
|
|
|
# To make negative tests easier, append 0 for success to the
|
|
|
|
# generated cases
|
|
|
|
return [self.format_result(result), "0"]
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2022-12-15 17:59:40 +01:00
|
|
|
class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
|
|
|
|
"""Test cases for bignum mpi_mod_inv() - not in Montgomery form."""
|
|
|
|
moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
|
|
|
|
symbol = "^ -1"
|
|
|
|
test_function = "mpi_mod_inv_non_mont"
|
|
|
|
test_name = "mbedtls_mpi_mod_inv non-Mont. form"
|
|
|
|
input_style = "fixed"
|
|
|
|
arity = 1
|
|
|
|
suffix = True
|
2022-12-16 04:53:17 +01:00
|
|
|
disallow_zero_a = True
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
def result(self) -> List[str]:
|
2022-12-16 04:53:17 +01:00
|
|
|
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
2022-12-15 17:59:40 +01:00
|
|
|
# To make negative tests easier, append 0 for success to the
|
|
|
|
# generated cases
|
|
|
|
return [self.format_result(result), "0"]
|
|
|
|
|
|
|
|
class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget):
|
|
|
|
"""Test cases for bignum mpi_mod_inv() - Montgomery form."""
|
|
|
|
moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
|
|
|
|
symbol = "^ -1"
|
|
|
|
test_function = "mpi_mod_inv_mont"
|
|
|
|
test_name = "mbedtls_mpi_mod_inv Mont. form"
|
|
|
|
input_style = "arch_split" # Mont. form requires arch_split
|
|
|
|
arity = 1
|
|
|
|
suffix = True
|
2022-12-16 04:53:17 +01:00
|
|
|
disallow_zero_a = True
|
2022-12-16 17:10:36 +01:00
|
|
|
montgomery_form_a = True
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
def result(self) -> List[str]:
|
2022-12-16 04:53:17 +01:00
|
|
|
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
2022-12-15 17:59:40 +01:00
|
|
|
mont_result = self.to_montgomery(result)
|
|
|
|
# To make negative tests easier, append 0 for success to the
|
|
|
|
# generated cases
|
|
|
|
return [self.format_result(mont_result), "0"]
|
|
|
|
|
2022-11-02 15:46:23 +01:00
|
|
|
|
2022-11-29 13:25:05 +01:00
|
|
|
class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget):
|
|
|
|
"""Test cases for bignum mpi_mod_add()."""
|
|
|
|
count = 0
|
|
|
|
symbol = "+"
|
|
|
|
test_function = "mpi_mod_add"
|
|
|
|
test_name = "mbedtls_mpi_mod_add"
|
|
|
|
input_style = "fixed"
|
|
|
|
|
|
|
|
def result(self) -> List[str]:
|
|
|
|
result = (self.int_a + self.int_b) % self.int_n
|
|
|
|
# To make negative tests easier, append "0" for success to the
|
|
|
|
# generated cases
|
|
|
|
return [self.format_result(result), "0"]
|