PIRReplyGeneratorTrivial.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "PIRReplyGeneratorTrivial.hpp"
  18. #include "sys/time.h"
  19. PIRReplyGeneratorTrivial::PIRReplyGeneratorTrivial():
  20. firstTimeImport(true)
  21. {
  22. }
  23. PIRReplyGeneratorTrivial::PIRReplyGeneratorTrivial(PIRParameters& param, DBHandler *db):
  24. GenericPIRReplyGenerator(param, db),
  25. firstTimeImport(true)
  26. {
  27. }
  28. //void PIRReplyGeneratorTrivial::importFakeData(uint64_t file_kb_size)
  29. //{
  30. // uint64_t files_nbr = 1;
  31. //
  32. // for (unsigned int i = 0 ; i < pirParam.d ; i++) files_nbr *= pirParam.n[i];
  33. //
  34. // input_data = new lwe_in_data[files_nbr];
  35. // currentMaxNbPolys = ceil(double(file_kb_size * 1024) / double(cryptoMethod->getPublicParameters().getAbsorptionBitsize()));
  36. //
  37. // for (unsigned int i = 0 ; i < files_nbr ; i++)
  38. // {
  39. // input_data[i].p = new poly64[currentMaxNbPolys];
  40. // for (unsigned int j = 0 ; j < currentMaxNbPolys ; j++)
  41. // {
  42. // input_data[i].p[j] = cryptoMethod->getnflInstance().allocBoundedRandomPoly(
  43. // cryptoMethod->getpolyDegree(), cryptoMethod->getmoduli()[0]);
  44. // input_data[i].nbPolys = currentMaxNbPolys;
  45. // }
  46. // }
  47. //}
  48. //
  49. //
  50. void PIRReplyGeneratorTrivial::importData()
  51. {
  52. uint64_t maxFileBytesize = dbhandler->getmaxFileBytesize();
  53. unsigned int size = static_cast<double>(cryptoMethod->getPublicParameters().getAbsorptionBitsize()/GlobalConstant::kBitsPerByte);
  54. char* dataptr;
  55. totalNbChunks = ceil((double)maxFileBytesize*dbhandler->getNbStream()/double(size));
  56. input_data = (char *)calloc(size*totalNbChunks,1);
  57. dataptr = input_data;
  58. //pour tous les fichiers.
  59. for (unsigned int i = 0 ; i < dbhandler->getNbStream() ; i++)
  60. {
  61. ifstream* stream = dbhandler->openStream(i, 0);
  62. dbhandler->readStream(stream, dataptr, maxFileBytesize);
  63. dbhandler->closeStream(stream);
  64. dataptr += maxFileBytesize;
  65. }
  66. }
  67. void PIRReplyGeneratorTrivial::generateReply()
  68. {
  69. unsigned int size = static_cast<double>(cryptoMethod->getPublicParameters().getAbsorptionBitsize()/GlobalConstant::kBitsPerByte);
  70. char* buffer;
  71. #ifdef PERF_TIMERS
  72. double vtstart = omp_get_wtime();
  73. #endif
  74. // Init the reply array
  75. repliesArray = (char**) calloc(totalNbChunks, sizeof(char*));
  76. repliesAmount = totalNbChunks;
  77. for (unsigned int j = 0 ; j < totalNbChunks ; j++)
  78. {
  79. buffer = (char*) malloc(size*sizeof(char));
  80. memcpy(buffer, input_data + j*size, size);
  81. repliesArray[j] = buffer;
  82. #ifdef PERF_TIMERS
  83. // Give some feedback if it takes too long
  84. double vtstop = omp_get_wtime();
  85. if (vtstop - vtstart > 1)
  86. {
  87. vtstart = vtstop;
  88. std::cout <<"PIRReplyGeneratorTrivial: Reply chunk " << j+1 << "/" << totalNbChunks << " generated\r" << std::flush;
  89. }
  90. #endif
  91. }
  92. // Always print feedback for last chunk
  93. std::cout <<"PIRReplyGenerator: Reply chunk " << totalNbChunks << "/" << totalNbChunks << " generated" << std::endl;
  94. }
  95. imported_database_t PIRReplyGeneratorTrivial::generateReplyGeneric(bool keep_imported_data){
  96. imported_database_t database_wrapper;
  97. if(firstTimeImport)
  98. {
  99. importData();
  100. database_wrapper.imported_database_ptr = (void*) input_data;
  101. database_wrapper.polysPerElement = totalNbChunks;
  102. firstTimeImport = false;
  103. }
  104. boost::mutex::scoped_lock l(mutex);
  105. generateReply();
  106. if(keep_imported_data==false)
  107. {
  108. free(input_data);
  109. }
  110. return database_wrapper;
  111. }
  112. void PIRReplyGeneratorTrivial::generateReplyGenericFromData(const imported_database_t database)
  113. {
  114. input_data = (char*) database.imported_database_ptr;
  115. totalNbChunks = database.polysPerElement;
  116. boost::mutex::scoped_lock l(mutex);
  117. generateReply();
  118. }
  119. double PIRReplyGeneratorTrivial::generateReplySimulation(const PIRParameters& pir_params, uint64_t plaintext_nbr)
  120. {
  121. return (double) 0;
  122. }
  123. ///**
  124. // * Compute Reply Size une chunks.
  125. // * WARNING blocking function.
  126. // **/
  127. unsigned long PIRReplyGeneratorTrivial::computeReplySizeInChunks(unsigned long int maxFileBytesize)
  128. {
  129. double ciphBytesize =
  130. (double)cryptoMethod->getPublicParameters().getAbsorptionBitsize()
  131. /(double)GlobalConstant::kBitsPerByte;
  132. return ceil(double(maxFileBytesize) * dbhandler->getNbStream()
  133. / double(ciphBytesize));
  134. }
  135. ///**
  136. // * Overloaded fonction from GenericPIRReplyGenerator.
  137. // * Initalise queriesBuf.
  138. // **/
  139. //
  140. void PIRReplyGeneratorTrivial::initQueriesBuffer() {}
  141. void PIRReplyGeneratorTrivial::pushQuery(char* rawQuery, unsigned int size, int dim, int nbr)
  142. {
  143. std::cout << "PIRReplyGeneratorTrivial: Ignoring query element" << std::endl;
  144. }
  145. PIRReplyGeneratorTrivial::~PIRReplyGeneratorTrivial()
  146. {
  147. // Ensure that mutex is always unlocked upon destruction
  148. mutex.try_lock();
  149. mutex.unlock();
  150. freeResult();
  151. }
  152. void PIRReplyGeneratorTrivial::setCryptoMethod(CryptographicSystem* cm) {
  153. cryptoMethod = (NoCryptography *)cm;
  154. }
  155. void PIRReplyGeneratorTrivial::freeResult()
  156. {
  157. if(repliesArray!=NULL)
  158. {
  159. for(unsigned i=0 ; i < repliesAmount; i++)
  160. {
  161. if(repliesArray[i]!=NULL) free(repliesArray[i]);
  162. repliesArray[i] = NULL;
  163. }
  164. free(repliesArray);
  165. repliesArray=NULL;
  166. }
  167. }