mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-11-04 04:32:24 -05:00 
			
		
		
		
	Overhaul testing of mbedtls_mpi_swap
Similarly to "Overhaul testing of mbedtls_mpi_copy", simplify the code to test mbedtls_mpi_swap to have just one function for distinct MPIs and one function for swapping an MPI with itself, covering all cases of size (0, 1, >1) and sign (>0, <0). The test cases are exactly the same as for mbedtls_mpi_copy with the following replacements: * `Copy` -> `Swap` * ` to ` -> ` with ` * `_copy` -> `_swap` Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
		
							parent
							
								
									90ec8e857c
								
							
						
					
					
						commit
						fc1eeefc99
					
				@ -436,43 +436,130 @@ mpi_copy_self:"0"
 | 
			
		||||
Copy self: zero (null)
 | 
			
		||||
mpi_copy_self:""
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with positive (1 limb)
 | 
			
		||||
mbedtls_mpi_swap_sint:0:1500
 | 
			
		||||
Swap large negative with large negative
 | 
			
		||||
mbedtls_mpi_swap:"-ca5cadedb01dfaceacc01ade":"-face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with negative (1 limb)
 | 
			
		||||
mbedtls_mpi_swap_sint:0:-1500
 | 
			
		||||
Swap large negative with large positive
 | 
			
		||||
mbedtls_mpi_swap:"-ca5cadedb01dfaceacc01ade":"face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap positive (1 limb) with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap_sint:1500:0
 | 
			
		||||
Swap large negative with small negative
 | 
			
		||||
mbedtls_mpi_swap:"-ca5cadedb01dfaceacc01ade":"-beef"
 | 
			
		||||
 | 
			
		||||
Swap negative (1 limb) with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap_sint:-1500:0
 | 
			
		||||
Swap large negative with small positive
 | 
			
		||||
mbedtls_mpi_swap:"-ca5cadedb01dfaceacc01ade":"beef"
 | 
			
		||||
 | 
			
		||||
Swap positive (1 limb) with negative (1 limb)
 | 
			
		||||
mbedtls_mpi_swap_sint:1500:-42
 | 
			
		||||
Swap large negative with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap:"-ca5cadedb01dfaceacc01ade":"0"
 | 
			
		||||
 | 
			
		||||
Swap negative (1 limb) with positive (1 limb)
 | 
			
		||||
mbedtls_mpi_swap_sint:-42:1500
 | 
			
		||||
Swap large negative with zero (null)
 | 
			
		||||
mbedtls_mpi_swap:"-ca5cadedb01dfaceacc01ade":""
 | 
			
		||||
 | 
			
		||||
Swap large positive with large negative
 | 
			
		||||
mbedtls_mpi_swap:"ca5cadedb01dfaceacc01ade":"-face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap large positive with large positive
 | 
			
		||||
mbedtls_mpi_swap:"ca5cadedb01dfaceacc01ade":"face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap large positive with small negative
 | 
			
		||||
mbedtls_mpi_swap:"ca5cadedb01dfaceacc01ade":"-beef"
 | 
			
		||||
 | 
			
		||||
Swap large positive with small positive
 | 
			
		||||
mbedtls_mpi_swap:"ca5cadedb01dfaceacc01ade":"beef"
 | 
			
		||||
 | 
			
		||||
Swap large positive with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap:"ca5cadedb01dfaceacc01ade":"0"
 | 
			
		||||
 | 
			
		||||
Swap large positive with zero (null)
 | 
			
		||||
mbedtls_mpi_swap:"ca5cadedb01dfaceacc01ade":""
 | 
			
		||||
 | 
			
		||||
Swap small negative with large negative
 | 
			
		||||
mbedtls_mpi_swap:"-bead":"-face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap small negative with large positive
 | 
			
		||||
mbedtls_mpi_swap:"-bead":"face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap small negative with small negative
 | 
			
		||||
mbedtls_mpi_swap:"-bead":"-beef"
 | 
			
		||||
 | 
			
		||||
Swap small negative with small positive
 | 
			
		||||
mbedtls_mpi_swap:"-bead":"beef"
 | 
			
		||||
 | 
			
		||||
Swap small negative with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap:"-bead":"0"
 | 
			
		||||
 | 
			
		||||
Swap small negative with zero (null)
 | 
			
		||||
mbedtls_mpi_swap:"-bead":""
 | 
			
		||||
 | 
			
		||||
Swap small positive with large negative
 | 
			
		||||
mbedtls_mpi_swap:"bead":"-face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap small positive with large positive
 | 
			
		||||
mbedtls_mpi_swap:"bead":"face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap small positive with small negative
 | 
			
		||||
mbedtls_mpi_swap:"bead":"-beef"
 | 
			
		||||
 | 
			
		||||
Swap small positive with small positive
 | 
			
		||||
mbedtls_mpi_swap:"bead":"beef"
 | 
			
		||||
 | 
			
		||||
Swap small positive with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap:"bead":"0"
 | 
			
		||||
 | 
			
		||||
Swap small positive with zero (null)
 | 
			
		||||
mbedtls_mpi_swap:"bead":""
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with large negative
 | 
			
		||||
mbedtls_mpi_swap:"0":"-face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with large positive
 | 
			
		||||
mbedtls_mpi_swap:"0":"face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with small negative
 | 
			
		||||
mbedtls_mpi_swap:"0":"-beef"
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with small positive
 | 
			
		||||
mbedtls_mpi_swap:"0":"beef"
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap:"0":"0"
 | 
			
		||||
 | 
			
		||||
Swap zero (1 limb) with zero (null)
 | 
			
		||||
mbedtls_mpi_swap:"0":""
 | 
			
		||||
 | 
			
		||||
Swap zero (null) with large negative
 | 
			
		||||
mbedtls_mpi_swap:"":"-face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap zero (null) with large positive
 | 
			
		||||
mbedtls_mpi_swap:"":"face1e55ca11ab1ecab005e5"
 | 
			
		||||
 | 
			
		||||
Swap zero (null) with small negative
 | 
			
		||||
mbedtls_mpi_swap:"":"-beef"
 | 
			
		||||
 | 
			
		||||
Swap zero (null) with small positive
 | 
			
		||||
mbedtls_mpi_swap:"":"beef"
 | 
			
		||||
 | 
			
		||||
Swap zero (null) with zero (1 limb)
 | 
			
		||||
mbedtls_mpi_swap:"":"0"
 | 
			
		||||
 | 
			
		||||
Swap zero (null) with zero (null)
 | 
			
		||||
mbedtls_mpi_swap_binary:"":""
 | 
			
		||||
mbedtls_mpi_swap:"":""
 | 
			
		||||
 | 
			
		||||
Swap zero (null) with positive (1 limb)
 | 
			
		||||
mbedtls_mpi_swap_binary:"":"1234"
 | 
			
		||||
Swap self: large negative
 | 
			
		||||
mpi_swap_self:"-ca5cadedb01dfaceacc01ade"
 | 
			
		||||
 | 
			
		||||
Swap positive (1 limb) with zero (null)
 | 
			
		||||
mbedtls_mpi_swap_binary:"1234":""
 | 
			
		||||
Swap self: large positive
 | 
			
		||||
mpi_swap_self:"ca5cadedb01dfaceacc01ade"
 | 
			
		||||
 | 
			
		||||
Swap positive with larger
 | 
			
		||||
mbedtls_mpi_swap_binary:"bead":"ca5cadedb01dfaceacc01ade"
 | 
			
		||||
Swap self: small negative
 | 
			
		||||
mpi_swap_self:"-bead"
 | 
			
		||||
 | 
			
		||||
Swap positive with smaller
 | 
			
		||||
mbedtls_mpi_swap_binary:"ca5cadedb01dfaceacc01ade":"bead"
 | 
			
		||||
Swap self: small positive
 | 
			
		||||
mpi_swap_self:"bead"
 | 
			
		||||
 | 
			
		||||
Swap self: 1 limb
 | 
			
		||||
mpi_swap_self:"face"
 | 
			
		||||
Swap self: zero (1 limb)
 | 
			
		||||
mpi_swap_self:"0"
 | 
			
		||||
 | 
			
		||||
Swap self: null
 | 
			
		||||
Swap self: zero (null)
 | 
			
		||||
mpi_swap_self:""
 | 
			
		||||
 | 
			
		||||
Shrink 2 limbs in a buffer of size 2 to 4
 | 
			
		||||
 | 
			
		||||
@ -541,6 +541,48 @@ exit:
 | 
			
		||||
}
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
 | 
			
		||||
/* BEGIN_CASE */
 | 
			
		||||
void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
 | 
			
		||||
{
 | 
			
		||||
    mbedtls_mpi X, Y, X0, Y0;
 | 
			
		||||
    mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
 | 
			
		||||
    mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_test_read_mpi( &X,  16, X_hex ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_test_read_mpi( &Y,  16, Y_hex ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_test_read_mpi( &Y0, 16, Y_hex ) == 0 );
 | 
			
		||||
 | 
			
		||||
    mbedtls_mpi_swap( &X, &Y );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &X ) );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &Y ) );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
 | 
			
		||||
    mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
 | 
			
		||||
}
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
 | 
			
		||||
/* BEGIN_CASE */
 | 
			
		||||
void mpi_swap_self( char *X_hex )
 | 
			
		||||
{
 | 
			
		||||
    mbedtls_mpi X, X0;
 | 
			
		||||
    mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_test_read_mpi( &X,  16, X_hex ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 );
 | 
			
		||||
 | 
			
		||||
    mbedtls_mpi_swap( &X, &X );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &X ) );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
 | 
			
		||||
}
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
 | 
			
		||||
/* BEGIN_CASE */
 | 
			
		||||
void mbedtls_mpi_shrink( int before, int used, int min, int after )
 | 
			
		||||
{
 | 
			
		||||
@ -619,70 +661,6 @@ exit:
 | 
			
		||||
}
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
 | 
			
		||||
/* BEGIN_CASE */
 | 
			
		||||
void mbedtls_mpi_swap_sint( int input_X, int input_Y )
 | 
			
		||||
{
 | 
			
		||||
    mbedtls_mpi X, Y;
 | 
			
		||||
    mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
 | 
			
		||||
 | 
			
		||||
    mbedtls_mpi_swap( &X, &Y );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &X ) );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &Y ) );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
 | 
			
		||||
}
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
 | 
			
		||||
/* BEGIN_CASE */
 | 
			
		||||
void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
 | 
			
		||||
{
 | 
			
		||||
    mbedtls_mpi X, Y, X0, Y0;
 | 
			
		||||
    mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
 | 
			
		||||
    mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
 | 
			
		||||
 | 
			
		||||
    mbedtls_mpi_swap( &X, &Y );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &X ) );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &Y ) );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
 | 
			
		||||
    mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
 | 
			
		||||
}
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
 | 
			
		||||
/* BEGIN_CASE */
 | 
			
		||||
void mpi_swap_self( data_t *input_X )
 | 
			
		||||
{
 | 
			
		||||
    mbedtls_mpi X, X0;
 | 
			
		||||
    mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
 | 
			
		||||
 | 
			
		||||
    mbedtls_mpi_swap( &X, &X );
 | 
			
		||||
    TEST_ASSERT( sign_is_valid( &X ) );
 | 
			
		||||
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
 | 
			
		||||
}
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
 | 
			
		||||
/* BEGIN_CASE */
 | 
			
		||||
void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
 | 
			
		||||
                          char * input_Y, int radix_A, char * input_A )
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user