share.sage 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 __repr__(self):
  20. return "share: %s, mac: %s"%(self.share, self.mac)
  21. # SPDZ mac authentication
  22. def authenticated_open(self, peer_authenticated_share):
  23. opened_share = open_2pc(self.share, peer_authenticated_share.share)
  24. mac_key = random.randint(0,global_key)
  25. mac_share = mac_key * (opened_share + self.public_modifier) - self.mac
  26. peer_mac_key = global_key - mac_key
  27. peer_mac_share = peer_mac_key * (opened_share + peer_authenticated_share.public_modifier) - peer_authenticated_share.mac
  28. assert (mac_share + peer_mac_share) == 0
  29. return opened_share
  30. def sub_scalar(self, scalar, party_id):
  31. 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)
  32. def add_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 mul_scalar(self, scalar):
  35. return AuthenticatedShare(self.share * scalar, self.mac * scalar, self.public_modifier * scalar)
  36. def mul_point(self, point):
  37. return ECAuthenticatedShare(self.share * point, self.mac * point, self.public_modifier * point)
  38. def __add__(self, rhs):
  39. '''
  40. add additive shares
  41. '''
  42. return AuthenticatedShare(self.share + rhs.share, self.mac + rhs.mac, self.public_modifier + rhs.public_modifier)
  43. def __sub__(self, rhs):
  44. '''
  45. sub additive shares
  46. '''
  47. return AuthenticatedShare(self.share - rhs.share, self.mac - rhs.mac, self.public_modifier - rhs.public_modifier)
  48. class MultiplicationAuthenticatedShares(object):
  49. def __init__(self, alpha, beta, triplet, party_id):
  50. # authenticated shares
  51. self.alpha_as = alpha
  52. self.beta_as = beta
  53. self.a_as = triplet[0]
  54. self.b_as = triplet[1]
  55. self.c_as = triplet[2]
  56. self.party_id = party_id
  57. d1 = self.alpha_as - self.a_as
  58. e1 = self.beta_as - self.b_as
  59. self.d = d1
  60. self.e = e1
  61. def mul(self, d2, e2):
  62. d = open_2pc(self.d.share, d2.share)
  63. e = open_2pc(self.e.share, e2.share)
  64. if self.party_id==0:
  65. bd = self.b_as.mul_scalar(d)
  66. ae = self.a_as.mul_scalar(e)
  67. return (bd + ae + self.c_as).add_scalar(d*e, self.party_id)
  68. else:
  69. bd = self.b_as.mul_scalar(d)
  70. ae = self.a_as.mul_scalar(e)
  71. #return (bd + ae + self.c_as).add_scalar(d*e, self.party_id)
  72. return bd + ae + self.c_as