392 lines
11 KiB
Text
392 lines
11 KiB
Text
|
BEGIN_HEADER
|
||
|
#include <polarssl/bignum.h>
|
||
|
|
||
|
static int myrand( void *rng_state )
|
||
|
{
|
||
|
if( rng_state != NULL )
|
||
|
rng_state = NULL;
|
||
|
|
||
|
return( rand() );
|
||
|
}
|
||
|
|
||
|
END_HEADER
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_read_write_string:radix_X:input_X:radix_A:input_A
|
||
|
{
|
||
|
mpi X;
|
||
|
char str[1000];
|
||
|
int len = 1000;
|
||
|
|
||
|
mpi_init(&X, NULL);
|
||
|
|
||
|
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );
|
||
|
TEST_ASSERT( mpi_write_string( &X, {radix_A}, str, &len ) == 0 );
|
||
|
TEST_ASSERT( strcmp( str, {input_A} ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_gcd:radix_X:input_X:radix_Y:input_Y:radix_A:input_A
|
||
|
{
|
||
|
mpi A, X, Y, Z;
|
||
|
mpi_init(&A, &X, &Y, &Z, NULL);
|
||
|
|
||
|
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_gcd( &Z, &X, &Y ) == 0 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_cmp_int:input_X:input_A:result_CMP
|
||
|
{
|
||
|
mpi X;
|
||
|
mpi_init(&X, NULL);
|
||
|
|
||
|
TEST_ASSERT( mpi_lset( &X, {input_X} ) == 0);
|
||
|
TEST_ASSERT( mpi_cmp_int( &X, {input_A} ) == {result_CMP});
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_cmp_mpi:radix_X:input_X:radix_Y:input_Y:input_A
|
||
|
{
|
||
|
mpi X, Y;
|
||
|
mpi_init(&X, &Y, NULL);
|
||
|
|
||
|
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} );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_cmp_abs:radix_X:input_X:radix_Y:input_Y:input_A
|
||
|
{
|
||
|
mpi X, Y;
|
||
|
mpi_init(&X, &Y, NULL);
|
||
|
|
||
|
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} );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_copy:input_X:input_A
|
||
|
{
|
||
|
mpi X, Y, A;
|
||
|
mpi_init(&X, &Y, &A, NULL);
|
||
|
|
||
|
TEST_ASSERT( mpi_lset( &X, {input_X} ) == 0 );
|
||
|
TEST_ASSERT( mpi_lset( &Y, {input_A} ) == 0 );
|
||
|
TEST_ASSERT( mpi_lset( &A, {input_A} ) == 0 );
|
||
|
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 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_swap:input_X:input_Y
|
||
|
{
|
||
|
mpi X, Y, A;
|
||
|
mpi_init(&X, &Y, &A, NULL);
|
||
|
|
||
|
TEST_ASSERT( mpi_lset( &X, {input_X} ) == 0 );
|
||
|
TEST_ASSERT( mpi_lset( &Y, {input_Y} ) == 0 );
|
||
|
TEST_ASSERT( mpi_lset( &A, {input_X} ) == 0 );
|
||
|
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 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_add_mpi:radix_X:input_X:radix_Y:input_Y:radix_A:input_A
|
||
|
{
|
||
|
mpi X, Y, Z, A;
|
||
|
mpi_init(&X, &Y, &Z, &A, NULL);
|
||
|
|
||
|
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_add_mpi( &Z, &X, &Y ) == 0 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_add_abs:radix_X:input_X:radix_Y:input_Y:radix_A:input_A
|
||
|
{
|
||
|
mpi X, Y, Z, A;
|
||
|
mpi_init(&X, &Y, &Z, &A, NULL);
|
||
|
|
||
|
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_add_abs( &Z, &X, &Y ) == 0 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_add_int:radix_X:input_X:input_Y:radix_A:input_A
|
||
|
{
|
||
|
mpi X, Z, A;
|
||
|
mpi_init(&X, &Z, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_sub_mpi:radix_X:input_X:radix_Y:input_Y:radix_A:input_A
|
||
|
{
|
||
|
mpi X, Y, Z, A;
|
||
|
mpi_init(&X, &Y, &Z, &A, NULL);
|
||
|
|
||
|
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_sub_mpi( &Z, &X, &Y ) == 0 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_sub_abs:radix_X:input_X:radix_Y:input_Y:radix_A:input_A:sub_result
|
||
|
{
|
||
|
mpi X, Y, Z, A;
|
||
|
int res;
|
||
|
mpi_init(&X, &Y, &Z, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
|
||
|
res = mpi_sub_abs( &Z, &X, &Y );
|
||
|
TEST_ASSERT( res == {sub_result} );
|
||
|
if( res == 0 )
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_sub_int:radix_X:input_X:input_Y:radix_A:input_A
|
||
|
{
|
||
|
mpi X, Z, A;
|
||
|
mpi_init(&X, &Z, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_mul_mpi:radix_X:input_X:radix_Y:input_Y:radix_A:input_A
|
||
|
{
|
||
|
mpi X, Y, Z, A;
|
||
|
mpi_init(&X, &Y, &Z, &A, NULL);
|
||
|
|
||
|
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_mul_mpi( &Z, &X, &Y ) == 0 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_mul_int:radix_X:input_X:input_Y:radix_A:input_A:result_comparison
|
||
|
{
|
||
|
mpi X, Z, A;
|
||
|
mpi_init(&X, &Z, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) {result_comparison} 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_div_mpi:radix_X:input_X:radix_Y:input_Y:radix_A:input_A:radix_B:input_B:div_result
|
||
|
{
|
||
|
mpi X, Y, Q, R, A, B;
|
||
|
int res;
|
||
|
mpi_init(&X, &Y, &Q, &R, &A, &B, NULL);
|
||
|
|
||
|
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 );
|
||
|
res = mpi_div_mpi( &Q, &R, &X, &Y );
|
||
|
TEST_ASSERT( res == {div_result} );
|
||
|
if( res == 0 )
|
||
|
{
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Q, &A ) == 0 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &R, &B ) == 0 );
|
||
|
}
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_div_int:radix_X:input_X:input_Y:radix_A:input_A:radix_B:input_B:div_result
|
||
|
{
|
||
|
mpi X, Q, R, A, B;
|
||
|
int res;
|
||
|
mpi_init(&X, &Q, &R, &A, &B, NULL);
|
||
|
|
||
|
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} );
|
||
|
if( res == 0 )
|
||
|
{
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Q, &A ) == 0 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &R, &B ) == 0 );
|
||
|
}
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_mod_mpi:radix_X:input_X:radix_Y:input_Y:radix_A:input_A:div_result
|
||
|
{
|
||
|
mpi X, Y, Z, A;
|
||
|
int res;
|
||
|
mpi_init(&X, &Y, &Z, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
res = mpi_mod_mpi( &Z, &X, &Y );
|
||
|
TEST_ASSERT( res == {div_result} );
|
||
|
if( res == 0 )
|
||
|
{
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_mod_int:radix_X:input_X:input_Y:input_A:div_result
|
||
|
{
|
||
|
mpi X;
|
||
|
int res;
|
||
|
t_int r;
|
||
|
mpi_init(&X, NULL);
|
||
|
|
||
|
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );
|
||
|
res = mpi_mod_int( &r, &X, {input_Y} );
|
||
|
TEST_ASSERT( res == {div_result} );
|
||
|
if( res == 0 )
|
||
|
{
|
||
|
TEST_ASSERT( r == {input_A} );
|
||
|
}
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_exp_mod:radix_A:input_A:radix_E:input_E:radix_N:input_N:radix_RR:input_RR:radix_X:input_X:div_result
|
||
|
{
|
||
|
mpi A, E, N, RR, Z, X;
|
||
|
int res;
|
||
|
mpi_init(&A, &E, &N, &RR, &Z, &X, NULL);
|
||
|
|
||
|
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 );
|
||
|
|
||
|
if( strlen( {input_RR} ) )
|
||
|
TEST_ASSERT( mpi_read_string( &RR, {radix_RR}, {input_RR} ) == 0 );
|
||
|
|
||
|
res = mpi_exp_mod( &Z, &A, &E, &N, &RR );
|
||
|
TEST_ASSERT( res == {div_result} );
|
||
|
if( res == 0 )
|
||
|
{
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &X ) == 0 );
|
||
|
}
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_inv_mod:radix_X:input_X:radix_Y:input_Y:radix_A:input_A:div_result
|
||
|
{
|
||
|
mpi X, Y, Z, A;
|
||
|
int res;
|
||
|
mpi_init(&X, &Y, &Z, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
res = mpi_inv_mod( &Z, &X, &Y );
|
||
|
TEST_ASSERT( res == {div_result} );
|
||
|
if( res == 0 )
|
||
|
{
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
}
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_is_prime:radix_X:input_X:div_result
|
||
|
{
|
||
|
mpi X;
|
||
|
int res;
|
||
|
mpi_init(&X, NULL);
|
||
|
|
||
|
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );
|
||
|
res = mpi_is_prime( &X, myrand, NULL );
|
||
|
TEST_ASSERT( res == {div_result} );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_shift_l:radix_X:input_X:shift_X:radix_A:input_A
|
||
|
{
|
||
|
mpi X, A;
|
||
|
mpi_init(&X, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
BEGIN_CASE
|
||
|
mpi_shift_r:radix_X:input_X:shift_X:radix_A:input_A
|
||
|
{
|
||
|
mpi X, A;
|
||
|
mpi_init(&X, &A, NULL);
|
||
|
|
||
|
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 );
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &X, &A ) == 0 );
|
||
|
}
|
||
|
END_CASE
|
||
|
|
||
|
/* Helper Code
|
||
|
char str[1000];
|
||
|
int len = 1000;
|
||
|
mpi_write_string(&Z, 10, str, &len);
|
||
|
printf("Z: %d %s\n", Z.s, str);
|
||
|
TEST_ASSERT( mpi_cmp_mpi( &Z, &A ) == 0 );
|
||
|
*/
|