2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/bignum.h"
|
2020-11-25 15:37:20 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
2022-07-21 10:29:32 +02:00
|
|
|
#include "bignum_core.h"
|
2022-08-02 11:54:44 +02:00
|
|
|
#include "bignum_mod.h"
|
|
|
|
#include "bignum_mod_raw.h"
|
2022-07-22 19:24:06 +02:00
|
|
|
#include "constant_time_internal.h"
|
|
|
|
#include "test/constant_flow.h"
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
2020-12-03 18:44:03 +01:00
|
|
|
#if MBEDTLS_MPI_MAX_BITS > 792
|
|
|
|
#define MPI_MAX_BITS_LARGER_THAN_792
|
2020-12-03 15:24:33 +01:00
|
|
|
#endif
|
2022-08-12 15:36:56 +02:00
|
|
|
|
2021-06-10 15:34:15 +02:00
|
|
|
/* Check the validity of the sign bit in an MPI object. Reject representations
|
|
|
|
* that are not supported by the rest of the library and indicate a bug when
|
|
|
|
* constructing the value. */
|
|
|
|
static int sign_is_valid( const mbedtls_mpi *X )
|
|
|
|
{
|
|
|
|
if( X->s != 1 && X->s != -1 )
|
|
|
|
return( 0 ); // invalid sign bit, e.g. 0
|
|
|
|
if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
|
|
|
|
return( 0 ); // negative zero
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
typedef struct mbedtls_test_mpi_random
|
|
|
|
{
|
|
|
|
data_t *data;
|
|
|
|
size_t pos;
|
|
|
|
size_t chunk_len;
|
|
|
|
} mbedtls_test_mpi_random;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called by the Miller-Rabin primality test each time it
|
|
|
|
* chooses a random witness. The witnesses (or non-witnesses as provided by the
|
|
|
|
* test) are stored in the data member of the state structure. Each number is in
|
|
|
|
* the format that mbedtls_mpi_read_string understands and is chunk_len long.
|
|
|
|
*/
|
|
|
|
int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
|
|
|
|
unsigned char* buf,
|
|
|
|
size_t len )
|
|
|
|
{
|
|
|
|
mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
|
|
|
|
|
|
|
|
if( random == NULL || random->data->x == NULL || buf == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if( random->pos + random->chunk_len > random->data->len
|
|
|
|
|| random->chunk_len > len )
|
|
|
|
{
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( buf, 0, len );
|
|
|
|
|
|
|
|
/* The witness is written to the end of the buffer, since the buffer is
|
|
|
|
* used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
|
|
|
|
* Writing the witness to the start of the buffer would result in the
|
|
|
|
* buffer being 'witness 000...000', which would be treated as
|
|
|
|
* witness * 2^n for some n. */
|
|
|
|
memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
|
|
|
|
random->chunk_len );
|
|
|
|
|
|
|
|
random->pos += random->chunk_len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2020-11-25 15:37:20 +01:00
|
|
|
|
|
|
|
/* Random generator that is told how many bytes to return. */
|
|
|
|
static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
size_t *bytes_left = state;
|
|
|
|
size_t i;
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
if( *bytes_left == 0 )
|
|
|
|
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
|
|
|
buf[i] = *bytes_left & 0xff;
|
|
|
|
--( *bytes_left );
|
|
|
|
}
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-04-13 19:50:04 +02:00
|
|
|
/* Test whether bytes represents (in big-endian base 256) a number b that
|
|
|
|
* is significantly above a power of 2. That is, b must not have a long run
|
|
|
|
* of unset bits after the most significant bit.
|
|
|
|
*
|
|
|
|
* Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
|
|
|
|
* This function returns 1 if, when drawing a number between 0 and b,
|
|
|
|
* the probability that this number is at least 2^n is not negligible.
|
|
|
|
* This probability is (b - 2^n) / b and this function checks that this
|
|
|
|
* number is above some threshold A. The threshold value is heuristic and
|
|
|
|
* based on the needs of mpi_random_many().
|
2021-03-29 22:02:55 +02:00
|
|
|
*/
|
|
|
|
static int is_significantly_above_a_power_of_2( data_t *bytes )
|
|
|
|
{
|
|
|
|
const uint8_t *p = bytes->x;
|
|
|
|
size_t len = bytes->len;
|
|
|
|
unsigned x;
|
2021-04-13 19:50:04 +02:00
|
|
|
|
|
|
|
/* Skip leading null bytes */
|
2021-03-29 22:02:55 +02:00
|
|
|
while( len > 0 && p[0] == 0 )
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
--len;
|
|
|
|
}
|
2021-04-13 19:50:04 +02:00
|
|
|
/* 0 is not significantly above a power of 2 */
|
2021-03-29 22:02:55 +02:00
|
|
|
if( len == 0 )
|
|
|
|
return( 0 );
|
2021-04-13 19:50:04 +02:00
|
|
|
/* Extract the (up to) 2 most significant bytes */
|
|
|
|
if( len == 1 )
|
2021-03-29 22:02:55 +02:00
|
|
|
x = p[0];
|
|
|
|
else
|
|
|
|
x = ( p[0] << 8 ) | p[1];
|
|
|
|
|
2021-04-13 19:50:04 +02:00
|
|
|
/* Shift the most significant bit of x to position 8 and mask it out */
|
|
|
|
while( ( x & 0xfe00 ) != 0 )
|
|
|
|
x >>= 1;
|
|
|
|
x &= 0x00ff;
|
2021-03-29 22:02:55 +02:00
|
|
|
|
2021-04-13 19:50:04 +02:00
|
|
|
/* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
|
|
|
|
* a power of 2 iff x is significantly above 0 compared to 2^8.
|
|
|
|
* Testing x >= 2^4 amounts to picking A = 1/16 in the function
|
|
|
|
* description above. */
|
|
|
|
return( x >= 0x10 );
|
2021-03-29 22:02:55 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2015-04-29 17:02:01 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mpi_null( )
|
2015-04-29 17:02:01 +02:00
|
|
|
{
|
2015-04-30 10:28:51 +02:00
|
|
|
mbedtls_mpi X, Y, Z;
|
2015-04-29 17:02:01 +02:00
|
|
|
|
2015-04-30 10:28:51 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
mbedtls_mpi_init( &Y );
|
|
|
|
mbedtls_mpi_init( &Z );
|
2015-04-29 17:02:01 +02:00
|
|
|
|
2015-04-30 10:28:51 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
|
2015-06-18 16:47:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
|
2015-04-30 10:28:51 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
|
2015-04-29 17:02:01 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-30 10:28:51 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2015-04-29 17:02:01 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
|
|
|
|
char * input_A, int output_size, int result_read,
|
2013-08-20 11:48:36 +02:00
|
|
|
int result_write )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-06-28 23:50:27 +02:00
|
|
|
char str[1000];
|
2015-06-02 16:41:48 +02:00
|
|
|
size_t len;
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2019-03-06 13:29:37 +01:00
|
|
|
memset( str, '!', sizeof( str ) );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result_read == 0 )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-06-02 16:41:48 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result_write == 0 )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
|
2019-03-06 13:29:37 +01:00
|
|
|
TEST_ASSERT( str[len] == '!' );
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_read_binary( data_t * buf, char * input_A )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2019-02-25 17:11:58 +01:00
|
|
|
char str[1000];
|
2015-06-02 16:41:48 +02:00
|
|
|
size_t len;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2022-07-07 12:38:44 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
|
2022-08-01 14:55:41 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
|
2019-02-13 11:28:28 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X );
|
|
|
|
}
|
2022-07-21 10:29:32 +02:00
|
|
|
/* END_CASE */
|
|
|
|
|
2022-07-22 15:24:58 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_core_io_null()
|
2022-07-22 15:24:58 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint X = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mbedtls_mpi_core_read_be( &X, 1, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
ret = mbedtls_mpi_core_write_be( &X, 1, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
|
|
|
|
ret = mbedtls_mpi_core_read_be( NULL, 0, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
ret = mbedtls_mpi_core_write_be( NULL, 0, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
|
|
|
|
ret = mbedtls_mpi_core_read_le( &X, 1, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
ret = mbedtls_mpi_core_write_le( &X, 1, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
|
|
|
|
ret = mbedtls_mpi_core_read_le( NULL, 0, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
ret = mbedtls_mpi_core_write_le( NULL, 0, NULL, 0 );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-07-22 15:24:58 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-07-21 10:29:32 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_core_io_be( data_t *input, int nb_int, int nx_32_int, int iret,
|
|
|
|
int oret )
|
2022-07-21 10:29:32 +02:00
|
|
|
{
|
|
|
|
if( iret != 0 )
|
|
|
|
TEST_ASSERT( oret == 0 );
|
|
|
|
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_S( 0, nb_int );
|
2022-08-15 12:13:38 +02:00
|
|
|
size_t nb = nb_int;
|
|
|
|
|
|
|
|
unsigned char buf[1024];
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( nb, sizeof( buf ) );
|
2022-07-21 10:29:32 +02:00
|
|
|
|
2022-08-11 11:50:04 +02:00
|
|
|
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
|
|
|
* to halve the number of limbs to have the same size. */
|
2022-08-22 10:54:25 +02:00
|
|
|
size_t nx;
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_S( 0, nx_32_int );
|
2022-08-22 10:54:25 +02:00
|
|
|
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
|
|
|
nx = nx_32_int / 2 + nx_32_int % 2;
|
|
|
|
else
|
|
|
|
nx = nx_32_int;
|
2022-07-21 10:29:32 +02:00
|
|
|
|
2022-08-15 12:13:38 +02:00
|
|
|
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
2022-08-15 12:13:38 +02:00
|
|
|
|
|
|
|
int ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, iret );
|
2022-07-21 10:29:32 +02:00
|
|
|
|
|
|
|
if( iret == 0 )
|
|
|
|
{
|
|
|
|
ret = mbedtls_mpi_core_write_be( X, nx, buf, nb );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, oret );
|
2022-07-21 10:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( iret == 0 ) && ( oret == 0 ) )
|
|
|
|
{
|
|
|
|
if( nb > input->len )
|
|
|
|
{
|
|
|
|
size_t leading_zeroes = nb - input->len;
|
|
|
|
TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
|
|
|
|
for( size_t i = 0; i < leading_zeroes; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( buf[i], 0 );
|
2022-07-21 10:29:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t leading_zeroes = input->len - nb;
|
|
|
|
TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
|
|
|
|
for( size_t i = 0; i < leading_zeroes; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( input->x[i], 0 );
|
2022-07-21 10:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
2022-07-21 16:27:21 +02:00
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_core_io_le( data_t *input, int nb_int, int nx_32_int, int iret,
|
|
|
|
int oret )
|
2022-07-21 16:27:21 +02:00
|
|
|
{
|
|
|
|
if( iret != 0 )
|
|
|
|
TEST_ASSERT( oret == 0 );
|
|
|
|
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_S( 0, nb_int );
|
2022-08-15 12:13:38 +02:00
|
|
|
size_t nb = nb_int;
|
|
|
|
|
|
|
|
unsigned char buf[1024];
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( nb, sizeof( buf ) );
|
2022-07-21 16:27:21 +02:00
|
|
|
|
2022-08-11 13:15:55 +02:00
|
|
|
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
|
|
|
* to halve the number of limbs to have the same size. */
|
2022-08-22 10:54:25 +02:00
|
|
|
size_t nx;
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_S( 0, nx_32_int );
|
2022-08-22 10:54:25 +02:00
|
|
|
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
|
|
|
nx = nx_32_int / 2 + nx_32_int % 2;
|
|
|
|
else
|
|
|
|
nx = nx_32_int;
|
2022-08-15 12:13:38 +02:00
|
|
|
|
|
|
|
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
2022-07-21 16:27:21 +02:00
|
|
|
|
2022-08-15 12:13:38 +02:00
|
|
|
int ret = mbedtls_mpi_core_read_le( X, nx, input->x, input->len );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, iret );
|
2022-07-21 16:27:21 +02:00
|
|
|
|
|
|
|
if( iret == 0 )
|
|
|
|
{
|
|
|
|
ret = mbedtls_mpi_core_write_le( X, nx, buf, nb );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, oret );
|
2022-07-21 16:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( iret == 0 ) && ( oret == 0 ) )
|
|
|
|
{
|
|
|
|
if( nb > input->len )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
|
|
|
|
for( size_t i = input->len; i < nb; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( buf[i], 0 );
|
2022-07-21 16:27:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
|
|
|
|
for( size_t i = nb; i < input->len; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( input->x[i], 0 );
|
2022-07-21 16:27:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
;
|
2022-08-02 11:54:44 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-08-08 14:37:20 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_mod_setup( int ext_rep, int int_rep, int iret )
|
2022-08-08 14:37:20 +02:00
|
|
|
{
|
|
|
|
#define MLIMBS 8
|
|
|
|
mbedtls_mpi_uint mp[MLIMBS];
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset( mp, 0xFF, sizeof(mp) );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, iret );
|
2022-08-08 14:37:20 +02:00
|
|
|
|
|
|
|
/* Address sanitiser should catch if we try to free mp */
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
|
|
|
|
/* Make sure that the modulus doesn't have reference to mp anymore */
|
|
|
|
TEST_ASSERT( m.p != mp );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* It should be safe to call an mbedtls free several times */
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
|
|
|
|
#undef MLIMBS
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
|
2022-08-02 11:54:44 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_mod_raw_io( data_t *input, int nb_int, int nx_32_int,
|
|
|
|
int iendian, int iret, int oret )
|
2022-08-02 11:54:44 +02:00
|
|
|
{
|
|
|
|
if( iret != 0 )
|
|
|
|
TEST_ASSERT( oret == 0 );
|
|
|
|
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_S( 0, nb_int );
|
2022-08-15 12:13:38 +02:00
|
|
|
size_t nb = nb_int;
|
|
|
|
|
|
|
|
unsigned char buf[1024];
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( nb, sizeof( buf ) );
|
2022-08-02 11:54:44 +02:00
|
|
|
|
2022-08-12 18:00:33 +02:00
|
|
|
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
|
|
|
* to halve the number of limbs to have the same size. */
|
2022-08-22 10:54:25 +02:00
|
|
|
size_t nx;
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_S( 0, nx_32_int );
|
2022-08-22 10:54:25 +02:00
|
|
|
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
|
|
|
nx = nx_32_int / 2 + nx_32_int % 2;
|
|
|
|
else
|
|
|
|
nx = nx_32_int;
|
2022-08-02 11:54:44 +02:00
|
|
|
|
2022-08-15 12:13:38 +02:00
|
|
|
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
2022-08-15 12:13:38 +02:00
|
|
|
|
|
|
|
int endian;
|
2022-08-02 11:54:44 +02:00
|
|
|
if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID )
|
|
|
|
endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
|
|
|
|
else
|
|
|
|
endian = iendian;
|
|
|
|
|
2022-08-15 12:13:38 +02:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
2022-08-02 11:54:44 +02:00
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
2022-08-15 12:13:38 +02:00
|
|
|
mbedtls_mpi_uint init[sizeof( X ) / sizeof( X[0] )];
|
|
|
|
memset( init, 0xFF, sizeof( init ) );
|
|
|
|
int ret = mbedtls_mpi_mod_modulus_setup( &m, init, nx, endian,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, 0 );
|
2022-08-02 11:54:44 +02:00
|
|
|
|
|
|
|
if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && iret != 0 )
|
|
|
|
m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
|
|
|
|
|
|
|
|
ret = mbedtls_mpi_mod_raw_read( X, &m, input->x, input->len );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, iret );
|
2022-08-02 11:54:44 +02:00
|
|
|
|
|
|
|
if( iret == 0 )
|
|
|
|
{
|
|
|
|
if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && oret != 0 )
|
|
|
|
m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
|
|
|
|
|
|
|
|
ret = mbedtls_mpi_mod_raw_write( X, &m, buf, nb );
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, oret );
|
2022-08-02 11:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( iret == 0 ) && ( oret == 0 ) )
|
|
|
|
{
|
|
|
|
if( nb > input->len )
|
|
|
|
{
|
|
|
|
if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
|
|
|
|
{
|
|
|
|
size_t leading_zeroes = nb - input->len;
|
|
|
|
TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
|
|
|
|
for( size_t i = 0; i < leading_zeroes; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( buf[i], 0 );
|
2022-08-02 11:54:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
|
|
|
|
for( size_t i = input->len; i < nb; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( buf[i], 0 );
|
2022-08-02 11:54:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
|
|
|
|
{
|
|
|
|
size_t leading_zeroes = input->len - nb;
|
|
|
|
TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
|
|
|
|
for( size_t i = 0; i < leading_zeroes; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( input->x[i], 0 );
|
2022-08-02 11:54:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
|
|
|
|
for( size_t i = nb; i < input->len; i++ )
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( input->x[i], 0 );
|
2022-08-02 11:54:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
2022-07-21 16:27:21 +02:00
|
|
|
}
|
2019-02-13 11:28:28 +01:00
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_read_binary_le( data_t * buf, char * input_A )
|
2019-02-13 11:28:28 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
2019-02-25 17:11:58 +01:00
|
|
|
char str[1000];
|
2019-02-13 11:28:28 +01:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2022-07-07 12:38:44 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
|
2022-08-01 14:55:41 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_write_binary( char * input_X, data_t * input_A,
|
|
|
|
int output_size, int result )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-07-06 08:40:23 +02:00
|
|
|
unsigned char buf[1000];
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t buflen;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
memset( buf, 0x00, 1000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-10-30 09:23:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
buflen = mbedtls_mpi_size( &X );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( buflen > (size_t) output_size )
|
|
|
|
buflen = (size_t) output_size;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result == 0)
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2020-06-10 11:42:32 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
|
|
|
|
buflen, input_A->len ) == 0 );
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2019-02-19 17:17:40 +01:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_write_binary_le( char * input_X, data_t * input_A,
|
|
|
|
int output_size, int result )
|
2019-02-19 17:17:40 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
|
|
|
unsigned char buf[1000];
|
|
|
|
size_t buflen;
|
|
|
|
|
|
|
|
memset( buf, 0x00, 1000 );
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2019-02-19 17:17:40 +01:00
|
|
|
|
|
|
|
buflen = mbedtls_mpi_size( &X );
|
|
|
|
if( buflen > (size_t) output_size )
|
|
|
|
buflen = (size_t) output_size;
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
|
|
|
|
if( result == 0)
|
|
|
|
{
|
|
|
|
|
2020-06-10 11:42:32 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
|
|
|
|
buflen, input_A->len ) == 0 );
|
2019-02-19 17:17:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_read_file( char * input_file, data_t * input_A, int result )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-07-06 08:40:23 +02:00
|
|
|
unsigned char buf[1000];
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t buflen;
|
2009-07-11 21:15:20 +02:00
|
|
|
FILE *file;
|
2015-02-14 17:01:34 +01:00
|
|
|
int ret;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
memset( buf, 0x00, 1000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
file = fopen( input_file, "r" );
|
2014-04-17 16:08:20 +02:00
|
|
|
TEST_ASSERT( file != NULL );
|
2022-07-07 12:38:44 +02:00
|
|
|
ret = mbedtls_mpi_read_file( &X, 16, file );
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file);
|
2015-02-14 17:01:34 +01:00
|
|
|
TEST_ASSERT( ret == result );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result == 0 )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
buflen = mbedtls_mpi_size( &X );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
|
2020-06-10 11:42:32 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
|
|
|
|
buflen, input_A->len ) == 0 );
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_write_file( char * input_X, char * output_file )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
2009-07-11 21:15:20 +02:00
|
|
|
FILE *file_out, *file_in;
|
2015-06-24 01:08:09 +02:00
|
|
|
int ret;
|
2009-07-11 21:15:20 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
file_out = fopen( output_file, "w" );
|
2011-05-26 15:16:06 +02:00
|
|
|
TEST_ASSERT( file_out != NULL );
|
2022-07-07 12:38:44 +02:00
|
|
|
ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out );
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file_out);
|
2015-06-24 01:08:09 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
file_in = fopen( output_file, "r" );
|
2011-05-26 15:16:06 +02:00
|
|
|
TEST_ASSERT( file_in != NULL );
|
2022-07-07 12:38:44 +02:00
|
|
|
ret = mbedtls_mpi_read_file( &Y, 16, file_in );
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file_in);
|
2015-06-24 01:08:09 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_get_bit( char * input_X, int pos, int val )
|
2011-05-18 17:47:11 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
|
|
|
mbedtls_mpi_init( &X );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2011-05-18 17:47:11 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_set_bit( char * input_X, int pos, int val,
|
|
|
|
char * output_Y, int result )
|
2011-05-18 17:47:11 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 );
|
2016-07-14 13:47:07 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
|
|
|
|
|
|
|
|
if( result == 0 )
|
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2016-07-14 13:47:07 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
|
|
|
|
}
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
2011-05-18 17:47:11 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_lsb( char * input_X, int nr_bits )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_bitlen( char * input_X, int nr_bits )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-06-18 16:47:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_gcd( char * input_X, char * input_Y,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi A, X, Y, Z;
|
|
|
|
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_cmp_int( int input_X, int input_A, int result_CMP )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_cmp_mpi( char * input_X, char * input_Y,
|
|
|
|
int input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-22 19:24:06 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-08-19 14:32:17 +02:00
|
|
|
void mpi_core_lt_ct( data_t * input_X, data_t * input_Y, int input_ret )
|
2022-07-22 19:24:06 +02:00
|
|
|
{
|
|
|
|
#define MAX_LEN 64
|
|
|
|
mbedtls_mpi_uint X[MAX_LEN];
|
|
|
|
mbedtls_mpi_uint Y[MAX_LEN];
|
|
|
|
unsigned exp_ret = input_ret;
|
|
|
|
unsigned ret;
|
|
|
|
size_t len = CHARS_TO_LIMBS(
|
|
|
|
input_X->len > input_Y->len ? input_X->len : input_Y->len );
|
|
|
|
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( len, MAX_LEN );
|
2022-07-22 19:24:06 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_core_read_be( X, len, input_X->x, input_X->len )
|
|
|
|
== 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_core_read_be( Y, len, input_Y->x, input_Y->len )
|
|
|
|
== 0 );
|
|
|
|
|
|
|
|
TEST_CF_SECRET( X, len * sizeof( mbedtls_mpi_uint ) );
|
|
|
|
TEST_CF_SECRET( Y, len * sizeof( mbedtls_mpi_uint ) );
|
|
|
|
|
|
|
|
ret = mbedtls_mpi_core_lt_ct( X, Y, len );
|
|
|
|
|
|
|
|
TEST_CF_PUBLIC( X, len * sizeof( mbedtls_mpi_uint ) );
|
|
|
|
TEST_CF_PUBLIC( Y, len * sizeof( mbedtls_mpi_uint ) );
|
|
|
|
TEST_CF_PUBLIC( &ret, sizeof( ret ) );
|
|
|
|
|
2022-08-22 10:36:17 +02:00
|
|
|
TEST_EQUAL( ret, exp_ret );
|
2022-07-22 19:24:06 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
|
|
|
|
#undef MAX_LEN
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-09-11 17:07:14 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_lt_mpi_ct( int size_X, char * input_X,
|
|
|
|
int size_Y, char * input_Y,
|
|
|
|
int input_ret, int input_err )
|
2019-09-11 17:07:14 +02:00
|
|
|
{
|
2020-09-02 15:18:07 +02:00
|
|
|
unsigned ret = -1;
|
2019-10-11 15:21:53 +02:00
|
|
|
unsigned input_uret = input_ret;
|
2019-09-11 17:07:14 +02:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
2019-09-11 17:07:14 +02:00
|
|
|
|
2020-01-21 16:30:53 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
|
2019-09-11 17:07:14 +02:00
|
|
|
|
2019-10-11 15:21:53 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
|
2019-09-11 17:07:14 +02:00
|
|
|
if( input_err == 0 )
|
2019-10-11 15:21:53 +02:00
|
|
|
TEST_ASSERT( ret == input_uret );
|
2019-09-11 17:07:14 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_cmp_abs( char * input_X, char * input_Y,
|
|
|
|
int input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_copy( char *src_hex, char *dst_hex )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2021-06-10 23:00:33 +02:00
|
|
|
mbedtls_mpi src, dst, ref;
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
mbedtls_mpi_init( &src );
|
|
|
|
mbedtls_mpi_init( &dst );
|
2021-06-10 23:00:33 +02:00
|
|
|
mbedtls_mpi_init( &ref );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
|
2020-01-20 21:01:51 +01:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_copy() */
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
|
2021-06-10 23:00:33 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &dst ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
|
2020-01-20 21:01:51 +01:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_safe_cond_assign(), assignment done */
|
|
|
|
mbedtls_mpi_free( &dst );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
|
2021-06-10 23:00:33 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &dst ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_safe_cond_assign(), assignment not done */
|
|
|
|
mbedtls_mpi_free( &dst );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
|
2021-06-10 23:00:33 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
|
|
|
|
TEST_ASSERT( sign_is_valid( &dst ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
mbedtls_mpi_free( &src );
|
|
|
|
mbedtls_mpi_free( &dst );
|
2021-06-10 23:00:33 +02:00
|
|
|
mbedtls_mpi_free( &ref );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
void mpi_copy_self( char *input_X )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
mbedtls_mpi X, A;
|
|
|
|
mbedtls_mpi_init( &A );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.
Also generalize mpi_copy_self to support arbitrary inputs.
Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:
```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
"zero (null)" => "",
"zero (1 limb)" => "0",
"small positive" => "bead",
"large positive" => "ca5cadedb01dfaceacc01ade",
"small negative" => "-bead",
"large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
foreach $d (sort keys %v) {
printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
$s, $d, $v{$s}, rhs($v{$d});
}
}
foreach $s (sort keys %v) {
printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 15:17:30 +02:00
|
|
|
mbedtls_mpi_free( &A );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2021-06-10 22:29:57 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_swap( char *X_hex, char *Y_hex )
|
2021-06-10 22:29:57 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi X, Y, X0, Y0;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
|
|
|
mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
|
2021-06-10 22:29:57 +02:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_swap() */
|
2022-09-15 16:38:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
|
2021-06-10 22:29:57 +02:00
|
|
|
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 );
|
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_safe_cond_swap(), swap done */
|
|
|
|
mbedtls_mpi_free( &X );
|
|
|
|
mbedtls_mpi_free( &Y );
|
2022-09-15 16:38:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
|
2021-06-10 23:00:33 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
|
|
|
|
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 );
|
|
|
|
|
|
|
|
/* mbedtls_mpi_safe_cond_swap(), swap not done */
|
|
|
|
mbedtls_mpi_free( &X );
|
|
|
|
mbedtls_mpi_free( &Y );
|
2022-09-15 16:38:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
|
2021-06-10 23:00:33 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
|
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
|
|
|
TEST_ASSERT( sign_is_valid( &Y ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
|
|
|
|
|
2021-06-10 22:29:57 +02:00
|
|
|
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 );
|
|
|
|
|
2022-09-15 16:38:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
|
2021-06-10 22:29:57 +02:00
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2013-11-21 10:39:37 +01:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_shrink( int before, int used, int min, int after )
|
2013-11-21 10:39:37 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
|
|
|
mbedtls_mpi_init( &X );
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
|
2021-06-15 21:19:18 +02:00
|
|
|
if( used > 0 )
|
|
|
|
{
|
|
|
|
size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
|
|
|
|
}
|
|
|
|
TEST_EQUAL( X.n, (size_t) before );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
|
2021-06-15 21:19:18 +02:00
|
|
|
TEST_EQUAL( X.n, (size_t) after );
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2013-11-21 10:39:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_add_mpi( char * input_X, char * input_Y,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2020-07-23 01:18:11 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Y ) );
|
2020-07-23 01:18:11 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-10-25 10:58:03 +01:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_add_mpi_inplace( char * input_X, char * input_A )
|
2015-10-25 10:58:03 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-10-25 12:29:13 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-10-25 12:29:13 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-10-25 12:29:13 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-10-25 12:29:13 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-10-25 12:29:13 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-10-25 10:58:03 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-10-25 10:58:03 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_add_abs( char * input_X, char * input_Y,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Y ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2020-07-23 01:18:11 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-12 13:01:32 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_add_int( char * input_X, int input_Y,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_sub_mpi( char * input_X, char * input_Y,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2020-07-23 01:18:11 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Y ) );
|
2020-07-23 01:18:11 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_sub_abs( char * input_X, char * input_Y,
|
|
|
|
char * input_A, int sub_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-10-30 09:23:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == sub_result );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2020-07-23 01:18:11 +02:00
|
|
|
if( sub_result == 0 )
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Y ) );
|
2020-07-23 01:18:11 +02:00
|
|
|
if( sub_result == 0 )
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_sub_int( char * input_X, int input_Y,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_mul_mpi( char * input_X, char * input_Y,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_mul_int( char * input_X, int input_Y,
|
|
|
|
char * input_A, char * result_comparison )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2013-08-16 13:38:47 +02:00
|
|
|
if( strcmp( result_comparison, "==" ) == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2013-08-16 13:38:47 +02:00
|
|
|
else if( strcmp( result_comparison, "!=" ) == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
|
2013-08-16 13:38:47 +02:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "unknown operator" == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_div_mpi( char * input_X, char * input_Y,
|
|
|
|
char * input_A, char * input_B,
|
|
|
|
int div_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Q, R, A, B;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
|
|
|
|
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Q ) );
|
|
|
|
TEST_ASSERT( sign_is_valid( &R ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
|
|
|
|
mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_div_int( char * input_X, int input_Y,
|
|
|
|
char * input_A, char * input_B,
|
|
|
|
int div_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Q, R, A, B;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
|
|
|
|
mbedtls_mpi_init( &B );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Q ) );
|
|
|
|
TEST_ASSERT( sign_is_valid( &R ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
|
|
|
|
mbedtls_mpi_free( &B );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_mod_mpi( char * input_X, char * input_Y,
|
|
|
|
char * input_A, int div_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, A;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_mod_int( char * input_X, int input_Y,
|
|
|
|
int input_A, int div_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint r;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_mod_int( &r, &X, input_Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_exp_mod( char * input_A, char * input_E,
|
|
|
|
char * input_N, char * input_X,
|
|
|
|
int exp_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi A, E, N, RR, Z, X;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
|
|
|
|
mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2021-06-09 18:31:35 +02:00
|
|
|
res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
|
2021-06-15 21:55:05 +02:00
|
|
|
TEST_ASSERT( res == exp_result );
|
2021-06-09 18:31:35 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now test again with the speed-up parameter supplied as an output. */
|
|
|
|
res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
|
2021-06-15 21:55:05 +02:00
|
|
|
TEST_ASSERT( res == exp_result );
|
2021-06-09 18:31:35 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now test again with the speed-up parameter supplied in calculated form. */
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
|
2021-06-15 21:55:05 +02:00
|
|
|
TEST_ASSERT( res == exp_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
|
|
|
|
mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2020-12-02 11:41:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
|
|
|
|
char * input_RR, int exp_result )
|
2020-12-02 11:41:50 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi A, E, N, RR, Z;
|
|
|
|
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
|
|
|
|
mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
|
|
|
|
|
2020-12-03 12:35:41 +01:00
|
|
|
/* Set A to 2^(A_bytes - 1) + 1 */
|
2020-12-02 11:41:50 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
|
2020-12-03 12:35:41 +01:00
|
|
|
|
|
|
|
/* Set E to 2^(E_bytes - 1) + 1 */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
|
2020-12-02 11:41:50 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
|
2020-12-03 12:35:41 +01:00
|
|
|
|
|
|
|
/* Set N to 2^(N_bytes - 1) + 1 */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
|
2020-12-02 11:41:50 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
|
|
|
|
|
|
|
|
if( strlen( input_RR ) )
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
|
2020-12-02 11:41:50 +01:00
|
|
|
|
2020-12-03 12:35:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
|
2020-12-02 11:41:50 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
|
|
|
|
mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_inv_mod( char * input_X, char * input_Y,
|
|
|
|
char * input_A, int div_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &Z ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_is_prime( char * input_X, int div_result )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
2020-06-10 12:12:18 +02:00
|
|
|
res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_is_prime_det( data_t * input_X, data_t * witnesses,
|
|
|
|
int chunk_len, int rounds )
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
|
|
|
int res;
|
|
|
|
mbedtls_test_mpi_random rand;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
rand.data = witnesses;
|
|
|
|
rand.pos = 0;
|
|
|
|
rand.chunk_len = chunk_len;
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
|
2018-10-02 16:30:39 +02:00
|
|
|
res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
|
|
|
|
mbedtls_test_mpi_miller_rabin_determinizer,
|
|
|
|
&rand );
|
|
|
|
TEST_ASSERT( res == 0 );
|
|
|
|
|
|
|
|
rand.data = witnesses;
|
|
|
|
rand.pos = 0;
|
|
|
|
rand.chunk_len = chunk_len;
|
|
|
|
|
2018-09-18 15:48:23 +02:00
|
|
|
res = mbedtls_mpi_is_prime_ext( &X, rounds,
|
|
|
|
mbedtls_test_mpi_miller_rabin_determinizer,
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
&rand );
|
2018-10-02 16:30:39 +02:00
|
|
|
TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_gen_prime( int bits, int flags, int ref_ret )
|
2014-06-16 17:12:40 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2014-06-16 17:12:40 +02:00
|
|
|
int my_ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2014-06-16 17:12:40 +02:00
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL );
|
2014-06-16 17:12:40 +02:00
|
|
|
TEST_ASSERT( my_ret == ref_ret );
|
|
|
|
|
|
|
|
if( ref_ret == 0 )
|
|
|
|
{
|
2015-06-18 16:47:17 +02:00
|
|
|
size_t actual_bits = mbedtls_mpi_bitlen( &X );
|
2014-06-16 17:12:40 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( actual_bits >= (size_t) bits );
|
|
|
|
TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2014-06-16 17:12:40 +02:00
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL ) == 0 );
|
2018-08-14 16:31:54 +02:00
|
|
|
if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
|
2014-06-16 17:12:40 +02:00
|
|
|
{
|
2018-01-10 08:12:01 +01:00
|
|
|
/* X = ( X - 1 ) / 2 */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL ) == 0 );
|
2014-06-16 17:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2014-06-16 17:12:40 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_shift_l( char * input_X, int shift_X,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_shift_r( char * input_X, int shift_X,
|
|
|
|
char * input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2020-11-25 15:37:20 +01:00
|
|
|
/* BEGIN_CASE */
|
2021-04-02 00:02:27 +02:00
|
|
|
void mpi_fill_random( int wanted_bytes, int rng_bytes,
|
|
|
|
int before, int expected_ret )
|
2020-11-25 15:37:20 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
|
|
|
int ret;
|
|
|
|
size_t bytes_left = rng_bytes;
|
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
|
2021-04-02 00:02:27 +02:00
|
|
|
if( before != 0 )
|
|
|
|
{
|
|
|
|
/* Set X to sign(before) * 2^(|before|-1) */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
|
|
|
|
if( before < 0 )
|
|
|
|
before = - before;
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:37:20 +01:00
|
|
|
ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
|
|
|
|
f_rng_bytes_left, &bytes_left );
|
|
|
|
TEST_ASSERT( ret == expected_ret );
|
|
|
|
|
|
|
|
if( expected_ret == 0 )
|
|
|
|
{
|
|
|
|
/* mbedtls_mpi_fill_random is documented to use bytes from the RNG
|
|
|
|
* as a big-endian representation of the number. We know when
|
|
|
|
* our RNG function returns null bytes, so we know how many
|
|
|
|
* leading zero bytes the number has. */
|
|
|
|
size_t leading_zeros = 0;
|
|
|
|
if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
|
|
|
|
leading_zeros = 1;
|
|
|
|
TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
|
|
|
|
(size_t) wanted_bytes );
|
|
|
|
TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &X ) );
|
2020-11-25 15:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-29 22:02:55 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_random_many( int min, data_t *bound_bytes, int iterations )
|
|
|
|
{
|
|
|
|
/* Generate numbers in the range 1..bound-1. Do it iterations times.
|
|
|
|
* This function assumes that the value of bound is at least 2 and
|
|
|
|
* that iterations is large enough that a one-in-2^iterations chance
|
|
|
|
* effectively never occurs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_mpi upper_bound;
|
|
|
|
size_t n_bits;
|
|
|
|
mbedtls_mpi result;
|
|
|
|
size_t b;
|
|
|
|
/* If upper_bound is small, stats[b] is the number of times the value b
|
|
|
|
* has been generated. Otherwise stats[b] is the number of times a
|
|
|
|
* value with bit b set has been generated. */
|
|
|
|
size_t *stats = NULL;
|
|
|
|
size_t stats_len;
|
|
|
|
int full_stats;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &upper_bound );
|
|
|
|
mbedtls_mpi_init( &result );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
|
|
|
|
bound_bytes->x, bound_bytes->len ) );
|
|
|
|
n_bits = mbedtls_mpi_bitlen( &upper_bound );
|
|
|
|
/* Consider a bound "small" if it's less than 2^5. This value is chosen
|
|
|
|
* to be small enough that the probability of missing one value is
|
|
|
|
* negligible given the number of iterations. It must be less than
|
|
|
|
* 256 because some of the code below assumes that "small" values
|
|
|
|
* fit in a byte. */
|
|
|
|
if( n_bits <= 5 )
|
|
|
|
{
|
|
|
|
full_stats = 1;
|
|
|
|
stats_len = bound_bytes->x[bound_bytes->len - 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
full_stats = 0;
|
|
|
|
stats_len = n_bits;
|
|
|
|
}
|
|
|
|
ASSERT_ALLOC( stats, stats_len );
|
|
|
|
|
|
|
|
for( i = 0; i < (size_t) iterations; i++ )
|
|
|
|
{
|
|
|
|
mbedtls_test_set_step( i );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
|
|
|
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &result ) );
|
2021-03-29 22:02:55 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
|
|
|
|
if( full_stats )
|
|
|
|
{
|
|
|
|
uint8_t value;
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
|
|
|
|
TEST_ASSERT( value < stats_len );
|
|
|
|
++stats[value];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( b = 0; b < n_bits; b++ )
|
|
|
|
stats[b] += mbedtls_mpi_get_bit( &result, b );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( full_stats )
|
|
|
|
{
|
2021-04-13 20:45:05 +02:00
|
|
|
for( b = min; b < stats_len; b++ )
|
2021-03-29 22:02:55 +02:00
|
|
|
{
|
|
|
|
mbedtls_test_set_step( 1000000 + b );
|
|
|
|
/* Assert that each value has been reached at least once.
|
|
|
|
* This is almost guaranteed if the iteration count is large
|
|
|
|
* enough. This is a very crude way of checking the distribution.
|
|
|
|
*/
|
|
|
|
TEST_ASSERT( stats[b] > 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-02 21:24:04 +02:00
|
|
|
int statistically_safe_all_the_way =
|
|
|
|
is_significantly_above_a_power_of_2( bound_bytes );
|
2021-03-29 22:02:55 +02:00
|
|
|
for( b = 0; b < n_bits; b++ )
|
|
|
|
{
|
|
|
|
mbedtls_test_set_step( 1000000 + b );
|
|
|
|
/* Assert that each bit has been set in at least one result and
|
|
|
|
* clear in at least one result. Provided that iterations is not
|
|
|
|
* too small, it would be extremely unlikely for this not to be
|
|
|
|
* the case if the results are uniformly distributed.
|
|
|
|
*
|
|
|
|
* As an exception, the top bit may legitimately never be set
|
|
|
|
* if bound is a power of 2 or only slightly above.
|
|
|
|
*/
|
2021-06-02 21:24:04 +02:00
|
|
|
if( statistically_safe_all_the_way || b != n_bits - 1 )
|
2021-03-29 22:02:55 +02:00
|
|
|
{
|
|
|
|
TEST_ASSERT( stats[b] > 0 );
|
|
|
|
}
|
|
|
|
TEST_ASSERT( stats[b] < (size_t) iterations );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &upper_bound );
|
|
|
|
mbedtls_mpi_free( &result );
|
|
|
|
mbedtls_free( stats );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-01 15:57:18 +02:00
|
|
|
/* BEGIN_CASE */
|
2021-04-02 00:02:27 +02:00
|
|
|
void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
|
2021-04-01 15:57:18 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi upper_bound;
|
|
|
|
mbedtls_mpi result;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &upper_bound );
|
|
|
|
mbedtls_mpi_init( &result );
|
|
|
|
|
2021-04-02 00:02:27 +02:00
|
|
|
if( before != 0 )
|
|
|
|
{
|
|
|
|
/* Set result to sign(before) * 2^(|before|-1) */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
|
|
|
|
if( before < 0 )
|
|
|
|
before = - before;
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:57:18 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
|
|
|
|
bound_bytes->x, bound_bytes->len ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) );
|
2021-06-10 15:34:15 +02:00
|
|
|
TEST_ASSERT( sign_is_valid( &result ) );
|
2021-04-01 15:57:18 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &upper_bound );
|
|
|
|
mbedtls_mpi_free( &result );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-29 22:14:51 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
|
|
|
|
{
|
|
|
|
mbedtls_mpi upper_bound;
|
|
|
|
mbedtls_mpi result;
|
|
|
|
int actual_ret;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &upper_bound );
|
|
|
|
mbedtls_mpi_init( &result );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
|
|
|
|
bound_bytes->x, bound_bytes->len ) );
|
|
|
|
actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL );
|
|
|
|
TEST_EQUAL( expected_ret, actual_ret );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &upper_bound );
|
|
|
|
mbedtls_mpi_free( &result );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:46:09 +02:00
|
|
|
void mpi_core_add_if( char * input_A, char * input_B,
|
2022-09-15 15:28:30 +02:00
|
|
|
char * input_S4, int carry4,
|
|
|
|
char * input_S8, int carry8 )
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
{
|
2022-09-15 15:28:30 +02:00
|
|
|
mbedtls_mpi S4, S8, A, B;
|
2022-09-02 11:46:09 +02:00
|
|
|
mbedtls_mpi_uint *a = NULL; /* first value to add */
|
|
|
|
mbedtls_mpi_uint *b = NULL; /* second value to add */
|
2022-09-15 15:28:30 +02:00
|
|
|
mbedtls_mpi_uint *sum = NULL;
|
|
|
|
mbedtls_mpi_uint *d = NULL; /* destination - the in/out first operand */
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-02 11:46:09 +02:00
|
|
|
mbedtls_mpi_init( &A );
|
|
|
|
mbedtls_mpi_init( &B );
|
2022-09-15 15:28:30 +02:00
|
|
|
mbedtls_mpi_init( &S4 );
|
|
|
|
mbedtls_mpi_init( &S8 );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-02 11:46:09 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
2022-09-15 15:28:30 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S4, input_S4 ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S8, input_S8 ) );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-15 15:28:30 +02:00
|
|
|
/* We only need to work with one of (S4, carry4) or (S8, carry8) depending
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
* on sizeof(mbedtls_mpi_uint)
|
|
|
|
*/
|
2022-09-15 15:28:30 +02:00
|
|
|
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &S4 : &S8;
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8;
|
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2022-09-02 11:46:09 +02:00
|
|
|
TEST_EQUAL( 1, A.s );
|
|
|
|
TEST_EQUAL( 1, B.s );
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 1, X->s );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-02 11:46:09 +02:00
|
|
|
/* Test cases are such that A <= B, so #limbs should be <= */
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( A.n, B.n );
|
|
|
|
TEST_LE_U( X->n, B.n );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
|
|
|
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
|
|
|
|
2022-09-02 11:46:09 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2022-09-02 12:46:18 +02:00
|
|
|
size_t limbs = B.n;
|
|
|
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
2022-09-01 16:04:43 +02:00
|
|
|
|
2022-09-15 15:07:18 +02:00
|
|
|
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
|
|
|
ASSERT_ALLOC( a, bytes );
|
|
|
|
ASSERT_ALLOC( b, bytes );
|
2022-09-15 15:28:30 +02:00
|
|
|
ASSERT_ALLOC( sum, bytes );
|
2022-09-15 15:07:18 +02:00
|
|
|
ASSERT_ALLOC( d, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
|
|
|
/* 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
|
2022-09-15 15:07:18 +02:00
|
|
|
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
*/
|
2022-09-02 11:46:09 +02:00
|
|
|
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
|
|
|
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
|
2022-09-15 15:28:30 +02:00
|
|
|
memcpy( sum, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-15 16:36:23 +02:00
|
|
|
/* 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 */
|
2022-09-15 16:23:56 +02:00
|
|
|
|
|
|
|
/* a + b */
|
|
|
|
|
|
|
|
/* cond = 0 => d unchanged, no carry */
|
2022-09-02 11:46:09 +02:00
|
|
|
memcpy( d, a, bytes );
|
2022-09-02 12:46:18 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, limbs, 0 ) );
|
2022-09-02 11:46:09 +02:00
|
|
|
ASSERT_COMPARE( d, bytes, a, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-15 16:23:56 +02:00
|
|
|
/* cond = 1 => correct result and carry */
|
2022-09-02 12:46:18 +02:00
|
|
|
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, limbs, 1 ) );
|
2022-09-15 15:28:30 +02:00
|
|
|
ASSERT_COMPARE( d, bytes, sum, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-02 12:46:18 +02:00
|
|
|
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
|
|
|
{
|
2022-09-15 16:36:23 +02:00
|
|
|
/* a == b, so test where a and b are aliased */
|
|
|
|
|
2022-09-15 16:23:56 +02:00
|
|
|
/* cond = 0 => d unchanged, no carry */
|
2022-09-02 12:46:18 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( b, b, limbs, 0 ) );
|
|
|
|
ASSERT_COMPARE( b, bytes, B.p, bytes );
|
|
|
|
|
2022-09-15 16:23:56 +02:00
|
|
|
/* cond = 1 => correct result and carry */
|
2022-09-02 12:46:18 +02:00
|
|
|
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( b, b, limbs, 1 ) );
|
2022-09-15 15:28:30 +02:00
|
|
|
ASSERT_COMPARE( b, bytes, sum, bytes );
|
2022-09-02 12:46:18 +02:00
|
|
|
}
|
2022-09-15 16:36:23 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* a != b, so test b + a */
|
|
|
|
|
|
|
|
/* cond = 0 => d unchanged, no carry */
|
|
|
|
memcpy( d, b, bytes );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, limbs, 0 ) );
|
|
|
|
ASSERT_COMPARE( d, bytes, b, bytes );
|
|
|
|
|
|
|
|
/* cond = 1 => correct result and carry */
|
|
|
|
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, limbs, 1 ) );
|
|
|
|
ASSERT_COMPARE( d, bytes, sum, bytes );
|
|
|
|
}
|
2022-09-02 12:46:18 +02:00
|
|
|
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
exit:
|
2022-09-02 11:46:09 +02:00
|
|
|
mbedtls_free( a );
|
|
|
|
mbedtls_free( b );
|
2022-09-15 15:28:30 +02:00
|
|
|
mbedtls_free( sum );
|
2022-09-02 11:46:09 +02:00
|
|
|
mbedtls_free( d );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
|
2022-09-15 15:28:30 +02:00
|
|
|
mbedtls_mpi_free( &S4 );
|
|
|
|
mbedtls_mpi_free( &S8 );
|
2022-09-02 11:46:09 +02:00
|
|
|
mbedtls_mpi_free( &A );
|
|
|
|
mbedtls_mpi_free( &B );
|
Add unit tests for the new function mbedtls_mpi_core_add_if() in bignum_new.c
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b, and gives the
sum unconditionally; the test code exercises a >= b and cond == 0 using these
values. The .data file gives two values for the carry out, which are for when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-add-if.pl - generate MPI tests in Perl for mbedtls_mpi_core_add_if()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @add_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_add_if();
}
sub generate_mbedtls_mpi_core_add_if {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_add_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@add_mpis) {
for my $bh (@add_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
# $b is the larger (or equal) of the two numbers. That's the number of limbs
# we'll be using.
my $bound4 = bound_mpi4($b);
my $bound8 = bound_mpi8($b);
my $r = $a + $b;
my ($r4, $carry4) = ($r->copy(), 0);
my ($r8, $carry8) = ($r->copy(), 0);
($carry4, $r4) = $r4->bdiv($bound4);
($carry8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh = (0x$rh4, carry $carry4)/(0x$rh8, carry $carry8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($rh4), $carry4, str($rh8), $carry8);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-23 17:26:52 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:59:59 +02:00
|
|
|
void mpi_core_sub( char * input_A, char * input_B,
|
2022-09-02 11:24:55 +02:00
|
|
|
char * input_X4, char * input_X8,
|
|
|
|
int carry )
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
{
|
2022-09-02 11:59:59 +02:00
|
|
|
mbedtls_mpi A, B, X4, X8;
|
|
|
|
mbedtls_mpi_uint *a = NULL;
|
|
|
|
mbedtls_mpi_uint *b = NULL;
|
|
|
|
mbedtls_mpi_uint *x = NULL; /* expected */
|
|
|
|
mbedtls_mpi_uint *r = NULL; /* result */
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
mbedtls_mpi_init( &A );
|
|
|
|
mbedtls_mpi_init( &B );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
mbedtls_mpi_init( &X4 );
|
|
|
|
mbedtls_mpi_init( &X8 );
|
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2022-09-02 11:59:59 +02:00
|
|
|
TEST_EQUAL( 1, A.s );
|
|
|
|
TEST_EQUAL( 1, B.s );
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 1, X4.s );
|
|
|
|
TEST_EQUAL( 1, X8.s );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
|
|
|
/* Get the number of limbs we will need */
|
2022-09-15 15:40:10 +02:00
|
|
|
size_t limbs = MAX( A.n, B.n );
|
2022-09-01 16:04:43 +02:00
|
|
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
|
|
|
/* We only need to work with X4 or X8, depending on sizeof(mbedtls_mpi_uint) */
|
|
|
|
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
|
|
|
|
|
|
|
/* The result shouldn't have more limbs than the longest input */
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( X->n, limbs );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
|
|
|
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
|
|
|
|
2022-09-15 15:07:18 +02:00
|
|
|
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
|
|
|
ASSERT_ALLOC( a, bytes );
|
|
|
|
ASSERT_ALLOC( b, bytes );
|
|
|
|
ASSERT_ALLOC( x, bytes );
|
|
|
|
ASSERT_ALLOC( r, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
|
|
|
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
2022-09-02 11:59:59 +02:00
|
|
|
* processed by mbedtls_mpi_core_sub()) are little endian, we can just
|
2022-09-15 15:07:18 +02:00
|
|
|
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
*/
|
2022-09-02 11:59:59 +02:00
|
|
|
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
|
|
|
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
|
|
|
|
memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
/* 1a) r = a - b => we should get the correct carry */
|
2022-09-15 15:32:38 +02:00
|
|
|
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
/* 1b) r = a - b => we should get the correct result */
|
|
|
|
ASSERT_COMPARE( r, bytes, x, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
/* 2 and 3 test "r may be aliased to a or b" */
|
|
|
|
/* 2a) r = a; r -= b => we should get the correct carry (use r to avoid clobbering a) */
|
|
|
|
memcpy( r, a, bytes );
|
2022-09-15 15:32:38 +02:00
|
|
|
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, b, limbs ) );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
/* 2b) r -= b => we should get the correct result */
|
|
|
|
ASSERT_COMPARE( r, bytes, x, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
/* 3a) r = b; r = a - r => we should get the correct carry (use r to avoid clobbering b) */
|
|
|
|
memcpy( r, b, bytes );
|
2022-09-15 15:32:38 +02:00
|
|
|
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, r, limbs ) );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
/* 3b) r = a - b => we should get the correct result */
|
|
|
|
ASSERT_COMPARE( r, bytes, x, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-20 10:08:31 +02:00
|
|
|
/* 4 tests "r may be aliased to [...] both" */
|
|
|
|
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
|
|
|
{
|
|
|
|
memcpy( r, b, bytes );
|
|
|
|
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, r, limbs ) );
|
|
|
|
ASSERT_COMPARE( r, bytes, x, bytes );
|
|
|
|
}
|
|
|
|
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
exit:
|
2022-09-02 11:59:59 +02:00
|
|
|
mbedtls_free( a );
|
|
|
|
mbedtls_free( b );
|
|
|
|
mbedtls_free( x );
|
|
|
|
mbedtls_free( r );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
|
2022-09-02 11:59:59 +02:00
|
|
|
mbedtls_mpi_free( &A );
|
|
|
|
mbedtls_mpi_free( &B );
|
Add unit tests for the new function mbedtls_mpi_core_sub() in bignum_new.c
The test cases use the following MPI values.
The .data file includes two results, for the cases when
sizeof(mbedtls_mpi_uint) == 4 or 8.
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffff 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-sub.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_sub()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @sub_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_sub();
}
sub generate_mbedtls_mpi_core_sub {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@sub_mpis) {
for my $bh (@sub_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my ($rh4, $rh8, $carry);
if ($a >= $b) {
my $r = $a - $b;
$rh4 = $rh8 = $r->to_hex();
$carry = 0;
} else {
my $r4 = bound_mpi4($b) + $a - $b;
my $r8 = bound_mpi8($b) + $a - $b;
$rh4 = $r4->to_hex();
$rh8 = $r8->to_hex();
$carry = 1;
}
my $desc = "$test_name #NUMBER: 0x$ah - 0x$bh = 0x$rh4/${rh8}EXPLAIN, carry ${carry}";
my $case = output($test_name, str($ah), str($bh), str($rh4), str($rh8), $carry);
push(@cases, [$case, $desc]);
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:43:54 +02:00
|
|
|
mbedtls_mpi_free( &X4 );
|
|
|
|
mbedtls_mpi_free( &X8 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 12:16:39 +02:00
|
|
|
void mpi_core_mla( char * input_A, char * input_B, char * input_S,
|
2022-09-02 11:24:55 +02:00
|
|
|
char * input_X4, char * input_cy4,
|
|
|
|
char * input_X8, char * input_cy8 )
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
{
|
2022-09-02 12:16:39 +02:00
|
|
|
/* We are testing A += B * s; A, B are MPIs, s is a scalar.
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
*
|
2022-09-15 15:52:34 +02:00
|
|
|
* However, we encode s as an MPI in the .data file as the test framework
|
|
|
|
* currently only supports `int`-typed scalars, and that doesn't cover the
|
|
|
|
* full range of `mbedtls_mpi_uint`.
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
*
|
|
|
|
* We also have the different results for sizeof(mbedtls_mpi_uint) == 4 or 8.
|
|
|
|
*/
|
2022-09-02 12:16:39 +02:00
|
|
|
mbedtls_mpi A, B, S, X4, X8, cy4, cy8;
|
|
|
|
mbedtls_mpi_uint *a = NULL;
|
|
|
|
mbedtls_mpi_uint *x = NULL;
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
2022-09-02 12:16:39 +02:00
|
|
|
mbedtls_mpi_init( &A );
|
|
|
|
mbedtls_mpi_init( &B );
|
|
|
|
mbedtls_mpi_init( &S );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
mbedtls_mpi_init( &X4 );
|
|
|
|
mbedtls_mpi_init( &X8 );
|
|
|
|
mbedtls_mpi_init( &cy4 );
|
|
|
|
mbedtls_mpi_init( &cy8 );
|
|
|
|
|
2022-09-02 12:16:39 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) );
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy4, input_cy4 ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy8, input_cy8 ) );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
2022-09-02 12:16:39 +02:00
|
|
|
/* The MPI encoding of scalar s must be only 1 limb */
|
|
|
|
TEST_EQUAL( 1, S.n );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
|
|
|
/* We only need to work with X4 or X8, and cy4 or cy8, depending on sizeof(mbedtls_mpi_uint) */
|
|
|
|
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
|
|
|
mbedtls_mpi *cy = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &cy4 : &cy8;
|
|
|
|
|
|
|
|
/* The carry should only have one limb */
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 1, cy->n );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2022-09-02 12:16:39 +02:00
|
|
|
TEST_EQUAL( 1, A.s );
|
|
|
|
TEST_EQUAL( 1, B.s );
|
|
|
|
TEST_EQUAL( 1, S.s );
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 1, X->s );
|
|
|
|
TEST_EQUAL( 1, cy->s );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
|
|
|
/* Get the (max) number of limbs we will need */
|
2022-09-15 15:40:10 +02:00
|
|
|
size_t limbs = MAX( A.n, B.n );
|
2022-09-01 16:04:43 +02:00
|
|
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
|
|
|
/* The result shouldn't have more limbs than the longest input */
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( X->n, limbs );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
|
|
|
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
|
|
|
|
2022-09-15 15:07:18 +02:00
|
|
|
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
|
|
|
ASSERT_ALLOC( a, bytes );
|
|
|
|
ASSERT_ALLOC( x, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
|
|
|
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
2022-09-02 12:16:39 +02:00
|
|
|
* processed by mbedtls_mpi_core_mla()) are little endian, we can just
|
2022-09-15 15:07:18 +02:00
|
|
|
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()).
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
*/
|
2022-09-02 12:16:39 +02:00
|
|
|
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
|
|
|
memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
2022-09-02 12:16:39 +02:00
|
|
|
/* 1a) A += B * s => we should get the correct carry */
|
|
|
|
TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, B.p, B.n, *S.p ), *cy->p );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
2022-09-02 12:16:39 +02:00
|
|
|
/* 1b) A += B * s => we should get the correct result */
|
|
|
|
ASSERT_COMPARE( a, bytes, x, bytes );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
|
|
|
exit:
|
2022-09-02 12:16:39 +02:00
|
|
|
mbedtls_free( a );
|
|
|
|
mbedtls_free( x );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
|
2022-09-02 12:16:39 +02:00
|
|
|
mbedtls_mpi_free( &A );
|
|
|
|
mbedtls_mpi_free( &B );
|
|
|
|
mbedtls_mpi_free( &S );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
mbedtls_mpi_free( &X4 );
|
|
|
|
mbedtls_mpi_free( &X8 );
|
2022-09-02 12:16:39 +02:00
|
|
|
mbedtls_mpi_free( &cy4 );
|
|
|
|
mbedtls_mpi_free( &cy8 );
|
Add unit tests for the new function mbedtls_mpi_core_mla() in bignum_new.c
The test cases use the following MPI values:
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
and the following scalars. The .data files include two sets of results (final
accumulator and carry) for the cases sizeof(mbedtls_mpi_uint) == 4 or 8.
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
The lines in the .data file were generated by the following script
#!/usr/bin/env perl
#
# mpi-test-core-mla.pl - generate/run MPI tests in Perl for mbedtls_mpi_core_mla()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my @mla_mpis = qw(
0 1 fffe ffffffff 100000000 20000000000000 ffffffffffffffff
10000000000000000 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_scalars = qw(
0 3 fe ff ffff 10000 ffffffff 100000000 7f7f7f7f7f7f7f7f 8000000000000000
fffffffffffffffe
);
my @mla_full_mpis = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000 1f7f7f7f7f7f7f
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
10000000000000000 1234567890abcdef0
fffffffffffffffffefefefefefefefe fffffffffffffffffffffffffffffffe ffffffffffffffffffffffffffffffff
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
fffffffffffffffffffffffffffffffffffffffffffffffffefefefefefefefe
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
10000000000000000000000000000000000000000000000000000000000000000
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
my @mla_full_scalars = qw(
0 1 3 f fe ff 100 ff00 fffe ffff 10000
fffffffe ffffffff 100000000
8000000000000000 fefefefefefefefe fffffffffffffffe ffffffffffffffff
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mla();
}
sub generate_mbedtls_mpi_core_mla {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mla_mpis) {
for my $bh (@mla_mpis) {
for my $ch (@mla_scalars) {
# a += b * c (c is scalar)
# a_len >= b_len. need carry out.
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $c = Math::BigInt->from_hex($ch);
my $max = ($a > $b) ? $a : $b;
my $bound4 = bound_mpi4($max);
my $bound8 = bound_mpi8($max);
my $r = $a + $b * $c;
my ($r4, $cy4) = ($r->copy(), 0);
my ($r8, $cy8) = ($r->copy(), 0);
($cy4, $r4) = $r4->bdiv($bound4);
($cy8, $r8) = $r8->bdiv($bound8);
my $rh4 = $r4->to_hex();
my $rh8 = $r8->to_hex();
my $cyh4 = $cy4->to_hex();
my $cyh8 = $cy8->to_hex();
# If the scalar c is too big for 1 x 4-byte MPI, we can only run this test on a system with 8-byte MPIs
my $depends = mpi4s($c) > 1 ? "MBEDTLS_HAVE_INT64" : "";
my $desc = "$test_name #NUMBER: 0x$ah + 0x$bh * 0x$ch = (0x$rh4, carry 0x$cyh4)/(0x$rh8, carry 0x$cyh8)EXPLAIN";
my $case = output($test_name, str($ah), str($bh), str($ch), str($rh4), str($cyh4), str($rh8), str($cyh8));
push(@cases, [$case, $desc, $depends]);
}
}
}
output_cases(" (for when sizeof(mbedtls_mpi_uint) == 4/8)", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 06:45:19 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-08-17 07:17:00 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_montg_init( char * input_N, char * input_mm )
|
2022-08-17 07:17:00 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi N, mm;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &N );
|
|
|
|
mbedtls_mpi_init( &mm );
|
|
|
|
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &mm, input_mm ) );
|
2022-08-17 07:17:00 +02:00
|
|
|
|
|
|
|
/* The MPI encoding of mm should be 1 limb (sizeof(mbedtls_mpi_uint) == 8) or
|
|
|
|
* 2 limbs (sizeof(mbedtls_mpi_uint) == 4).
|
|
|
|
*
|
|
|
|
* The data file contains the expected result for sizeof(mbedtls_mpi_uint) == 8;
|
|
|
|
* for sizeof(mbedtls_mpi_uint) == 4 it's just the LSW of this.
|
|
|
|
*/
|
2022-08-24 18:45:58 +02:00
|
|
|
TEST_ASSERT( mm.n == 1 || mm.n == 2 );
|
2022-08-17 07:17:00 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 1, N.s );
|
|
|
|
TEST_EQUAL( 1, mm.s );
|
2022-08-17 07:17:00 +02:00
|
|
|
|
2022-09-15 16:05:59 +02:00
|
|
|
/* mbedtls_mpi_core_montmul_init() only returns a result, no error possible */
|
|
|
|
mbedtls_mpi_uint result = mbedtls_mpi_core_montmul_init( N.p );
|
2022-08-17 07:17:00 +02:00
|
|
|
|
|
|
|
/* Check we got the correct result */
|
|
|
|
TEST_EQUAL( result, mm.p[0] );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &N );
|
|
|
|
mbedtls_mpi_free( &mm );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
/* BEGIN_CASE */
|
2022-09-02 11:24:55 +02:00
|
|
|
void mpi_core_montmul( int limbs_AN4, int limbs_B4,
|
|
|
|
int limbs_AN8, int limbs_B8,
|
|
|
|
char * input_A,
|
|
|
|
char * input_B,
|
|
|
|
char * input_N,
|
|
|
|
char * input_X4,
|
|
|
|
char * input_X8 )
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
{
|
2022-08-05 17:59:43 +02:00
|
|
|
mbedtls_mpi A, B, N, X4, X8, T, R;
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
|
|
|
mbedtls_mpi_init( &A );
|
|
|
|
mbedtls_mpi_init( &B );
|
|
|
|
mbedtls_mpi_init( &N );
|
|
|
|
mbedtls_mpi_init( &X4 ); /* expected result, sizeof(mbedtls_mpi_uint) == 4 */
|
|
|
|
mbedtls_mpi_init( &X8 ); /* expected result, sizeof(mbedtls_mpi_uint) == 8 */
|
|
|
|
mbedtls_mpi_init( &T );
|
2022-08-05 17:59:43 +02:00
|
|
|
mbedtls_mpi_init( &R ); /* for the result */
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
|
|
|
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
|
|
|
|
|
|
|
int limbs_AN = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_AN4 : limbs_AN8;
|
|
|
|
int limbs_B = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_B4 : limbs_B8;
|
|
|
|
|
2022-09-15 15:22:35 +02:00
|
|
|
TEST_LE_U( A.n, (size_t)limbs_AN );
|
|
|
|
TEST_LE_U( X->n, (size_t)limbs_AN );
|
|
|
|
TEST_LE_U( B.n, (size_t)limbs_B );
|
|
|
|
TEST_LE_U( limbs_B, limbs_AN );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 1, A.s );
|
|
|
|
TEST_EQUAL( 1, B.s );
|
|
|
|
TEST_EQUAL( 1, N.s );
|
|
|
|
TEST_EQUAL( 1, X->s );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( &A, limbs_AN ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( &N, limbs_AN ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( X, limbs_AN ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( &B, limbs_B ) );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ) );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
|
|
|
/* Calculate the Montgomery constant (this is unit tested separately) */
|
2022-09-15 16:05:59 +02:00
|
|
|
mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N.p );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
2022-09-01 14:02:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( &R, limbs_AN ) ); /* ensure it's got the right number of limbs */
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
2022-08-05 17:59:43 +02:00
|
|
|
mbedtls_mpi_core_montmul( R.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
2022-09-01 16:04:43 +02:00
|
|
|
size_t bytes = N.n * sizeof(mbedtls_mpi_uint);
|
|
|
|
ASSERT_COMPARE( R.p, bytes, X->p, bytes );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &A );
|
|
|
|
mbedtls_mpi_free( &B );
|
|
|
|
mbedtls_mpi_free( &N );
|
|
|
|
mbedtls_mpi_free( &X4 );
|
|
|
|
mbedtls_mpi_free( &X8 );
|
|
|
|
mbedtls_mpi_free( &T );
|
2022-08-05 17:59:43 +02:00
|
|
|
mbedtls_mpi_free( &R );
|
Add unit tests for bignum_new.c:mbedtls_mpi_core_montmul()
These tests are also used to test the existing mpi_montmul() function (which
too is renamed with mbedtls_ prefix). Some of these are replays of captured
invocations during unit test runs. Others are generated. They use a mixture
of primes and odd numbers for N, with four randomly-generated cases for each N.
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-montmul.pl - generate MPI tests in Perl for mbedtls_mpi_core_montmul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_montmul();
}
# XXX mbedtls_mpi_grow() and mbedtls_mpi_shrink() work in little-endian manner
# \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
#
# \param[out] X The destination MPI, as a big endian array of length \p n.
# On successful completion, X contains the result of
# the multiplication A * B * R^-1 mod N where
# R = (2^ciL)^n.
# \param[in] A Big endian presentation of first operand.
# Must have exactly \p n limbs.
# \param[in] B Big endian presentation of second operand.
# \param[in] B_len The number of limbs in \p B.
# \param[in] N Big endian presentation of the modulus.
# This must be odd and have exactly \p n limbs.
# \param[in] n The number of limbs in \p X, \p A, \p N.
# \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
# This can be calculated by `mbedtls_mpi_montg_init()`.
# \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
# Its initial content is unused and
# its final content is indeterminate.
#
# void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
# const mbedtls_mpi_uint *A,
# const mbedtls_mpi_uint *B, size_t B_len,
# const mbedtls_mpi_uint *N, size_t n,
# mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
sub generate_mbedtls_mpi_core_montmul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
my @replay = (
# [ limbsAN_4, limbsB_4, limbsAN_8, limbsB_8, hexA, hexB, hexN, hexExpected ]
[ 2, 1, 1, 1, "19", "1", "1D", "18" ],
[ 2, 1, 1, 1, "7", "1", "9", "1" ],
[ 2, 1, 1, 1, "4", "1", "9", "7" ],
#montmul:
#A.n = 3
#A.p = FFFE000000008004
# 0000000000007FFC
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 3
#N.p = 0000000000000001
# 0000000000008000
# 0000000000000000
#mm = FFFFFFFFFFFFFFFF
#res.n = 3
#res.p = EFFF9FFF3FFF8001
# 0000000000007FFF
# 0000000000000000
#[ "MBEDTLS_HAVE_INT32", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "2000C001800100000000" ],
#[ "MBEDTLS_HAVE_INT64", 3, 1, 3, "7FFCFFFE000000008004", "1", "80000000000000000001", "7FFFEFFF9FFF3FFF8001" ],
[ 12, 1, 6, 1, "3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402B07CC8C7CE0B781C641A1EA8DB2F4343", "1", "66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A594B6BED27A168D31D4A52F88925AA8F5", "36E139AEA55215609D2816998ED020BBBD96C37890F65171D948E9BC7CBAA4D9325D24D6A3C12710F10A09FA08AB87" ],
#A.n = 5
#A.p = 340E918CE03C6211
# 9888165CB75BFA1F
# FCCE74B999E470CA
# 1E442976B0E63D64
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 4
#N.p = 8054B3D124D0E561
# 92A338655DCE4CA8
# E28581ECD892E0F5
# B3A119602EE213CD
#mm = E41CFB909805815F
#res.n = 5
#res.p = 0E65383B59F8CA5B
# B103B17A2EEF84E6
# F23BC08FD0801C55
# 38EB7749F4A5DA80
# 0000000000000000
[ 8, 1, 4, 1, "1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211", "1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561", "38EB7749F4A5DA80F23BC08FD0801C55B103B17A2EEF84E60E65383B59F8CA5B" ],
#A.n = 12
#A.p = 542306BCA7A2366E
# D2780B2B4968F8D8
# CBDFC696104353E4
# 7776839B0AC9DB23
# B7E125BE407E7415
# D711917FD7537E13
# 82392870D6D08F87
# D83ED5FA38560FFB
# 9994B0FED1D2A8D3
# 63C65413F57249F5
# 007CF5AC97304E0B
# 0000000000000000
#B.n = 1
#B.p = 0000000000000001
#N.n = 11
#N.p = E1AD22CEB7BA0123
# 32B2A6AA42ADA923
# C56C62082912B661
# C6F0EAD752500A32
# DBC8D651793E93C9
# 0B2F60D99CC1950C
# 5B4CDCB5734C58F9
# 09D3CB5BC5585472
# 9A2C2BE12ED487A8
# BE09A8111926AAA3
# 0284139EA19C139E
#mm = C02E2164B293C975
#res.n = 12
#res.p = F6B14471839D8D31
# FF843ED3B17C44D7
# 1C3D52C7CB9E0BA6
# 82F3590C866BF9F8
# 49C371DB2A4FB164
# 964ECA2527A031ED
# FAACEC6982E0E5BE
# 1F70C4CB2426AEE1
# 2C92B02886267AB4
# 0630B14113BEAD74
# 01E4426A3D6C425F
# 0000000000000000
[ 22, 1, 11, 1, "7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E", "1", "284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC55854725B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123", "1E4426A3D6C425F0630B14113BEAD742C92B02886267AB41F70C4CB2426AEE1FAACEC6982E0E5BE964ECA2527A031ED49C371DB2A4FB16482F3590C866BF9F81C3D52C7CB9E0BA6FF843ED3B17C44D7F6B14471839D8D31" ],
);
for my $c (@replay) {
# For all of these, la4 = 2 * la8, so $xh4 == $xh8 (so we just have $xh)
my ($la4, $lb4, $la8, $lb8, $ah, $bh, $nh, $xh) = @$c; # limbs(A), limbs(B), limbs(N), (A, B, N, expected) hex
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $desc = "$test_name #NUMBER (replay)";
# mbedtls_mpi_core_montmul:mpiSize:limbs(A,N):limbs(B):<A>:<B>:<N>:<expected4>:<expected8>
# (just repeat $xh, as la4 = 2 * la8, so $xh4 == $xh8)
my $case = output($test_name, $la4, $lb4, $la8, $lb8, str($ah), str($bh), str($nh), str($xh), str($xh));
push(@cases, [$case, $desc]);
}
# see mpi-modmul-gen.pl for the source of these test cases
my @generate = (
# [ hexN, hexA, hexB, info ]
[ "3", "2", "2", "" ],
[ "3", "1", "2", "" ],
[ "3", "2", "1", "" ],
[ "7", "6", "5", "" ],
[ "7", "3", "4", "" ],
[ "7", "1", "6", "" ],
[ "7", "5", "6", "" ],
[ "B", "3", "4", "" ],
[ "B", "7", "4", "" ],
[ "B", "9", "7", "" ],
[ "B", "2", "a", "" ],
[ "29", "25", "16", "(0x29 is prime)" ],
[ "29", "8", "28", "" ],
[ "29", "18", "21", "" ],
[ "29", "15", "f", "" ],
[ "FF", "e2", "ea", "" ],
[ "FF", "43", "72", "" ],
[ "FF", "d8", "70", "" ],
[ "FF", "3c", "7c", "" ],
[ "101", "99", "b9", "(0x101 is prime)" ],
[ "101", "65", "b2", "" ],
[ "101", "81", "32", "" ],
[ "101", "51", "dd", "" ],
[ "38B", "d5", "143", "(0x38B is prime)" ],
[ "38B", "3d", "387", "" ],
[ "38B", "160", "2e5", "" ],
[ "38B", "10f", "137", "" ],
[ "8003", "7dac", "25a", "(0x8003 is prime)" ],
[ "8003", "6f1c", "3286", "" ],
[ "8003", "59ed", "2f3f", "" ],
[ "8003", "6893", "736d", "" ],
[ "10001", "d199", "2832", "(0x10001 is prime)" ],
[ "10001", "c3b2", "3e5b", "" ],
[ "10001", "abe4", "214e", "" ],
[ "10001", "4360", "a05d", "" ],
[ "7F7F7", "3f5a1", "165b2", "" ],
[ "7F7F7", "3bd29", "37863", "" ],
[ "7F7F7", "60c47", "64819", "" ],
[ "7F7F7", "16584", "12c49", "" ],
[ "800009", "1ff03f", "610347", "(0x800009 is prime)" ],
[ "800009", "340fd5", "19812e", "" ],
[ "800009", "3fe2e8", "4d0dc7", "" ],
[ "800009", "40356", "e6392", "" ],
[ "100002B", "dd8a1d", "266c0e", "(0x100002B is prime)" ],
[ "100002B", "3fa1cb", "847fd6", "" ],
[ "100002B", "5f439d", "5c3196", "" ],
[ "100002B", "18d645", "f72dc6", "" ],
[ "37EEE9D", "20051ad", "37def6e", "(0x37EEE9D is prime)" ],
[ "37EEE9D", "2ec140b", "3580dbf", "" ],
[ "37EEE9D", "1d91b46", "190d4fc", "" ],
[ "37EEE9D", "34e488d", "1224d24", "" ],
[ "8000000B", "2a4fe2cb", "263466a9", "(0x8000000B is prime)" ],
[ "8000000B", "5643fe94", "29a1aefa", "" ],
[ "8000000B", "29633513", "7b007ac4", "" ],
[ "8000000B", "2439cef5", "5c9d5a47", "" ],
[ "8CD626B9", "4de3cfaa", "50dea178", "(0x8CD626B9 is prime)" ],
[ "8CD626B9", "b8b8563", "10dbbbac", "" ],
[ "8CD626B9", "4e8a6151", "5574ec19", "" ],
[ "8CD626B9", "69224878", "309cfc23", "" ],
[ "10000000F", "fb6f7fb6", "afb05423", "(0x10000000F is prime)" ],
[ "10000000F", "8391a243", "26034dcd", "" ],
[ "10000000F", "d26b98c", "14b2d6aa", "" ],
[ "10000000F", "6b9f1371", "a21daf1d", "" ],
[ "174876E7E9", "9f49435ad", "c8264ade8", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "174876E7E9", "c402da434", "1fb427acf", "" ],
[ "174876E7E9", "f6ebc2bb1", "1096d39f2a", "" ],
[ "174876E7E9", "153b7f7b6b", "878fda8ff", "" ],
[ "8000000017", "2c1adbb8d6", "4384d2d3c6", "(0x8000000017 is prime)" ],
[ "8000000017", "2e4f9cf5fb", "794f3443d9", "" ],
[ "8000000017", "149e495582", "3802b8f7b7", "" ],
[ "8000000017", "7b9d49df82", "69c68a442a", "" ],
[ "864CB9076D", "683a134600", "6dd80ea9f6", "(0x864CB9076D is prime)" ],
[ "864CB9076D", "13a870ff0d", "59b099694a", "" ],
[ "864CB9076D", "37d06b0e63", "4d2147e46f", "" ],
[ "864CB9076D", "661714f8f4", "22e55df507", "" ],
[ "F7F7F7F7F7", "2f0a96363", "52693307b4", "" ],
[ "F7F7F7F7F7", "3c85078e64", "f2275ecb6d", "" ],
[ "F7F7F7F7F7", "352dae68d1", "707775b4c6", "" ],
[ "F7F7F7F7F7", "37ae0f3e0b", "912113040f", "" ],
[ "1000000000F", "6dada15e31", "f58ed9eff7", "(0x1000000000F is prime)" ],
[ "1000000000F", "69627a7c89", "cfb5ebd13d", "" ],
[ "1000000000F", "a5e1ad239b", "afc030c731", "" ],
[ "1000000000F", "f1cc45f4c5", "c64ad607c8", "" ],
[ "800000000005", "2ebad87d2e31", "4c72d90bca78", "(0x800000000005 is prime)" ],
[ "800000000005", "a30b3cc50d", "29ac4fe59490", "" ],
[ "800000000005", "33674e9647b4", "5ec7ee7e72d3", "" ],
[ "800000000005", "3d956f474f61", "74070040257d", "" ],
[ "800795D9BA47", "48348e3717d6", "43fcb4399571", "(0x800795D9BA47 is prime)" ],
[ "800795D9BA47", "5234c03cc99b", "2f3cccb87803", "" ],
[ "800795D9BA47", "3ed13db194ab", "44b8f4ba7030", "" ],
[ "800795D9BA47", "1c11e843bfdb", "95bd1b47b08", "" ],
[ "1000000000015", "a81d11cb81fd", "1e5753a3f33d", "(0x1000000000015 is prime)" ],
[ "1000000000015", "688c4db99232", "36fc0cf7ed", "" ],
[ "1000000000015", "f0720cc07e07", "fc76140ed903", "" ],
[ "1000000000015", "2ec61f8d17d1", "d270c85e36d2", "" ],
[ "100000000000051", "6a24cd3ab63820", "ed4aad55e5e348", "(0x100000000000051 is prime)" ],
[ "100000000000051", "e680c160d3b248", "31e0d8840ed510", "" ],
[ "100000000000051", "a80637e9aebc38", "bb81decc4e1738", "" ],
[ "100000000000051", "9afa5a59e9d630", "be9e65a6d42938", "" ],
[ "ABCDEF0123456789", "ab5e104eeb71c000", "2cffbd639e9fea00", "" ],
[ "ABCDEF0123456789", "197b867547f68a00", "44b796cf94654800", "" ],
[ "ABCDEF0123456789", "329f9483a04f2c00", "9892f76961d0f000", "" ],
[ "ABCDEF0123456789", "4a2e12dfb4545000", "1aa3e89a69794500", "" ],
[ "25A55A46E5DA99C71C7", "8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "25A55A46E5DA99C71C7", "1b8d960ea277e3f5500", "14418aa980e37dd000", "" ],
[ "25A55A46E5DA99C71C7", "7314524977e8075980", "8172fa45618ccd0d80", "" ],
[ "25A55A46E5DA99C71C7", "ca14f031769be63580", "147a2f3cf2964ca9400", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "314DC643FB763F2B8C0E2DE00879", "a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000", "" ],
[ "314DC643FB763F2B8C0E2DE00879", "e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "1ea3ade786a095d978d387f30df9f20000000", "127c448575f04af5a367a7be06c7da0000000", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "16e15b0ca82764e72e38357b1f10a20000000", "43e2355d8514bbe22b0838fdc3983a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "be39332529d93f25c3d116c004c620000000", "5cccec42370a0a2c89c6772da801a0000000", "" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "ecaa468d90de0eeda474d39b3e1fc0000000", "1e714554018de6dc0fe576bfd3b5660000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000", "8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000", "6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000", "7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000", "" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000", "530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000", "75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000", "397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000", "ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000", "" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000", "925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000", "51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000", "520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000", "41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000", "" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000", "6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbfa1e6000000000000000000000000", "1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a00315800000000000000000000000", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de4740800000000000000000000000", "c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f7096034193c800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba5ee800000000000000000000000", "1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c3365b800000000000000000000000", "" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd01a800000000000000000000000", "54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b8b000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b2308991784306d48f78686115ee19e25400000000000000000000000000000000", "677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0a0148d1625745e9c4c827300000000000000000000000000000000", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865cb6464174795d07e30015b80000000000000000000000000000000", "6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c79a52af190abd51739009a900000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd63d1d8cfbc1d498162786ba00000000000000000000000000000000", "1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb4c2f6d0667f9467fdcf33400000000000000000000000000000000", "" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5a9f0ad04facb69aa3715f00000000000000000000000000000000", "764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d799020e244a835a145ec9800000000000000000000000000000000", "" ],
);
my %described = ();
for my $g (@generate) {
my ($nh, $ah, $bh, $info) = @$g;
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
my $n = Math::BigInt->from_hex($nh);
my $ln4 = mpi4s($n);
my $la4 = mpi4s($a);
my $lb4 = mpi4s($b);
my $ln8 = mpi8s($n);
my $la8 = mpi8s($a);
my $lb8 = mpi8s($b);
my $r4 = bound_mpi4($n->copy());
my $i4 = $r4->copy()->bmodinv($n);
my $x4 = $a * $b * $i4;
$x4->bmod($n);
my $xh4 = Math::BigInt->new($x4)->to_hex();
my $r8 = bound_mpi8($n->copy());
my $i8 = $r8->copy()->bmodinv($n);
my $x8 = $a * $b * $i8;
$x8->bmod($n);
my $xh8 = Math::BigInt->new($x8)->to_hex();
die("") if $la4 > $ln4 || $la8 > $ln8;
my $desc = "$test_name #NUMBER (gen)";
if ($ln4 > 1) {
if (!$described{"2-MPI4"}) {
$desc .= " (start of 2-MPI 4-byte bignums)";
$described{"2-MPI4"} = 1;
}
}
if ($ln8 > 1) {
if (!$described{"2-MPI8"}) {
$desc .= " (start of 2-MPI 8-byte bignums)";
$described{"2-MPI8"} = 1;
}
}
if (length($info) && !$described{$info}) {
$desc .= " " . $info;
$described{$info} = 1;
}
my $case = output($test_name, $ln4, $lb4, $ln8, $lb8, str($ah), str($bh), str($nh), str($xh4), str($xh8));
#push(@cases, [$case, $desc, "MBEDTLS_HAVE_INT64"]); -- now doing it differently
push(@cases, [$case, $desc]);
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
# The first number (a power of 2) that won't fit in the number of MPIs
# needed for the given number
sub bound_mpi4 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi4($_[0]));
}
sub bound_mpi8 {
my $one = Math::BigInt->new(1); # blsft modifies caller
return $one->blsft(bits_mpi8($_[0]));
}
# How many bits (a multiple of 32) needed to store the specified number
# when using 4-byte MPIs
sub bits_mpi4 {
return 32 * mpi4s($_[0]);
}
# How many bits (a multiple of 64) needed to store the specified number
# when using 8-byte MPIs
sub bits_mpi8 {
return 64 * mpi8s($_[0]);
}
# How many 4-byte MPIs needed to store the specified number
sub mpi4s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 7) / 8);
}
# How many 8-byte MPIs needed to store the specified number
sub mpi8s {
my ($n) = @_;
my $h = $n->to_hex();
return int((length($h) + 15) / 16);
}
sub output {
#run_test(@_);
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
The data for the generated test cases (@generate) for mpi-test-core-montmul.pl
was created by
```
#!/usr/bin/env perl
#
# mpi-modmul-gen.pl - randomly generate test cases for mpi-test-core-montmul.pl
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my %seen = ();
my @primes = (
"3",
"7",
"B",
"29",
"101",
"38B",
"8003",
"10001",
"800009",
"100002B",
"37EEE9D",
"8000000B",
"8CD626B9",
# From here they require > 1 4-byte MPI
"10000000F",
"174876E7E9",
"8000000017",
"864CB9076D",
"1000000000F",
"800000000005",
"800795D9BA47",
"1000000000015",
"100000000000051",
# From here they require > 1 8-byte MPI
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
# 10^143 + 3^4: (which is prime)
# 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051",
);
my %prime = map { $_ => 1 } @primes;
my @moduli = (
[ "3", "" ],
[ "7", "" ],
[ "B", "" ],
[ "29", "" ],
[ "FF", "" ],
[ "101", "" ],
[ "38B", "" ],
[ "8003", "" ],
[ "10001", "" ],
[ "7F7F7", "" ],
[ "800009", "" ],
[ "100002B", "" ],
[ "37EEE9D", "" ],
[ "8000000B", "" ],
[ "8CD626B9", "" ],
[ "10000000F", "" ],
[ "174876E7E9", "0x174876E7E9 is prime (dec) 99999999977" ],
[ "8000000017", "" ],
[ "864CB9076D", "" ],
[ "F7F7F7F7F7", "" ],
[ "1000000000F", "" ],
[ "800000000005", "" ],
[ "800795D9BA47", "" ],
[ "1000000000015", "" ],
[ "100000000000051", "" ],
[ "ABCDEF0123456789", "" ],
[ "25A55A46E5DA99C71C7", "0x25A55A46E5DA99C71C7 is the 3rd repunit prime (dec) 11111111111111111111111" ],
[ "314DC643FB763F2B8C0E2DE00879", "0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3" ],
[ "47BF19662275FA2F6845C74942ED1D852E521", "0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4" ],
[ "97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", "0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931 is (dec) 99999999977^6" ],
[ "DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", "0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499 is (dec) 99999999977^7" ],
[ "141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41", "0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41 is 99999999977^8" ],
[ "2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451", "0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10" ],
[ "8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051", "0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime, (dec) 10^143 + 3^4" ], # 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081
);
#99999999977^2:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9
#99999999977^3:
#ibase=16 ; obase=10 ; 174876E7E9*174876E7E9*174876E7E9
#99999999977^2: 21E19E0C58BACE25211
#99999999977^3: 314DC643FB763F2B8C0E2DE00879
#99999999977^4: 47BF19662275FA2F6845C74942ED1D852E521
#99999999977^5: 6867A5A664437D20ED7941408583AADA2193CE95695209
#99999999977^6: 97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931
#99999999977^7: DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499
#99999999977^8: 141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41
#99999999977^9: 1D42AEA1837AA78C6339224E9B39A483E4AAAF12CE7752E1EA1681082CBC8AB056A36B6299557D7A029
#99999999977^10: 2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC902713E40F51E3B3C214EDFABC451
my %mentioned = ();
for my $mod (@moduli) {
my ($nh, $info) = @$mod;
my $n = Math::BigInt->from_hex($nh);
my $xxx = $n->to_hex();
die("$xxx != $nh") unless lc($xxx) eq lc($nh);
my $cases = ($n < 5) ? 3 : 4;
for (my $case = 0; $case < $cases; $case++) {
my ($a, $b);
for ($a = 0; $a == 0; ) {
$a = int(rand($n));
}
for ($b = 0; $b == 0; ) {
$b = int(rand($n));
}
my $cstr = "$a|$b|$n";
if (exists($seen{$cstr})) { # don't repeat ourselves
$case--;
next;
}
$seen{$cstr} = 1;
my $ah = Math::BigInt->new($a)->to_hex();
my $bh = Math::BigInt->new($b)->to_hex();
my $desc = "";
if (length($info)) {
$desc = $info if !$mentioned{$info};
$mentioned{$info} = 1;
} elsif (length($nh) > 1 && $prime{$nh} && !$mentioned{$nh}) {
$desc = "(0x$nh is prime)";
$mentioned{$nh} = 1;
}
print <<EOF;
[ "$nh", "$ah", "$bh", "$desc" ],
EOF
}
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
2022-08-17 07:29:32 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mpi_selftest( )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2016-09-09 10:10:28 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|