Helpers for generating representation-aware test cases

Add a class for modulus representations (mbedtls_mpi_mod_rep_selector).

Add a method to convert a number to any representation.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-12-20 19:19:18 +01:00
parent 5623ecc2d6
commit 7a708fd49f

View file

@ -15,6 +15,7 @@
# limitations under the License. # limitations under the License.
from abc import abstractmethod from abc import abstractmethod
import enum
from typing import Iterator, List, Tuple, TypeVar, Any from typing import Iterator, List, Tuple, TypeVar, Any
from itertools import chain from itertools import chain
@ -240,6 +241,23 @@ class OperationCommon(test_data_generation.BaseTest):
) )
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]
class ModOperationCommon(OperationCommon): class ModOperationCommon(OperationCommon):
#pylint: disable=abstract-method #pylint: disable=abstract-method
"""Target for bignum mod_raw test case generation.""" """Target for bignum mod_raw test case generation."""
@ -259,6 +277,17 @@ class ModOperationCommon(OperationCommon):
def from_montgomery(self, val: int) -> int: def from_montgomery(self, val: int) -> int:
return (val * self.r_inv) % self.int_n return (val * self.r_inv) % self.int_n
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))
@property @property
def boundary(self) -> int: def boundary(self) -> int:
return self.int_n return self.int_n