Cryptography Reference
In-Depth Information
1111(15)
1111(15)
11110(30)
As you can see, the result fi ts into fi ve bits. Also notice that although if you
add four bits to four bits and they overfl ow, they wrap. The wrapped result is
the lower four bits of the correct response. Therefore, you can just add the two
ints, keep track of whether they overfl owed, and carry this overfl ow (of one bit)
into the next addition.
Notice that
sum
— the temporary workspace for each addition operation — is
itself an
int
. You check for overfl ow by comparing it to
MAXCHAR
(0xFF). Although
the Intel instruction set does keep track of whether or not an operation — such
as an add — overfl owed and updates an overfl ow bit (also called a carry bit),
the C language has never standardized access to such a bit, in spite of the fact
that every microprocessor or microcontroller defi nes one. Because you don't
have access to the overfl ow bit, you have to check for it on each add operation.
The only other point to notice is that you must continue until you've looped
through all of the bytes of
h1
. You can't stop after you hit the last (actually, the
fi rst because you're working backward) byte of
h2
because there may be a carry
bit that needs to be propagated backward. In one extreme case, which actually
does occur in cryptographic applications,
h1
may be 0x01FFFFFEFF and
h2
may be 0x0100. In this case, each byte of
h1
overfl ows all the way to the very
fi rst. Thus, the
while
loop continues until
i
is 0, even if there's nothing left to
do with
h2
(for example,
j = 0
).
Finally, there's a possibility that you can get to the end of the add operation
and still have a carry bit left over. If this is the case, you need to expand h1 by
exactly one
char
and set its lower-order bit to 1 as shown in Listing 3-5.
Listing 3-5:
“huge.c” add (overfl ow expansion)
// Still overflowed; allocate more space
if ( carry )
{
expand( h1 );
}
Here you call the function
expand
, shown in listing 3-6, which is defi ned for
precisely this purpose.
Listing 3-6:
“huge.c” expand
/**
* Extend the space for h by 1 char and set the LSB of that int
* to 1.
*/
void expand( huge *h )
(Continued)
Search WWH ::
Custom Search