paillier_check.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #
  2. # Copyright 2019 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 numpy import ndarray, ndim
  17. from federatedml.util import LOGGER
  18. from federatedml.secureprotol.fate_paillier import PaillierEncryptedNumber
  19. ipcl_enabled = False
  20. try:
  21. from ipcl_python import PaillierEncryptedNumber as IpclPaillierEncryptedNumber
  22. ipcl_enabled = True
  23. except ImportError:
  24. LOGGER.info("ipcl_python failed to import")
  25. pass
  26. def is_encrypted_number(values, encrypted_type):
  27. if isinstance(values, ndarray):
  28. return isinstance(values.item(0), encrypted_type)
  29. elif isinstance(values, list):
  30. return isinstance(values[0], encrypted_type)
  31. else:
  32. return isinstance(values, encrypted_type)
  33. def is_fate_paillier_encrypted_number(values):
  34. return is_encrypted_number(values, PaillierEncryptedNumber)
  35. def is_ipcl_encrypted_number(values):
  36. if ipcl_enabled:
  37. return is_encrypted_number(values, IpclPaillierEncryptedNumber)
  38. return False
  39. def is_paillier_encrypted_number(values):
  40. return is_fate_paillier_encrypted_number(values) or is_ipcl_encrypted_number(values)
  41. def is_single_ipcl_encrypted_number(values):
  42. """
  43. Return True if input numpy array contains only one IPCL encrypted number, not a list
  44. Args:
  45. values (numpy.ndarray)
  46. """
  47. if ipcl_enabled and isinstance(values, ndarray):
  48. return ndim(values) == 0 and isinstance(values.item(0), IpclPaillierEncryptedNumber)
  49. return False