encrypt_param.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. from federatedml.param.base_param import BaseParam
  19. from federatedml.util import consts, LOGGER
  20. class EncryptParam(BaseParam):
  21. """
  22. Define encryption method that used in federated ml.
  23. Parameters
  24. ----------
  25. method : {'Paillier'}
  26. If method is 'Paillier', Paillier encryption will be used for federated ml.
  27. To use non-encryption version in HomoLR, set this to None.
  28. For detail of Paillier encryption, please check out the paper mentioned in README file.
  29. key_length : int, default: 1024
  30. Used to specify the length of key in this encryption method.
  31. """
  32. def __init__(self, method=consts.PAILLIER, key_length=1024):
  33. super(EncryptParam, self).__init__()
  34. self.method = method
  35. self.key_length = key_length
  36. def check(self):
  37. if self.method is not None and type(self.method).__name__ != "str":
  38. raise ValueError(
  39. "encrypt_param's method {} not supported, should be str type".format(
  40. self.method))
  41. elif self.method is None:
  42. pass
  43. else:
  44. user_input = self.method.lower()
  45. if user_input == "paillier":
  46. self.method = consts.PAILLIER
  47. elif user_input == consts.ITERATIVEAFFINE.lower() or user_input == consts.RANDOM_ITERATIVEAFFINE:
  48. LOGGER.warning('Iterative Affine and Random Iterative Affine are not supported in version>=1.7.1 '
  49. 'due to safety concerns, encrypt method will be reset to Paillier')
  50. self.method = consts.PAILLIER
  51. elif user_input == "ipcl":
  52. self.method = consts.PAILLIER_IPCL
  53. else:
  54. raise ValueError(
  55. "encrypt_param's method {} not supported".format(user_input))
  56. if type(self.key_length).__name__ != "int":
  57. raise ValueError(
  58. "encrypt_param's key_length {} not supported, should be int type".format(self.key_length))
  59. elif self.key_length <= 0:
  60. raise ValueError(
  61. "encrypt_param's key_length must be greater or equal to 1")
  62. LOGGER.debug("Finish encrypt parameter check!")
  63. return True