Rename variables and update comments in mpi_core_add_if test

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
Tom Cosgrove 2022-09-02 10:46:09 +01:00
parent 1b2947a614
commit eceb4ccfc3

View File

@ -1721,23 +1721,23 @@ exit:
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mpi_core_add_if( char * input_l, char * input_r, void mpi_core_add_if( char * input_A, char * input_B,
char * input_X4, int carry4, char * input_X4, int carry4,
char * input_X8, int carry8 ) char * input_X8, int carry8 )
{ {
mbedtls_mpi X4, X8, l, r; mbedtls_mpi X4, X8, A, B;
mbedtls_mpi_uint *la = NULL; mbedtls_mpi_uint *a = NULL; /* first value to add */
mbedtls_mpi_uint *ra = NULL; mbedtls_mpi_uint *b = NULL; /* second value to add */
mbedtls_mpi_uint *Xa = NULL; mbedtls_mpi_uint *x = NULL; /* expected */
mbedtls_mpi_uint *da = NULL; mbedtls_mpi_uint *d = NULL; /* destination - the in/out first op */
mbedtls_mpi_init( &A );
mbedtls_mpi_init( &B );
mbedtls_mpi_init( &X4 ); mbedtls_mpi_init( &X4 );
mbedtls_mpi_init( &X8 ); mbedtls_mpi_init( &X8 );
mbedtls_mpi_init( &l );
mbedtls_mpi_init( &r );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &l, input_l ) ); TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &r, input_r ) ); 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( &X4, input_X4 ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) ); TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
@ -1748,71 +1748,81 @@ void mpi_core_add_if( char * input_l, char * input_r,
mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8; mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8;
/* All of the inputs are +ve (or zero) */ /* All of the inputs are +ve (or zero) */
TEST_EQUAL( 1, l.s ); TEST_EQUAL( 1, A.s );
TEST_EQUAL( 1, r.s ); TEST_EQUAL( 1, B.s );
TEST_EQUAL( 1, X->s ); TEST_EQUAL( 1, X->s );
/* Test cases are such that l <= r, so #limbs should be <= */ /* Test cases are such that A <= B, so #limbs should be <= */
TEST_ASSERT( l.n <= r.n ); TEST_ASSERT( A.n <= B.n );
TEST_ASSERT( X->n <= r.n ); TEST_ASSERT( X->n <= B.n );
/* 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 */
la = mbedtls_calloc( r.n, sizeof(mbedtls_mpi_uint) );
ra = mbedtls_calloc( r.n, sizeof(mbedtls_mpi_uint) );
Xa = mbedtls_calloc( r.n, sizeof(mbedtls_mpi_uint) );
da = mbedtls_calloc( r.n, sizeof(mbedtls_mpi_uint) );
size_t bytes = r.n * sizeof(mbedtls_mpi_uint); /* mbedtls_mpi_core_add_if() uses input arrays of mbedtls_mpi_uints which
* must be the same size. The MPIs we've read in will only have arrays
* 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.
*/
a = mbedtls_calloc( B.n, sizeof(mbedtls_mpi_uint) );
b = mbedtls_calloc( B.n, sizeof(mbedtls_mpi_uint) );
x = mbedtls_calloc( B.n, sizeof(mbedtls_mpi_uint) );
d = mbedtls_calloc( B.n, sizeof(mbedtls_mpi_uint) );
TEST_ASSERT( la != NULL ); size_t bytes = B.n * sizeof(mbedtls_mpi_uint);
TEST_ASSERT( ra != NULL );
TEST_ASSERT( Xa != NULL ); TEST_ASSERT( a != NULL );
TEST_ASSERT( da != NULL ); TEST_ASSERT( b != NULL );
TEST_ASSERT( x != NULL );
TEST_ASSERT( d != NULL );
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as /* 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 * 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 calloc()) * copy what we have as long as MSBs are 0 (which they are from calloc())
*/ */
memcpy( la, l.p, l.n * sizeof(mbedtls_mpi_uint) ); memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
memcpy( ra, r.p, r.n * sizeof(mbedtls_mpi_uint) ); memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
memcpy( Xa, X->p, X->n * sizeof(mbedtls_mpi_uint) ); memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
/* 1a) a += b, cond = 0 => there should be no carry */ /* 1a) a + b: d = a; d += b, cond = 0 => there should be no carry */
memcpy( da, la, r.n * sizeof(mbedtls_mpi_uint) ); memcpy( d, a, bytes );
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( da, ra, r.n, 0 ) ); TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, B.n, 0 ) );
/* 1b) and a should be unchanged */ /* 1b) and d should be unchanged */
ASSERT_COMPARE( da, bytes, la, bytes ); ASSERT_COMPARE( d, bytes, a, bytes );
/* 2a) a += b, cond = 1 => we should get the correct carry */ /* 2a) a + b: d = a; d += b, cond = 1 => we should get the correct carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( da, ra, r.n, 1 ) ); TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, B.n, 1 ) );
/* 2b) and a should have the correct result */ /* 2b) and d should have the correct result */
ASSERT_COMPARE( da, bytes, Xa, bytes ); ASSERT_COMPARE( d, bytes, x, bytes );
/* 3a) b += a, cond = 0 => there should be no carry */ /* 3a) b + a: d = b; d += a, cond = 0 => there should be no carry */
memcpy( da, ra, r.n * sizeof(mbedtls_mpi_uint) ); memcpy( d, b, bytes );
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( da, la, r.n, 0 ) ); TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, B.n, 0 ) );
/* 3b) and b should be unchanged */ /* 3b) and d should be unchanged */
ASSERT_COMPARE( da, bytes, ra, bytes ); ASSERT_COMPARE( d, bytes, b, bytes );
/* 4a) b += a, cond = 1 => we should get the correct carry */ /* 4a) b + a: d = b; d += a, cond = 1 => we should get the correct carry */
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( da, la, r.n, 1 ) ); TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, B.n, 1 ) );
/* 4b) and b should have the correct result */ /* 4b) and d should have the correct result */
ASSERT_COMPARE( da, bytes, Xa, bytes ); ASSERT_COMPARE( d, bytes, x, bytes );
exit: exit:
mbedtls_free( la ); mbedtls_free( a );
mbedtls_free( ra ); mbedtls_free( b );
mbedtls_free( Xa ); mbedtls_free( x );
mbedtls_free( da ); mbedtls_free( d );
mbedtls_mpi_free( &X4 ); mbedtls_mpi_free( &X4 );
mbedtls_mpi_free( &X8 ); mbedtls_mpi_free( &X8 );
mbedtls_mpi_free( &l ); mbedtls_mpi_free( &A );
mbedtls_mpi_free( &r ); mbedtls_mpi_free( &B );
} }
/* END_CASE */ /* END_CASE */