mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-25 17:55:42 +01:00
Add mpi_shrink()
This commit is contained in:
parent
e282012219
commit
5868163e07
@ -201,6 +201,17 @@ void mpi_free( mpi *X );
|
||||
*/
|
||||
int mpi_grow( mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Resize down, keeping at least the specified number of limbs
|
||||
*
|
||||
* \param X MPI to shrink
|
||||
* \param nblimbs The minimum number of limbs to keep
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_shrink( mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Copy the contents of Y into X
|
||||
*
|
||||
|
@ -119,6 +119,45 @@ int mpi_grow( mpi *X, size_t nblimbs )
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Resize down as much as possible,
|
||||
* while keeping at least the specified number of limbs
|
||||
*/
|
||||
int mpi_shrink( mpi *X, size_t nblimbs )
|
||||
{
|
||||
t_uint *p;
|
||||
size_t i;
|
||||
|
||||
/* Actually resize up in this case */
|
||||
if( X->n <= nblimbs )
|
||||
return( mpi_grow( X, nblimbs ) );
|
||||
|
||||
for( i = X->n - 1; i > 0; i-- )
|
||||
if( X->p[i] != 0 )
|
||||
break;
|
||||
i++;
|
||||
|
||||
if( i < nblimbs )
|
||||
i = nblimbs;
|
||||
|
||||
if( ( p = (t_uint *) polarssl_malloc( i * ciL ) ) == NULL )
|
||||
return( POLARSSL_ERR_MPI_MALLOC_FAILED );
|
||||
|
||||
memset( p, 0, i * ciL );
|
||||
|
||||
if( X->p != NULL )
|
||||
{
|
||||
memcpy( p, X->p, i * ciL );
|
||||
memset( X->p, 0, X->n * ciL );
|
||||
polarssl_free( X->p );
|
||||
}
|
||||
|
||||
X->n = i;
|
||||
X->p = p;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy the contents of Y into X
|
||||
*/
|
||||
|
@ -181,6 +181,30 @@ mpi_copy_self:14
|
||||
Base test mpi_swap #1
|
||||
mpi_swap:0:1500
|
||||
|
||||
Test mpi_shrink #1
|
||||
mpi_shrink:2:2:4:4
|
||||
|
||||
Test mpi_shrink #2
|
||||
mpi_shrink:4:2:4:4
|
||||
|
||||
Test mpi_shrink #3
|
||||
mpi_shrink:8:2:4:4
|
||||
|
||||
Test mpi_shrink #4
|
||||
mpi_shrink:8:4:4:4
|
||||
|
||||
Test mpi_shrink #5
|
||||
mpi_shrink:8:6:4:6
|
||||
|
||||
Test mpi_shrink #6
|
||||
mpi_shrink:4:2:0:2
|
||||
|
||||
Test mpi_shrink #7
|
||||
mpi_shrink:4:1:0:1
|
||||
|
||||
Test mpi_shrink #8
|
||||
mpi_shrink:4:0:0:1
|
||||
|
||||
Base test mpi_add_abs #1
|
||||
mpi_add_abs:10:"12345678":10:"642531":10:"12988209"
|
||||
|
||||
|
@ -292,6 +292,22 @@ void mpi_copy_self( int input_X )
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_shrink( int before, int used, int min, int after )
|
||||
{
|
||||
mpi X;
|
||||
mpi_init( &X );
|
||||
|
||||
TEST_ASSERT( mpi_grow( &X, before ) == 0 );
|
||||
TEST_ASSERT( used <= before );
|
||||
memset( X.p, 0x2a, used * sizeof( t_uint ) );
|
||||
TEST_ASSERT( mpi_shrink( &X, min ) == 0 );
|
||||
TEST_ASSERT( X.n == (size_t) after );
|
||||
|
||||
mpi_free( &X );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_swap( int input_X, int input_Y )
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user