Cryptography Reference
In-Depth Information
x
0
x
2
1
¨
multiply
(x
2
)
2
0
((x
2
)
2
)
2
1
¨
multiply
multiplying the two “hits” together, you get
x
2
((
x
2
)
2
)
2
or [((
x
2
)
2
)
x
]
2
which is what
you got when you deconstructed decimal 10 in the fi rst place.
Listing 3-18 shows how you can implement this in code. Compare this to the
implementation of multiply in Listing 3-9.
Listing 3-18:
“huge.c” exponentiate
/**
* Raise h1 to the power of exp. Return the result in h1.
*/
void exponentiate( huge *h1, huge *exp )
{
int i = exp->size, mask;
huge tmp1, tmp2;
set_huge( &tmp1, 0 );
set_huge( &tmp2, 0 );
copy_huge( &tmp1, h1 );
set_huge( h1, 1 );
do
{
i--;
for ( mask = 0x01; mask; mask <<= 1 )
{
if ( exp->rep[ i ] & mask )
{
multiply( h1, &tmp1 );
}
// Square tmp1
copy_huge( &tmp2, &tmp1 );
multiply( &tmp1, &tmp2 );
}
}
while ( i );
free_huge( &tmp1 );
free_huge( &tmp2 );
}
Search WWH ::
Custom Search