The book I am reading, explains the algorithm as follows:
Exchange happens as illustrated
I put together the following python code to see how this works and .... it does not. Please help me understand what am i missing:
#!/usr/bin/python
n=22 # publicly known
g=42 # publicly known
x=13 # only Alice knows this
y=53 # only Bob knows this
aliceSends = (g**x)%n
bobComputes = aliceSends**y
bobSends = (g**y)%n
aliceComputes = bobSends**x
print "Alice sends ", aliceSends
print "Bob computes ", bobComputes
print "Bob sends ", bobSends
print "Alice computes ", aliceComputes
print "In theory both should have ", (g**(x*y))%n
---
Alice sends 14
Bob computes 5556302616191343498765890791686005349041729624255239232159744
Bob sends 14
Alice computes 793714773254144
In theory both should have 16
You forgot two more modulos:
>>> 5556302616191343498765890791686005349041729624255239232159744 % 22
16L
>>> 793714773254144 % 22
16
Roman is right. However, you'd better have a look at pow() three arguments function. Much faster and third argument is modulus
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With