Extend the unit tests for mbedtls_mpi_core_add_if() to also test mbedtls_mpi_core_add()

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
Tom Cosgrove 2022-10-25 12:45:50 +01:00
parent af7d44b4d2
commit eee0d6ce6b
2 changed files with 53 additions and 19 deletions

View File

@ -144,12 +144,12 @@ 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(BignumCoreOperationArchSplit):
"""Test cases for bignum core add if."""
class BignumCoreAddAndAddIf(BignumCoreOperationArchSplit):
"""Test cases for bignum core add and add-if."""
count = 0
symbol = "+"
test_function = "mpi_core_add_if"
test_name = "mbedtls_mpi_core_add_if"
test_function = "mpi_core_add_and_add_if"
test_name = "mpi_core_add_and_add_if"
def result(self) -> List[str]:
result = self.int_a + self.int_b

View File

@ -359,41 +359,52 @@ exit:
/* END_CASE */
/* BEGIN_CASE */
void mpi_core_add_if( char * input_A, char * input_B,
void mpi_core_add_and_add_if( char * input_A, char * input_B,
char * input_S, int carry )
{
mbedtls_mpi_uint *A = NULL; /* first value to add */
size_t A_limbs;
mbedtls_mpi_uint *B = NULL; /* second value to add */
size_t B_limbs;
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;
size_t A_limbs, B_limbs, S_limbs;
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &A, &A_limbs, input_A ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &B, &B_limbs, input_B ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &S, &S_limbs, input_S ) );
X_limbs = S_limbs;
ASSERT_ALLOC( X, X_limbs );
/* add_if expects all operands to be the same length */
/* add and add_if expect all operands to be the same length */
TEST_EQUAL( A_limbs, B_limbs );
TEST_EQUAL( A_limbs, S_limbs );
size_t limbs = A_limbs;
size_t bytes = limbs * sizeof( *A );
ASSERT_ALLOC( X, limbs );
/* 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 => X unchanged, no carry */
/* add => correct result and carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, A, B, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add, alias output and first operand => correct result and carry */
memcpy( X, A, bytes );
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, X, B, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add, alias output and second operand => correct result and carry */
memcpy( X, B, bytes );
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, A, X, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add_if: cond = 0 => X unchanged, no carry */
memcpy( X, A, bytes );
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( X, B, limbs, 0 ) );
ASSERT_COMPARE( X, bytes, A, bytes );
/* cond = 1 => correct result and carry */
/* add_if: cond = 1 => correct result and carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( X, B, limbs, 1 ) );
ASSERT_COMPARE( X, bytes, S, bytes );
@ -401,12 +412,21 @@ void mpi_core_add_if( char * input_A, char * input_B,
{
/* A == B, so test where A and B are aliased */
/* cond = 0 => X unchanged, no carry */
/* add => correct result and carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, A, A, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add, output aliased to both operands => correct result and carry */
memcpy( X, A, bytes );
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, X, X, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add_if: cond = 0 => X unchanged, no carry */
memcpy( X, B, bytes );
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( X, X, limbs, 0 ) );
ASSERT_COMPARE( X, bytes, B, bytes );
/* cond = 1 => correct result and carry */
/* add_if: cond = 1 => correct result and carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( X, X, limbs, 1 ) );
ASSERT_COMPARE( X, bytes, S, bytes );
}
@ -414,12 +434,26 @@ void mpi_core_add_if( char * input_A, char * input_B,
{
/* A != B, so test B + A */
/* cond = 0 => d unchanged, no carry */
/* add => correct result and carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, B, A, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add, alias output and first operand => correct result and carry */
memcpy( X, B, bytes );
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, X, A, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add, alias output and second operand => correct result and carry */
memcpy( X, A, bytes );
TEST_EQUAL( carry, mbedtls_mpi_core_add( X, B, X, limbs ) );
ASSERT_COMPARE( X, bytes, S, bytes );
/* add_if: cond = 0 => d unchanged, no carry */
memcpy( X, B, bytes );
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( X, A, limbs, 0 ) );
ASSERT_COMPARE( X, bytes, B, bytes );
/* cond = 1 => correct result and carry */
/* add_if: cond = 1 => correct result and carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( X, A, limbs, 1 ) );
ASSERT_COMPARE( X, bytes, S, bytes );
}