Calculating very large exponents in python

geremy condra debatem1 at
Sun Mar 7 21:40:59 CET 2010

On Sun, Mar 7, 2010 at 1:55 PM, Fahad Ahmad <miraclesoul at> wrote:
> Dear All,
> i am writing my crytographic scheme in python, i am just a new user to it.
> I have written the complete code, the only point i am stuck it is that i am
> using 256 exponentiation which is normal in crytography but python just
> hangs on it.
> g**x  [where both g and x are 256 bit numbers , in decimal they are around
> 77]
> after reading several forums, i just come to know it can be done using
> numpy, i have installed python(x,y) has has both numpy and scipy installed
> but i am not able to make it happen.
> any idea which library, module, or piece of code can solve this mystery :S
> sorry for bad english

A couple of things:

1) if you're working with modular exponentiation, remember that pow() takes
three arguments, ie:

a = 222222222222222222222222222
b = 5555555555555555555555555555
pow(a, b, 1200)

will calculate the correct answer (768) very quickly, while

a**b % 1200

has not terminated in the time it took me to compose this

2) sage has a lot of excellent tools for crypto/cryptanalysis that you
may want to take a look at.

3) not saying you don't know what you're doing, but be careful when
rolling your own cryptosystems- even very good cryptographers make
implementation mistakes!

Geremy Condra

More information about the Python-list mailing list