2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2009-06-28 21:50:27 +00:00
|
|
|
#include <polarssl/bignum.h>
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:POLARSSL_BIGNUM_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_read_write_string( int radix_X, char *input_X, int radix_A,
|
|
|
|
char *input_A, int output_size, int result_read,
|
|
|
|
int result_write )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
|
|
|
char str[1000];
|
2013-08-20 09:48:36 +00:00
|
|
|
size_t len = output_size;
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == result_read );
|
|
|
|
if( result_read == 0 )
|
2009-07-12 11:01:32 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_write_string( &X, radix_A, str, &len ) == result_write );
|
|
|
|
if( result_write == 0 )
|
2009-07-12 11:01:32 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
|
2009-07-12 11:01:32 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_read_binary( char *input_X, int radix_A, char *input_A )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
|
|
|
unsigned char str[1000];
|
|
|
|
unsigned char buf[1000];
|
2011-04-24 15:53:29 +00:00
|
|
|
size_t len = 1000;
|
|
|
|
size_t input_len;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
input_len = unhexify( buf, input_X );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( mpi_read_binary( &X, buf, input_len ) == 0 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_write_string( &X, radix_A, (char *) str, &len ) == 0 );
|
|
|
|
TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_write_binary( int radix_X, char *input_X, char *input_A,
|
|
|
|
int output_size, int result )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
|
|
|
unsigned char str[1000];
|
|
|
|
unsigned char buf[1000];
|
2011-04-24 15:53:29 +00:00
|
|
|
size_t buflen;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset( buf, 0x00, 1000 );
|
|
|
|
memset( str, 0x00, 1000 );
|
|
|
|
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2009-07-11 19:15:20 +00:00
|
|
|
buflen = mpi_size( &X );
|
2013-08-20 09:48:36 +00:00
|
|
|
if( buflen > (size_t) output_size )
|
|
|
|
buflen = (size_t) output_size;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_write_binary( &X, buf, buflen ) == result );
|
|
|
|
if( result == 0)
|
2009-07-12 11:01:32 +00:00
|
|
|
{
|
|
|
|
hexify( str, buf, buflen );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
|
2009-07-12 11:01:32 +00:00
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-09-15 13:20:37 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
|
2013-08-20 09:48:36 +00:00
|
|
|
void mpi_read_file( int radix_X, char *input_file, char *input_A,
|
|
|
|
int result )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
|
|
|
unsigned char str[1000];
|
|
|
|
unsigned char buf[1000];
|
2011-04-24 15:53:29 +00:00
|
|
|
size_t buflen;
|
2009-07-11 19:15:20 +00:00
|
|
|
FILE *file;
|
2009-07-06 06:40:23 +00:00
|
|
|
|
|
|
|
memset( buf, 0x00, 1000 );
|
|
|
|
memset( str, 0x00, 1000 );
|
|
|
|
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
file = fopen( input_file, "r" );
|
|
|
|
TEST_ASSERT( mpi_read_file( &X, radix_X, file ) == result );
|
2009-07-06 06:40:23 +00:00
|
|
|
fclose(file);
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( result == 0 )
|
2009-07-12 11:01:32 +00:00
|
|
|
{
|
|
|
|
buflen = mpi_size( &X );
|
|
|
|
TEST_ASSERT( mpi_write_binary( &X, buf, buflen ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2009-07-12 11:01:32 +00:00
|
|
|
hexify( str, buf, buflen );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
|
2009-07-12 11:01:32 +00:00
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-09-15 13:20:37 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
|
2013-08-20 09:48:36 +00:00
|
|
|
void mpi_write_file( int radix_X, char *input_X, int output_radix,
|
|
|
|
char *output_file )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
mpi X, Y;
|
2009-07-11 19:15:20 +00:00
|
|
|
FILE *file_out, *file_in;
|
|
|
|
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
file_out = fopen( output_file, "w" );
|
2011-05-26 13:16:06 +00:00
|
|
|
TEST_ASSERT( file_out != NULL );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_write_file( NULL, &X, output_radix, file_out ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
fclose(file_out);
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
file_in = fopen( output_file, "r" );
|
2011-05-26 13:16:06 +00:00
|
|
|
TEST_ASSERT( file_in != NULL );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_file( &Y, output_radix, file_in ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
fclose(file_in);
|
|
|
|
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_get_bit( int radix_X, char *input_X, int pos, int val )
|
2011-05-18 15:47:11 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
|
|
|
mpi_init( &X );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_get_bit( &X, pos ) == val );
|
2011-05-18 15:47:11 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-05-18 15:47:11 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_set_bit( int radix_X, char *input_X, int pos, int val, int radix_Y,
|
|
|
|
char *output_Y )
|
2011-05-18 15:47:11 +00:00
|
|
|
{
|
|
|
|
mpi X, Y;
|
|
|
|
mpi_init( &X ); mpi_init( &Y );
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_set_bit( &X, pos, val ) == 0 );
|
2011-05-18 15:47:11 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
|
|
|
|
|
2012-11-17 11:42:51 +00:00
|
|
|
mpi_free( &X ); mpi_free( &Y );
|
2011-05-18 15:47:11 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-05-18 15:47:11 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_lsb( int radix_X, char *input_X, int nr_bits )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lsb( &X ) == (size_t) nr_bits );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_msb( int radix_X, char *input_X, int nr_bits )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_msb( &X ) == (size_t) nr_bits );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_gcd( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi A, X, Y, Z;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &A ); mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_gcd( &Z, &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &A ); mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_cmp_int( int input_X, int input_A, int result_CMP )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_lset( &X, input_X ) == 0);
|
|
|
|
TEST_ASSERT( mpi_cmp_int( &X, input_A ) == result_CMP);
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_cmp_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == input_A );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_abs( &X, &Y ) == input_A );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_copy( int input_X, int input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_lset( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &Y, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) != 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_copy( &Y, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) != 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_copy_self( int input_X )
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_lset( &X, input_X ) == 0 );
|
2009-07-06 06:40:23 +00:00
|
|
|
TEST_ASSERT( mpi_copy( &X, &X ) == 0 );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_int( &X, input_X ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-07-06 06:40:23 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-06 06:40:23 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_swap( int input_X, int input_Y )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_lset( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_lset( &A, input_X ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) != 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
|
|
|
|
mpi_swap( &X, &Y );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) != 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, Z, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_add_mpi( &Z, &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, Z, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_add_abs( &Z, &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
|
|
|
|
char *input_Y, int radix_A, char *input_A )
|
2009-07-12 11:01:32 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
|
2009-07-12 11:01:32 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-07-12 11:01:32 +00:00
|
|
|
TEST_ASSERT( mpi_add_abs( &X, &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
|
2009-07-12 11:01:32 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-12 11:01:32 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
|
|
|
|
char *input_Y, int radix_A, char *input_A )
|
2009-07-12 11:01:32 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
|
2009-07-12 11:01:32 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-07-12 11:01:32 +00:00
|
|
|
TEST_ASSERT( mpi_add_abs( &Y, &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Y, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
|
2009-07-12 11:01:32 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-07-12 11:01:32 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
|
|
|
char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Z, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_add_int( &Z, &X, input_Y ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, Z, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_sub_mpi( &Z, &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A, int sub_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, Z, A;
|
|
|
|
int res;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
res = mpi_sub_abs( &Z, &X, &Y );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( res == sub_result );
|
2009-06-28 21:50:27 +00:00
|
|
|
if( res == 0 )
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
|
|
|
char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Z, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_sub_int( &Z, &X, input_Y ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, Z, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_mul_mpi( &Z, &X, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
|
|
|
char *input_A, char *result_comparison )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Z, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_mul_int( &Z, &X, input_Y ) == 0 );
|
2013-08-16 11:38:47 +00:00
|
|
|
if( strcmp( result_comparison, "==" ) == 0 )
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
|
|
|
else if( strcmp( result_comparison, "!=" ) == 0 )
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) != 0 );
|
|
|
|
else
|
|
|
|
TEST_ASSERT( "unknown operator" == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A, int radix_B, char *input_B,
|
|
|
|
int div_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, Q, R, A, B;
|
|
|
|
int res;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Q ); mpi_init( &R );
|
|
|
|
mpi_init( &A ); mpi_init( &B );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &B, radix_B, input_B ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
res = mpi_div_mpi( &Q, &R, &X, &Y );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 21:50:27 +00:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Q, &A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R, &B ) == 0 );
|
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Q ); mpi_free( &R );
|
|
|
|
mpi_free( &A ); mpi_free( &B );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
|
|
|
char *input_A, int radix_B, char *input_B, int div_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Q, R, A, B;
|
|
|
|
int res;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Q ); mpi_init( &R ); mpi_init( &A );
|
|
|
|
mpi_init( &B );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &B, radix_B, input_B ) == 0 );
|
|
|
|
res = mpi_div_int( &Q, &R, &X, input_Y );
|
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 21:50:27 +00:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Q, &A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &R, &B ) == 0 );
|
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Q ); mpi_free( &R ); mpi_free( &A );
|
|
|
|
mpi_free( &B );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A, int div_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
2012-11-13 10:25:21 +00:00
|
|
|
mpi X, Y, A;
|
2009-06-28 21:50:27 +00:00
|
|
|
int res;
|
2012-11-13 10:25:21 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2012-11-13 10:25:21 +00:00
|
|
|
res = mpi_mod_mpi( &X, &X, &Y );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 21:50:27 +00:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2012-11-13 10:25:21 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
2012-11-13 10:25:21 +00:00
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
|
|
|
|
int div_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
|
|
|
int res;
|
2011-04-24 09:11:17 +00:00
|
|
|
t_uint r;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
res = mpi_mod_int( &r, &X, input_Y );
|
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 21:50:27 +00:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( r == (t_uint) input_A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
|
|
|
|
int radix_N, char *input_N, int radix_RR, char *input_RR,
|
|
|
|
int radix_X, char *input_X, int div_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi A, E, N, RR, Z, X;
|
|
|
|
int res;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &A ); mpi_init( &E ); mpi_init( &N );
|
|
|
|
mpi_init( &RR ); mpi_init( &Z ); mpi_init( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &E, radix_E, input_E ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &N, radix_N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
if( strlen( input_RR ) )
|
|
|
|
TEST_ASSERT( mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
|
|
|
res = mpi_exp_mod( &Z, &A, &E, &N, &RR );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 21:50:27 +00:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &X ) == 0 );
|
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &A ); mpi_free( &E ); mpi_free( &N );
|
|
|
|
mpi_free( &RR ); mpi_free( &Z ); mpi_free( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
|
|
|
int radix_A, char *input_A, int div_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, Y, Z, A;
|
|
|
|
int res;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
res = mpi_inv_mod( &Z, &X, &Y );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 21:50:27 +00:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
|
|
|
}
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-22 16:29:07 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_GENPRIME */
|
2013-08-20 09:48:36 +00:00
|
|
|
void mpi_is_prime( int radix_X, char *input_X, int div_result )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X;
|
|
|
|
int res;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2011-05-26 13:16:06 +00:00
|
|
|
res = mpi_is_prime( &X, rnd_std_rand, NULL );
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( res == div_result );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
|
|
|
|
char *input_A)
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_shift_l( &X, shift_X ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
|
|
|
|
char *input_A )
|
2009-06-28 21:50:27 +00:00
|
|
|
{
|
|
|
|
mpi X, A;
|
2011-05-05 11:49:20 +00:00
|
|
|
mpi_init( &X ); mpi_init( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mpi_shift_r( &X, shift_X ) == 0 );
|
2009-06-28 21:50:27 +00:00
|
|
|
TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
|
2011-05-05 11:49:20 +00:00
|
|
|
|
|
|
|
mpi_free( &X ); mpi_free( &A );
|
2009-06-28 21:50:27 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2009-06-28 21:50:27 +00:00
|
|
|
|
2013-10-10 10:48:03 +00:00
|
|
|
/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
|
2013-08-20 09:48:36 +00:00
|
|
|
void mpi_selftest()
|
2009-07-06 06:40:23 +00:00
|
|
|
{
|
|
|
|
TEST_ASSERT( mpi_self_test( 0 ) == 0 );
|
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|