Cryptography Reference
In-Depth Information
Listing 3-15: “huge.c” right_shift
static void right_shift( huge *h1 )
{
int i;
unsigned int old_carry, carry = 0;
i = 0;
do
{
old_carry = carry;
carry = ( h1->rep[ i ] & 0x01 ) << 7;
h1->rep[ i ] = ( h1->rep[ i ] >> 1 ) | old_carry;
}
while ( ++i < h1->size );
contract( h1 );
}
Optimizing for Modulo Arithmetic
One optimization you might as well make is to allow the caller to indicate that
the quotient is unimportant. For public-key cryptography operations you never
actually care what the quotient is; you're interested in the remainder, which the
dividend operator is turned into after a call to divide . Extend divide just a bit
to enable the caller to pass in a NULL pointer for quotient that indicates the
quotient itself should not be computed, as shown in Listing 3-16.
Listing 3-16: “huge.c” divide
void divide( huge *dividend, huge *divisor, huge *quotient )
{
int i, bit_size, bit_position;
bit_size = bit_position = 0;
while ( compare( divisor, dividend ) < 0 )
{
left_shift( divisor );
bit_size++;
}
if ( quotient )
{
quotient->size = ( bit_size / 8 ) + 1;
quotient->rep = ( unsigned char * )
calloc( quotient->size, sizeof( unsigned char ) );
memset( quotient->rep, 0, quotient->size );
}
Search WWH ::




Custom Search