- Added mpi_get_bit() and mpi_set_bit() individual bit setter/getter functions.

This commit is contained in:
Paul Bakker 2011-05-18 15:47:11 +00:00
parent 831a755d9e
commit 2f5947e1f6
5 changed files with 120 additions and 0 deletions

View file

@ -9,6 +9,8 @@ Features
by undefining POLARSSL_FS_IO
* A error_strerror function() has been added to translate between
error codes and their description.
* Added mpi_get_bit() and mpi_set_bit() individual bit setter/getter
functions.
Changes
* Major argument / variable rewrite. Introduced use of size_t

View file

@ -146,6 +146,32 @@ void mpi_swap( mpi *X, mpi *Y );
*/
int mpi_lset( mpi *X, t_sint z );
/*
* \brief Get a specific bit from X
*
* \param X MPI to use
* \param pos Zero-based index of the bit in X
*
* \return Either a 0 or a 1
*/
int mpi_get_bit( mpi *X, size_t pos );
/*
* \brief Set a bit of X to a specific value of 0 or 1
*
* \note Will grow X if necessary to set a bit to 1 in a not yet
* existing limb. Will not grow if bit should be set to 0
*
* \param X MPI to use
* \param pos Zero-based index of the bit in X
* \param val The value to set the bit to (0 or 1)
*
* \return 0 if successful,
* 1 if memory allocation failed,
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
*/
int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
/**
* \brief Return the number of least significant bits
*

View file

@ -170,6 +170,44 @@ cleanup:
return( ret );
}
/*
* Get a specific bit
*/
int mpi_get_bit( mpi *X, size_t pos )
{
if( X->n * biL <= pos )
return( 0 );
return ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01;
}
/*
* Set a bit to a specific value of 0 or 1
*/
int mpi_set_bit( mpi *X, size_t pos, unsigned char val )
{
int ret = 0;
size_t off = pos / biL;
size_t idx = pos % biL;
if( val != 0 && val != 1 )
return POLARSSL_ERR_MPI_BAD_INPUT_DATA;
if( X->n * biL <= pos )
{
if( val == 0 )
return ( 0 );
MPI_CHK( mpi_grow( X, off + 1 ) );
}
X->p[off] = ( X->p[off] & ~( 0x01 << idx ) ) | ( val << idx );
cleanup:
return( ret );
}
/*
* Return the number of least significant bits
*/

View file

@ -518,6 +518,33 @@ mpi_is_prime:10:"32452867":0
Test mpi_is_prime #20
mpi_is_prime:10:"49979687":0
Test bit getting (Value bit 25)
mpi_get_bit:10:"49979687":25:1
Test bit getting (Larger but same limb)
mpi_get_bit:10:"49979687":26:0
Test bit getting (Larger and non-existing limb)
mpi_get_bit:10:"49979687":500:0
Test bit getting (Value bit 24)
mpi_get_bit:10:"49979687":24:0
Test bit getting (Value bit 23)
mpi_get_bit:10:"49979687":23:1
Test bit set (Change existing value with a 1)
mpi_set_bit:10:"49979687":24:1:10:"66756903"
Test bit set (Change existing value with a 0)
mpi_set_bit:10:"49979687":25:0:10:"16425255"
Test bit set (Add above existing limbs with a 0)
mpi_set_bit:10:"49979687":80:0:10:"49979687"
Test bit set (Add above existing limbs with a 1)
mpi_set_bit:10:"49979687":80:1:10:"1208925819614629224685863"
MPI Selftest
depends_on:POLARSSL_SELF_TEST
mpi_selftest:

View file

@ -142,6 +142,33 @@ mpi_write_file:radix_X:input_X:output_radix:output_file
}
END_CASE
BEGIN_CASE
mpi_get_bit:radix_X:input_X:pos:val
{
mpi X;
mpi_init( &X );
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );
TEST_ASSERT( mpi_get_bit( &X, {pos} ) == {val} );
mpi_free( &X );
}
END_CASE
BEGIN_CASE
mpi_set_bit:radix_X:input_X:pos:val:radix_Y:output_Y
{
mpi X, Y;
mpi_init( &X ); mpi_init( &Y );
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );
TEST_ASSERT( mpi_read_string( &Y, {radix_Y}, {output_Y} ) == 0 );
TEST_ASSERT( mpi_set_bit( &X, {pos}, {val} ) == 0 );
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
mpi_free( &X );
}
END_CASE
BEGIN_CASE
mpi_lsb:radix_X:input_X:nr_bits
{