main.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from random import randint, choice
  2. import math
  3. #assumes p prime returns cube root of a mod p
  4. def cuberoot(a, p):
  5. if p == 2:
  6. return a
  7. if p == 3:
  8. return a
  9. if (p%3) == 2:
  10. return pow(a,(2*p - 1)/3, p)
  11. if (p%9) == 4:
  12. root = pow(a,(2*p + 1)/9, p)
  13. if pow(root,3,p) == a%p:
  14. return root
  15. else:
  16. return None
  17. if (p%9) == 7:
  18. root = pow(a,(p + 2)/9, p)
  19. if pow(root,3,p) == a%p:
  20. return root
  21. else:
  22. return None
  23. else:
  24. print "Not implemented yet. See the second paper"
  25. # Define a Peer object
  26. class Peer(object):
  27. # Construct a new peer
  28. # @param crush bool Whether peer has crush or not
  29. # @param peer Peer Reference to other peer
  30. def __init__(self, crush=False, peer=None):
  31. self.__crush = crush
  32. self.__N = 0
  33. self.__peer = peer
  34. self.__r = 0
  35. def setPeer(self, peer):
  36. self.__peer = peer
  37. def setN(self, N):
  38. self.__N = N
  39. def __residue(self, a):
  40. i = choice(range(1000, 100000))
  41. return a + self.__N * i
  42. def __step1(self):
  43. # Generate an RSA
  44. a = 13
  45. b = 11
  46. self.__rsa = self.__residue(a*b)
  47. # Reveals N to untrusted peer
  48. self.__peer.setN(self.__N)
  49. def __step2(self):
  50. y = 15 if self.__crush else 0
  51. y = self.__residue(y)
  52. x = self.__residue(0)
  53. self.__peer.step4(x**3, y**3)
  54. def step4(self, x3, y3):
  55. i = choice(range(1, 100))
  56. r = self.__residue(i)
  57. self.__r = r
  58. a3r3 = y3*r**3 if self.__crush else x3*r**3
  59. self.__peer.step5(a3r3)
  60. def step5(self, a3r3):
  61. print a3r3
  62. ar = cuberoot(a3r3, self.__N)
  63. print ar
  64. self.__peer.step6(ar)
  65. def step6(self, ar):
  66. print ar / self.__r
  67. def run(self):
  68. self.__step1()
  69. self.__step2()
  70. alice = Peer(crush=True)
  71. bob = Peer(crush=True)
  72. bob.setPeer(alice)
  73. alice.setPeer(bob)
  74. alice.setN(13)
  75. alice.run()