title: How do I compute a^b mod p for very large a, b and p?
body: <p>So I have to perform the following calculation in python using the following values:</p>
  
  <pre><code>g^response mod p == (commitment)*(public_key_k)^c mod p
  
  response = 1798504909811498641585788989097480059610472466605727419221908325725492625454059229439555828626804680183806468816373215601063565006206068675156743699829546676540107235594790722537029068950539064303645556838303768140715990225949480617994868684304692684872657878847353607016743753387060553449827051212521100889882315213754701194584603400975539123612457745335627513231512419845120373289319998525762252114847939075623683635101459254064343707561020578062272583241711744
  
  g = 633902738424928856783669360417409461352724866437176267937054971987929518113968311572018846775440350331394872441420725806863767569147521628581387346133794141162759618915434384470928048515684966754389921404728037087585951549298706749491681316440418023335644037157549668734734747234193236480208211700649047792505290394509276323498712019417085994608675098219625068478389802372911974790447602798848267203035795626948013815751746314708193865142515067213438779931341448784231764283922931059803394647357407601820746377200693540251395985610151207325893305136968984729108604308872514815118245429658506703427331797397729626291989388778680839647127066755635696870257359738766274560298982571341199340105150191282665463341766016615086716556537263439886148093374656225718217401337340651580107886515914073965138178083420939392671278560530056147682312589783964279302141118430614587577025403023718516789910534505871873011436491653121601912717709648600938567837813521742472036386528727473354399846339619270536399678071529700504925046483796750809603796528358402843506478188359404393987635666119244256746743854126114174948922250715011664059118382465474343042744744366613138372697678748514832068141362891787033831013749278870696574778057534613154041019988
  
  p = 1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085895011648256092372242446818525911665961045150145231572613786749168750228798758833
  
  commitment=  2020675137930448463428712210699816377193388795956936955368481577678186252928308173553464572252495743781550875659773513809435786840535637124578067477896971665575935166527060316619051471051210246502668698327245923852291583447595092128659406887368657131869401939279706099725440871802711550213026404990472910817206252771895691223697718745541630125166110241325111941992598904064238282617139280375363809225737959729994345492410515939174560166672596708090703781989210954
  
  public_key_k=  1003984929417870213515336739925548790770111120760264823891351725159288043410871239949310577373062378763331943177698297734039338641147796251418513873689452683273850688383067446727776769706425799938906550445532369849808734611985822181736172724148838646620601396815561586226959391463589873364266684088955346195425684063242763135652048735053741050269224316588887929259000548116678926935189145525181639366543164685162598318329861171236716093650271474650457781832849534
  </code></pre>
  
  <p>I have the following function that can perform <code>2^a mod p</code> where a and p are large:</p>
  
  <pre><code>def fast_powmod(n, m):
  
  
      pow2 = 2
        result = 1
        while n &gt; 0:
          if n % 2 == 1:
            result = (result * pow2) % m
          pow2 = (pow2 * pow2) % m
          n &gt;&gt;= 1
      return result
  </code></pre>
  
  <p>How do I do the same thing where it's <code>a^b mod p</code> with a and b both being very large?</p>