mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-28 00:21:48 +03:00
mpi_core_add_if test: Remove dependency on old API
Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
@ -61,8 +61,8 @@ class BignumCoreOperation(bignum_common.OperationCommon, BignumCoreTarget, metac
|
|||||||
generated to provide some context to the test case.
|
generated to provide some context to the test case.
|
||||||
"""
|
"""
|
||||||
if not self.case_description:
|
if not self.case_description:
|
||||||
self.case_description = "{} {} {}".format(
|
self.case_description = "{:x} {} {:x}".format(
|
||||||
self.arg_a, self.symbol, self.arg_b
|
self.int_a, self.symbol, self.int_b
|
||||||
)
|
)
|
||||||
return super().description()
|
return super().description()
|
||||||
|
|
||||||
@ -82,10 +82,20 @@ class BignumCoreOperationArchSplit(BignumCoreOperation):
|
|||||||
bound_val = max(self.int_a, self.int_b)
|
bound_val = max(self.int_a, self.int_b)
|
||||||
self.bits_in_limb = bits_in_limb
|
self.bits_in_limb = bits_in_limb
|
||||||
self.bound = bignum_common.bound_mpi(bound_val, self.bits_in_limb)
|
self.bound = bignum_common.bound_mpi(bound_val, self.bits_in_limb)
|
||||||
|
limbs = bignum_common.limbs_mpi(bound_val, self.bits_in_limb)
|
||||||
|
byte_len = limbs*self.bits_in_limb//8
|
||||||
|
self.hex_digits = 2*byte_len
|
||||||
if self.bits_in_limb == 32:
|
if self.bits_in_limb == 32:
|
||||||
self.dependencies = ["MBEDTLS_HAVE_INT32"]
|
self.dependencies = ["MBEDTLS_HAVE_INT32"]
|
||||||
elif self.bits_in_limb == 64:
|
elif self.bits_in_limb == 64:
|
||||||
self.dependencies = ["MBDTLS_HAVE_INT64"]
|
self.dependencies = ["MBEDTLS_HAVE_INT64"]
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid number of bits in limb!")
|
||||||
|
self.arg_a = self.arg_a.zfill(self.hex_digits)
|
||||||
|
self.arg_b = self.arg_b.zfill(self.hex_digits)
|
||||||
|
|
||||||
|
def pad_to_limbs(self, val) -> str:
|
||||||
|
return "{:x}".format(val).zfill(self.hex_digits)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||||
@ -106,11 +116,10 @@ class BignumCoreAddIf(BignumCoreOperationArchSplit):
|
|||||||
carry, result = divmod(result, self.bound)
|
carry, result = divmod(result, self.bound)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
"\"{:x}\"".format(result),
|
bignum_common.quote_str(self.pad_to_limbs(result)),
|
||||||
str(carry)
|
str(carry)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class BignumCoreSub(BignumCoreOperation):
|
class BignumCoreSub(BignumCoreOperation):
|
||||||
"""Test cases for bignum core sub."""
|
"""Test cases for bignum core sub."""
|
||||||
count = 0
|
count = 0
|
||||||
|
@ -342,106 +342,73 @@ exit:
|
|||||||
void mpi_core_add_if( char * input_A, char * input_B,
|
void mpi_core_add_if( char * input_A, char * input_B,
|
||||||
char * input_S, int carry )
|
char * input_S, int carry )
|
||||||
{
|
{
|
||||||
mbedtls_mpi S, A, B;
|
mbedtls_mpi_uint *A = NULL; /* first value to add */
|
||||||
mbedtls_mpi_uint *a = NULL; /* first value to add */
|
size_t A_limbs;
|
||||||
mbedtls_mpi_uint *b = NULL; /* second value to add */
|
mbedtls_mpi_uint *B = NULL; /* second value to add */
|
||||||
mbedtls_mpi_uint *sum = NULL;
|
size_t B_limbs;
|
||||||
mbedtls_mpi_uint *d = NULL; /* destination - the in/out first operand */
|
mbedtls_mpi_uint *S = NULL; /* expected result */
|
||||||
|
size_t S_limbs;
|
||||||
|
mbedtls_mpi_uint *X = NULL; /* destination - the in/out first operand */
|
||||||
|
size_t X_limbs;
|
||||||
|
|
||||||
mbedtls_mpi_init( &A );
|
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &A, &A_limbs, input_A ) );
|
||||||
mbedtls_mpi_init( &B );
|
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &B, &B_limbs, input_B ) );
|
||||||
mbedtls_mpi_init( &S );
|
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &S, &S_limbs, input_S ) );
|
||||||
|
X_limbs = S_limbs;
|
||||||
|
ASSERT_ALLOC( X, X_limbs );
|
||||||
|
|
||||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
/* add_if expects all operands to be the same length */
|
||||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
TEST_EQUAL( A_limbs, B_limbs );
|
||||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) );
|
TEST_EQUAL( A_limbs, S_limbs );
|
||||||
|
size_t limbs = A_limbs;
|
||||||
|
size_t bytes = limbs * sizeof( *A );
|
||||||
|
|
||||||
/* All of the inputs are +ve (or zero) */
|
/* The test cases have A <= B to avoid repetition, so we test A + B then,
|
||||||
TEST_EQUAL( 1, A.s );
|
* if A != B, B + A. If A == B, we can test when A and B are aliased */
|
||||||
TEST_EQUAL( 1, B.s );
|
|
||||||
TEST_EQUAL( 1, S.s );
|
|
||||||
|
|
||||||
/* Test cases are such that A <= B, so #limbs should be <= */
|
/* A + B */
|
||||||
TEST_LE_U( A.n, B.n );
|
|
||||||
TEST_LE_U( S.n, B.n );
|
|
||||||
|
|
||||||
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
/* cond = 0 => X unchanged, no carry */
|
||||||
|
memcpy( X, A, bytes );
|
||||||
/* mbedtls_mpi_core_add_if() uses input arrays of mbedtls_mpi_uints which
|
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( X, B, limbs, 0 ) );
|
||||||
* must be the same size. The MPIs we've read in will only have arrays
|
ASSERT_COMPARE( X, bytes, A, bytes );
|
||||||
* large enough for the number they represent. Therefore we create new
|
|
||||||
* raw arrays of mbedtls_mpi_uints and populate them from the MPIs we've
|
|
||||||
* just read in.
|
|
||||||
*
|
|
||||||
* We generated test data such that B was always >= A, so that's how many
|
|
||||||
* limbs each of these need.
|
|
||||||
*/
|
|
||||||
size_t limbs = B.n;
|
|
||||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
|
||||||
|
|
||||||
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
|
||||||
ASSERT_ALLOC( a, bytes );
|
|
||||||
ASSERT_ALLOC( b, bytes );
|
|
||||||
ASSERT_ALLOC( sum, bytes );
|
|
||||||
ASSERT_ALLOC( d, bytes );
|
|
||||||
|
|
||||||
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
|
||||||
* processed by mbedtls_mpi_core_add_if()) are little endian, we can just
|
|
||||||
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
|
||||||
*/
|
|
||||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
|
||||||
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
|
|
||||||
memcpy( sum, S.p, S.n * sizeof(mbedtls_mpi_uint) );
|
|
||||||
|
|
||||||
/* The test cases have a <= b to avoid repetition, so we test a + b then,
|
|
||||||
* if a != b, b + a. If a == b, we can test when a and b are aliased */
|
|
||||||
|
|
||||||
/* a + b */
|
|
||||||
|
|
||||||
/* cond = 0 => d unchanged, no carry */
|
|
||||||
memcpy( d, a, bytes );
|
|
||||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, limbs, 0 ) );
|
|
||||||
ASSERT_COMPARE( d, bytes, a, bytes );
|
|
||||||
|
|
||||||
/* cond = 1 => correct result and carry */
|
/* cond = 1 => correct result and carry */
|
||||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, limbs, 1 ) );
|
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( X, B, limbs, 1 ) );
|
||||||
ASSERT_COMPARE( d, bytes, sum, bytes );
|
ASSERT_COMPARE( X, bytes, S, bytes );
|
||||||
|
|
||||||
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
if ( memcmp( A, B, bytes ) == 0 )
|
||||||
{
|
{
|
||||||
/* a == b, so test where a and b are aliased */
|
/* A == B, so test where A and B are aliased */
|
||||||
|
|
||||||
/* cond = 0 => d unchanged, no carry */
|
/* cond = 0 => X unchanged, no carry */
|
||||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( b, b, limbs, 0 ) );
|
memcpy( X, B, bytes );
|
||||||
ASSERT_COMPARE( b, bytes, B.p, bytes );
|
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( X, B, limbs, 0 ) );
|
||||||
|
ASSERT_COMPARE( X, bytes, B, bytes );
|
||||||
|
|
||||||
/* cond = 1 => correct result and carry */
|
/* cond = 1 => correct result and carry */
|
||||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( b, b, limbs, 1 ) );
|
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( X, X, limbs, 1 ) );
|
||||||
ASSERT_COMPARE( b, bytes, sum, bytes );
|
ASSERT_COMPARE( X, bytes, S, bytes );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* a != b, so test b + a */
|
/* A != B, so test B + A */
|
||||||
|
|
||||||
/* cond = 0 => d unchanged, no carry */
|
/* cond = 0 => d unchanged, no carry */
|
||||||
memcpy( d, b, bytes );
|
memcpy( X, B, bytes );
|
||||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, limbs, 0 ) );
|
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( X, A, limbs, 0 ) );
|
||||||
ASSERT_COMPARE( d, bytes, b, bytes );
|
ASSERT_COMPARE( X, bytes, B, bytes );
|
||||||
|
|
||||||
/* cond = 1 => correct result and carry */
|
/* cond = 1 => correct result and carry */
|
||||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, limbs, 1 ) );
|
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( X, A, limbs, 1 ) );
|
||||||
ASSERT_COMPARE( d, bytes, sum, bytes );
|
ASSERT_COMPARE( X, bytes, S, bytes );
|
||||||
}
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
mbedtls_free( a );
|
mbedtls_free( A );
|
||||||
mbedtls_free( b );
|
mbedtls_free( B );
|
||||||
mbedtls_free( sum );
|
mbedtls_free( S );
|
||||||
mbedtls_free( d );
|
mbedtls_free( X );
|
||||||
|
|
||||||
mbedtls_mpi_free( &S );
|
|
||||||
mbedtls_mpi_free( &A );
|
|
||||||
mbedtls_mpi_free( &B );
|
|
||||||
}
|
}
|
||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user