Calculating Powers
HugeInt pow(HugeInt x, HugeInt n, HugeInt modulo)
{
if (n == 0)
return 1;
if (n == 1)
return x;
HugeInt squared = x * x % modulo;
if (isEven(n))
return pow(squared, n / 2);
else
return (pow(squared, n/2) * x) % modulo;
}
// If 1 < x < modulo - 1
// but squared == 1,
// then modulo isn’t prime!
Previous slide
Next slide
Back to first slide
View graphic version