PIRReplyGeneratorNFL_internal.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 "PIRReplyGeneratorNFL_internal.hpp"
  18. #include "sys/time.h"
  19. //#define SIMULATE_PRE_NFL_DATA //Use this to simulate imported data is in NFL form
  20. //#define TEST_NFL_PERF_ITERATIONS //Loop to simulate very large databases
  21. //#define SNIFFER_CHUNK_BYTESIZE 12800
  22. //#define SNIFFER_CHUNK_BYTESIZE 2560
  23. //#define SNIFFER_CHUNK_BYTESIZE 1500
  24. //#define SNIFFER_CHUNK_BYTESIZE 600
  25. //#define SNIFFER //Use this to activate a sniffer like behavior
  26. PIRReplyGeneratorNFL_internal::PIRReplyGeneratorNFL_internal():
  27. lwe(false)
  28. {
  29. }
  30. /**
  31. * Constructor of the class.
  32. * Params :
  33. * - vector <File*>& database : reference of a File pointer vector.
  34. * - PIRParameters& param : reference to a PIRParameters object.
  35. **/
  36. PIRReplyGeneratorNFL_internal::PIRReplyGeneratorNFL_internal( PIRParameters& param, DBHandler* db):
  37. lwe(false),
  38. currentMaxNbPolys(0),
  39. GenericPIRReplyGenerator(param,db),
  40. current_query_index(0),
  41. current_dim_index(0)
  42. {
  43. // cryptoMethod will be set later by setCryptoMethod
  44. }
  45. void PIRReplyGeneratorNFL_internal::importFakeData(uint64_t plaintext_nbr)
  46. {
  47. uint64_t files_nbr = 1;
  48. for (unsigned int i = 0 ; i < pirParam.d ; i++) files_nbr *= pirParam.n[i];
  49. uint64_t plain_bytesize = cryptoMethod->getnflInstance().getpolyDegree()*cryptoMethod->getnflInstance().getnbModuli()*8;
  50. dbhandler = new DBGenerator(files_nbr, plaintext_nbr*plain_bytesize, true);
  51. currentMaxNbPolys = plaintext_nbr;
  52. importDataNFL(0, plaintext_nbr*plain_bytesize);
  53. }
  54. /**
  55. * Convert raw data from file in usable NFL data.
  56. **/
  57. void PIRReplyGeneratorNFL_internal::importDataNFL(uint64_t offset, uint64_t bytes_per_file)
  58. {
  59. uint64_t fileByteSize = min(bytes_per_file, dbhandler->getmaxFileBytesize()-offset);
  60. uint64_t theoretical_files_nbr = 1;
  61. uint64_t nbFiles = dbhandler->getNbStream();
  62. for (unsigned int i = 0 ; i < pirParam.d ; i++) theoretical_files_nbr *= pirParam.n[i];
  63. input_data = (lwe_in_data *) malloc(sizeof(lwe_in_data)*theoretical_files_nbr);
  64. char *rawBits = (char*)calloc(fileByteSize*pirParam.alpha, sizeof(char));
  65. currentMaxNbPolys=0;
  66. #ifdef PERF_TIMERS
  67. double vtstart = omp_get_wtime();
  68. bool wasVerbose = false;
  69. uint64_t lastindex = 0;
  70. #endif
  71. // For global time measurement
  72. double start = omp_get_wtime();double now,delta;
  73. int nbruns=ceil((double)nbFiles/pirParam.alpha);
  74. // WARNING this section should not be multithreade as rawbits is shared and readAggregatedStream
  75. // is not threadsafe
  76. for (int i=0; i < nbruns; i++)
  77. {
  78. dbhandler->readAggregatedStream(i, pirParam.alpha, offset, bytes_per_file, rawBits);
  79. #ifdef SIMULATE_PRE_NFL_DATA
  80. uint64_t abssize = cryptoMethod->getPublicParameters().getAbsorptionBitsize();
  81. uint64_t polysize = cryptoMethod->getpolyDegree() * cryptoMethod->getnbModuli()*sizeof(uint64_t);
  82. uint64_t nbpolys = ceil((double)fileByteSize * pirParam.alpha * 8 / abssize);
  83. input_data[i].p = (poly64*) malloc(nbpolys*sizeof(poly64*));
  84. input_data[i].p[0] = (poly64) malloc(nbpolys*polysize);
  85. for (unsigned j = 0; j < nbpolys ; j++)
  86. {
  87. input_data[i].p[j] = input_data[i].p[0]+j*polysize/8;
  88. memcpy(input_data[i].p[j], rawBits, min(fileByteSize, polysize));
  89. }
  90. input_data[i].nbPolys = nbpolys;
  91. #else
  92. input_data[i].p = cryptoMethod->deserializeDataNFL((unsigned char**)&rawBits, (uint64_t) 1, fileByteSize*pirParam.alpha*GlobalConstant::kBitsPerByte, input_data[i].nbPolys);
  93. #endif
  94. #ifdef PERF_TIMERS
  95. // Give some feedback if it takes too long
  96. double vtstop = omp_get_wtime();
  97. if (vtstop - vtstart > 1)
  98. {
  99. vtstart = vtstop;
  100. std::cout <<"PIRReplyGeneratorNFL_internal: Element " << i+1 << "/" << nbruns << " imported\r" << std::flush;
  101. wasVerbose = true;
  102. lastindex = i+1;
  103. }
  104. #endif
  105. }
  106. for (int i=0; i < nbruns; i++)
  107. if (input_data[i].nbPolys>currentMaxNbPolys) currentMaxNbPolys=input_data[i].nbPolys;
  108. #ifdef PERF_TIMERS
  109. // If feedback was given say we finished
  110. if (wasVerbose && lastindex != nbFiles) std::cout <<"PIRReplyGeneratorNFL_internal: Element " << nbruns << "/" << nbFiles/pirParam.alpha << " imported" << std::endl;
  111. #endif
  112. /** FILE PADDING **/
  113. for (uint64_t i = ceil((double)nbFiles/pirParam.alpha) ; i < theoretical_files_nbr ; i++)
  114. {
  115. input_data[i].p = (poly64 *) malloc(currentMaxNbPolys*sizeof(poly64));
  116. input_data[i].p[0] = (poly64) calloc(cryptoMethod->getpolyDegree()*cryptoMethod->getnbModuli()*currentMaxNbPolys,sizeof(uint64_t));
  117. for (uint64_t j = 1 ; j < currentMaxNbPolys ; j++) input_data[i].p[j] = input_data[i].p[0]+cryptoMethod->getpolyDegree()*cryptoMethod->getnbModuli()*j;
  118. input_data[i].nbPolys = currentMaxNbPolys;
  119. }
  120. free(rawBits);
  121. std::cout<<"PIRReplyGeneratorNFL_internal: Finished importing the database in " << omp_get_wtime() - start << " seconds" << std::endl;
  122. }
  123. #ifdef SNIFFER
  124. imported_database_t PIRReplyGeneratorNFL_internal::generateReplyGeneric(bool keep_imported_data)
  125. {
  126. imported_database_t database_wrapper;
  127. boost::mutex::scoped_lock l(mutex);
  128. const uint64_t chunkBytesize = SNIFFER_CHUNK_BYTESIZE;
  129. const uint64_t iterations = dbhandler->getmaxFileBytesize()/(chunkBytesize+1);
  130. // std::ifstream *is = dbhandler->openStream(0,0);
  131. const uint64_t nbFiles = dbhandler->getNbStream();
  132. const unsigned int polysize = cryptoMethod->getpolyDegree()*cryptoMethod->getnbModuli();
  133. const unsigned int jumpcipher = 2*polysize / sizeof(uint64_t);
  134. currentMaxNbPolys=0;
  135. lwe_in_data *input = new lwe_in_data[iterations];
  136. lwe_cipher *resul = new lwe_cipher[iterations];
  137. uint64_t index;
  138. lwe_query **queries;
  139. char **rawBits = (char**) malloc(iterations*sizeof(char*));
  140. cryptoMethod->setandgetAbsBitPerCiphertext(1);
  141. queries = queriesBuf[0];
  142. const uint64_t mask = (1<<((int)log2(nbFiles)))-1;
  143. for (uint64_t it = 0 ; it < iterations ; it++)
  144. {
  145. resul[it].a = (uint64_t *) malloc(2*polysize*sizeof(uint64_t));
  146. resul[it].b = (uint64_t *) resul[it].a + polysize;
  147. rawBits[it] = (char*)malloc(((chunkBytesize)*sizeof(char)+sizeof(int)));
  148. }
  149. double start = omp_get_wtime();
  150. #pragma omp parallel for firstprivate(input,queries,resul)
  151. for (uint64_t it = 0 ; it < iterations ; it++)
  152. {
  153. dbhandler->readStream(0, rawBits[it], chunkBytesize+sizeof(int));
  154. input[it].p = cryptoMethod->deserializeDataNFL((unsigned char**)&(rawBits[it]), (uint64_t) 1, chunkBytesize*GlobalConstant::kBitsPerByte, input[it].nbPolys);
  155. index = *(int *)(rawBits[it]+chunkBytesize) & mask;
  156. cryptoMethod->mul(resul[it], input[it], queries[0][index],queries[1][index], 0, 0);
  157. }
  158. double end = omp_get_wtime();
  159. std::cout<<"PIRReplyGeneratorNFL_internal: Finished processing the sniffed data in " << end - start << " seconds" << std::endl;
  160. std::cout<<"PIRReplyGeneratorNFL_internal: Processing throughput " << (double)chunkBytesize*8*iterations / ((end - start)*1000000000ULL) << " Gbps" << std::endl;
  161. return database_wrapper;
  162. }
  163. #else
  164. imported_database_t PIRReplyGeneratorNFL_internal::generateReplyGeneric(bool keep_imported_data)
  165. {
  166. imported_database_t database_wrapper;
  167. uint64_t usable_memory, database_size, max_memory_per_file, max_readable_size, nbr_of_iterations;
  168. double start, end;
  169. // Init database_wrapper to NULL values so that we are able to know if it has been initialized
  170. database_wrapper.imported_database_ptr = NULL;
  171. database_wrapper.nbElements = 0;
  172. database_wrapper.polysPerElement = 0;
  173. database_wrapper.beforeImportElementBytesize = 0;
  174. // Don't use more than half of the computer's memory
  175. usable_memory = getTotalSystemMemory()/2;
  176. database_size = dbhandler->getmaxFileBytesize() * dbhandler->getNbStream();
  177. #ifndef TEST_NFL_PERF_ITERATIONS
  178. // This is the maximum amount of data per file we can get in memory
  179. max_memory_per_file = usable_memory / dbhandler->getNbStream();
  180. // Given the expansion factor of importation we get the max we can read per file
  181. max_readable_size = max_memory_per_file / 4 ;
  182. // Reduce it so that we have full absorption in all the ciphertexts sent
  183. max_readable_size = (max_readable_size * GlobalConstant::kBitsPerByte / cryptoMethod->getPublicParameters().getAbsorptionBitsize(0)) * cryptoMethod->getPublicParameters().getAbsorptionBitsize(0)/GlobalConstant::kBitsPerByte;
  184. #else
  185. // For our tests we will need to have databases of an integer amount of gigabits
  186. max_readable_size = 1280000000UL/dbhandler->getNbStream();
  187. #endif
  188. // If we reduced it too much set it at least to a ciphertext
  189. if (max_readable_size == 0) max_readable_size = cryptoMethod->getPublicParameters().getAbsorptionBitsize(0);
  190. // Ensure it is not larger than maxfilebytesize
  191. max_readable_size = min(max_readable_size, dbhandler->getmaxFileBytesize());
  192. // Given readable size we get how many iterations we need
  193. nbr_of_iterations = ceil((double)dbhandler->getmaxFileBytesize()/max_readable_size);
  194. #ifndef TEST_NFL_PERF_ITERATIONS
  195. // If aggregation is used we cannot iterate
  196. if ((pirParam.alpha != 1 || pirParam.d > 1) && nbr_of_iterations > 1)
  197. {
  198. std::cout << "PIRReplyGeneratorNFL_internal: Cannot handle aggregation or dimensions on databases requiring multiple iterations" << std::endl;
  199. std::cout << "PIRReplyGeneratorNFL_internal: Handling the database on a single iteration, this can cause memory issues ..." << std::endl;
  200. nbr_of_iterations = 1;
  201. max_readable_size = dbhandler->getmaxFileBytesize();
  202. }
  203. // If we cannot read the whole database we cannot store it precomputed
  204. if (nbr_of_iterations > 1) keep_imported_data = false;
  205. #endif
  206. // If we need to do more than an iteration say it
  207. if (nbr_of_iterations > 1)
  208. {
  209. std::cout << "PIRReplyGeneratorNFL_internal: Database is considered too large, processing it in "
  210. << nbr_of_iterations << " iterations" << std::endl;
  211. }
  212. start = omp_get_wtime();
  213. // #pragma omp parallel for
  214. for (unsigned iteration = 0; iteration < nbr_of_iterations; iteration++)
  215. {
  216. if (nbr_of_iterations > 1) cout << "PIRReplyGeneratorNFL_internal: Iteration " << iteration << endl;
  217. repliesIndex = computeReplySizeInChunks(iteration*max_readable_size);
  218. // Import a chunk of max_readable_size bytes per file with an adapted offset
  219. importDataNFL(iteration*max_readable_size, max_readable_size);
  220. if(keep_imported_data && iteration == nbr_of_iterations - 1) // && added for Perf test but is no harmful
  221. {
  222. database_wrapper.polysPerElement = currentMaxNbPolys;
  223. }
  224. boost::mutex::scoped_lock l(mutex);
  225. repliesAmount = computeReplySizeInChunks(dbhandler->getmaxFileBytesize());
  226. generateReply();
  227. end = omp_get_wtime();
  228. if(keep_imported_data && iteration == nbr_of_iterations - 1) // && added for Perf test but is no harmful
  229. {
  230. database_wrapper.imported_database_ptr = (void*)input_data;
  231. database_wrapper.beforeImportElementBytesize = dbhandler->getmaxFileBytesize();
  232. database_wrapper.nbElements = dbhandler->getNbStream();
  233. }
  234. else
  235. {
  236. freeInputData();
  237. }
  238. }
  239. std::cout<<"PIRReplyGeneratorNFL_internal: Total process time " << end - start << " seconds" << std::endl;
  240. std::cout<<"PIRReplyGeneratorNFL_internal: DB processing throughput " << 8*database_size/(end - start) << "bps" << std::endl;
  241. std::cout<<"PIRReplyGeneratorNFL_internal: Client cleartext reception throughput " << 8*dbhandler->getmaxFileBytesize()/(end - start) << "bps" << std::endl;
  242. freeQueries();
  243. return database_wrapper;
  244. }
  245. #endif
  246. // Function used to generate a PIR reply if:
  247. // - database is small enough to be kept in memory
  248. // - it has already been imported to it
  249. void PIRReplyGeneratorNFL_internal::generateReplyGenericFromData(const imported_database_t database)
  250. {
  251. #ifndef TEST_NFL_PERF_ITERATIONS
  252. input_data = (lwe_in_data*) database.imported_database_ptr;
  253. currentMaxNbPolys = database.polysPerElement;
  254. boost::mutex::scoped_lock l(mutex);
  255. double start = omp_get_wtime();
  256. repliesAmount = computeReplySizeInChunks(database.beforeImportElementBytesize);
  257. generateReply();
  258. #else
  259. uint64_t max_readable_size, database_size, nbr_of_iterations;
  260. database_size = database.beforeImportElementBytesize * database.nbElements;
  261. max_readable_size = 1280000000UL/database.nbElements;
  262. // Ensure it is not larger than maxfilebytesize
  263. max_readable_size = min(max_readable_size, database.beforeImportElementBytesize);
  264. // Given readable size we get how many iterations we need
  265. nbr_of_iterations = ceil((double)database.beforeImportElementBytesize/max_readable_size);
  266. boost::mutex::scoped_lock l(mutex);
  267. double start = omp_get_wtime();
  268. for (unsigned iteration = 0; iteration < nbr_of_iterations; iteration++)
  269. {
  270. input_data = (lwe_in_data*) database.imported_database_ptr;
  271. currentMaxNbPolys = database.polysPerElement;
  272. repliesAmount = computeReplySizeInChunks(database.beforeImportElementBytesize);
  273. generateReply();
  274. }
  275. freeInputData();
  276. #endif
  277. double end = omp_get_wtime();
  278. std::cout<<"PIRReplyGeneratorNFL_internal: Total process time " << end - start << " seconds" << std::endl;
  279. std::cout<<"PIRReplyGeneratorNFL_internal: DB processing throughput " << 8*dbhandler->getmaxFileBytesize()*dbhandler->getNbStream()/(end - start) << "bps" << std::endl;
  280. std::cout<<"PIRReplyGeneratorNFL_internal: Client cleartext reception throughput " << 8*dbhandler->getmaxFileBytesize()/(end - start) << "bps" << std::endl;
  281. freeQueries();
  282. }
  283. // Function used to generate a PIR reply if:
  284. // - database is small enough to be kept in memory
  285. // - it has already been imported to it
  286. void PIRReplyGeneratorNFL_internal::generateReplyExternal(imported_database_t* database)
  287. {
  288. uint64_t max_readable_size, database_size, nbr_of_iterations;
  289. database_size = database->beforeImportElementBytesize * database->nbElements;
  290. max_readable_size = 1280000000UL/database->nbElements;
  291. // Ensure it is not larger than maxfilebytesize
  292. max_readable_size = min(max_readable_size, database->beforeImportElementBytesize);
  293. // Given readable size we get how many iterations we need
  294. nbr_of_iterations = ceil((double)database->beforeImportElementBytesize/max_readable_size);
  295. boost::mutex::scoped_lock l(mutex);
  296. double start = omp_get_wtime();
  297. for (unsigned iteration = 0; iteration < nbr_of_iterations; iteration++)
  298. {
  299. input_data = (lwe_in_data*) database->imported_database_ptr;
  300. currentMaxNbPolys = database->polysPerElement;
  301. repliesAmount = computeReplySizeInChunks(database->beforeImportElementBytesize);
  302. generateReply();
  303. }
  304. freeInputData();
  305. double end = omp_get_wtime();
  306. std::cout<<"PIRReplyGeneratorNFL_internal: Total process time " << end - start << " seconds" << std::endl;
  307. std::cout<<"PIRReplyGeneratorNFL_internal: DB processing throughput " << 8*dbhandler->getmaxFileBytesize()*dbhandler->getNbStream()/(end - start) << "bps" << std::endl;
  308. std::cout<<"PIRReplyGeneratorNFL_internal: Client cleartext reception throughput " << 8*dbhandler->getmaxFileBytesize()/(end - start) << "bps" << std::endl;
  309. freeQueries();
  310. }
  311. /**
  312. * Prepare reply and start absoptions.
  313. **/
  314. void PIRReplyGeneratorNFL_internal::generateReply()
  315. {
  316. lwe_in_data *in_data = input_data;
  317. lwe_cipher **inter_reply;
  318. #ifdef SHOUP
  319. lwe_query **queries;
  320. #else
  321. lwe_query *queries;
  322. #endif
  323. uint64_t old_reply_elt_nbr = 0;
  324. uint64_t reply_elt_nbr = 1;
  325. uint64_t old_poly_nbr = 1;
  326. // Allocate memory for the reply array
  327. if (repliesArray != NULL) free(repliesArray);
  328. repliesArray = (char**)calloc(repliesAmount,sizeof(char*));
  329. // Start global timers
  330. double start = omp_get_wtime();
  331. #ifdef PERF_TIMERS
  332. double vtstart = start;
  333. bool wasVerbose = false;
  334. #endif
  335. for (unsigned int i = 0 ; i < pirParam.d ; i++) // For each recursion level
  336. {
  337. old_reply_elt_nbr = reply_elt_nbr;
  338. reply_elt_nbr = 1;
  339. for (unsigned int j = i + 1 ; j < pirParam.d ; j++ ) reply_elt_nbr *= pirParam.n[j];
  340. #ifdef DEBUG
  341. cout << "PIRReplyGeneratorNFL_internal: currentMaxNbPolys = " << currentMaxNbPolys << endl;
  342. #endif
  343. inter_reply = new lwe_cipher*[reply_elt_nbr]();
  344. queries = queriesBuf[i];
  345. for (uint64_t j = 0 ; j < reply_elt_nbr ; j++) // Boucle de reply_elt_nbr PIR
  346. {
  347. inter_reply[j] = new lwe_cipher[currentMaxNbPolys];
  348. // Warning of the trick in case SHOUP is defined : we cast quesries to a (lwe_query*) and will have to uncast it
  349. generateReply((lwe_query*)queries , in_data + (pirParam.n[i] * j ), i, inter_reply[j]);
  350. #ifdef DEBUG_WITH_FILE_OUTPUT
  351. if (i ==0 && j==1) {
  352. std::ofstream file(std::string("output_level_"+ std::to_string(i)).c_str(), std::ios::out| std::ios::binary);
  353. for (int k = 0 ; k < currentMaxNbPolys ; k++)
  354. {
  355. file.write((char*)inter_reply[j][k].a,1024*2*8);
  356. }
  357. file.close();
  358. }
  359. #endif
  360. #ifdef PERF_TIMERS
  361. // Give some feedback if it takes too long
  362. double vtstop = omp_get_wtime();
  363. if (vtstop - vtstart > 1)
  364. {
  365. vtstart = vtstop;
  366. std::cout <<"PIRReplyGeneratorNFL_internal: Reply " << j+1 << "/" << reply_elt_nbr << " generated\r" << std::flush;
  367. wasVerbose = true;
  368. }
  369. #endif
  370. }
  371. /*****************/
  372. /*MEMORY CLEANING*/
  373. /*****************/
  374. #ifdef DEBUG
  375. if ( i > 0)
  376. {
  377. cout << "PIRReplyGeneratorNFL_internal: reply_elt_nbr_OLD: " << old_reply_elt_nbr << endl;
  378. }
  379. #endif
  380. // When i=> 2 clean old in_data.
  381. if (i < pirParam.d - 1) {
  382. old_poly_nbr = currentMaxNbPolys;
  383. in_data = fromResulttoInData(inter_reply, reply_elt_nbr, i);
  384. }
  385. for (uint64_t j = 0 ; j < reply_elt_nbr ; j++) {
  386. for (uint64_t k = 0 ; (k < old_poly_nbr) && (i < pirParam.d - 1); k++){
  387. free(inter_reply[j][k].a);
  388. }
  389. delete[] inter_reply[j];
  390. }
  391. delete[] inter_reply; // allocated with a 'new' above.
  392. }
  393. // Compute execution time
  394. printf( "PIRReplyGeneratorNFL_internal: Global reply generation took %f (omp)seconds\n", omp_get_wtime() - start);
  395. }
  396. double PIRReplyGeneratorNFL_internal::generateReplySimulation(const PIRParameters& pir_params, uint64_t plaintext_nbr)
  397. {
  398. setPirParams((PIRParameters&)pir_params);
  399. initQueriesBuffer();
  400. pushFakeQuery();
  401. importFakeData(plaintext_nbr);
  402. repliesAmount = computeReplySizeInChunks(plaintext_nbr*cryptoMethod->getPublicParameters().getCiphertextBitsize() / CHAR_BIT);
  403. repliesIndex = 0;
  404. double start = omp_get_wtime();
  405. generateReply();
  406. double result = omp_get_wtime() - start;
  407. freeQueries();
  408. freeInputData();
  409. freeResult();
  410. delete dbhandler;
  411. return result;
  412. }
  413. double PIRReplyGeneratorNFL_internal::precomputationSimulation(const PIRParameters& pir_params, uint64_t plaintext_nbr)
  414. {
  415. NFLlib *nflptr = &(cryptoMethod->getnflInstance());
  416. setPirParams((PIRParameters&)pir_params);
  417. initQueriesBuffer();
  418. pushFakeQuery();
  419. importFakeData(plaintext_nbr);
  420. uint64_t files_nbr = 1;
  421. for (unsigned int i = 0 ; i < pir_params.d ; i++) files_nbr *= pir_params.n[i];
  422. double start = omp_get_wtime();
  423. for (unsigned int i = 0 ; i < files_nbr ; i++)
  424. {
  425. {
  426. poly64 *tmp;
  427. tmp= cryptoMethod->deserializeDataNFL((unsigned char**)(input_data[i].p), (uint64_t) plaintext_nbr, cryptoMethod->getPublicParameters().getCiphertextBitsize()/2 , input_data[i].nbPolys);
  428. free(tmp[0]);
  429. }
  430. }
  431. double result = omp_get_wtime() - start;
  432. std::cout << "PIRReplyGeneratorNFL_internal: Deserialize took " << result << " (omp)seconds" << std::endl;
  433. freeQueries();
  434. freeInputData();
  435. freeResult();
  436. delete dbhandler;
  437. return result;
  438. }
  439. /**
  440. * Multiply each query parts by each files and sum the result.
  441. * Params :
  442. * - lwe_queries* : the query ;
  443. * - lwe_in_data* : data to be processed.
  444. * - int begin_data : index where begins the data absorption
  445. * - int lvl : recursion level ;
  446. * - lwe_cipher* result : Array to store the result.
  447. **/
  448. void PIRReplyGeneratorNFL_internal::generateReply( lwe_query *queries_,
  449. lwe_in_data* data,
  450. unsigned int lvl,
  451. lwe_cipher* result)
  452. {
  453. #ifdef SHOUP
  454. lwe_query **queries=(lwe_query**)queries_;
  455. #else
  456. lwe_query *queries=queries_;
  457. #endif
  458. unsigned int query_size = pirParam.n[lvl];
  459. #ifdef PERF_TIMERS
  460. bool wasVerbose = false;
  461. double vtstart = omp_get_wtime();
  462. #endif
  463. // In order to parallelize we must ensure replies are somehow ordered
  464. // (see comment at the end of PIRReplyExtraction)
  465. //#pragma omp parallel for firstprivate(result,data, lvl, queries)
  466. #ifdef MULTI_THREAD
  467. # pragma omp parallel for
  468. #endif
  469. for (unsigned int current_poly=0 ; current_poly < currentMaxNbPolys ; current_poly++)
  470. {
  471. posix_memalign((void**) &(result[current_poly].a), 32,
  472. 2*cryptoMethod->getpolyDegree()*cryptoMethod->getnbModuli()*sizeof(uint64_t));
  473. memset(result[current_poly].a,0,
  474. 2*cryptoMethod->getpolyDegree()*cryptoMethod->getnbModuli()*sizeof(uint64_t));
  475. result[current_poly].b = (uint64_t *) result[current_poly].a +
  476. cryptoMethod->getpolyDegree()*cryptoMethod->getnbModuli();
  477. for (unsigned int offset = 0; offset < query_size; offset += 200)
  478. {
  479. for (unsigned int query_index = offset, ggg=0; query_index < query_size && ggg < 200 ;
  480. query_index++, ggg++)
  481. {
  482. #ifdef SHOUP
  483. #ifdef CRYPTO_DEBUG
  484. if(current_poly==0)
  485. {
  486. std::cout<<"Query poped.a ";NFLTools::print_poly64hex(queries[0][query_index].a,4);
  487. if (lwe)
  488. {
  489. std::cout<<"Query poped.b ";NFLTools::print_poly64hex(queries[0][query_index].b,4);
  490. }
  491. std::cout<<"Query poped.a' ";NFLTools::print_poly64hex(queries[1][query_index].a,4);
  492. if (lwe)
  493. {
  494. std::cout<<"Query poped.b' ";NFLTools::print_poly64hex(queries[1][query_index].b,4);
  495. }
  496. }
  497. #endif
  498. cryptoMethod->mulandadd(result[current_poly], data[query_index], queries[0][query_index],
  499. queries[1][query_index], current_poly, lvl);
  500. #else
  501. cryptoMethod->mulandadd(result[current_poly], data[query_index], queries[query_index],
  502. current_poly, lvl);
  503. #endif
  504. }
  505. if ( lvl == pirParam.d-1 && offset + 200 >= query_size)
  506. {
  507. // Watchout lwe_cipher.a and .b need to be allocated contiguously
  508. repliesArray[repliesIndex+current_poly] = (char*)result[current_poly].a;
  509. }
  510. #ifdef PERF_TIMERS
  511. // Give some feedback if it takes too long
  512. double vtstop = omp_get_wtime();
  513. if (vtstop - vtstart > 1)
  514. {
  515. vtstart = vtstop;
  516. if(currentMaxNbPolys != 1) std::cout <<"PIRReplyGeneratorNFL_internal: Dealt with chunk " <<
  517. current_poly+1 << "/" << currentMaxNbPolys << "\r" << std::flush;
  518. wasVerbose = true;
  519. }
  520. #endif
  521. }
  522. }
  523. #ifdef PERF_TIMERS
  524. if (wasVerbose) std::cout <<" \r" << std::flush;
  525. #endif
  526. }
  527. // New version using the multiple buffer serialize function
  528. lwe_in_data* PIRReplyGeneratorNFL_internal::fromResulttoInData(lwe_cipher** inter_reply, uint64_t reply_elt_nbr, unsigned int reply_rec_lvl)
  529. {
  530. uint64_t in_data2b_bytes = cryptoMethod->getPublicParameters().getAbsorptionBitsize()/8;
  531. uint64_t in_data2b_nbr_polys = ceil((double(currentMaxNbPolys * cryptoMethod->getPublicParameters().getCiphertextBitsize())/8.)/double(in_data2b_bytes));
  532. lwe_in_data *in_data2b = new lwe_in_data[reply_elt_nbr]();
  533. uint64_t **bufferOfBuffers = (uint64_t **) calloc(currentMaxNbPolys,sizeof(uint64_t*));
  534. //For each element in the reply
  535. for (uint64_t i = 0 ; i < reply_elt_nbr ; i++)
  536. {
  537. //Build the buffer of buffers
  538. for (uint64_t j = 0 ; j < currentMaxNbPolys ; j++)
  539. {
  540. bufferOfBuffers[j]=inter_reply[i][j].a;
  541. }
  542. // Ciphertexts can be serialized in a single block as a,b are allocatted contiguously
  543. in_data2b[i].p = cryptoMethod->deserializeDataNFL((unsigned char**)bufferOfBuffers,
  544. currentMaxNbPolys,
  545. cryptoMethod->getPublicParameters().getCiphertextBitsize(),
  546. in_data2b[i].nbPolys);
  547. //delete[] inter_reply[i]; free in generateReplyGeneric
  548. }
  549. //delete[] inter_reply;
  550. free(bufferOfBuffers);
  551. currentMaxNbPolys = in_data2b_nbr_polys;
  552. return in_data2b;
  553. }
  554. //// Original function
  555. //lwe_in_data* PIRReplyGeneratorNFL_internal::fromResulttoInData(lwe_cipher** inter_reply, uint64_t reply_elt_nbr, unsigned int reply_rec_lvl)
  556. //{
  557. // uint64_t in_data2b_bytes = cryptoMethod->getPublicParameters().getAbsorptionBitsize()/8;
  558. // uint64_t in_data2b_polys_per_reply_poly = ceil((double)(cryptoMethod->getPublicParameters().getCiphertextBitsize()/8)/in_data2b_bytes);
  559. // uint64_t in_data2b_nbr_polys = currentMaxNbPolys * in_data2b_polys_per_reply_poly;
  560. //
  561. // lwe_in_data *in_data2b = new lwe_in_data[reply_elt_nbr]();
  562. // lwe_in_data tmp_in_data;
  563. //
  564. // //For each element in the reply
  565. // for (uint64_t i = 0 ; i < reply_elt_nbr ; i++)
  566. // {
  567. // in_data2b[i].p = (poly64 *) malloc(in_data2b_nbr_polys*sizeof(poly64));
  568. // in_data2b[i].nbPolys = 0;
  569. //
  570. // // For each polynomial in a reply element
  571. // for (uint64_t j = 0 ; j < currentMaxNbPolys ; j++)
  572. // {
  573. // // Ciphertexts can be serialized in a single block as a,b are allocatted contiguously
  574. // tmp_in_data.p = cryptoMethod->deserializeDataNFL((unsigned char*)inter_reply[i][j].a, cryptoMethod->getPublicParameters().getCiphertextBitsize(), tmp_in_data.nbPolys);
  575. // for (uint64_t k = 0 ; k < in_data2b_polys_per_reply_poly; k++)
  576. // {
  577. // in_data2b[i].p[k + j * in_data2b_polys_per_reply_poly] = tmp_in_data.p[k];
  578. // }
  579. // in_data2b[i].nbPolys += in_data2b_polys_per_reply_poly;
  580. // }
  581. // delete[] inter_reply[i];
  582. // }
  583. // delete[] inter_reply;
  584. //
  585. // currentMaxNbPolys = in_data2b_nbr_polys;
  586. // return in_data2b;
  587. //}
  588. /**
  589. * Compute Reply Size une chunks.
  590. * WARNING blocking function.
  591. **/
  592. unsigned long PIRReplyGeneratorNFL_internal::computeReplySizeInChunks(unsigned long int maxFileBytesize)
  593. {
  594. using namespace GlobalConstant;
  595. unsigned int out = ceil((double)maxFileBytesize*kBitsPerByte*pirParam.alpha/cryptoMethod->getPublicParameters().getAbsorptionBitsize(0));
  596. for (unsigned int i = 1; i < pirParam.d; i++) {
  597. out = ceil(out * double(cryptoMethod->getPublicParameters().getCiphBitsizeFromRecLvl(i)/kBitsPerByte) / double(cryptoMethod->getPublicParameters().getAbsorptionBitsize(i) / kBitsPerByte));
  598. }
  599. return out;
  600. }
  601. /**
  602. * Overloaded fonction from GenericPIRReplyGenerator.
  603. * Initalise queriesBuf.
  604. **/
  605. void PIRReplyGeneratorNFL_internal::initQueriesBuffer() {
  606. const unsigned int nbQueriesBuf=pirParam.d;;
  607. #ifdef SHOUP
  608. queriesBuf = new lwe_query**[nbQueriesBuf]();
  609. for (unsigned int i = 0 ; i < nbQueriesBuf ; i++)
  610. {
  611. queriesBuf[i] = new lwe_query*[2];
  612. queriesBuf[i][0] = new lwe_query[pirParam.n[i]]();
  613. queriesBuf[i][1] = new lwe_query[pirParam.n[i]]();
  614. }
  615. #else
  616. queriesBuf = new lwe_query*[nbQueriesBuf]();
  617. for (unsigned int i = 0 ; i < nbQueriesBuf ; i++)
  618. {
  619. queriesBuf[i] = new lwe_query[pirParam.n[i]]();
  620. }
  621. #endif
  622. #ifdef DEBUG
  623. std::cout<<"Created a queriesBuf for "<<nbQueriesBuf<<" queries"<<std::endl;
  624. #endif
  625. }
  626. void PIRReplyGeneratorNFL_internal::pushFakeQuery()
  627. {
  628. char* query_element = cryptoMethod->encrypt(0, 1);
  629. for (unsigned int dim = 0 ; dim < pirParam.d ; dim++) {
  630. for(unsigned int j = 0 ; j < pirParam.n[dim] ; j++) {
  631. pushQuery(query_element, cryptoMethod->getPublicParameters().getCiphertextBitsize()/8, dim, j);
  632. }
  633. }
  634. free(query_element);
  635. }
  636. void PIRReplyGeneratorNFL_internal::pushQuery(char* rawQuery)
  637. {
  638. pushQuery(rawQuery, cryptoMethod->getPublicParameters().getCiphertextBitsize()/8, current_dim_index, current_query_index);
  639. current_query_index++;
  640. if (current_query_index >= pirParam.n[current_dim_index])
  641. {
  642. current_query_index = 0;
  643. current_dim_index++;
  644. }
  645. if (current_dim_index >= pirParam.d)
  646. {
  647. std::cout << "PIRReplyGeneratorNFL: Finished importing query (this message should appear only once)" << std::endl;
  648. }
  649. }
  650. void PIRReplyGeneratorNFL_internal::pushQuery(char* rawQuery, unsigned int size, int dim, int nbr)
  651. {
  652. unsigned int polyDegree = cryptoMethod->getpolyDegree();
  653. unsigned int nbModuli = cryptoMethod->getnbModuli();
  654. // Trick, we get both a and b at the same time, b needs to be set afterwards
  655. uint64_t *a,*b;
  656. a = (poly64) calloc(size, 1);
  657. memcpy(a,rawQuery,size);
  658. if (lwe) b = a+nbModuli*polyDegree;
  659. #ifdef CRYPTO_DEBUG
  660. std::cout<<"\nQuery received.a ";NFLTools::print_poly64(a,4);
  661. if (lwe) {std::cout<<"Query received.b ";NFLTools::print_poly64hex(b,4);}
  662. #endif
  663. #ifdef SHOUP
  664. uint64_t *ap,*bp;
  665. ap = (poly64) calloc(size, 1);
  666. if (lwe) bp = ap+nbModuli*polyDegree;
  667. for (unsigned int cm = 0 ; cm < nbModuli ; cm++)
  668. {
  669. for (unsigned i = 0 ; i < polyDegree ;i++)
  670. {
  671. ap[i+cm*polyDegree] = ((uint128_t) a[i+cm*polyDegree] << 64) / cryptoMethod->getmoduli()[cm];
  672. if (lwe) bp[i+cm*polyDegree] = ((uint128_t) b[i+cm*polyDegree] << 64) / cryptoMethod->getmoduli()[cm];
  673. }
  674. }
  675. queriesBuf[dim][0][nbr].a = a;
  676. queriesBuf[dim][0][nbr].b = b;
  677. queriesBuf[dim][1][nbr].a = ap;
  678. queriesBuf[dim][1][nbr].b = bp;
  679. #ifdef CRYPTO_DEBUG
  680. std::cout << "Query NFL pushed.a' "; NFLTools::print_poly64hex(queriesBuf[dim][1][nbr].a,4);
  681. if (lwe) { std::cout << "Query NFL pushed.b' "; NFLTools::print_poly64hex(queriesBuf[dim][1][nbr].b,4);}
  682. #endif
  683. #else
  684. queriesBuf[dim][nbr].a = a;
  685. queriesBuf[dim][nbr].b = b;
  686. #endif
  687. }
  688. size_t PIRReplyGeneratorNFL_internal::getTotalSystemMemory()
  689. {
  690. #ifdef __APPLE__
  691. int m[2];
  692. m[0] = CTL_HW;
  693. m[1] = HW_MEMSIZE;
  694. int64_t size = 0;
  695. size_t len = sizeof( size );
  696. sysctl( m, 2, &size, &len, NULL, 0 );
  697. return (size_t)size;
  698. #else
  699. long pages = /*get_phys_pages();*/sysconf(_SC_PHYS_PAGES);
  700. long page_size = /*getpagesize();*/sysconf(_SC_PAGE_SIZE);
  701. return pages * page_size;
  702. #endif
  703. }
  704. void PIRReplyGeneratorNFL_internal::setPirParams(PIRParameters& param)
  705. {
  706. pirParam = param;
  707. cryptoMethod->setandgetAbsBitPerCiphertext(pirParam.n[0]);
  708. }
  709. void PIRReplyGeneratorNFL_internal::setCryptoMethod(CryptographicSystem* cm)
  710. {
  711. //cryptoMethod = (NFLLWE*) cm;
  712. cryptoMethod = (LatticesBasedCryptosystem*) cm;
  713. lwe = (cryptoMethod->toString() == "LWE") ? true : false;
  714. }
  715. void PIRReplyGeneratorNFL_internal::freeInputData()
  716. {
  717. uint64_t theoretical_files_nbr = 1;
  718. for (unsigned int i = 0 ; i < pirParam.d ; i++) theoretical_files_nbr *= pirParam.n[i];
  719. for (unsigned int i = 0 ; i < theoretical_files_nbr ; i++){
  720. #ifdef DEBUG
  721. printf( "PIRReplyGeneratorNFL_internal: freeing input_data[%d]\n",i);
  722. #endif
  723. free(input_data[i].p[0]);
  724. free(input_data[i].p);
  725. }
  726. delete[] input_data;
  727. #ifdef DEBUG
  728. printf( "PIRReplyGeneratorNFL_internal: input_data freed\n");
  729. #endif
  730. }
  731. void PIRReplyGeneratorNFL_internal::freeQueries()
  732. {
  733. for (unsigned int i = 0; i < pirParam.d; i++)
  734. {
  735. for (unsigned int j = 0 ; j < pirParam.n[i] ; j++) {
  736. if (queriesBuf[i][0][j].a != NULL){
  737. free(queriesBuf[i][0][j].a); //only free a because a and b and contingus, see pushQuery
  738. queriesBuf[i][0][j].a = NULL;
  739. }
  740. if (queriesBuf[i][1][j].a != NULL){
  741. free(queriesBuf[i][1][j].a); //only free a because a and b and contingus, see pushQuery
  742. queriesBuf[i][1][j].a = NULL;
  743. }
  744. }
  745. }
  746. current_query_index = 0;
  747. current_dim_index = 0;
  748. #ifdef DEBUG
  749. printf( "queriesBuf freed\n");
  750. #endif
  751. }
  752. void PIRReplyGeneratorNFL_internal::freeQueriesBuffer()
  753. {
  754. for (unsigned int i = 0; i < pirParam.d; i++)
  755. {
  756. if (queriesBuf[i][0] != NULL){
  757. delete[] queriesBuf[i][0]; //allocated in intQueriesBuf with new.
  758. queriesBuf[i][0] = NULL;
  759. }
  760. if (queriesBuf[i][1] != NULL){
  761. delete[] queriesBuf[i][1]; //allocated in intQueriesBuf with new.
  762. queriesBuf[i][1] = NULL;
  763. }
  764. if (queriesBuf[i] != NULL){
  765. delete[] queriesBuf[i]; //allocated in intQueriesBuf with new.
  766. queriesBuf[i] = NULL;
  767. }
  768. delete[] queriesBuf[i];
  769. }
  770. if (queriesBuf != NULL){
  771. delete[] queriesBuf; //allocated in intQueriesBuf with new.
  772. queriesBuf = NULL;
  773. }
  774. delete[] queriesBuf;//allocated in intQueriesBuf with new.
  775. }
  776. void PIRReplyGeneratorNFL_internal::freeResult()
  777. {
  778. if(repliesArray!=NULL)
  779. {
  780. for(unsigned i=0 ; i < repliesAmount; i++)
  781. {
  782. if(repliesArray[i]!=NULL) free(repliesArray[i]);
  783. repliesArray[i] = NULL;
  784. }
  785. free(repliesArray);
  786. repliesArray=NULL;
  787. }
  788. }
  789. PIRReplyGeneratorNFL_internal::~PIRReplyGeneratorNFL_internal()
  790. {
  791. freeQueries();
  792. freeQueriesBuffer();
  793. freeResult();
  794. }