PIROptimizer.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "PIROptimizer.hpp"
  18. static const unsigned int kPrecision = 5;
  19. using boost::asio::ip::tcp;
  20. PIROptimizer::PIROptimizer(DBHandler *db) :
  21. pir("lipmaa"),
  22. filePath("optim/preCompute.abs")
  23. {
  24. fileCount = db->getNbStream();
  25. maxFileBytesize = db->getmaxFileBytesize();
  26. }
  27. PIROptimizer::~PIROptimizer()
  28. {}
  29. void PIROptimizer::speedTest(boost::asio::ip::tcp::socket& s)
  30. {
  31. try
  32. {
  33. unsigned int loop = 1;
  34. char* msg = (char *) malloc(MEGA_BYTE);
  35. boost::system::error_code error;
  36. double start = omp_get_wtime();
  37. for (unsigned int i = 0 ; i < loop ; i++) boost::asio::read(s, boost::asio::buffer(msg, MEGA_BYTE));
  38. cout << "PIROptimizer: Speed test gives upload=" << double(loop / (omp_get_wtime() - start) ) << " MB/s";
  39. start = omp_get_wtime();
  40. for (unsigned int i = 0 ; i < loop ; i++) write(s, boost::asio::buffer(msg, MEGA_BYTE));
  41. cout << ", download=" << double(loop / (omp_get_wtime() - start) ) << " MB/s" << endl;
  42. free(msg);
  43. }
  44. catch(std::exception const& ex)
  45. {
  46. cout << "Error : " << ex.what() << endl;
  47. }
  48. }
  49. unsigned int PIROptimizer::sendAbsAndPrecomputeCaches(boost::asio::ip::tcp::socket& s)
  50. {
  51. try
  52. {
  53. unsigned int crypto_name_size = 0;
  54. read(s, boost::asio::buffer(&crypto_name_size, sizeof(int)));
  55. char crypto_name[crypto_name_size + 1];
  56. crypto_name_size = read(s, boost::asio::buffer(crypto_name, crypto_name_size));
  57. crypto_name[crypto_name_size] = '\0';
  58. cout << "PIROptimizer: Sending absorption and precompute costs for " << crypto_name << endl;
  59. std::string file_path(OptimService::folderName + OptimService::fileName + crypto_name + OptimService::absFileExtension);
  60. std::string file_content;
  61. if (OptimService::readEntireFile(file_content, file_path) < 0) { cout << "PIROptimizer: Error when reading file : " << file_path << ", abort." << endl; return 0;}
  62. int file_content_size = file_content.size();
  63. #ifdef DEBUG
  64. std::cout << "PIROptimizer: Absorption and precompute costs file size " << file_content_size << std::endl;
  65. #endif
  66. write(s, boost::asio::buffer(&file_content_size, sizeof(file_content_size)));
  67. write(s, boost::asio::buffer(file_content));
  68. }
  69. catch(std::exception const& ex)
  70. {
  71. cout << "Error : " << ex.what() << endl;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * Build cache if necessary.
  77. **/
  78. void PIROptimizer::prepareOptimData()
  79. {
  80. for (auto crypto_name : HomomorphicCryptoFactory_internal::crypto_method_name_vec)
  81. {
  82. std::string file_path(OptimService::folderName + OptimService::fileName + crypto_name + OptimService::absFileExtension);
  83. if (OptimService::fileOutdated(crypto_name, OptimService::absFileExtension))
  84. {
  85. std::cout << "PIROptimizer: Absorption and precompute performance cache is outdated, regenerating it" << std::endl;
  86. std::string optim_data2write = computeOptimData(crypto_name);
  87. if(OptimService::writeOptimDataBuffer(optim_data2write, file_path)) {std::cout << "PIROptimizer: Error when writing optimization data, aborting." << std::endl; exit(1);}
  88. std::cout << "PIROptimizer: Finished generating the absorption and precompute performance cache" << std::endl;
  89. }
  90. }
  91. }
  92. std::string PIROptimizer::computeOptimData(const std::string& crypto_name)
  93. {
  94. double abs1plaintext_time, precompute1plaintext_time;
  95. GenericPIRReplyGenerator* generator_ptr = PIRReplyGeneratorFactory::getPIRReplyGenerator(crypto_name);
  96. HomomorphicCrypto* crypto_ptr = HomomorphicCryptoFactory_internal::getCrypto(crypto_name);
  97. std::set<std::string> crypto_params_set;
  98. string optim_data2write;
  99. generator_ptr->setCryptoMethod(crypto_ptr);
  100. crypto_ptr->getAllCryptoParams(crypto_params_set);
  101. for (auto crypto_param : crypto_params_set)
  102. {
  103. std::cout << "PIROptimizer: Generating cache for " << crypto_param << std::endl;
  104. crypto_ptr->getPublicParameters().computeNewParameters(crypto_param);
  105. crypto_ptr->getPublicParameters().setMockedPubKey();
  106. abs1plaintext_time = getAbs1PlaintextTime(crypto_ptr, generator_ptr);
  107. precompute1plaintext_time = getPrecompute1PlaintextTime(crypto_ptr, generator_ptr);
  108. std::ostringstream out;
  109. out << std::setprecision(kPrecision) << abs1plaintext_time << " " << precompute1plaintext_time;
  110. optim_data2write += crypto_param + " " + out.str() + "\n";
  111. }
  112. delete generator_ptr;
  113. delete crypto_ptr;
  114. return optim_data2write;
  115. }
  116. double PIROptimizer::getAbs1PlaintextTime(HomomorphicCrypto* crypto_ptr, GenericPIRReplyGenerator* generator)
  117. {
  118. double result;
  119. uint64_t plaintext_nbr;
  120. PIRParameters pir_params;
  121. pir_params.d = 1;
  122. pir_params.alpha = 1;
  123. pir_params.n[0] = 4;
  124. crypto_ptr->setandgetAbsBitPerCiphertext(pir_params.n[0]); // Set best absorption possible
  125. plaintext_nbr = 4;
  126. do
  127. {
  128. generator->mutex.try_lock();
  129. generator->mutex.unlock();
  130. result = generator->generateReplySimulation(pir_params, plaintext_nbr);
  131. plaintext_nbr *= 2;
  132. }
  133. while(result < 0.5 && plaintext_nbr*pir_params.n[0]*crypto_ptr->getPublicParameters().getCiphertextBitsize() < (1UL<<30));
  134. plaintext_nbr /= 2;
  135. double plaintexts_in_database = (double)pir_params.n[0] * plaintext_nbr;
  136. return result / plaintexts_in_database;
  137. }
  138. double PIROptimizer::getPrecompute1PlaintextTime(HomomorphicCrypto* crypto_ptr, GenericPIRReplyGenerator* generator)
  139. {
  140. double result;
  141. uint64_t plaintext_nbr;
  142. PIRParameters pir_params;
  143. pir_params.d = 1;
  144. pir_params.alpha = 1;
  145. pir_params.n[0] = 4;
  146. crypto_ptr->setandgetAbsBitPerCiphertext(pir_params.n[0]); // Set best absorption possible
  147. plaintext_nbr = 128;
  148. do
  149. {
  150. generator->mutex.try_lock();
  151. generator->mutex.unlock();
  152. result = generator->precomputationSimulation(pir_params, plaintext_nbr);
  153. plaintext_nbr *= 2;
  154. }
  155. while(result < 0.5 && result != 0.0 && plaintext_nbr*pir_params.n[0]*crypto_ptr->getPublicParameters().getCiphertextBitsize() < (1UL<<30));
  156. plaintext_nbr /= 2;
  157. double plaintexts_in_database = (double)pir_params.n[0] * plaintext_nbr;
  158. return result / plaintexts_in_database;
  159. }
  160. void PIROptimizer::serve()
  161. {
  162. tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), COMMAND_AND_CONTROL_PORT));
  163. acceptor.listen(5);
  164. cout << "Waiting client..." << endl;
  165. while(true)
  166. {
  167. boost::asio::ip::tcp::socket socket(acceptor.get_io_service());
  168. acceptor.accept(socket);
  169. // boost::thread t(boost::bind(&PIROptimizer::controlAndCommand, this, socket));
  170. controlAndCommand(socket);
  171. }
  172. }
  173. void PIROptimizer::sendDatabaseInfos(boost::asio::ip::tcp::socket&s)
  174. {
  175. cout << "PIROptimizer: Sending database infos file_count=" << fileCount << ", max_bytesize=" << maxFileBytesize << endl;
  176. try{
  177. /*Send number of files*/
  178. boost::asio::write(s, boost::asio::buffer(&fileCount, sizeof(fileCount)));
  179. /*Send max file size*/
  180. boost::asio::write(s, boost::asio::buffer(&maxFileBytesize, sizeof(maxFileBytesize)));
  181. }catch(std::exception const& ex)
  182. {
  183. cerr << "Error when sending database informations : " << ex.what() << std::endl;
  184. }
  185. }
  186. void PIROptimizer::controlAndCommand(boost::asio::ip::tcp::socket& s)
  187. {
  188. size_t cmd = NOP;
  189. try{
  190. while(cmd != EXIT)
  191. {
  192. boost::asio::read(s, boost::asio::buffer(&cmd, sizeof(size_t)));
  193. switch (cmd)
  194. {
  195. case ABS :
  196. {
  197. sendAbsAndPrecomputeCaches(s);
  198. break;
  199. }
  200. case SPEED :
  201. {
  202. speedTest(s);
  203. break;
  204. }
  205. case EXIT :
  206. {
  207. cout << "PIROptimizer: EXIT command recieved" << endl;
  208. break;
  209. }
  210. case DATA :
  211. {
  212. sendDatabaseInfos(s);
  213. break;
  214. }
  215. default :
  216. {
  217. cout << "PIROptimizer: Looping 30s as requested by client ..." << endl;
  218. sleep(30);
  219. break;
  220. }
  221. }
  222. }
  223. }catch(std::exception const& ex)
  224. {
  225. cerr << "Client quit without send EXIT code: " << ex.what() << endl;
  226. }
  227. }
  228. void PIROptimizer::optimize()
  229. {
  230. serve();
  231. }