Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python modulo result differs from wolfram alpha?

When I run my python 3 program:

exp = 211
p = 199
q = 337

d = (exp ** (-1)) % ((p - 1)*(q - 1))

results in 211^(-1).

But when I run the calculation in wolfram alpha I get the result I was expecting.

I did some test outputs and the variables exp, p and q in the program are all the integer values I used in wolfram alpha.

My goal is to derive a private key from a (weakly) encrypted integer. If I test my wolfram alpha result, I can decrypt the encrypted message correctly.

like image 676
mrcdnk Avatar asked Nov 23 '15 19:11

mrcdnk


People also ask

Does modulo work in Python?

Python supports a wide range of arithmetic operators that you can use when working with numbers in your code. One of these operators is the modulo operator ( % ), which returns the remainder of dividing two numbers.

What are the different modulus in Python?

The modulo operator is considered an arithmetic operation, along with + , - , / , * , ** , // . In the previous example a is divided by b , and the remainder is returned. Let's see an example with numbers. The result of the previous example is one.

What is mod and div in Python?

Modulus: The remainder that is left over when a number is divided by another. Some programming languages will use the % symbol for MOD. 16 MOD 3 = 1 (16 / 3 = 5, with 1 left over) DIV. Integer division: Used to find the quotient (integer number before the decimal point) after division.


3 Answers

Wolfram Alpha is computing the modular inverse. That is, it's finding the integer x such that

exp*x == 1 mod (p - 1)*(q - 1)

This is not the same as the modulo operator %. Here, Python is simply calculating the remainder when 1/exp is divided by (p - 1)*(q - 1) when given the expression in your question.

Copying the Python code from this answer, you can compute the desired value with Python too:

>>> modinv(exp, (p - 1)*(q - 1))
45403
like image 90
Alex Riley Avatar answered Nov 15 '22 16:11

Alex Riley


Wolfram Alpha does not have well-defined syntax. It takes arbitrary text you provide and attempts to figure out what you meant by that input. In this case, it decided you were probably looking for a modular inverse, and it gave you one.

Python has well-defined syntax. In Python, the parser does not take the ** and the % together and guess that that combination makes the two operators have a meaning other than their usual meaning. The ** is computed the usual way, and then % is the modulo operator. If you want a modular inverse, you'll have to write one yourself.

like image 44
user2357112 supports Monica Avatar answered Nov 15 '22 16:11

user2357112 supports Monica


I think the idea here is that wolfram alpha and python define the modulo operation differently depending on the fact that you are dealing with integers or real numbers. In this case, Wolfram Alpha is using the modulo inverse because it detects the first number is 0 < x < 1

More information about the definition on real numbers here

like image 20
nichochar Avatar answered Nov 15 '22 16:11

nichochar