An implementation of fast exponentiation that can calculate exponential for big numbers ideally in log(n) time complexity The large number is supported by BigInt, which unfortunatly is not in constant time when multyplicate with another large number. Therefore it is not strictly in log(N) time complexity.
npm install fast-exponential
/*
@param x This is the base
@param n This is the power
@param mod Optional modulus
@return x^n if mod is not specified, else x^n % mod
*/
fastExp(x,n,mod = null)
const fastExp = require("fast-exponential")
console.log(fastExp(2,10)) //1024
n = "1139631134290681913324518075250462509444792614577115360833700594253534083115108212461164873379591734542309312064780949257819665132832661342154198437454459926525649486600336464897081397167045104842672493488133506984881500857942197501"
M = "3141592651842593"
d = "20729576806810227945651433503304642530313216592724403339332811669890870"
+"507980537712665435487675836653308618504240738644446969730044899317107941502"
+"247799584959444798172916891463972996495752944622965018659022099059225470003"
+"8562058305"
console.log(fastExp(M,d,n))
//450185963693909864026895836829398229699587317498476752798024488061316488533033295859758263339821291798020665834789679241243278095751940650540713944197579512398507578002732869853224053105027949343093018580947275265543427402941263699n
npm test