You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cipher_reconstruct.cc 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #include "fl/armour/cipher/cipher_reconstruct.h"
  17. #include "fl/server/common.h"
  18. #include "fl/armour/secure_protocol/masking.h"
  19. #include "fl/armour/secure_protocol/key_agreement.h"
  20. #include "fl/armour/cipher/cipher_meta_storage.h"
  21. namespace mindspore {
  22. namespace armour {
  23. bool CipherReconStruct::CombineMask(std::vector<Share *> *shares_tmp,
  24. std::map<std::string, std::vector<float>> *client_noise,
  25. const std::vector<std::string> &clients_share_list,
  26. const std::map<std::string, std::vector<std::vector<uint8_t>>> &record_public_keys,
  27. const std::map<std::string, std::vector<clientshare_str>> &reconstruct_secret_list,
  28. const std::vector<string> &client_list,
  29. const std::map<std::string, std::vector<std::vector<uint8_t>>> &client_ivs) {
  30. bool retcode = true;
  31. #ifdef _WIN32
  32. MS_LOG(ERROR) << "Unsupported feature in Windows platform.";
  33. retcode = false;
  34. #else
  35. if (shares_tmp == nullptr || client_noise == nullptr) {
  36. MS_LOG(ERROR) << "shares_tmp or client_noise is nullptr.";
  37. return false;
  38. }
  39. for (auto iter = reconstruct_secret_list.begin(); iter != reconstruct_secret_list.end(); ++iter) {
  40. // define flag_share: judge we need b or s
  41. bool flag_share = true;
  42. const std::string fl_id = iter->first;
  43. if (find(client_list.begin(), client_list.end(), fl_id) != client_list.end()) {
  44. // the client is online
  45. flag_share = false;
  46. }
  47. MS_LOG(INFO) << "fl_id_src : " << fl_id;
  48. BIGNUM *prime = BN_new();
  49. if (prime == nullptr) {
  50. return false;
  51. }
  52. auto publicparam_ = CipherInit::GetInstance().GetPublicParams();
  53. (void)BN_bin2bn(publicparam_->prime, PRIME_MAX_LEN, prime);
  54. if (iter->second.size() >= cipher_init_->secrets_minnums_) { // combine private key seed.
  55. MS_LOG(INFO) << "start assign secrets shares to public shares ";
  56. for (int i = 0; i < static_cast<int>(cipher_init_->secrets_minnums_); ++i) {
  57. shares_tmp->at(i)->index = (iter->second)[i].index;
  58. shares_tmp->at(i)->len = (iter->second)[i].share.size();
  59. if (memcpy_s(shares_tmp->at(i)->data, IntToSize(SHARE_MAX_SIZE), (iter->second)[i].share.data(),
  60. shares_tmp->at(i)->len) != 0) {
  61. MS_LOG(ERROR) << "shares_tmp copy failed";
  62. retcode = false;
  63. }
  64. }
  65. MS_LOG(INFO) << "end assign secrets shares to public shares ";
  66. size_t length;
  67. uint8_t secret[SECRET_MAX_LEN] = {0};
  68. SecretSharing combine(prime);
  69. if (combine.Combine(cipher_init_->secrets_minnums_, *shares_tmp, secret, &length) < 0) retcode = false;
  70. length = SECRET_MAX_LEN;
  71. MS_LOG(INFO) << "combine secrets shares Success.";
  72. if (flag_share) {
  73. // reconstruct pairwise noise
  74. MS_LOG(INFO) << "start reconstruct pairwise noise.";
  75. std::vector<float> noise(cipher_init_->featuremap_, 0.0);
  76. if (GetSuvNoise(clients_share_list, record_public_keys, client_ivs, fl_id, &noise, secret, length) == false) {
  77. MS_LOG(ERROR) << "GetSuvNoise failed";
  78. BN_clear_free(prime);
  79. if (memset_s(secret, SECRET_MAX_LEN, 0, length) != 0) {
  80. MS_LOG(EXCEPTION) << "Memset failed.";
  81. }
  82. return false;
  83. }
  84. (void)client_noise->emplace(std::pair<std::string, std::vector<float>>(fl_id, noise));
  85. } else {
  86. // reconstruct individual noise
  87. MS_LOG(INFO) << "start reconstruct individual noise.";
  88. std::vector<float> noise;
  89. auto it = client_ivs.find(fl_id);
  90. if (it == client_ivs.end()) {
  91. MS_LOG(ERROR) << "cannot get ivs for client: " << fl_id;
  92. return false;
  93. }
  94. if (it->second.size() != IV_NUM) {
  95. MS_LOG(ERROR) << "get " << it->second.size() << " ivs, the iv num required is: " << IV_NUM;
  96. return false;
  97. }
  98. std::vector<uint8_t> ind_iv = it->second[0];
  99. if (Masking::GetMasking(&noise, SizeToInt(cipher_init_->featuremap_), (const uint8_t *)secret, SECRET_MAX_LEN,
  100. ind_iv.data(), SizeToInt(ind_iv.size())) < 0) {
  101. MS_LOG(ERROR) << "Get Masking failed";
  102. if (memset_s(secret, SECRET_MAX_LEN, 0, length) != 0) {
  103. MS_LOG(EXCEPTION) << "Memset failed.";
  104. }
  105. BN_clear_free(prime);
  106. return false;
  107. }
  108. for (size_t index_noise = 0; index_noise < cipher_init_->featuremap_; index_noise++) {
  109. noise[index_noise] *= -1;
  110. }
  111. (void)client_noise->emplace(std::pair<std::string, std::vector<float>>(fl_id, noise));
  112. }
  113. BN_clear_free(prime);
  114. if (memset_s(secret, SECRET_MAX_LEN, 0, length) != 0) {
  115. MS_LOG(EXCEPTION) << "Memset failed.";
  116. }
  117. } else {
  118. MS_LOG(ERROR) << "reconstruct secret failed: the number of secret shares for fl_id: " << fl_id
  119. << " is not enough";
  120. MS_LOG(ERROR) << "get " << iter->second.size()
  121. << "shares, however the secrets_minnums_ required is: " << cipher_init_->secrets_minnums_;
  122. return false;
  123. }
  124. }
  125. #endif
  126. return retcode;
  127. }
  128. bool CipherReconStruct::ReconstructSecretsGenNoise(const std::vector<string> &client_list) {
  129. // get reconstruct_secrets from memory server
  130. MS_LOG(INFO) << "CipherReconStruct::ReconstructSecretsGenNoise START";
  131. bool retcode = true;
  132. std::map<std::string, std::vector<clientshare_str>> reconstruct_secrets;
  133. cipher_init_->cipher_meta_storage_.GetClientSharesFromServer(fl::server::kCtxClientsReconstructShares,
  134. &reconstruct_secrets);
  135. std::map<std::string, std::vector<std::vector<uint8_t>>> record_public_keys;
  136. cipher_init_->cipher_meta_storage_.GetClientKeysFromServer(fl::server::kCtxClientsKeys, &record_public_keys);
  137. std::map<std::string, std::vector<std::vector<uint8_t>>> client_ivs;
  138. cipher_init_->cipher_meta_storage_.GetClientIVsFromServer(fl::server::kCtxClientsKeys, &client_ivs);
  139. std::vector<std::string> clients_share_list;
  140. cipher_init_->cipher_meta_storage_.GetClientListFromServer(fl::server::kCtxShareSecretsClientList,
  141. &clients_share_list);
  142. if (record_public_keys.size() < cipher_init_->exchange_key_threshold ||
  143. clients_share_list.size() < cipher_init_->share_secrets_threshold ||
  144. record_public_keys.size() != client_ivs.size()) {
  145. MS_LOG(ERROR) << "send share client size: " << clients_share_list.size()
  146. << ", send public-key client size: " << record_public_keys.size()
  147. << ", send ivs client size: " << client_ivs.size();
  148. MS_LOG(ERROR) << "get data from server memory failed";
  149. return false;
  150. }
  151. std::map<std::string, std::vector<clientshare_str>> reconstruct_secret_list;
  152. if (!ConvertSharesToShares(reconstruct_secrets, &reconstruct_secret_list)) {
  153. MS_LOG(ERROR) << "ConvertSharesToShares failed.";
  154. return false;
  155. }
  156. MS_LOG(ERROR) << "recombined shares";
  157. for (auto iter = reconstruct_secret_list.begin(); iter != reconstruct_secret_list.end(); ++iter) {
  158. MS_LOG(ERROR) << "fl_id: " << iter->first;
  159. MS_LOG(ERROR) << "share size: " << iter->second.size();
  160. }
  161. std::vector<Share *> shares_tmp;
  162. if (!MallocShares(&shares_tmp, (SizeToInt)(cipher_init_->secrets_minnums_))) {
  163. MS_LOG(ERROR) << "Reconstruct malloc shares_tmp invalid.";
  164. DeleteShares(&shares_tmp);
  165. return false;
  166. }
  167. MS_LOG(INFO) << "Reconstruct secrets shares: ";
  168. std::map<std::string, std::vector<float>> client_noise;
  169. retcode = CombineMask(&shares_tmp, &client_noise, clients_share_list, record_public_keys, reconstruct_secret_list,
  170. client_list, client_ivs);
  171. DeleteShares(&shares_tmp);
  172. if (retcode) {
  173. std::vector<float> noise;
  174. if (!GetNoiseMasksSum(&noise, client_noise)) {
  175. MS_LOG(ERROR) << " GetNoiseMasksSum failed";
  176. return false;
  177. }
  178. client_noise.clear();
  179. MS_LOG(INFO) << " ReconstructSecretsGenNoise updata noise to server";
  180. if (!cipher_init_->cipher_meta_storage_.UpdateClientNoiseToServer(fl::server::kCtxClientNoises, noise)) {
  181. MS_LOG(ERROR) << " ReconstructSecretsGenNoise failed. because UpdateClientNoiseToServer failed";
  182. return false;
  183. }
  184. MS_LOG(INFO) << " ReconstructSecretsGenNoise Success";
  185. } else {
  186. MS_LOG(ERROR) << " ReconstructSecretsGenNoise failed. because gen noise inside failed";
  187. }
  188. return retcode;
  189. }
  190. bool CipherReconStruct::CheckInputs(const schema::SendReconstructSecret *reconstruct_secret_req,
  191. const std::shared_ptr<fl::server::FBBuilder> &fbb, const int cur_iterator,
  192. const std::string &next_req_time) {
  193. if (reconstruct_secret_req == nullptr) {
  194. std::string reason = "Request is nullptr";
  195. MS_LOG(ERROR) << reason;
  196. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_RequestError, reason, cur_iterator, next_req_time);
  197. return false;
  198. }
  199. if (cipher_init_ == nullptr) {
  200. std::string reason = "cipher_init_ is nullptr";
  201. MS_LOG(ERROR) << reason;
  202. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_SystemError, reason, cur_iterator, next_req_time);
  203. return false;
  204. }
  205. return true;
  206. }
  207. // reconstruct secrets
  208. bool CipherReconStruct::ReconstructSecrets(const int cur_iterator, const std::string &next_req_time,
  209. const schema::SendReconstructSecret *reconstruct_secret_req,
  210. const std::shared_ptr<fl::server::FBBuilder> &fbb,
  211. const std::vector<std::string> &client_list) {
  212. MS_LOG(INFO) << "CipherReconStruct::ReconstructSecrets START";
  213. clock_t start_time = clock();
  214. bool inputs_check = CheckInputs(reconstruct_secret_req, fbb, cur_iterator, next_req_time);
  215. if (!inputs_check) return false;
  216. int iterator = reconstruct_secret_req->iteration();
  217. std::string fl_id = reconstruct_secret_req->fl_id()->str();
  218. if (iterator != cur_iterator) {
  219. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_OutOfTime,
  220. "The iteration round of the client does not match the current iteration.", cur_iterator,
  221. next_req_time);
  222. MS_LOG(ERROR) << "Client " << fl_id << " The iteration round of the client does not match the current iteration.";
  223. return false;
  224. }
  225. if (client_list.size() < cipher_init_->reconstruct_secrets_threshold) {
  226. MS_LOG(ERROR) << "illegal parameters. update model client_list size: " << client_list.size();
  227. BuildReconstructSecretsRsp(
  228. fbb, schema::ResponseCode_RequestError,
  229. "illegal parameters: update model client_list size must larger than reconstruct_clients_num_need", cur_iterator,
  230. next_req_time);
  231. return false;
  232. }
  233. std::vector<std::string> get_clients_list;
  234. cipher_init_->cipher_meta_storage_.GetClientListFromServer(fl::server::kCtxGetUpdateModelClientList,
  235. &get_clients_list);
  236. // client not in get client list.
  237. if (find(get_clients_list.begin(), get_clients_list.end(), fl_id) == get_clients_list.end()) {
  238. std::string reason;
  239. MS_LOG(INFO) << "The client " << fl_id << " is not in get update model client list.";
  240. // client in update model client list.
  241. if (find(client_list.begin(), client_list.end(), fl_id) != client_list.end()) {
  242. reason = "The client " + fl_id + " is not in get clients list, but in update model client list.";
  243. MS_LOG(INFO) << reason;
  244. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_SUCCEED, reason, cur_iterator, next_req_time);
  245. return false;
  246. }
  247. reason = "The client " + fl_id + " is not in get clients list, and not in update model client list.";
  248. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_RequestError, "The client is not in update model client list.",
  249. cur_iterator, next_req_time);
  250. return false;
  251. }
  252. std::map<std::string, std::vector<clientshare_str>> reconstruct_shares;
  253. cipher_init_->cipher_meta_storage_.GetClientSharesFromServer(fl::server::kCtxClientsReconstructShares,
  254. &reconstruct_shares);
  255. size_t count_client_num = reconstruct_shares.size();
  256. if (reconstruct_shares.find(fl_id) != reconstruct_shares.end()) {
  257. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_SUCCEED, "Client has sended messages.", cur_iterator,
  258. next_req_time);
  259. MS_LOG(INFO) << "Error, client " << fl_id << " has sended messages.";
  260. return false;
  261. }
  262. auto reconstruct_secret_shares = reconstruct_secret_req->reconstruct_secret_shares();
  263. bool retcode_client =
  264. cipher_init_->cipher_meta_storage_.UpdateClientToServer(fl::server::kCtxReconstructClientList, fl_id);
  265. bool retcode_share = cipher_init_->cipher_meta_storage_.UpdateClientShareToServer(
  266. fl::server::kCtxClientsReconstructShares, fl_id, reconstruct_secret_shares);
  267. if (!(retcode_client && retcode_share)) {
  268. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_OutOfTime, "reconstruct update shares or client failed.",
  269. cur_iterator, next_req_time);
  270. MS_LOG(ERROR) << "reconstruct update shares or client failed.";
  271. return false;
  272. }
  273. count_client_num = count_client_num + 1;
  274. if (count_client_num < cipher_init_->reconstruct_secrets_threshold) {
  275. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_SUCCEED,
  276. "Success, but the server is not ready to reconstruct secret yet.", cur_iterator,
  277. next_req_time);
  278. MS_LOG(INFO) << "Get reconstruct shares from " << fl_id << " Success, but count " << count_client_num
  279. << " is not enough.";
  280. return true;
  281. }
  282. const fl::PBMetadata &clients_noises_pb_out =
  283. fl::server::DistributedMetadataStore::GetInstance().GetMetadata(fl::server::kCtxClientNoises);
  284. const fl::ClientNoises &clients_noises_pb = clients_noises_pb_out.client_noises();
  285. if (clients_noises_pb.has_one_client_noises() == false) {
  286. MS_LOG(INFO) << "Success, the secret will be reconstructed.";
  287. if (ReconstructSecretsGenNoise(client_list)) {
  288. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_SUCCEED, "Success,the secret is reconstructing.",
  289. cur_iterator, next_req_time);
  290. MS_LOG(INFO) << "CipherReconStruct::ReconstructSecrets" << fl_id << " Success, reconstruct ok.";
  291. } else {
  292. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_OutOfTime, "the secret restructs failed.", cur_iterator,
  293. next_req_time);
  294. MS_LOG(ERROR) << "CipherReconStruct::ReconstructSecrets" << fl_id << " failed.";
  295. }
  296. } else {
  297. BuildReconstructSecretsRsp(fbb, schema::ResponseCode_SUCCEED, "Clients' number is full.", cur_iterator,
  298. next_req_time);
  299. MS_LOG(INFO) << "CipherReconStruct::ReconstructSecrets" << fl_id << " Success : no need reconstruct.";
  300. }
  301. clock_t end_time = clock();
  302. double duration = static_cast<double>((end_time - start_time) * 1.0 / CLOCKS_PER_SEC);
  303. MS_LOG(INFO) << "Reconstruct get + gennoise data time is : " << duration;
  304. return true;
  305. }
  306. bool CipherReconStruct::GetNoiseMasksSum(std::vector<float> *result,
  307. const std::map<std::string, std::vector<float>> &client_noise) {
  308. std::vector<float> sum(cipher_init_->featuremap_, 0.0);
  309. for (auto iter = client_noise.begin(); iter != client_noise.end(); iter++) {
  310. if (iter->second.size() != cipher_init_->featuremap_) {
  311. return false;
  312. }
  313. for (size_t i = 0; i < cipher_init_->featuremap_; i++) {
  314. sum[i] += iter->second[i];
  315. }
  316. }
  317. for (size_t i = 0; i < cipher_init_->featuremap_; i++) {
  318. result->push_back(sum[i]);
  319. }
  320. return true;
  321. }
  322. void CipherReconStruct::ClearReconstructSecrets() {
  323. MS_LOG(INFO) << "CipherReconStruct::ClearReconstructSecrets START";
  324. fl::server::DistributedMetadataStore::GetInstance().ResetMetadata(fl::server::kCtxReconstructClientList);
  325. fl::server::DistributedMetadataStore::GetInstance().ResetMetadata(fl::server::kCtxClientsReconstructShares);
  326. fl::server::DistributedMetadataStore::GetInstance().ResetMetadata(fl::server::kCtxClientNoises);
  327. MS_LOG(INFO) << "CipherReconStruct::ClearReconstructSecrets Success";
  328. }
  329. void CipherReconStruct::BuildReconstructSecretsRsp(const std::shared_ptr<fl::server::FBBuilder> &fbb,
  330. const schema::ResponseCode retcode, const std::string &reason,
  331. const int iteration, const std::string &next_req_time) {
  332. auto fbs_reason = fbb->CreateString(reason);
  333. auto fbs_next_req_time = fbb->CreateString(next_req_time);
  334. schema::ReconstructSecretBuilder rsp_reconstruct_secret_builder(*(fbb.get()));
  335. rsp_reconstruct_secret_builder.add_retcode(static_cast<int>(retcode));
  336. rsp_reconstruct_secret_builder.add_reason(fbs_reason);
  337. rsp_reconstruct_secret_builder.add_iteration(iteration);
  338. rsp_reconstruct_secret_builder.add_next_req_time(fbs_next_req_time);
  339. auto rsp_reconstruct_secret = rsp_reconstruct_secret_builder.Finish();
  340. fbb->Finish(rsp_reconstruct_secret);
  341. return;
  342. }
  343. bool CipherReconStruct::GetSuvNoise(const std::vector<std::string> &clients_share_list,
  344. const std::map<std::string, std::vector<std::vector<uint8_t>>> &record_public_keys,
  345. const std::map<std::string, std::vector<std::vector<uint8_t>>> &client_ivs,
  346. const string &fl_id, std::vector<float> *noise, const uint8_t *secret,
  347. size_t length) {
  348. for (auto p_key = clients_share_list.begin(); p_key != clients_share_list.end(); ++p_key) {
  349. if (*p_key != fl_id) {
  350. PrivateKey *privKey = KeyAgreement::FromPrivateBytes(secret, length);
  351. if (privKey == NULL) {
  352. MS_LOG(ERROR) << "create privKey failed\n";
  353. return false;
  354. }
  355. std::vector<uint8_t> public_key = record_public_keys.at(*p_key)[1];
  356. std::string iv_fl_id;
  357. if (fl_id < *p_key) {
  358. iv_fl_id = fl_id;
  359. } else {
  360. iv_fl_id = *p_key;
  361. }
  362. auto iter = client_ivs.find(iv_fl_id);
  363. if (iter == client_ivs.end()) {
  364. MS_LOG(ERROR) << "cannot get ivs for client: " << iv_fl_id;
  365. delete privKey;
  366. return false;
  367. }
  368. if (iter->second.size() != IV_NUM) {
  369. MS_LOG(ERROR) << "get " << iter->second.size() << " ivs, the iv num required is: " << IV_NUM;
  370. return false;
  371. }
  372. std::vector<uint8_t> pw_iv = iter->second[PW_IV_INDEX];
  373. std::vector<uint8_t> pw_salt = iter->second[PW_SALT_INDEX];
  374. PublicKey *pubKey = KeyAgreement::FromPublicBytes(public_key.data(), public_key.size());
  375. if (pubKey == NULL) {
  376. MS_LOG(ERROR) << "create pubKey failed\n";
  377. return false;
  378. }
  379. MS_LOG(INFO) << "private_key fl_id : " << fl_id << " public_key fl_id : " << *p_key;
  380. uint8_t secret1[SECRET_MAX_LEN] = {0};
  381. int ret = KeyAgreement::ComputeSharedKey(privKey, pubKey, SECRET_MAX_LEN, pw_salt.data(),
  382. SizeToInt(pw_salt.size()), secret1);
  383. delete privKey;
  384. delete pubKey;
  385. if (ret < 0) {
  386. MS_LOG(ERROR) << "ComputeSharedKey failed\n";
  387. return false;
  388. }
  389. std::vector<float> noise_tmp;
  390. if (Masking::GetMasking(&noise_tmp, SizeToInt(cipher_init_->featuremap_), (const uint8_t *)secret1,
  391. SECRET_MAX_LEN, pw_iv.data(), pw_iv.size()) < 0) {
  392. MS_LOG(ERROR) << "Get Masking failed\n";
  393. return false;
  394. }
  395. bool symbol_noise = GetSymbol(fl_id, *p_key);
  396. size_t index = 0;
  397. size_t size_noise = noise_tmp.size();
  398. if (symbol_noise == false) {
  399. for (; index < size_noise; ++index) {
  400. noise_tmp[index] = noise_tmp[index] * -1;
  401. noise->at(index) += noise_tmp[index];
  402. }
  403. } else {
  404. for (; index < size_noise; ++index) {
  405. noise->at(index) += noise_tmp[index];
  406. }
  407. }
  408. }
  409. }
  410. return true;
  411. }
  412. bool CipherReconStruct::GetSymbol(const std::string &str1, const std::string &str2) {
  413. if (str1 > str2) {
  414. return true;
  415. } else {
  416. return false;
  417. }
  418. }
  419. // recombined shares by their source fl_id (ownners)
  420. bool CipherReconStruct::ConvertSharesToShares(const std::map<std::string, std::vector<clientshare_str>> &src,
  421. std::map<std::string, std::vector<clientshare_str>> *des) {
  422. if (des == nullptr) return false;
  423. for (auto iter = src.begin(); iter != src.end(); ++iter) {
  424. std::string des_id = iter->first;
  425. auto &cur_clientshare_str = iter->second;
  426. for (size_t index_clientshare = 0; index_clientshare < cur_clientshare_str.size(); ++index_clientshare) {
  427. std::string src_id = cur_clientshare_str[index_clientshare].fl_id;
  428. clientshare_str value;
  429. value.fl_id = des_id;
  430. value.share = cur_clientshare_str[index_clientshare].share;
  431. value.index = cur_clientshare_str[index_clientshare].index;
  432. if (des->find(src_id) == des->end()) { // src_id is not in recombined shares list
  433. std::vector<clientshare_str> value_list;
  434. value_list.push_back(value);
  435. (void)des->emplace(std::pair<std::string, std::vector<clientshare_str>>(src_id, value_list));
  436. } else {
  437. des->at(src_id).push_back(value);
  438. }
  439. }
  440. }
  441. return true;
  442. }
  443. bool CipherReconStruct::MallocShares(std::vector<Share *> *shares_tmp, size_t shares_size) {
  444. if (shares_tmp == nullptr) return false;
  445. for (size_t i = 0; i < shares_size; ++i) {
  446. Share *share_i = new (std::nothrow) Share();
  447. if (share_i == nullptr) {
  448. MS_LOG(ERROR) << "new Share failed.";
  449. return false;
  450. }
  451. share_i->data = new uint8_t[SHARE_MAX_SIZE];
  452. if (share_i->data == nullptr) {
  453. MS_LOG(ERROR) << "malloc memory failed.";
  454. delete share_i;
  455. return false;
  456. }
  457. share_i->index = 0;
  458. share_i->len = SHARE_MAX_SIZE;
  459. shares_tmp->push_back(share_i);
  460. }
  461. return true;
  462. }
  463. void CipherReconStruct::DeleteShares(std::vector<Share *> *shares_tmp) {
  464. if (shares_tmp == nullptr) return;
  465. if (shares_tmp->size() != 0) {
  466. for (size_t i = 0; i < shares_tmp->size(); ++i) {
  467. if (shares_tmp->at(i) != nullptr && shares_tmp->at(i)->data != nullptr) {
  468. delete[](shares_tmp->at(i)->data);
  469. shares_tmp->at(i)->data = nullptr;
  470. }
  471. delete shares_tmp->at(i);
  472. shares_tmp->at(i) = nullptr;
  473. }
  474. }
  475. return;
  476. }
  477. } // namespace armour
  478. } // namespace mindspore