-1

I had a problem that of calculation of a^b mod m , is possible using modular exponentiation but the problem i am having is that the b I have is of very large value , b > 2^63 - 1 so could we modify the modular exponentiation code

function modular_pow(base, exponent, modulus)
    result := 1
    while exponent > 0
        if (exponent mod 2 == 1):
           result := (result * base) mod modulus
        exponent := exponent >> 1
        base = (base * base) mod modulus
    return result

to accomodate for such a large b

or is it correct that a^b mod m is equal to (a^(b mod m)) mod m ?

RiaD
  • 46,822
  • 11
  • 79
  • 123
Mod
  • 383
  • 3
  • 14

1 Answers1

0

It's correct that a^b mod m = a^(b mod phi(m)) mod m, where phi(m) is Euler totient function

Your code is correct too (if types are long enough to represent all values)

You may also combine two methods

RiaD
  • 46,822
  • 11
  • 79
  • 123