diff --git a/scripts/mbedtls_dev/bignum_common.py b/scripts/mbedtls_dev/bignum_common.py index 0339b1ad18..dd4fc36840 100644 --- a/scripts/mbedtls_dev/bignum_common.py +++ b/scripts/mbedtls_dev/bignum_common.py @@ -39,6 +39,11 @@ def invmod(a: int, n: int) -> int: return b raise ValueError("Not invertible") +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 + def hex_to_int(val: str) -> int: """Implement the syntax accepted by mbedtls_test_read_mpi(). @@ -244,6 +249,8 @@ class ModOperationCommon(OperationCommon): #pylint: disable=abstract-method """Target for bignum mod_raw test case generation.""" moduli = MODULI_DEFAULT # type: List[str] + mongtomgery_form_a = False + disallow_zero_a = False def __init__(self, val_n: str, val_a: str, val_b: str = "0", bits_in_limb: int = 64) -> None: @@ -263,6 +270,14 @@ class ModOperationCommon(OperationCommon): def boundary(self) -> int: return self.int_n + @property + def arg_a(self) -> str: + if self.mongtomgery_form_a: + value_a = self.to_montgomery(self.int_a) + else: + value_a = self.int_a + return self.format_arg('{:x}'.format(value_a)) + @property def arg_n(self) -> str: return self.format_arg(self.val_n) @@ -287,6 +302,8 @@ class ModOperationCommon(OperationCommon): def is_valid(self) -> bool: if self.int_a >= self.int_n: return False + if self.disallow_zero_a and self.int_a == 0: + return False if self.arity == 2 and self.int_b >= self.int_n: return False return True diff --git a/scripts/mbedtls_dev/bignum_core.py b/scripts/mbedtls_dev/bignum_core.py index 1a8c22bfae..3bd7f111cf 100644 --- a/scripts/mbedtls_dev/bignum_core.py +++ b/scripts/mbedtls_dev/bignum_core.py @@ -757,15 +757,7 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon): test_function = "mpi_core_exp_mod" test_name = "Core modular exponentiation (Mongtomery form only)" input_style = "fixed" - - def arguments(self) -> List[str]: - # Input 'a' has to be given in Montgomery form - mont_a = self.to_montgomery(self.int_a) - arg_mont_a = self.format_arg('{:x}'.format(mont_a)) - return [bignum_common.quote_str(n) for n in [self.arg_n, - arg_mont_a, - self.arg_b] - ] + self.result() + mongtomgery_form_a = True def result(self) -> List[str]: # Result has to be given in Montgomery form too diff --git a/scripts/mbedtls_dev/bignum_mod.py b/scripts/mbedtls_dev/bignum_mod.py index 9f98131bd6..6428873541 100644 --- a/scripts/mbedtls_dev/bignum_mod.py +++ b/scripts/mbedtls_dev/bignum_mod.py @@ -58,15 +58,10 @@ class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget): input_style = "fixed" arity = 1 suffix = True - - @property - def is_valid(self) -> bool: - return self.int_a > 0 and self.int_a < self.int_n + disallow_zero_a = True def result(self) -> List[str]: - result = bignum_common.invmod(self.int_a, self.int_n) - if result < 0: - result += self.int_n + result = bignum_common.invmod_positive(self.int_a, self.int_n) # To make negative tests easier, append 0 for success to the # generated cases return [self.format_result(result), "0"] @@ -80,20 +75,11 @@ class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget): input_style = "arch_split" # Mont. form requires arch_split arity = 1 suffix = True - - @property - def is_valid(self) -> bool: - return self.int_a > 0 and self.int_a < self.int_n - - @property - def arg_a(self) -> str: - mont_a = self.to_montgomery(self.int_a) - return self.format_arg('{:x}'.format(mont_a)) + disallow_zero_a = True + mongtomgery_form_a = True def result(self) -> List[str]: - result = bignum_common.invmod(self.int_a, self.int_n) - if result < 0: - result += self.int_n + result = bignum_common.invmod_positive(self.int_a, self.int_n) mont_result = self.to_montgomery(result) # To make negative tests easier, append 0 for success to the # generated cases diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 0084898bee..461b1f2b9a 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -64,21 +64,11 @@ class BignumModRawInvPrime(bignum_common.ModOperationCommon, input_style = "arch_split" arity = 1 suffix = True - - @property - def is_valid(self) -> bool: - return self.int_a > 0 and self.int_a < self.int_n - - @property - def arg_a(self) -> str: - # Input has to be given in Montgomery form - mont_a = self.to_montgomery(self.int_a) - return self.format_arg('{:x}'.format(mont_a)) + mongtomgery_form_a = True + disallow_zero_a = True def result(self) -> List[str]: - result = bignum_common.invmod(self.int_a, self.int_n) - if result < 0: - result += self.int_n + result = bignum_common.invmod_positive(self.int_a, self.int_n) mont_result = self.to_montgomery(result) return [self.format_result(mont_result)]