Cryptography Reference
In-Depth Information
2. There are three initializer functions that create
huge
s:
set_huge
,
copy_huge
,
and
load_huge
. Each needs to be updated to initialize the sign bit, as shown
in Listing 3-30.
Listing 3-30:
“huge.c” initializer routines with negative number support included
void set_huge( huge *h, unsigned int val )
{
unsigned int mask, i, shift;
// Negative number support
h->sign = 0; // sign of 0 means positive
...
void copy_huge( huge *tgt, huge *src )
{
if ( tgt->rep )
{
// TODO make this a little more efficient by reusing “rep”
// if it's big enough
free( tgt->rep );
}
tgt->sign = src->sign;
...
void load_huge( huge *h, const unsigned char *bytes, int length )
{
while ( !( *bytes ) )
{
bytes++;
length--;
}
h->sign = 0;
...
Notice that there's no way to initialize a
huge
as a negative number; you
don't need one and, in fact, negative numbers get in the way if, for example,
you treat a high 1 bit as a negative number indicator in
load_huge
. If a
computation results in a negative number, the routines keep track of it
internally.
3. Because the current
add
and
subtract
routines do a good job of computing
magnitudes of arbitrarily sized numbers — provided, of course, that
h1
is greater than
h2
in the case of subtraction — those routines can be used
unchanged to compute magnitudes, and you can do sign computation
and swapping in a separate routine. As such, rename
add
and
subtract
and
make them static as shown in Listing 3-31.
Search WWH ::
Custom Search