the implementation of redPow is N multiplications. it could be optimised to O(logN) using an approach as detailed here https://en.algorithmica.org/hpc/number-theory/exponentiation/