PIROptimizer.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef DEF_PIROPTIMIZER
  18. #define DEF_PIROPTIMIZER
  19. #include <math.h>
  20. #include <map>
  21. #include <limits>
  22. #include <iomanip>
  23. #include <string>
  24. #include <iostream>
  25. #include <algorithm>
  26. #include <omp.h>
  27. #include <regex>
  28. #include <boost/asio.hpp>
  29. #include <boost/shared_ptr.hpp>
  30. #include <boost/algorithm/string.hpp>
  31. #include "pir/optim/OptimVars.hpp"
  32. #include "pir/optim/OptimService.hpp"
  33. #include "pir/PIRParameters.hpp"
  34. #include "pir/queryGen/PIRQueryGenerator_internal.hpp"
  35. #include "pir/replyExtraction/PIRReplyExtraction_internal.hpp"
  36. #include "crypto/HomomorphicCryptoFactory_internal.hpp"
  37. #include "crypto/NFLLWEPublicParameters.hpp"
  38. #include "pir/ClientDefines.hpp"
  39. #include "pir/PIROptimizerCommand.hpp"
  40. #ifndef MODULBITS
  41. #define MODULBITS 1024
  42. #endif
  43. #define MAX_D 10
  44. #define MEGA_BYTE 1000000
  45. using namespace std;
  46. using boost::asio::ip::tcp;
  47. typedef HomomorphicCrypto* crypto_ptr;
  48. class PIROptimizer {
  49. protected:
  50. FitnessType fitnessMethod;
  51. PIRParameters pirParameters;
  52. HomomorphicCrypto* crypto;
  53. AbstractPublicParameters* currentPublicParams;
  54. string serv_ip;
  55. int server_optimport;
  56. boost::asio::io_service ios;
  57. boost::asio::ip::tcp::socket s;
  58. ofstream file;
  59. map<string, double> encrypt_cache;
  60. map<string, double> decrypt_cache;
  61. map<string, double> abs_cache;
  62. map<string, double> precompute_cache;
  63. /** Fixed Parameters **/
  64. FixedVars fixedVars;
  65. unsigned int nbc; /** number of client **/
  66. unsigned int dn[MAX_D];
  67. unsigned int security_bits;
  68. double ax[MAX_D], ayDec[MAX_D], ayEnc[MAX_D], axAbs[MAX_D], ayAbs[MAX_D];
  69. /** Free PIR Parameters **/
  70. string pir;
  71. /**Outputs**/
  72. OptimVars optimum, global_optimum;
  73. double Tcr(unsigned i);
  74. double Tcq(unsigned i);
  75. double Tp(unsigned i);
  76. void getAbsAndPreCachesFromServer(boost::asio::ip::tcp::socket& s, crypto_ptr crypto_system);
  77. void makeAbsAndPrecomputeCaches(char* serialized_cache);
  78. double getAbs1PlaintextTime(HomomorphicCrypto* crypto_system);
  79. double getPrecompute1PlaintextTime(HomomorphicCrypto* crypto_system);
  80. double getQueryElemGenCost(unsigned int d, crypto_ptr crypto);
  81. double eltSize(unsigned int alpha, unsigned int s);
  82. double getDecCost(unsigned int i, crypto_ptr);
  83. double getDecCost();
  84. void analyseResult(unsigned int alpha, unsigned int d, OptimVars& vars);
  85. void showBestResults(unsigned int i);
  86. void writeTestBestResult(unsigned int i);
  87. void getPreComputedData(HomomorphicCrypto* crypto);
  88. void computeOptimData(set<string>& crypto_params_set);
  89. void sendExitCommand(boost::asio::ip::tcp::socket& s);
  90. void disconnect();
  91. void connect(); //const throw(std::exception);
  92. public:
  93. bool silent;
  94. PIROptimizer(string server_ip, int port, FitnessType fitnessMethod);
  95. PIROptimizer();
  96. virtual ~PIROptimizer();
  97. void optimize(boost::asio::ip::tcp::socket& s, FixedVars& fixed_vars, unsigned int exp_nbr);
  98. static const unsigned int kalphaBound;
  99. void optimize(FixedVars& fixed_vars, unsigned int exp_nbr);
  100. OptimVars optimizeFromServer(FixedVars& partial_fixed_vars,boost::asio::ip::tcp::socket& socket);
  101. void optimizeFromConfiguredCryptosystem(FixedVars& fixed_vars, HomomorphicCrypto* crypto_system);
  102. void noCryptoSystemOptim(unsigned int exp_nbr);
  103. void findAlpha(unsigned int d, OptimVars& vars, unsigned int exp_nbr);
  104. void findAlpha(unsigned int d, unsigned int alpha_min, unsigned int alpha_max, OptimVars& vars, unsigned int exp_nbr);
  105. void findAlphaDicho(unsigned int inf, unsigned int sup, unsigned int d, unsigned int exp_nbr, OptimVars& vars);
  106. int slop(unsigned int alphaMul, unsigned int inf, unsigned int sup, unsigned int d, OptimVars& vars, unsigned int exp_nbr);
  107. const PIRParameters& getParameters();
  108. static void getDimSize(unsigned int n, unsigned int alpha, unsigned int d, unsigned int *dn);
  109. unsigned int getMaxAlpha();
  110. unsigned int getMinAlpha();
  111. double getGenQueryCost(unsigned int d, unsigned int *dn);
  112. double getSendCost(double Tupc, double Tdoc, unsigned int nbc, unsigned int d, unsigned int *dn);
  113. double getReplyGenCost(unsigned int alpha, unsigned int d, unsigned int *dn);
  114. double getReceiveCost(unsigned int alpha, double Tups, double Tdoc, unsigned int nbc, unsigned int d);
  115. double getDecryptCost(unsigned int alpha, unsigned int d);
  116. void getFixedVarsFromServer(FixedVars& fixed_vars0,boost::asio::ip::tcp::socket& socket);
  117. void getNetworkInfos(FixedVars& fixed_vars, boost::asio::ip::tcp::socket& s);
  118. static void writeBigMessage(string msg);
  119. void processResults(unsigned int i);
  120. double computeOptimVars(unsigned int alphalt, unsigned int d, OptimVars& vars);
  121. };
  122. #endif