Cryptography Reference
In-Depth Information
{
unsigned char *tmp = h->rep;
h->size++;
h->rep = ( unsigned char * )
calloc( h->size, sizeof( unsigned char ) );
memcpy( h->rep + 1, tmp,
( h->size - 1 ) * sizeof( unsigned char ) );
h->rep[ 0 ] = 0x01;
free( tmp );
}
The code in Listing 3-6 should look familiar. It is a special case of the expan-
sion of h1 that was done when h2 was larger than h1 . In this case, the expansion
is just a bit simpler because you know you're expanding by exactly one char .
Implementing Large-Number Subtraction
Another thing to note about this add routine — and the huge datatype in
general — is that you use unsigned chars for your internal representation.
That means that there's no concept of negative numbers or two's-complement
arithmetic. As such, you need a specifi c subtract routine, shown in Listing 3-7.
Listing 3-7: “huge.c” subtract
static void subtract( huge *h1, huge *h2 )
{
int i = h1->size;
int j = h2->size;
int difference; // signed int - important!
unsigned int borrow = 0;
do
{
i--;
if ( j )
{
j--;
difference = h1->rep[ i ] - h2->rep[ j ] - borrow;
}
else
{
difference = h1->rep[ i ] - borrow;
}
borrow = ( difference < 0 ) ;
h1->rep[ i ] = difference;
}
while ( i );
 
Search WWH ::




Custom Search