Merge pull request #6750 from tom-cosgrove-arm/issue-6023-mod_inv_prime

Bignum: Implement mbedtls_mpi_mod_raw_inv_prime()
This commit is contained in:
Manuel Pégourié-Gonnard 2022-12-12 09:52:21 +01:00 committed by GitHub
commit cd98b939b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 197 additions and 4 deletions

View file

@ -99,6 +99,7 @@ class OperationCommon(test_data_generation.BaseTest):
limb_sizes = [32, 64] # type: List[int]
arities = [1, 2]
arity = 2
suffix = False # for arity = 1, symbol can be prefix (default) or suffix
def __init__(self, val_a: str, val_b: str = "0", bits_in_limb: int = 32) -> None:
self.val_a = val_a
@ -170,7 +171,8 @@ class OperationCommon(test_data_generation.BaseTest):
"""
if not self.case_description:
if self.arity == 1:
self.case_description = "{} {:x}".format(
format_string = "{1:x} {0}" if self.suffix else "{0} {1:x}"
self.case_description = format_string.format(
self.symbol, self.int_a
)
elif self.arity == 2:

View file

@ -90,8 +90,8 @@ RANDOM_1024_BIT_SEED_4_NO5 = ("53be4721f5b9e1f5acdac615bc20f6264922b9ccf469aef8"
"4708d9893a973000b54a23020fc5b043d6e4a51519d9c9cc"
"52d32377e78131c1")
# Adding 192 bit and 1024 bit numbers because these are the shortest required
# for ECC and RSA respectively.
# Adding 192 bit and 1024 bit numbers because these are the shortest required
# for ECC and RSA respectively.
INPUTS_DEFAULT = [
"0", "1", # corner cases
"2", "3", # small primes
@ -110,13 +110,21 @@ INPUTS_DEFAULT = [
# supported for now.
MODULI_DEFAULT = [
"53", # safe prime
"45", # non-prime
"45", # non-prime
SAFE_PRIME_192_BIT_SEED_1, # safe prime
RANDOM_192_BIT_SEED_2_NO4, # not a prime
SAFE_PRIME_1024_BIT_SEED_3, # safe prime
RANDOM_1024_BIT_SEED_4_NO5, # not a prime
]
# Some functions, e.g. mbedtls_mpi_mod_raw_inv_prime(), only support prime moduli.
ONLY_PRIME_MODULI = [
"53", # safe prime
"8ac72304057392b5", # 9999999997777777333 (longer, not safe, prime)
SAFE_PRIME_192_BIT_SEED_1, # safe prime
SAFE_PRIME_1024_BIT_SEED_3, # safe prime
]
def __gen_safe_prime(bits, seed):
'''
Generate a safe prime.

View file

@ -18,6 +18,7 @@ from typing import Dict, List
from . import test_data_generation
from . import bignum_common
from .bignum_data import ONLY_PRIME_MODULI
class BignumModRawTarget(test_data_generation.BaseTarget):
#pylint: disable=abstract-method, too-few-public-methods
@ -53,6 +54,34 @@ class BignumModRawSub(bignum_common.ModOperationCommon,
# BEGIN MERGE SLOT 3
class BignumModRawInvPrime(bignum_common.ModOperationCommon,
BignumModRawTarget):
"""Test cases for bignum mpi_mod_raw_inv_prime()."""
moduli = ONLY_PRIME_MODULI
symbol = "^ -1"
test_function = "mpi_mod_raw_inv_prime"
test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
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
@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))
def result(self) -> List[str]:
result = bignum_common.invmod(self.int_a, self.int_n)
if result < 0:
result += self.int_n
mont_result = self.to_montgomery(result)
return [self.format_result(mont_result)]
# END MERGE SLOT 3
# BEGIN MERGE SLOT 4