PIRClient.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "PIRClient.hpp"
  18. //Use this to limit the upload speed to UPLOAD_LIMIT bits per second
  19. //#define UPLOAD_LIMIT 100000000UL
  20. PIRClientSimple::PIRClientSimple(boost::asio::io_service& ios, ClientParams params, FixedVars vars):
  21. socket_up(ios),
  22. replyWriter(pirParams, writeListeners, messageListeners),
  23. catalog(messageListeners),
  24. clientParams(params),
  25. fixedVars(vars),
  26. optimum(vars),
  27. no_pipeline_mode(false)
  28. {
  29. replyWriter.setdontWrite(params.dontwrite);
  30. //string lwe_params(crypto_params_const);
  31. //cryptoMethod.getPublicParameters().computeNewParameters(lwe_params);
  32. //pirParams.d = kDimensionNumber;
  33. }
  34. void PIRClientSimple::joinAllThreads()
  35. {
  36. replyExt->replyThread.join();
  37. replyWriter.join();
  38. }
  39. PIRClientSimple::~PIRClientSimple()
  40. {
  41. joinAllThreads();
  42. delete cryptoMethod;
  43. }
  44. void PIRClientSimple::connect()
  45. {
  46. using namespace boost::asio::ip;
  47. try
  48. {
  49. socket_up.connect(tcp::endpoint(address::from_string(clientParams.server_ip), clientParams.port));
  50. // Tell the server we are a client
  51. int is_client = 1;
  52. write(socket_up, boost::asio::buffer(&is_client, sizeof(int)));
  53. }
  54. catch (const std::exception& ex)
  55. {
  56. exitWithErrorMessage(__FUNCTION__, ex.what());
  57. }
  58. }
  59. /**
  60. * Downloads the file catalog from the server.
  61. * Exception :
  62. * - Stop the programme if any network trouble.
  63. **/
  64. void PIRClientSimple::downloadCatalog()
  65. {
  66. boost::uint64_t size = 1;
  67. try
  68. {
  69. read(socket_up, boost::asio::buffer(&size, sizeof(size)));
  70. std::cout<< "PIRClient: Catalog bytesize is "<<size<<std::endl;
  71. char* buf = new char[size + 1]();
  72. if (read(socket_up, boost::asio::buffer(buf, size)) < size )
  73. writeWarningMessage(__FUNCTION__, " Catalog has not been fully received.");
  74. catalog.makeMenu(buf);
  75. if (catalog.getFilesNum() == 0)
  76. {
  77. writeWarningMessage(__FUNCTION__,"Empty catalog recieved ! Is there a db directory with files in the server/ directory ? Aborting ");
  78. exit(1);
  79. }
  80. delete[] buf;
  81. std::cout << "PIRClient: Catalog received with " << catalog.getFilesNum() << " files" << std::endl;
  82. }
  83. catch (const std::exception& ex)
  84. {
  85. exitWithErrorMessage(__FUNCTION__, ex.what());
  86. }
  87. }
  88. void PIRClientSimple::optimize()
  89. {
  90. if (exchange_method == CLIENT_DRIVEN)
  91. {
  92. PIROptimizer optimizer;
  93. if (clientParams.verboseoptim == false) optimizer.silent = true;
  94. std::cout << "PIRClient: Starting optimization ..." << std::endl;
  95. OptimVars op = optimizer.optimizeFromServer(fixedVars, socket_up);
  96. optimum = op;
  97. std::cout << "PIRClient: Optimization returned alpha=" << optimum.alpha << " d=" << optimum.d << " crypto_params=" << optimum.crypto_params << std::endl;
  98. pirParams.d = optimum.d;
  99. pirParams.alpha = optimum.alpha;
  100. optimizer.getDimSize(optimum.getFixedVars().n, optimum.alpha, optimum.d, pirParams.n);
  101. pirParams.crypto_params = std::string(optimum.crypto_params);
  102. }
  103. }
  104. void PIRClientSimple::processPIRParams()
  105. {
  106. if (exchange_method == CLIENT_DRIVEN)
  107. {
  108. sendPirParams();
  109. }
  110. else rcvPirParams();
  111. }
  112. /**
  113. * Sends PIR parameters to the server.
  114. * Exception :
  115. * - Stop the programme if any network trouble.
  116. **/
  117. void PIRClientSimple::sendPirParams()
  118. {
  119. try
  120. {
  121. write(socket_up, boost::asio::buffer(&pirParams.d, sizeof(int)));
  122. write(socket_up, boost::asio::buffer(&pirParams.alpha, sizeof(int)));
  123. for (unsigned int i = 0 ; i < pirParams.d ; i++)
  124. {
  125. write(socket_up, boost::asio::buffer(&pirParams.n[i], sizeof(int)));
  126. }
  127. }
  128. catch (const std::exception& ex)
  129. {
  130. exitWithErrorMessage(__FUNCTION__, ex.what());
  131. }
  132. }
  133. void PIRClientSimple::rcvPirParams()
  134. {
  135. try
  136. {
  137. read(socket_up, boost::asio::buffer(&pirParams.d, sizeof(int)));
  138. read(socket_up, boost::asio::buffer(&pirParams.alpha, sizeof(int)));
  139. for (unsigned int i = 0 ; i < pirParams.d ; i++)
  140. {
  141. read(socket_up, boost::asio::buffer(&pirParams.n[i], sizeof(int)));
  142. }
  143. std::cout << "PIRClient: Received PIR parameters alpha=" << pirParams.alpha << " d=" << pirParams.d << std::endl;
  144. }
  145. catch (const std::exception& ex)
  146. {
  147. exitWithErrorMessage(__FUNCTION__, ex.what());
  148. }
  149. }
  150. void PIRClientSimple::rcvPIRParamsExchangeMethod()
  151. {
  152. try
  153. {
  154. read(socket_up, boost::asio::buffer(&exchange_method, sizeof(short)));
  155. if (exchange_method == CLIENT_DRIVEN)
  156. {
  157. std::cout << "PIRClient: Client-driven mode used" << std::endl;
  158. }
  159. else
  160. {
  161. std::cout << "PIRClient: Server-driven mode used" << std::endl;
  162. }
  163. }
  164. catch (const std::exception& ex)
  165. {
  166. exitWithErrorMessage(__FUNCTION__, ex.what());
  167. }
  168. }
  169. void PIRClientSimple::processCryptoParams() {
  170. bool send_paramsandkey = true;
  171. if (exchange_method == CLIENT_DRIVEN)
  172. {
  173. cryptoMethod = HomomorphicCryptoFactory_internal::getCryptoMethod(optimum.crypto_params);
  174. replyWriter.setCryptoMethod(cryptoMethod);
  175. sendCryptoParams(send_paramsandkey);
  176. }
  177. else
  178. {
  179. rcvCryptoParams();
  180. cryptoMethod = HomomorphicCryptoFactory_internal::getCryptoMethod(pirParams.crypto_params);
  181. replyWriter.setCryptoMethod(cryptoMethod);
  182. sendCryptoParams(!send_paramsandkey);
  183. }
  184. }
  185. /**
  186. * Send cryptographics parameters (e.g : key) to the server.
  187. * Exception :
  188. * - Stop the programme if any network trouble.
  189. **/
  190. void PIRClientSimple::sendCryptoParams(bool paramsandkey)
  191. {
  192. unsigned int size;
  193. char* key;
  194. try
  195. {
  196. if (paramsandkey == true)
  197. {
  198. bool shortversion = true;
  199. string crypto_params = cryptoMethod->getSerializedCryptoParams(!shortversion);
  200. size = crypto_params.length();
  201. write(socket_up, boost::asio::buffer(&size, sizeof(size)));
  202. write(socket_up, boost::asio::buffer(crypto_params));
  203. }
  204. size = ceil((double) cryptoMethod->getPublicParameters().getSerializedModulusBitsize() / GlobalConstant::kBitsPerByte);
  205. write(socket_up, boost::asio::buffer(&size, sizeof(size)));
  206. key = cryptoMethod->getPublicParameters().getByteModulus();
  207. write(socket_up, boost::asio::buffer(key, size));//Send key
  208. delete[] key;
  209. }
  210. catch (const std::exception& ex)
  211. {
  212. exitWithErrorMessage(__FUNCTION__, ex.what());
  213. }
  214. }
  215. void PIRClientSimple::rcvCryptoParams() {
  216. try
  217. {
  218. int crypto_params_size = 0;
  219. read(socket_up, boost::asio::buffer(&crypto_params_size, sizeof(crypto_params_size)));
  220. char crypto_params[crypto_params_size + 1];
  221. int read_size = read(socket_up, boost::asio::buffer(crypto_params,crypto_params_size));
  222. crypto_params[read_size] = '\0';
  223. pirParams.crypto_params = string(crypto_params);
  224. std::cout << "PIRClient: Received crypto parameters " << crypto_params << std::endl;
  225. }
  226. catch (const std::exception& ex)
  227. {
  228. exitWithErrorMessage(__FUNCTION__, ex.what());
  229. }
  230. }
  231. /**
  232. * Launches the choose File action to the recorded view. If autochoice is set,
  233. * the first file is chosen.
  234. **/
  235. void PIRClientSimple::chooseFile()
  236. {
  237. chosenElement = 0;
  238. if(!clientParams.autochoice)
  239. {
  240. CatalogEvent catalogEvent(catalog.getFileList());
  241. menuListeners(catalogEvent); // get user input and set chosenElement.
  242. }
  243. std::ostringstream oss;
  244. oss << "PIRClient: Retrieved file is nbr " + (chosenElement+1);
  245. oss << + ", \"" + catalog.getFileName(chosenElement) + "\"";
  246. MessageEvent messageEvent(oss.str());
  247. messageListeners(messageEvent);
  248. }
  249. /**
  250. * Set the query generator and launch parallely query generation and query upload.
  251. **/
  252. void PIRClientSimple::startProcessQuery()
  253. {
  254. PIRQueryGenerator_internal queryGen(pirParams, *cryptoMethod);
  255. queryGen.setChosenElement(chosenElement);
  256. if(no_pipeline_mode) {
  257. std::cout << "PIRClient: Calling query generation and upload without pipelining" << std::endl;
  258. gq_start = clock();
  259. queryGen.generateQuery();
  260. gq_stop = clock();
  261. } else {
  262. queryGen.startGenerateQuery();
  263. }
  264. uploadWorker(queryGen);
  265. }
  266. void sleepForBytes(unsigned int bytes) {
  267. #ifdef UPLOAD_LIMIT
  268. uint64_t seconds=(bytes*8)/UPLOAD_LIMIT;
  269. uint64_t nanoseconds=((((double)bytes*8.)/(double)UPLOAD_LIMIT)-(double)seconds)*1000000000UL;
  270. struct timespec req={0},rem={0};
  271. req.tv_sec=seconds;
  272. req.tv_nsec=nanoseconds;
  273. double st=omp_get_wtime();
  274. nanosleep(&req,&rem);
  275. #endif
  276. }
  277. /**
  278. * Upload query to the server et delete its parts from the shared query queue.
  279. * Exception :
  280. * - Stop the programme if any network trouble.
  281. **/
  282. void PIRClientSimple::uploadWorker(PIRQueryGenerator_internal& queryGen)
  283. {
  284. unsigned int size = 0;
  285. char *tmp;
  286. try
  287. {
  288. for (unsigned int j = 1 ; j <= pirParams.d ; j++)
  289. {
  290. size = cryptoMethod->getPublicParameters().getQuerySizeFromRecLvl(j) / GlobalConstant::kBitsPerByte;
  291. for (unsigned int i = 0 ; i < pirParams.n[j - 1] ; i++)
  292. {
  293. tmp = queryGen.queryBuffer.pop_front();
  294. write(socket_up, boost::asio::buffer(tmp, size));
  295. free(tmp);
  296. #ifdef UPLOAD_LIMIT
  297. sleepForBytes(size);
  298. #endif
  299. }
  300. }
  301. }
  302. catch (const std::exception& ex)
  303. {
  304. exitWithErrorMessage(__FUNCTION__, ex.what());
  305. }
  306. }
  307. /**
  308. * Sets reply extractpr and lauches parallely reply download and reply extraction.
  309. **/
  310. void PIRClientSimple::startProcessResult()
  311. {
  312. replyExt = new PIRReplyExtraction_internal(pirParams,*cryptoMethod);
  313. replyExt->setFileParam(catalog.getFileName(chosenElement), catalog.getFileSize(chosenElement));
  314. if(no_pipeline_mode)
  315. {
  316. std::cout << "PIRClient: Calling reply download, extraction and writting without pipelining" << std::endl;
  317. downloadWorker(*replyExt);
  318. er_start = clock();
  319. replyExt->extractReply(catalog.getMaxFileSize()*pirParams.alpha, replyWriter.getClearDataQueue());
  320. er_stop = clock();
  321. tclock = gq_stop - gq_start;
  322. std::cout<<"GenQuery Time = %f ms"<< tclock/CLOCKS_PER_SEC <<endl;
  323. tclock = er_stop - er_start;
  324. std::cout<<"ExtractReply Time = %f ms"<< tclock/CLOCKS_PER_SEC <<endl;
  325. // Tell reply writer we finished the extraction
  326. replyWriter.writeAggregatedFileSecurely(chosenElement, catalog);
  327. }
  328. else
  329. {
  330. replyExt->startExtractReply(catalog.getMaxFileSize()*pirParams.alpha, replyWriter.getClearDataQueue());
  331. replyWriter.startFileWritting(chosenElement, catalog);
  332. downloadWorker(*replyExt);
  333. }
  334. }
  335. /**
  336. * Download reply from the server and stores it chunks in shared replies queue.
  337. * Exception :
  338. * - Stop the program if any network trouble.
  339. **/
  340. void PIRClientSimple::downloadWorker(PIRReplyExtraction_internal& turlututu)
  341. {
  342. using namespace GlobalConstant;
  343. unsigned int i, ciph_siz = cryptoMethod->getPublicParameters().getCiphBitsizeFromRecLvl(pirParams.d)/kBitsPerByte;
  344. double paquet_nbr = ceil(static_cast<double>(catalog.getMaxFileSize()*pirParams.alpha) / double(cryptoMethod->getPublicParameters().getAbsorptionBitsize(0)/kBitsPerByte));
  345. #ifdef DEBUG
  346. cout << "PIRClient: First layer nbr of ciphertexts is " << paquet_nbr << endl;
  347. #endif
  348. for (unsigned int i = 1 ; i < pirParams.d; i++) paquet_nbr = ceil(paquet_nbr * double(cryptoMethod->getPublicParameters().getCiphBitsizeFromRecLvl(i)/kBitsPerByte) / double(cryptoMethod->getPublicParameters().getAbsorptionBitsize(i) / kBitsPerByte));
  349. char* buf;
  350. #ifdef DEBUG
  351. cout << "PIRClient: getMaxFileSize=" << catalog.getMaxFileSize() << " alpha=" << pirParams.alpha << " getAbsorptionBitsize=" << double(cryptoMethod->getPublicParameters().getAbsorptionBitsize(0))/*kBitsPerByte);*/ << endl;
  352. cout << "PIRClient: Last layer nbr of ciphertexts is " << paquet_nbr << endl;
  353. cout << "PIRClient: File size of the chosen element is " << catalog.getFileSize(chosenElement) << endl;
  354. cout << "PIRClient: Ciphertext bytesize is " << ciph_siz << endl;
  355. #endif
  356. try
  357. {
  358. for (i = 0 ; i < paquet_nbr ; i++)
  359. {
  360. buf = (char*) malloc(ciph_siz * sizeof(char));
  361. read(socket_up,boost::asio::buffer(buf, ciph_siz));
  362. replyExt->repliesBuffer.push(buf);
  363. }
  364. }
  365. catch (const std::exception& ex)
  366. {
  367. #ifdef DEBUG
  368. cerr << "PIRReplyWriter: Number of downloaded chunks: " << i << "/" << paquet_nbr << endl;
  369. #endif
  370. exitWithErrorMessage(__FUNCTION__, ex.what());
  371. return;
  372. }
  373. writeWarningMessage(__FUNCTION__, "done.");
  374. }
  375. /**
  376. * Sets the chosen element.
  377. **/
  378. void PIRClientSimple::setChosenElement(uint64_t choice)
  379. {
  380. chosenElement = choice;
  381. }
  382. //void PIRClientSimple::setPIRParameters(PIRParameters& pirParameters)
  383. //{
  384. // pirParams.alpha = pirParameters.alpha;
  385. // pirParams.d = pirParameters.d;
  386. // memcpy(pirParams.n, pirParameters.n, MAX_REC_LVL);
  387. // pirParams.r;
  388. //}
  389. /**
  390. * Add a new message listener.
  391. * Param :
  392. * - messageListener::slot_function_type subscriber, a message listener to add.
  393. **/
  394. boost::signals2::connection PIRClientSimple::addMessageListener(messageListener::slot_function_type subscriber)
  395. {
  396. return messageListeners.connect(subscriber);
  397. }
  398. /**
  399. * Add a new menu listener.
  400. * Param :
  401. * - menuListener::slot_function_type subscriber, a menu listener to add.
  402. **/
  403. boost::signals2::connection PIRClientSimple::addMenuListener(menuListener::slot_function_type subscriber)
  404. {
  405. return menuListeners.connect(subscriber);
  406. }
  407. /**
  408. * Add a new wite listener
  409. * Param :
  410. * - writeListener::slot_function_type subscriber, a write listener to add.
  411. **/
  412. boost::signals2::connection PIRClientSimple::addWriteListener(writeListener::slot_function_type subscriber)
  413. {
  414. return writeListeners.connect(subscriber);
  415. }
  416. void PIRClientSimple::exitWithErrorMessage(string s1, string s2)
  417. {
  418. writeErrorMessage(s1 ,s2);
  419. socket_up.close();
  420. exit(errno);
  421. }
  422. /**
  423. * Send ERROR message to listeners
  424. * Params :
  425. * - string funcName : usually the function name who throw the error message ;
  426. * - string message : message to show.
  427. **/
  428. void PIRClientSimple::writeErrorMessage(string funcName, string message)
  429. {
  430. MessageEvent event(ERROR, message, funcName);
  431. messageListeners(event);
  432. }
  433. /**
  434. * Send WARNING message to listeners
  435. * Params :
  436. * - string funcName : usually the function name who throw the warning message ;
  437. * - string message : message to show.
  438. **/
  439. void PIRClientSimple::writeWarningMessage(string funcName, string message)
  440. {
  441. MessageEvent event(WARNING, message, funcName);
  442. messageListeners(event);
  443. }
  444. void PIRClientSimple::no_pipeline(bool b)
  445. {
  446. no_pipeline_mode = b;
  447. }