Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Default Key Strength #5251

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/fate_client/flow_client/flow_cli/commands/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def sbt_train_pipeline(guest, host, guest_train_data, host_train_data):
"validation_freqs": 1,
"encrypt_param": {
"method": "paillier",
"key_length": 1024
},
"tree_param": {
"max_depth": 3,
Expand Down
2 changes: 2 additions & 0 deletions python/fate_client/pipeline/param/pearson_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(
need_run=True,
use_mix_rand=False,
calc_local_vif=True,
fixpoint_bit_length=1024
):
super().__init__()
self.column_names = column_names
Expand All @@ -41,6 +42,7 @@ def __init__(
if column_indexes is None:
self.column_indexes = []
self.calc_local_vif = calc_local_vif
self.fixpoint_bit_length = fixpoint_bit_length

def check(self):
if not isinstance(self.use_mix_rand, bool):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,4 @@ def compute_hess(y, y_pred):
hessian : ndarray, the hessian of softmax cross entropy loss

"""
return 2 * y_pred * (1 - y_pred)
return y_pred * (1 - y_pred)
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_compute_hess(self):
pred = np.asarray([random.random() for j in range(5)], dtype='float64')
label = random.randint(0, 4)
softmaxloss_hess = self.softmax_loss.compute_hess(label, pred)
hess = 2 * pred * (1 - pred)
hess = pred * (1 - pred)
self.assertTrue(np.fabs(hess - softmaxloss_hess).all() < consts.FLOAT_ZERO)

def test_compute_loss(self):
Expand Down
5 changes: 3 additions & 2 deletions python/federatedml/param/encrypt_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#
from federatedml.param.base_param import BaseParam
from federatedml.util import consts, LOGGER
from federatedml.secureprotol.conf import PAILLIER_KEYPAIR_N_LENGTH


class EncryptParam(BaseParam):
Expand All @@ -30,12 +31,12 @@ class EncryptParam(BaseParam):
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
key_length : int, default: 2048
Used to specify the length of key in this encryption method.

"""

def __init__(self, method=consts.PAILLIER, key_length=1024):
def __init__(self, method=consts.PAILLIER, key_length=PAILLIER_KEYPAIR_N_LENGTH):
super(EncryptParam, self).__init__()
self.method = method
self.key_length = key_length
Expand Down
11 changes: 6 additions & 5 deletions python/federatedml/param/intersect_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import copy

from federatedml.secureprotol.conf import RSA_BIT_LENGTH, DH_KEY_BIT_LENGTH
from federatedml.param.base_param import BaseParam, deprecated_param
from federatedml.param.base_param import BaseParam
from federatedml.util import consts, LOGGER
Expand Down Expand Up @@ -120,14 +121,14 @@ class RSAParam(BaseParam):
if not None, generate (fraction * public key id count) of r for encryption and reuse generated r;
note that value greater than 0.99 will be taken as 1, and value less than 0.01 will be rounded up to 0.01
key_length: int
value >= 1024, bit count of rsa key, default 1024
value >= 1024, bit count of rsa key, default 2048
random_bit: positive int
it will define the size of blinding factor in rsa algorithm, default 128

"""

def __init__(self, salt='', hash_method='sha256', final_hash_method='sha256',
split_calculation=False, random_base_fraction=None, key_length=consts.DEFAULT_KEY_LENGTH,
split_calculation=False, random_base_fraction=None, key_length=RSA_BIT_LENGTH,
random_bit=DEFAULT_RANDOM_BIT):
super().__init__()
self.salt = salt
Expand Down Expand Up @@ -177,11 +178,11 @@ class DHParam(BaseParam):
the src id will be str = str + salt, default ''
hash_method: str
the hash method of src id, support none, md5, sha1, sha 224, sha256, sha384, sha512, sm3, default sha256
key_length: int, value >= 1024
the key length of the commutative cipher p, default 1024
key_length: int, value >= 2048
the key length of the commutative cipher p, default 2048
"""

def __init__(self, salt='', hash_method='sha256', key_length=consts.DEFAULT_KEY_LENGTH):
def __init__(self, salt='', hash_method='sha256', key_length=DH_KEY_BIT_LENGTH):
super().__init__()
self.salt = salt
self.hash_method = hash_method
Expand Down
13 changes: 12 additions & 1 deletion python/federatedml/param/pearson_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import copy

from federatedml.param.base_param import BaseParam
from federatedml.param.encrypt_param import EncryptParam


class PearsonParam(BaseParam):
Expand All @@ -36,8 +38,10 @@ class PearsonParam(BaseParam):
set False to skip this party
use_mix_rand : bool, defalut: False
mix system random and pseudo random for quicker calculation
calc_loca_vif : bool, default True
calc_local_vif : bool, default True
calculate VIF for columns in local
fixpoint_bit_length: integer, default 2048
bit length to convert floating point number to a fixpoint-integer
"""

def __init__(
Expand All @@ -48,6 +52,7 @@ def __init__(
need_run=True,
use_mix_rand=False,
calc_local_vif=True,
fixpoint_bit_length=2048
):
super().__init__()
self.column_names = column_names
Expand All @@ -56,6 +61,7 @@ def __init__(
self.need_run = need_run
self.use_mix_rand = use_mix_rand
self.calc_local_vif = calc_local_vif
self.fixpoint_bit_length = fixpoint_bit_length

def check(self):
if not isinstance(self.use_mix_rand, bool):
Expand Down Expand Up @@ -97,3 +103,8 @@ def check(self):
):
if len(self.column_indexes) == 0 and len(self.column_names) == 0:
raise ValueError(f"provide at least one column")

if not isinstance(self.fixpoint_bit_length, int) or self.fixpoint_bit_length < 1024:
raise ValueError(
"fixpoint bit length should be integer >= 1024"
)
3 changes: 2 additions & 1 deletion python/federatedml/param/sir_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from federatedml.param.base_param import BaseParam, deprecated_param
from federatedml.param.base_param import BaseParam
from federatedml.param.intersect_param import DHParam
from federatedml.secureprotol.conf import DH_KEY_BIT_LENGTH
from federatedml.util import consts, LOGGER


Expand Down Expand Up @@ -56,7 +57,7 @@ def __init__(self, security_level=0.5,
oblivious_transfer_protocol=consts.OT_HAUCK,
commutative_encryption=consts.CE_PH,
non_committing_encryption=consts.AES,
key_size=consts.DEFAULT_KEY_LENGTH,
key_size=DH_KEY_BIT_LENGTH,
dh_params=DHParam(),
raw_retrieval=False,
target_cols=None):
Expand Down
19 changes: 19 additions & 0 deletions python/federatedml/secureprotol/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# 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.
#

PAILLIER_KEYPAIR_N_LENGTH = 2048
RSA_BIT_LENGTH = 2048
DH_KEY_BIT_LENGTH = 2048
12 changes: 7 additions & 5 deletions python/federatedml/secureprotol/diffie_hellman.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import gmpy2
from gmpy2 import mpz

from federatedml.secureprotol.conf import DH_KEY_BIT_LENGTH


class DiffieHellman(object):

Expand Down Expand Up @@ -159,7 +161,7 @@ def _additional_group_2048_256():
return p, g

@staticmethod
def _oakley_group_key_pair(num_bits=1024):
def _oakley_group_key_pair(num_bits=DH_KEY_BIT_LENGTH):
available = {
1024: DiffieHellman._oakley_group_1024_1024,
2048: DiffieHellman._oakley_group_2048_2048
Expand All @@ -172,7 +174,7 @@ def _oakley_group_key_pair(num_bits=1024):
return available[num_bits].__call__()

@staticmethod
def generate_oakley_group_key_pair(num_bits=1024, pair_name=None):
def generate_oakley_group_key_pair(num_bits=DH_KEY_BIT_LENGTH, pair_name=None):
if pair_name is None:
return DiffieHellman._oakley_group_key_pair(num_bits)

Expand All @@ -186,7 +188,7 @@ def generate_oakley_group_key_pair(num_bits=1024, pair_name=None):
return DiffieHellman._oakley_group_2048_2048()

@staticmethod
def _key_pair(num_bits=1024):
def _key_pair(num_bits=DH_KEY_BIT_LENGTH):
available = {
1024: [
DiffieHellman._oakley_group_1024_1024,
Expand All @@ -205,7 +207,7 @@ def _key_pair(num_bits=1024):
return random.choice(available[num_bits]).__call__()

@staticmethod
def key_pair(num_bits=1024, pair_name=None):
def key_pair(num_bits=DH_KEY_BIT_LENGTH, pair_name=None):
"""
Generate a primitive root for a big prime number is really slow!
Notice the fact that:
Expand Down Expand Up @@ -244,7 +246,7 @@ def key_pair(num_bits=1024, pair_name=None):

# noinspection PyArgumentList
@staticmethod
def generate_secret(p, num_bits=1024):
def generate_secret(p, num_bits=DH_KEY_BIT_LENGTH):
return mpz(random.SystemRandom().getrandbits(num_bits)) % p

@staticmethod
Expand Down
7 changes: 4 additions & 3 deletions python/federatedml/secureprotol/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from Cryptodome.PublicKey import RSA
from federatedml.feature.instance import Instance
from federatedml.secureprotol import gmpy_math
from federatedml.secureprotol.conf import PAILLIER_KEYPAIR_N_LENGTH, RSA_BIT_LENGTH
from federatedml.secureprotol.fate_paillier import PaillierKeypair
from federatedml.secureprotol.fate_paillier import PaillierEncryptedNumber
from federatedml.secureprotol.random import RandomPads
Expand Down Expand Up @@ -134,7 +135,7 @@ def __init__(self):
self.p = None
self.q = None

def generate_key(self, rsa_bit=1024):
def generate_key(self, rsa_bit=RSA_BIT_LENGTH):
random_generator = Random.new().read
rsa = RSA.generate(rsa_bit, random_generator)
self.e = rsa.e
Expand Down Expand Up @@ -180,7 +181,7 @@ class PaillierEncrypt(Encrypt):
def __init__(self):
super(PaillierEncrypt, self).__init__()

def generate_key(self, n_length=1024):
def generate_key(self, n_length=PAILLIER_KEYPAIR_N_LENGTH):
self.public_key, self.privacy_key = PaillierKeypair.generate_keypair(
n_length=n_length
)
Expand Down Expand Up @@ -233,7 +234,7 @@ class IpclPaillierEncrypt(Encrypt):
def __init__(self):
super(IpclPaillierEncrypt, self).__init__()

def generate_key(self, n_length=1024):
def generate_key(self, n_length=PAILLIER_KEYPAIR_N_LENGTH):
self.public_key, self.privacy_key = IpclPaillierKeypair.generate_keypair(
n_length=n_length
)
Expand Down
3 changes: 2 additions & 1 deletion python/federatedml/secureprotol/fate_paillier.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import random

from federatedml.secureprotol import gmpy_math
from federatedml.secureprotol.conf import PAILLIER_KEYPAIR_N_LENGTH
from federatedml.secureprotol.fixedpoint import FixedPointNumber


Expand All @@ -27,7 +28,7 @@ def __init__(self):
pass

@staticmethod
def generate_keypair(n_length=1024):
def generate_keypair(n_length=PAILLIER_KEYPAIR_N_LENGTH):
"""return a new :class:`PaillierPublicKey` and :class:`PaillierPrivateKey`.
"""
p = q = n = None
Expand Down
11 changes: 10 additions & 1 deletion python/federatedml/secureprotol/spdz/spdz.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#

from federatedml.secureprotol.conf import PAILLIER_KEYPAIR_N_LENGTH
from federatedml.secureprotol.fate_paillier import PaillierKeypair
from federatedml.secureprotol.spdz.communicator import Communicator
from federatedml.secureprotol.spdz.utils import NamingService
Expand All @@ -37,7 +38,15 @@ def set_instance(cls, instance):
def has_instance(cls):
return cls.__instance is not None

def __init__(self, name="ss", q_field=None, local_party=None, all_parties=None, use_mix_rand=False, n_length=1024):
def __init__(
self,
name="ss",
q_field=None,
local_party=None,
all_parties=None,
use_mix_rand=False,
n_length=PAILLIER_KEYPAIR_N_LENGTH
):
self.name_service = naming.NamingService(name)
self._prev_name_service = None
self._pre_instance = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#
import random

from federatedml.secureprotol.conf import DH_KEY_BIT_LENGTH
from federatedml.secureprotol.gmpy_math import is_prime, invert, gcd, powmod
from federatedml.secureprotol.symmetric_encryption.symmetric_encryption import SymmetricKey, SymmetricCiphertext
from federatedml.secureprotol.diffie_hellman import DiffieHellman
Expand Down Expand Up @@ -46,7 +47,7 @@ def __init__(self, mod_base, exponent=None):
self.exponent_inverse = None if exponent is None else invert(exponent, mod_base - 1)

@staticmethod
def generate_key(key_size=1024):
def generate_key(key_size=DH_KEY_BIT_LENGTH):
"""
Generate a self-typed object with public mod_base and vacant exponent
:param key_size: int
Expand Down
1 change: 1 addition & 0 deletions python/federatedml/statistic/correlation/hetero_pearson.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def fit(self, data_instance):
local_party=local_party,
all_parties=parties,
use_mix_rand=self.model_param.use_mix_rand,
n_length=self.model_param.fixpoint_bit_length
) as spdz:
LOGGER.info("secret share: prepare data")
if self.is_guest:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from federatedml.param.intersect_param import DEFAULT_RANDOM_BIT
from federatedml.secureprotol import gmpy_math
from federatedml.secureprotol.conf import RSA_BIT_LENGTH
from federatedml.secureprotol.encrypt import RsaEncrypt
from federatedml.secureprotol.hash.hash_factory import Hash
from federatedml.statistic.intersect import Intersect
Expand Down Expand Up @@ -95,7 +96,7 @@ def pubkey_id_generate(k, pair):
hash_operator, salt))

@staticmethod
def generate_rsa_key(rsa_bit=1024):
def generate_rsa_key(rsa_bit=RSA_BIT_LENGTH):
LOGGER.info(f"Generate {rsa_bit}-bit RSA key.")
encrypt_operator = RsaEncrypt()
encrypt_operator.generate_key(rsa_bit)
Expand Down
2 changes: 0 additions & 2 deletions python/federatedml/util/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,6 @@
INNER_JOIN = "inner_join"
LEFT_JOIN = "left_join"

DEFAULT_KEY_LENGTH = 1024

MIN_HASH_FUNC_COUNT = 4
MAX_HASH_FUNC_COUNT = 32

Expand Down
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ casbin==1.16.6
casbin-sqlalchemy-adapter==0.4.2
cloudpickle==2.1.0
cos-python-sdk-v5==1.9.10
Werkzeug<3.0.0
Flask==2.0.3
gmpy2==2.0.8
joblib==1.2.0
Expand Down
Loading