Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python modulus result is incorrect

Tags:

python

I am totally stumped. I was computing the cipher of the number 54 in RSA with the following values:

p=5; q=29; n=145 d=9; e=137

So the number 54 encrypted would be:

54^137 mod 145

or equivalently in python:

import math
math.pow(54,137)%145

My calculator gives me 24, my python statement gives me 54.0. Python is clearly wrong but I have no idea why or how. Try it on your installations of Python. My version is 2.5.1 but I also tried on 2.6.5 with the same incorrect result.

like image 598
Franz Avatar asked Feb 24 '11 02:02

Franz


1 Answers

>>> pow(54,137,145)
24

math.pow is floating point. You don't want that. Floating-point values have less than 17 digits of useful precision. The 54**137 has 237 digits.

like image 114
S.Lott Avatar answered Oct 29 '22 12:10

S.Lott