mpi_core_add_if: simplify tests

Use the new, limb size aware base class to generate tests for
mpi_core_add_if().

Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
Janos Follath 2022-10-17 10:25:29 +01:00
parent dd2e4683d0
commit e153a715f0
2 changed files with 16 additions and 28 deletions

View file

@ -91,7 +91,7 @@ class BignumCoreOperationArchSplit(BignumCoreOperation):
yield cls(a_value, b_value, 32).create_test_case()
yield cls(a_value, b_value, 64).create_test_case()
class BignumCoreAddIf(BignumCoreOperation):
class BignumCoreAddIf(BignumCoreOperationArchSplit):
"""Test cases for bignum core add if."""
count = 0
symbol = "+"
@ -99,17 +99,15 @@ class BignumCoreAddIf(BignumCoreOperation):
test_name = "mbedtls_mpi_core_add_if"
def result(self) -> List[str]:
tmp = self.int_a + self.int_b
result = self.int_a + self.int_b
bound_val = max(self.int_a, self.int_b)
bound_4 = bignum_common.bound_mpi(bound_val, 32)
bound_8 = bignum_common.bound_mpi(bound_val, 64)
carry_4, remainder_4 = divmod(tmp, bound_4)
carry_8, remainder_8 = divmod(tmp, bound_8)
bound = bignum_common.bound_mpi(bound_val, self.bits_in_limb)
carry, result = divmod(result, bound)
return [
"\"{:x}\"".format(remainder_4),
str(carry_4),
"\"{:x}\"".format(remainder_8),
str(carry_8)
"\"{:x}\"".format(result),
str(carry)
]

View file

@ -340,10 +340,9 @@ exit:
/* BEGIN_CASE */
void mpi_core_add_if( char * input_A, char * input_B,
char * input_S4, int carry4,
char * input_S8, int carry8 )
char * input_S, int carry )
{
mbedtls_mpi S4, S8, A, B;
mbedtls_mpi S, A, B;
mbedtls_mpi_uint *a = NULL; /* first value to add */
mbedtls_mpi_uint *b = NULL; /* second value to add */
mbedtls_mpi_uint *sum = NULL;
@ -351,28 +350,20 @@ void mpi_core_add_if( char * input_A, char * input_B,
mbedtls_mpi_init( &A );
mbedtls_mpi_init( &B );
mbedtls_mpi_init( &S4 );
mbedtls_mpi_init( &S8 );
mbedtls_mpi_init( &S );
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( &S4, input_S4 ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S8, input_S8 ) );
/* We only need to work with one of (S4, carry4) or (S8, carry8) depending
* on sizeof(mbedtls_mpi_uint)
*/
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &S4 : &S8;
mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8;
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) );
/* All of the inputs are +ve (or zero) */
TEST_EQUAL( 1, A.s );
TEST_EQUAL( 1, B.s );
TEST_EQUAL( 1, X->s );
TEST_EQUAL( 1, S.s );
/* Test cases are such that A <= B, so #limbs should be <= */
TEST_LE_U( A.n, B.n );
TEST_LE_U( X->n, B.n );
TEST_LE_U( S.n, B.n );
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
@ -400,7 +391,7 @@ void mpi_core_add_if( char * input_A, char * input_B,
*/
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
memcpy( sum, X->p, X->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 */
@ -448,8 +439,7 @@ exit:
mbedtls_free( sum );
mbedtls_free( d );
mbedtls_mpi_free( &S4 );
mbedtls_mpi_free( &S8 );
mbedtls_mpi_free( &S );
mbedtls_mpi_free( &A );
mbedtls_mpi_free( &B );
}