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