This algorithm is a combination of the Exponentiation by Squaring algorithm and modulo arithmetic.
, , , exponent 2. , , exponent = 2 ^ k, k , ..
res = (...((base ^ 2) ^2 ) ... ) ^2))
---------------------
k times
exponent 2, . , exponent 2 , . , , , base.
, , - , , . , exponent >> 1, floor(exponent / 2).