OptimService.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "OptimService.hpp"
  18. const std::string OptimService::folderName = "exp/";
  19. const std::string OptimService::fileName = "preCompute";
  20. const std::string OptimService::absFileExtension = ".abs";
  21. const std::string OptimService::decFileExtension = ".dec";
  22. const std::string OptimService::encFileExtension = ".enc";
  23. const std::string OptimService::getCurrentTime()
  24. {
  25. time_t now = time(0);
  26. struct tm tstruct;
  27. char buf[80];
  28. tstruct = *localtime(&now);
  29. strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
  30. return buf;
  31. }
  32. int OptimService::getNumberOfExperiences(std::string testValuesFileName)
  33. {
  34. std::string line;
  35. std::ifstream f(testValuesFileName);
  36. int i = 0;
  37. if(!f.is_open())
  38. return -1;
  39. while (std::getline(f, line))
  40. {
  41. if(!(line.c_str()[0] == '#')) i++;//jump over commented line
  42. }
  43. f.close();
  44. return --i;
  45. }
  46. int OptimService::readTestValues(unsigned int i, FixedVars& vars, std::string testValuesFileName)
  47. {
  48. std::ifstream f(testValuesFileName, std::ios::in);
  49. std::string line;
  50. std::vector<std::string> fields;
  51. if (f.is_open())
  52. {
  53. for (unsigned k = 0 ; k < i+1 ; k++)
  54. {
  55. std::getline(f, line);
  56. if(line.c_str()[0] == '#') i++;//jump over commented line
  57. }
  58. boost::algorithm::split(fields, line, boost::algorithm::is_any_of(" "));
  59. vars.n = atoi(fields[0].c_str());
  60. vars.l = atoi(fields[1].c_str());
  61. vars.Tupc = vars.Tdos = static_cast<double>(atol(fields[2].c_str()));
  62. vars.Tdoc = vars.Tups = static_cast<double>(atol(fields[3].c_str()));
  63. vars.k = atoi(fields[4].c_str());
  64. vars.alphaMax = atoi(fields[5].c_str());
  65. vars.dMax = atoi(fields[6].c_str());
  66. }
  67. else
  68. {
  69. return 1;
  70. }
  71. f.close();
  72. return 0;
  73. }
  74. void OptimService::writeHeadFile(unsigned int i, FixedVars& fixedVars)
  75. {
  76. std::ofstream file(std::string("exp/exp"+ std::to_string(i)).c_str(), std::ios::out);
  77. file << "# " << std::string("exp" + std::to_string(i)) << " " << getCurrentTime() << std::endl;
  78. file << "#Fixed Param : n " << fixedVars.n << ", l " << fixedVars.l << ", Tupc " << fixedVars.Tupc << ", Tdoc " << fixedVars.Tdoc <<", k " << fixedVars.k << std::endl;
  79. file << "#Bound Param : " << ", alphaMax " << fixedVars.alphaMax << ", dMax " << fixedVars.dMax << std::endl;
  80. file << "#1:d 2a:alpha_min\t 2b:alpha_max\t 2c:current_best_alpha\t2d:alpha_lowbound\t2e:alpha_upbound\t3:GenQ \t 4:SendQ \t 5:GenR \t 6:SendR \t 7:DecR \t 8:Total Time (pot. pipelined)" << std::endl;
  81. file.close();
  82. }
  83. int OptimService::readEntireFile(std::string& file_content, const std::string& file_path)
  84. {
  85. std::ifstream f(file_path);
  86. if (!f.is_open())
  87. {
  88. f.close();
  89. return 1;
  90. }
  91. getline(f, file_content, (char)EOF);
  92. f.close();
  93. return 0;
  94. }
  95. int OptimService::readOptimData(map<std::string,double>& values, const std::string& file_path)
  96. {
  97. std::ifstream f(file_path);
  98. if(!f.good())
  99. {
  100. f.close();
  101. return 1;
  102. }
  103. std::string line;
  104. std::vector<std::string> fields;
  105. while (std::getline(f, line))
  106. {
  107. boost::algorithm::split(fields, line, boost::algorithm::is_any_of(" "));
  108. values[fields.at(0)] = atof(fields.at(1).c_str());
  109. }
  110. f.close();
  111. return 0;
  112. }
  113. /**
  114. * Write test result into file exp/exp{$exp_nbr}
  115. * Params are self explanatory
  116. **/
  117. void OptimService::writeTestCurrentResult(unsigned int alpha_min, unsigned int alpha_max, unsigned int alpha_curr, unsigned int a_inf_bound, unsigned int a_sup_bound, unsigned int d, unsigned int exp_nbr, OptimVars& vars)
  118. {
  119. // Open output file exp/exp{$exp_nbr}
  120. std::ofstream file(std::string("exp/exp"+ std::to_string(exp_nbr)).c_str(), std::ios::out | std::ios::app );
  121. // Try to output double values always with the same amount of decimals
  122. file.setf( std::ios::fixed, std:: ios::floatfield );
  123. // Output test result in a line
  124. file << d << "\t" << alpha_min << "\t" << alpha_max << "\t" << alpha_curr << "\t" << a_inf_bound << " \t" << a_sup_bound << "\t" << vars.getGenQ() << " \t " << vars.getSendQ() << " \t " << vars.getGenR() << "\t" << vars.getSendR() << " \t " << vars.getDecR() << "\t" << vars.getValue() << std::endl;
  125. file.close();
  126. }
  127. int OptimService::writeOptimDataBuffer(const std::string& buffer, const std::string& file_path)
  128. {
  129. std::ofstream f(file_path);
  130. if (!f.good())
  131. {
  132. f.close();
  133. return 1;
  134. }
  135. f << buffer;
  136. f.close();
  137. return 0;
  138. }
  139. void OptimService::writeFootFile(unsigned int i)
  140. {
  141. std::ofstream file(std::string("exp/exp"+ std::to_string(i)).c_str(), std::ios::out | std::ios::app );
  142. file << std::endl <<"#End " << getCurrentTime() << std::endl;
  143. file.close();
  144. }
  145. void OptimService::writeTestCurrentResult(unsigned int alpha, unsigned int alphaMul, unsigned int d, unsigned int i, OptimVars& vars)
  146. {
  147. std::ofstream file(std::string("exp/exp"+ std::to_string(i)).c_str(), std::ios::out | std::ios::app );
  148. file.setf( std::ios::fixed, std:: ios::floatfield );
  149. file << d << "\t" << alpha << " \t" << alphaMul << "\t" << vars.getGenQ() << " \t " << vars.getSendQ() << " \t " << vars.getGenR() << "\t" << vars.getSendR() << " \t " << vars.getDecR() << "\t" << vars.getValue() << std::endl;
  150. file.close();
  151. }
  152. void OptimService::writeMessage(unsigned int i, std::string const& message)
  153. {
  154. std::ofstream file(std::string("exp/exp"+ std::to_string(i)).c_str(), std::ios::out | std::ios::app );
  155. file << message << std::endl;
  156. file.close();
  157. }
  158. void OptimService::writeConfigFile(unsigned int alpha, unsigned int alphaMul, unsigned int d, unsigned int exp_nbr)
  159. {
  160. std::ofstream file(std::string("configFile" + std::to_string(exp_nbr)).c_str(), std::ios::out);
  161. file << "alpha\t" << alpha << endl;
  162. file << "alphaM\t" << alphaMul << endl;
  163. file.close();
  164. }
  165. unsigned int OptimService::getNumberOfLines(std::ifstream& f)
  166. {
  167. unsigned int i = 0;
  168. string line;
  169. while (std::getline(f, line))
  170. {
  171. if(!(line.c_str()[0] == '#')) i++;//jump over commented line
  172. }
  173. return i;
  174. }
  175. // Returns true if optimization file does not exist or is outdated
  176. bool OptimService::fileOutdated(std::string crypto_name, std::string extension)
  177. {
  178. map<string, double> cache;
  179. std::string file_path(OptimService::folderName + OptimService::fileName + crypto_name
  180. + extension);
  181. // Try to open and read the file
  182. // If it fails suppose that it is because the file does not exist
  183. if(readOptimData(cache, file_path))
  184. {
  185. std::cout << "OptimService: Could not access cache file" << std::endl;
  186. return true;
  187. }
  188. // Get a set with all the crypto parameters of the requested cryptosystem
  189. CryptographicSystem* crypto_ptr = HomomorphicCryptoFactory_internal::getCrypto(crypto_name);
  190. std::set<std::string> crypto_params_set;
  191. crypto_ptr->getAllCryptoParams(crypto_params_set);
  192. // Try to find each crypto_param in the cache and remove it
  193. for (auto crypto_param : crypto_params_set)
  194. {
  195. // If there is an element missing in the cache file is outdated
  196. if (cache.erase(crypto_param) == 0)
  197. {
  198. std::cout << "OptimService: "<< crypto_param << " not found in the cache" << std::endl;
  199. delete crypto_ptr;
  200. return true;
  201. }
  202. }
  203. // If some values in the cache do not correspond to a crypto_param file is outdated
  204. if(!cache.empty())
  205. {
  206. std::cout << "OptimService: " << extension << " cache has too many entries" << std::endl;
  207. delete crypto_ptr;
  208. return true;
  209. }
  210. delete crypto_ptr;
  211. return false;
  212. }