For educational purpose I'm developing c++ library for operating with large numbers represented as vectors of chars (vector<char>
).
Here is algorithm that I am using for modular exponentiation:
BigInt modularExponentiation(BigInt base, BigInt exponent, BigInt mod) {
BigInt x = B_ONE; // 1
BigInt y = base;
while (exponent > B_ZERO) { // while exponent != 0
if (isOdd(exponent))
x = (x * y) % mod;
y = (y * y) % mod;
exponent /= B_TWO; // exponent = exponent / 2
}
return (x % mod);
};
Is there any more efficent algorithm, or can this be improved (like parallelized with openmp)?