ecdh_intersect_base.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. from federatedml.secureprotol.elliptic_curve_encryption import EllipticCurve
  17. from federatedml.secureprotol.hash.hash_factory import Hash
  18. from federatedml.statistic.intersect.base_intersect import Intersect
  19. from federatedml.transfer_variable.transfer_class.ecdh_intersect_transfer_variable import EcdhIntersectTransferVariable
  20. from federatedml.util import LOGGER, consts
  21. class EcdhIntersect(Intersect):
  22. """
  23. adapted from Secure Information Retrieval Module
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.role = None
  28. self.transfer_variable = EcdhIntersectTransferVariable()
  29. self.curve_instance = None
  30. def load_params(self, param):
  31. super().load_params(param=param)
  32. self.ecdh_params = param.ecdh_params
  33. self.hash_operator = Hash(param.ecdh_params.hash_method, hex_output=False)
  34. self.salt = self.ecdh_params.salt
  35. self.curve = self.ecdh_params.curve
  36. def get_intersect_method_meta(self):
  37. ecdh_meta = {"intersect_method": consts.ECDH,
  38. "hash_method": self.ecdh_params.hash_method,
  39. "salt": self.salt,
  40. "curve": self.curve}
  41. return ecdh_meta
  42. def init_curve(self, curve_key=None):
  43. self.curve_instance = EllipticCurve(self.curve, curve_key)
  44. @staticmethod
  45. def get_mode(reserve_original_key=False, reserve_original_value=False):
  46. if reserve_original_key and reserve_original_value:
  47. return 5
  48. if reserve_original_key:
  49. return 4
  50. if reserve_original_value:
  51. return 3
  52. return 1
  53. @staticmethod
  54. def _encrypt_id(data_instances, curve_instance, reserve_original_key=False, hash_operator=None, salt='',
  55. reserve_original_value=False):
  56. """
  57. Encrypt the key (ID) of input Table
  58. :param curve: curve object
  59. :param data_instance: Table
  60. :param reserve_original_key: (enc_key, ori_key) if reserve_original_key == True, otherwise (enc_key, -1)
  61. :param hash_operator: if provided, use map_hash_encrypt
  62. :param salt: if provided, use for map_hash_encrypt
  63. : param reserve_original_value:
  64. (enc_key, (ori_key, val)) for reserve_original_key == True and reserve_original_value==True;
  65. (ori_key, (enc_key, val)) for only reserve_original_value == True.
  66. :return:
  67. """
  68. mode = EcdhIntersect.get_mode(reserve_original_key, reserve_original_value)
  69. if hash_operator is not None:
  70. return curve_instance.map_hash_encrypt(data_instances, mode=mode, hash_operator=hash_operator, salt=salt)
  71. return curve_instance.map_encrypt(data_instances, mode=mode)
  72. @staticmethod
  73. def _sign_id(data_instances, curve_instance, reserve_original_key=False, reserve_original_value=False):
  74. """
  75. Encrypt the key (ID) of input Table
  76. :param curve_instance: curve object
  77. :param data_instance: Table
  78. :param reserve_original_key: (enc_key, ori_key) if reserve_original_key == True, otherwise (enc_key, -1)
  79. : param reserve_original_value:
  80. (enc_key, (ori_key, val)) for reserve_original_key == True and reserve_original_value==True;
  81. (ori_key, (enc_key, val)) for only reserve_original_value == True.
  82. :return:
  83. """
  84. mode = EcdhIntersect.get_mode(reserve_original_key, reserve_original_value)
  85. return curve_instance.map_sign(data_instances, mode=mode)
  86. def _exchange_id(self, id, replace_val=True):
  87. """
  88. :param id: Table in the form (id, 0)
  89. :return:
  90. """
  91. pass
  92. def _sync_doubly_encrypted_id(self, id):
  93. """
  94. host -> guest
  95. :param id:
  96. :return:
  97. """
  98. pass
  99. def get_intersect_doubly_encrypted_id(self, data_instances, keep_key=True):
  100. raise NotImplementedError("This method should not be called here")
  101. def decrypt_intersect_doubly_encrypted_id(self, id_intersect_cipher_cipher):
  102. raise NotImplementedError("This method should not be called here")
  103. def get_intersect_doubly_encrypted_id_from_cache(self, data_instances, cache_set):
  104. raise NotImplementedError("This method should not be called here")
  105. def run_intersect(self, data_instances):
  106. LOGGER.info("Start ECDH Intersection")
  107. id_intersect_cipher_cipher = self.get_intersect_doubly_encrypted_id(data_instances)
  108. intersect_ids = self.decrypt_intersect_doubly_encrypted_id(id_intersect_cipher_cipher)
  109. return intersect_ids
  110. def run_cache_intersect(self, data_instances, cache_data):
  111. LOGGER.info("Start ECDH Intersection with cache")
  112. id_intersect_cipher_cipher = self.get_intersect_doubly_encrypted_id_from_cache(data_instances, cache_data)
  113. intersect_ids = self.decrypt_intersect_doubly_encrypted_id(id_intersect_cipher_cipher)
  114. return intersect_ids