share.sage 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import random
  2. load('../mpc/curve.sage')
  3. load('../mpc/ec_share.sage')
  4. def open_2pc(party0_share, party1_share):
  5. return party0_share + party1_share
  6. def verify_2pc_mac_check(party0_mac, party1_mac):
  7. assert party0_mac+party1_mac == 0
  8. global_key = random.randint(0, p)
  9. class AuthenticatedShare(object):
  10. """
  11. additive share
  12. """
  13. def __init__(self, share, source, party_id, mac=None, modifier=None):
  14. self.share = share
  15. self.mac = global_key * self.share if mac==None else mac
  16. self.public_modifier = 0 if modifier == None else modifier # carry out extra addition/subtraction by public scalars until opening
  17. self.party_id = party_id
  18. self.source = source
  19. def copy(self):
  20. return AuthenticatedShare(K(self.share), self.source, self.party_id, K(self.mac), K(self.public_modifier))
  21. def __repr__(self):
  22. return "share: %s, mac: %s"%(self.share, self.mac)
  23. # SPDZ mac authentication
  24. def authenticated_open(self, peer_authenticated_share):
  25. opened_share = open_2pc(self.share, peer_authenticated_share.share)
  26. mac_key = random.randint(0,global_key)
  27. mac_share = mac_key * (opened_share + self.public_modifier) - self.mac
  28. peer_mac_key = global_key - mac_key
  29. peer_mac_share = peer_mac_key * (opened_share + peer_authenticated_share.public_modifier) - peer_authenticated_share.mac
  30. #assert (mac_share + peer_mac_share) == 0
  31. return opened_share
  32. def sub_scalar(self, scalar, party_id):
  33. return AuthenticatedShare(self.share - scalar, self.mac, self.public_modifier + scalar) if party_id == 0 else AuthenticatedShare(self.share , self.mac, self.public_modifier + scalar)
  34. def add_scalar(self, scalar, party_id):
  35. return AuthenticatedShare(self.share + scalar, self.mac , self.public_modifier - scalar) if party_id ==0 else AuthenticatedShare(self.share, self.mac, self.public_modifier - scalar)
  36. def mul_scalar(self, scalar):
  37. return AuthenticatedShare(self.share * scalar, self.mac * scalar, self.public_modifier * scalar)
  38. def mul_point(self, point):
  39. return ECAuthenticatedShare(self.share * point, self.mac * point, self.public_modifier * point)
  40. def __add__(self, rhs):
  41. '''
  42. add additive shares
  43. '''
  44. return AuthenticatedShare(self.share + rhs.share, self.mac + rhs.mac, self.public_modifier + rhs.public_modifier)
  45. def __sub__(self, rhs):
  46. '''
  47. sub additive shares
  48. '''
  49. return AuthenticatedShare(self.share - rhs.share, self.mac - rhs.mac, self.public_modifier - rhs.public_modifier)
  50. class MultiplicationAuthenticatedShares(object):
  51. def __init__(self, alpha, beta, triplet, party_id):
  52. # authenticated shares
  53. self.alpha_as = alpha
  54. self.beta_as = beta
  55. self.a_as = triplet[0]
  56. self.b_as = triplet[1]
  57. self.c_as = triplet[2]
  58. self.party_id = party_id
  59. d1 = self.alpha_as - self.a_as
  60. e1 = self.beta_as - self.b_as
  61. self.d = d1
  62. self.e = e1
  63. def mul(self, d2, e2):
  64. d = open_2pc(self.d.share, d2.share)
  65. e = open_2pc(self.e.share, e2.share)
  66. if self.party_id==0:
  67. bd = self.b_as.mul_scalar(d)
  68. ae = self.a_as.mul_scalar(e)
  69. return (bd + ae + self.c_as).add_scalar(d*e, self.party_id)
  70. else:
  71. bd = self.b_as.mul_scalar(d)
  72. ae = self.a_as.mul_scalar(e)
  73. #return (bd + ae + self.c_as).add_scalar(d*e, self.party_id)
  74. return bd + ae + self.c_as