Refactor mpi_core_sub tests to use arch_split

Tests are refactored to generate separate cases for 32-bit and 64-bit
limbs using arch_split. Duplicate arguments and branching in the test
function is removed.

Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
Werner Lewis 2022-12-12 17:04:16 +00:00
parent a9ac61203b
commit 91a2aabb86
2 changed files with 12 additions and 24 deletions

View File

@ -130,24 +130,20 @@ class BignumCoreAddAndAddIf(BignumCoreTarget, bignum_common.OperationCommon):
class BignumCoreSub(BignumCoreTarget, bignum_common.OperationCommon): class BignumCoreSub(BignumCoreTarget, bignum_common.OperationCommon):
"""Test cases for bignum core sub.""" """Test cases for bignum core sub."""
count = 0 count = 0
input_style = "arch_split"
symbol = "-" symbol = "-"
test_function = "mpi_core_sub" test_function = "mpi_core_sub"
test_name = "mbedtls_mpi_core_sub" test_name = "mbedtls_mpi_core_sub"
def result(self) -> List[str]: def result(self) -> List[str]:
if self.int_a >= self.int_b: if self.int_a >= self.int_b:
result_4 = result_8 = self.int_a - self.int_b result = self.int_a - self.int_b
carry = 0 carry = 0
else: else:
bound_val = max(self.int_a, self.int_b) result = self.limb_boundary + self.int_a - self.int_b
bound_4 = bignum_common.bound_mpi(bound_val, 32)
result_4 = bound_4 + self.int_a - self.int_b
bound_8 = bignum_common.bound_mpi(bound_val, 64)
result_8 = bound_8 + self.int_a - self.int_b
carry = 1 carry = 1
return [ return [
"\"{:x}\"".format(result_4), self.format_result(result),
"\"{:x}\"".format(result_8),
str(carry) str(carry)
] ]

View File

@ -533,10 +533,9 @@ exit:
/* BEGIN_CASE */ /* BEGIN_CASE */
void mpi_core_sub( char * input_A, char * input_B, void mpi_core_sub( char * input_A, char * input_B,
char * input_X4, char * input_X8, char * input_X, int carry )
int carry )
{ {
mbedtls_mpi A, B, X4, X8; mbedtls_mpi A, B, X;
mbedtls_mpi_uint *a = NULL; mbedtls_mpi_uint *a = NULL;
mbedtls_mpi_uint *b = NULL; mbedtls_mpi_uint *b = NULL;
mbedtls_mpi_uint *x = NULL; /* expected */ mbedtls_mpi_uint *x = NULL; /* expected */
@ -544,29 +543,23 @@ void mpi_core_sub( char * input_A, char * input_B,
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &A );
mbedtls_mpi_init( &B ); mbedtls_mpi_init( &B );
mbedtls_mpi_init( &X4 ); mbedtls_mpi_init( &X );
mbedtls_mpi_init( &X8 );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) ); TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) ); TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) ); TEST_EQUAL( 0, mbedtls_test_read_mpi( &X, input_X ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
/* All of the inputs are +ve (or zero) */ /* All of the inputs are +ve (or zero) */
TEST_EQUAL( 1, A.s ); TEST_EQUAL( 1, A.s );
TEST_EQUAL( 1, B.s ); TEST_EQUAL( 1, B.s );
TEST_EQUAL( 1, X4.s ); TEST_EQUAL( 1, X.s );
TEST_EQUAL( 1, X8.s );
/* Get the number of limbs we will need */ /* Get the number of limbs we will need */
size_t limbs = MAX( A.n, B.n ); size_t limbs = MAX( A.n, B.n );
size_t bytes = limbs * sizeof(mbedtls_mpi_uint); size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
/* We only need to work with X4 or X8, depending on sizeof(mbedtls_mpi_uint) */
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
/* The result shouldn't have more limbs than the longest input */ /* The result shouldn't have more limbs than the longest input */
TEST_LE_U( X->n, limbs ); TEST_LE_U( X.n, limbs );
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */ /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
@ -582,7 +575,7 @@ void mpi_core_sub( char * input_A, char * input_B,
*/ */
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) ); memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) ); memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) ); memcpy( x, X.p, X.n * sizeof(mbedtls_mpi_uint) );
/* 1a) r = a - b => we should get the correct carry */ /* 1a) r = a - b => we should get the correct carry */
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) ); TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) );
@ -621,8 +614,7 @@ exit:
mbedtls_mpi_free( &A ); mbedtls_mpi_free( &A );
mbedtls_mpi_free( &B ); mbedtls_mpi_free( &B );
mbedtls_mpi_free( &X4 ); mbedtls_mpi_free( &X );
mbedtls_mpi_free( &X8 );
} }
/* END_CASE */ /* END_CASE */