def fast_exponentiation(a, p, n):
    result = a % n
    remainders = []
    while p != 1:
        remainders.append(p & 1)
        p = p >> 1
    while remainders:
        rem = remainders.pop()
        result = ((a ** rem) * result ** 2) % n
    return result
Calculates r = a^p mod n
dataset/ETHPy150Open CouchPotato/CouchPotatoServer/libs/rsa/_version133.py/fast_exponentiation