Cryptography Reference
In-Depth Information
}
free_huge( &tmp );
}
// Use the stored sign to set the result
h1->sign = result_sign;
}
In fact, you can probably see how you could collapse these two functions
into one single function if you were so inclined.
7. Multiplication and division are even easier. The magnitudes of the results
are the same as they were in the unsigned case, and the sign of the result
is positive if the signs are equal and negative if the signs are unequal. This
is illustrated in Listing 3-34.
Listing 3-34: “huge.c” multiply with negative number support
void multiply( huge *h1, huge *h2 )
{
unsigned char mask;
unsigned int i;
int result_sign;
huge temp;
set_huge( &temp, 0 );
copy_huge( &temp, h1 );
result_sign = !( h1->sign == h2->sign );
...
}
while ( i );
h1->sign = result_sign;
}
8. To support signed numbers at division time, you don't even need to
remember a temporary sign because quotients are always initialized
dynamically as shown in Listing 3-35.
Listing 3-35: “huge.c” divide with negative number support
void divide( huge *dividend, huge *divisor, huge *quotient )
{
int i, bit_size, bit_position;
...
if ( quotient )
{
quotient->sign = !( dividend->sign == dividend->sign );
Search WWH ::




Custom Search