ipcl_operator.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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
  17. from federatedml.util import LOGGER
  18. try:
  19. from ipcl_python import PaillierEncryptedNumber as IpclPaillierEncryptedNumber
  20. from ipcl_python.bindings.ipcl_bindings import ipclCipherText
  21. except ImportError:
  22. LOGGER.info("ipcl_python failed to import")
  23. pass
  24. def get_coeffs(weights):
  25. """
  26. IPCL encrypts all weights (coefficients and intercept) into one single encrypted number.
  27. This function allows to get an IPCL encrypted number which contains all coefficents but intercept.
  28. Args:
  29. weights (IpclPaillierEncryptedNumber): all model weights in one encrypted number
  30. Returns:
  31. (IpclPaillierEncryptedNumber): all coefficients in one encrypted number
  32. """
  33. coeff_num = weights.__len__() - 1
  34. pub_key = weights.public_key
  35. bn = []
  36. exp = []
  37. for i in range(coeff_num):
  38. bn.append(weights.ciphertextBN(i))
  39. exp.append(weights.exponent(i))
  40. ct = ipclCipherText(pub_key.pubkey, bn)
  41. return IpclPaillierEncryptedNumber(pub_key, ct, exp, coeff_num)
  42. def get_intercept(weights):
  43. """
  44. IPCL encrypts all weights (coefficients and intercept) into one single encrypted number.
  45. This function allows to get the encrypted number of intercept.
  46. Args:
  47. weights (IpclPaillierEncryptedNumber): all model weights in one encrypted number
  48. Returns:
  49. (IpclPaillierEncryptedNumber): IPCL encrypted number of intercept
  50. """
  51. coeff_num = weights.__len__() - 1
  52. pub_key = weights.public_key
  53. bn = [weights.ciphertextBN(coeff_num)]
  54. exp = [weights.exponent(coeff_num)]
  55. ct = ipclCipherText(pub_key.pubkey, bn)
  56. return IpclPaillierEncryptedNumber(pub_key, ct, exp, 1)
  57. def merge_encrypted_number_array(values):
  58. """
  59. Put all IPCL encrypted numbers of a 1-d array into one encrypted number.
  60. Args:
  61. values (numpy.ndarray, list): an array of multiple IPCL encrypted numbers
  62. Returns:
  63. (IpclPaillierEncryptedNumber): one encrypted number contains all values
  64. """
  65. assert isinstance(values, (list, ndarray))
  66. pub_key = values[0].public_key
  67. bn, exp = [], []
  68. for i in range(len(values)):
  69. assert values[i].__len__() == 1
  70. bn.append(values[i].ciphertextBN(0))
  71. exp.append(values[i].exponent(0))
  72. ct = ipclCipherText(pub_key.pubkey, bn)
  73. return IpclPaillierEncryptedNumber(pub_key, ct, exp, len(values))