PIROptimizer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.unlock();
  129. result = generator->generateReplySimulation(pir_params, plaintext_nbr);
  130. plaintext_nbr *= 2;
  131. }
  132. while(result < 0.5 && plaintext_nbr*pir_params.n[0]*crypto_ptr->getPublicParameters().getCiphertextBitsize() < (1UL<<30));
  133. plaintext_nbr /= 2;
  134. double plaintexts_in_database = (double)pir_params.n[0] * plaintext_nbr;
  135. return result / plaintexts_in_database;
  136. }
  137. double PIROptimizer::getPrecompute1PlaintextTime(HomomorphicCrypto* crypto_ptr, GenericPIRReplyGenerator* generator)
  138. {
  139. double result;
  140. uint64_t plaintext_nbr;
  141. PIRParameters pir_params;
  142. pir_params.d = 1;
  143. pir_params.alpha = 1;
  144. pir_params.n[0] = 4;
  145. crypto_ptr->setandgetAbsBitPerCiphertext(pir_params.n[0]); // Set best absorption possible
  146. plaintext_nbr = 128;
  147. do
  148. {
  149. generator->mutex.unlock();
  150. result = generator->precomputationSimulation(pir_params, plaintext_nbr);
  151. plaintext_nbr *= 2;
  152. }
  153. while(result < 0.5 && result != 0.0 && plaintext_nbr*pir_params.n[0]*crypto_ptr->getPublicParameters().getCiphertextBitsize() < (1UL<<30));
  154. plaintext_nbr /= 2;
  155. double plaintexts_in_database = (double)pir_params.n[0] * plaintext_nbr;
  156. return result / plaintexts_in_database;
  157. }
  158. void PIROptimizer::serve()
  159. {
  160. tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), COMMAND_AND_CONTROL_PORT));
  161. acceptor.listen(5);
  162. cout << "Waiting client..." << endl;
  163. while(true)
  164. {
  165. boost::asio::ip::tcp::socket socket(acceptor.get_io_service());
  166. acceptor.accept(socket);
  167. // boost::thread t(boost::bind(&PIROptimizer::controlAndCommand, this, socket));
  168. controlAndCommand(socket);
  169. }
  170. }
  171. void PIROptimizer::sendDatabaseInfos(boost::asio::ip::tcp::socket&s)
  172. {
  173. cout << "PIROptimizer: Sending database infos file_count=" << fileCount << ", max_bytesize=" << maxFileBytesize << endl;
  174. try{
  175. /*Send number of files*/
  176. boost::asio::write(s, boost::asio::buffer(&fileCount, sizeof(fileCount)));
  177. /*Send max file size*/
  178. boost::asio::write(s, boost::asio::buffer(&maxFileBytesize, sizeof(maxFileBytesize)));
  179. }catch(std::exception const& ex)
  180. {
  181. cerr << "Error when sending database informations : " << ex.what() << std::endl;
  182. }
  183. }
  184. void PIROptimizer::controlAndCommand(boost::asio::ip::tcp::socket& s)
  185. {
  186. size_t cmd = NOP;
  187. try{
  188. while(cmd != EXIT)
  189. {
  190. boost::asio::read(s, boost::asio::buffer(&cmd, sizeof(size_t)));
  191. switch (cmd)
  192. {
  193. case ABS :
  194. {
  195. sendAbsAndPrecomputeCaches(s);
  196. break;
  197. }
  198. case SPEED :
  199. {
  200. speedTest(s);
  201. break;
  202. }
  203. case EXIT :
  204. {
  205. cout << "PIROptimizer: EXIT command recieved" << endl;
  206. break;
  207. }
  208. case DATA :
  209. {
  210. sendDatabaseInfos(s);
  211. break;
  212. }
  213. default :
  214. {
  215. cout << "PIROptimizer: Looping 30s as requested by client ..." << endl;
  216. sleep(30);
  217. break;
  218. }
  219. }
  220. }
  221. }catch(std::exception const& ex)
  222. {
  223. cerr << "Client quit without send EXIT code: " << ex.what() << endl;
  224. }
  225. }
  226. void PIROptimizer::optimize()
  227. {
  228. serve();
  229. }