1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
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
17 #include "fl/armour/cipher/cipher_init.h"
18
19 #include "fl/armour/cipher/cipher_meta_storage.h"
20 #include "fl/server/common.h"
21 #include "fl/server/model_store.h"
22
23 namespace mindspore {
24 namespace armour {
Init(const CipherPublicPara & param,size_t time_out_mutex,size_t cipher_exchange_keys_cnt,size_t cipher_get_keys_cnt,size_t cipher_share_secrets_cnt,size_t cipher_get_secrets_cnt,size_t cipher_get_clientlist_cnt,size_t cipher_reconstruct_secrets_up_cnt)25 bool CipherInit::Init(const CipherPublicPara ¶m, size_t time_out_mutex, size_t cipher_exchange_keys_cnt,
26 size_t cipher_get_keys_cnt, size_t cipher_share_secrets_cnt, size_t cipher_get_secrets_cnt,
27 size_t cipher_get_clientlist_cnt, size_t cipher_reconstruct_secrets_up_cnt) {
28 MS_LOG(INFO) << "CipherInit::Init START";
29 if (publicparam_.p == nullptr || param.p == nullptr || param.prime == nullptr || publicparam_.prime == nullptr) {
30 MS_LOG(ERROR) << "CipherInit::input data invalid.";
31 return false;
32 }
33 if (memcpy_s(publicparam_.p, SECRET_MAX_LEN, param.p, sizeof(param.p)) != 0) {
34 MS_LOG(ERROR) << "CipherInit::memory copy failed.";
35 return false;
36 }
37
38 publicparam_.g = param.g;
39 publicparam_.t = param.t;
40 secrets_minnums_ = IntToSize(param.t);
41 featuremap_ = fl::server::ModelStore::GetInstance().model_size() / sizeof(float);
42
43 exchange_key_threshold = cipher_exchange_keys_cnt;
44 get_key_threshold = cipher_get_keys_cnt;
45 share_secrets_threshold = cipher_share_secrets_cnt;
46 get_secrets_threshold = cipher_get_secrets_cnt;
47 client_list_threshold = cipher_get_clientlist_cnt;
48 reconstruct_secrets_threshold = cipher_reconstruct_secrets_up_cnt;
49
50 time_out_mutex_ = time_out_mutex;
51 publicparam_.dp_eps = param.dp_eps;
52 publicparam_.dp_delta = param.dp_delta;
53 publicparam_.dp_norm_clip = param.dp_norm_clip;
54 publicparam_.encrypt_type = param.encrypt_type;
55
56 if (param.encrypt_type == mindspore::ps::kDPEncryptType) {
57 MS_LOG(INFO) << "DP parameters init, dp_eps: " << param.dp_eps;
58 MS_LOG(INFO) << "DP parameters init, dp_delta: " << param.dp_delta;
59 MS_LOG(INFO) << "DP parameters init, dp_norm_clip: " << param.dp_norm_clip;
60 }
61
62 if (param.encrypt_type == mindspore::ps::kPWEncryptType) {
63 cipher_meta_storage_.RegisterClass();
64 const std::string new_prime(reinterpret_cast<const char *>(param.prime), PRIME_MAX_LEN);
65 cipher_meta_storage_.RegisterPrime(fl::server::kCtxCipherPrimer, new_prime);
66 if (!cipher_meta_storage_.GetPrimeFromServer(fl::server::kCtxCipherPrimer, publicparam_.prime)) {
67 MS_LOG(ERROR) << "Cipher Param Update is invalid.";
68 return false;
69 }
70 MS_LOG(INFO) << " CipherInit exchange_key_threshold : " << exchange_key_threshold;
71 MS_LOG(INFO) << " CipherInit get_key_threshold : " << get_key_threshold;
72 MS_LOG(INFO) << " CipherInit share_secrets_threshold : " << share_secrets_threshold;
73 MS_LOG(INFO) << " CipherInit get_secrets_threshold : " << get_secrets_threshold;
74 MS_LOG(INFO) << " CipherInit client_list_threshold : " << client_list_threshold;
75 MS_LOG(INFO) << " CipherInit reconstruct_secrets_threshold : " << reconstruct_secrets_threshold;
76 MS_LOG(INFO) << " CipherInit featuremap_ : " << featuremap_;
77 if (!Check_Parames()) {
78 MS_LOG(ERROR) << "Cipher parameters are illegal.";
79 return false;
80 }
81 MS_LOG(INFO) << " CipherInit::Init Success";
82 }
83 return true;
84 }
85
Check_Parames()86 bool CipherInit::Check_Parames() {
87 if (featuremap_ < 1) {
88 MS_LOG(ERROR) << "Featuremap size should be positive, but got " << featuremap_;
89 return false;
90 }
91
92 if (share_secrets_threshold < reconstruct_secrets_threshold) {
93 MS_LOG(ERROR) << "reconstruct_secrets_threshold should not be larger "
94 "than share_secrets_threshold, but got they are:"
95 << reconstruct_secrets_threshold << ", " << share_secrets_threshold;
96 return false;
97 }
98
99 return true;
100 }
101 } // namespace armour
102 } // namespace mindspore
103