secure_matrix.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #
  2. # Copyright 2021 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import numpy as np
  17. from fate_arch.common import Party
  18. from fate_arch.session import is_table
  19. from federatedml.secureprotol.fixedpoint import FixedPointEndec
  20. from federatedml.secureprotol.spdz.tensor import fixedpoint_numpy, fixedpoint_table
  21. from federatedml.transfer_variable.transfer_class.secret_share_transfer_variable import SecretShareTransferVariable
  22. from federatedml.util import consts
  23. class SecureMatrix(object):
  24. # SecureMatrix in SecretSharing With He;
  25. def __init__(self, party: Party, q_field, other_party):
  26. self.transfer_variable = SecretShareTransferVariable()
  27. self.party = party
  28. self.other_party = other_party
  29. self.q_field = q_field
  30. self.encoder = None
  31. self.get_or_create_endec(self.q_field)
  32. def set_flowid(self, flowid):
  33. self.transfer_variable.set_flowid(flowid)
  34. def get_or_create_endec(self, q_field, **kwargs):
  35. if self.encoder is None:
  36. self.encoder = FixedPointEndec(q_field)
  37. return self.encoder
  38. def secure_matrix_mul(self, matrix, tensor_name, cipher=None, suffix=tuple(), is_fixedpoint_table=True):
  39. current_suffix = ("secure_matrix_mul",) + suffix
  40. dst_role = consts.GUEST if self.party.role == consts.HOST else consts.HOST
  41. if cipher is not None:
  42. de_matrix = self.encoder.decode(matrix.value)
  43. if isinstance(matrix, fixedpoint_table.FixedPointTensor):
  44. encrypt_mat = cipher.distribute_encrypt(de_matrix)
  45. else:
  46. encrypt_mat = cipher.recursive_encrypt(de_matrix)
  47. # remote encrypted matrix;
  48. self.transfer_variable.encrypted_share_matrix.remote(encrypt_mat,
  49. role=dst_role,
  50. idx=0,
  51. suffix=current_suffix)
  52. share_tensor = SecureMatrix.from_source(tensor_name,
  53. self.other_party,
  54. cipher,
  55. self.q_field,
  56. self.encoder,
  57. is_fixedpoint_table=is_fixedpoint_table)
  58. return share_tensor
  59. else:
  60. share = self.transfer_variable.encrypted_share_matrix.get(role=dst_role,
  61. idx=0,
  62. suffix=current_suffix)
  63. if is_table(share):
  64. share = fixedpoint_table.PaillierFixedPointTensor(share)
  65. ret = share.dot(matrix)
  66. else:
  67. share = fixedpoint_numpy.PaillierFixedPointTensor(share)
  68. ret = share.dot(matrix)
  69. share_tensor = SecureMatrix.from_source(tensor_name,
  70. ret,
  71. cipher,
  72. self.q_field,
  73. self.encoder)
  74. return share_tensor
  75. def share_encrypted_matrix(self, suffix, is_remote, cipher, **kwargs):
  76. current_suffix = ("share_encrypted_matrix",) + suffix
  77. if is_remote:
  78. for var_name, var in kwargs.items():
  79. dst_role = consts.GUEST if self.party.role == consts.HOST else consts.HOST
  80. if isinstance(var, fixedpoint_table.FixedPointTensor):
  81. encrypt_var = cipher.distribute_encrypt(var.value)
  82. else:
  83. encrypt_var = cipher.recursive_encrypt(var.value)
  84. self.transfer_variable.encrypted_share_matrix.remote(encrypt_var, role=dst_role,
  85. suffix=(var_name,) + current_suffix)
  86. else:
  87. res = []
  88. for var_name in kwargs.keys():
  89. dst_role = consts.GUEST if self.party.role == consts.HOST else consts.HOST
  90. z = self.transfer_variable.encrypted_share_matrix.get(role=dst_role, idx=0,
  91. suffix=(var_name,) + current_suffix)
  92. if is_table(z):
  93. res.append(fixedpoint_table.PaillierFixedPointTensor(z))
  94. else:
  95. res.append(fixedpoint_numpy.PaillierFixedPointTensor(z))
  96. return tuple(res)
  97. @classmethod
  98. def from_source(cls, tensor_name, source, cipher, q_field, encoder, is_fixedpoint_table=True):
  99. if is_table(source):
  100. share_tensor = fixedpoint_table.PaillierFixedPointTensor.from_source(tensor_name=tensor_name,
  101. source=source,
  102. encoder=encoder,
  103. q_field=q_field)
  104. return share_tensor
  105. elif isinstance(source, np.ndarray):
  106. share_tensor = fixedpoint_numpy.PaillierFixedPointTensor.from_source(tensor_name=tensor_name,
  107. source=source,
  108. encoder=encoder,
  109. q_field=q_field)
  110. return share_tensor
  111. elif isinstance(source, (fixedpoint_table.PaillierFixedPointTensor,
  112. fixedpoint_numpy.PaillierFixedPointTensor)):
  113. return cls.from_source(tensor_name, source.value, cipher, q_field, encoder, is_fixedpoint_table)
  114. elif isinstance(source, Party):
  115. if is_fixedpoint_table:
  116. share_tensor = fixedpoint_table.PaillierFixedPointTensor.from_source(tensor_name=tensor_name,
  117. source=source,
  118. encoder=encoder,
  119. q_field=q_field,
  120. cipher=cipher)
  121. else:
  122. share_tensor = fixedpoint_numpy.PaillierFixedPointTensor.from_source(tensor_name=tensor_name,
  123. source=source,
  124. encoder=encoder,
  125. q_field=q_field,
  126. cipher=cipher)
  127. return share_tensor
  128. else:
  129. raise ValueError(f"type={type(source)}")