paillier_keygen_sync.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright 2019 The FATE Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. from federatedml.secureprotol.encrypt import PaillierEncrypt, IpclPaillierEncrypt
  16. from federatedml.util import consts
  17. class Arbiter(object):
  18. # noinspection PyAttributeOutsideInit
  19. def _register_paillier_keygen(self, pubkey_transfer):
  20. self._pubkey_transfer = pubkey_transfer
  21. def paillier_keygen(self, method, key_length, suffix=tuple()):
  22. if method == consts.PAILLIER:
  23. cipher = PaillierEncrypt()
  24. elif method == consts.PAILLIER_IPCL:
  25. cipher = IpclPaillierEncrypt()
  26. else:
  27. raise ValueError(f"Unsupported encryption method: {method}")
  28. cipher.generate_key(key_length)
  29. pub_key = cipher.get_public_key()
  30. self._pubkey_transfer.remote(obj=pub_key, role=consts.HOST, idx=-1, suffix=suffix)
  31. self._pubkey_transfer.remote(obj=pub_key, role=consts.GUEST, idx=-1, suffix=suffix)
  32. return cipher
  33. class _Client(object):
  34. # noinspection PyAttributeOutsideInit
  35. def _register_paillier_keygen(self, pubkey_transfer):
  36. self._pubkey_transfer = pubkey_transfer
  37. def gen_paillier_cipher_operator(self, suffix=tuple(), method=consts.PAILLIER):
  38. pubkey = self._pubkey_transfer.get(idx=0, suffix=suffix)
  39. if method == consts.PAILLIER:
  40. cipher = PaillierEncrypt()
  41. elif method == consts.PAILLIER_IPCL:
  42. cipher = IpclPaillierEncrypt()
  43. else:
  44. raise ValueError(f"Unsupported encryption method: {method}")
  45. cipher.set_public_key(pubkey)
  46. return cipher
  47. Host = _Client
  48. Guest = _Client