PIRQueryGenerator_internal.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "PIRQueryGenerator_internal.hpp"
  18. #include "../crypto/NFLLWE.hpp"
  19. /**
  20. * Class constructor
  21. * Params:
  22. * - PIRParameters& pirParameters_ : PIRParameters reference shared with PIRClient.
  23. * - crypto_ptr cryptoMethod_ : shared_pointer of Homomorphic crypto.
  24. **/
  25. PIRQueryGenerator_internal::PIRQueryGenerator_internal(PIRParameters& pirParameters_,HomomorphicCrypto& cryptoMethod_) :
  26. pirParams(pirParameters_),
  27. cryptoMethod(cryptoMethod_),
  28. queryBuffer("query_buffer"),
  29. mutex()
  30. {}
  31. /**
  32. * Generates asyncronously queries for each files.
  33. * Makes encrypted of 0 or 1.
  34. **/
  35. uint64_t simple_power(uint32_t d, uint32_t n){
  36. uint64_t out = n;
  37. for(int i=1;i<d;i++)
  38. out*=n;
  39. return out;
  40. }
  41. void PIRQueryGenerator_internal::generateQuery()
  42. {
  43. clock_t gq_start, gq_end;
  44. gq_start = clock();
  45. size_t single_encrypted_val_size=0, size_se =0;
  46. double start = omp_get_wtime();
  47. coord = new unsigned int[pirParams.d]();
  48. computeCoordinates();
  49. for (unsigned int j = 0 ; j < pirParams.d ; j++)
  50. {
  51. for (unsigned int i = 0 ; i < pirParams.n[j] ; i++)
  52. {
  53. if(j==0&&i==0){
  54. char *tmp = cryptoMethod.encrypt(0, j + 1);
  55. single_encrypted_val_size=strlen(tmp);
  56. size_se = cryptoMethod.getPublicParameters().getQuerySizeFromRecLvl(j+1) / (8);
  57. //std::cout << "ssasy_size: PIRQueryGenerator_internal: Generated a " << single_encrypted_val_size << " char(byte)-sized element query" << std::endl;
  58. std::cout << "measure_size_single:PIRQueryGenerator_internal:single_query_size:"<< size_se <<":bytes" << std::endl;
  59. //std::cout << "measure_size:PIRQueryGenerator_internal:single_query_size:"<< (size_t)(size_se*(size_t)(simple_power(pirParams.d, pirParams.n[0]))) <<":bytes" << std::endl;
  60. //std::cout << "measure_size:PIRQueryGenerator_internal:single_query_size:"<< (size_t)(size_se*(size_t)(compute_recursive_query_size(pirParams.d, pirParams.n))) <<":bytes" << std::endl;
  61. }
  62. if (i == coord[j]) queryBuffer.push(cryptoMethod.encrypt(1, j + 1 ));
  63. else
  64. queryBuffer.push(cryptoMethod.encrypt(0, j + 1));
  65. }
  66. }
  67. size_t final_size = 0, size = 0;
  68. for (unsigned int i=1; i<=pirParams.d; i++) {
  69. size_t size_se = cryptoMethod.getPublicParameters().getQuerySizeFromRecLvl(i) / (8);
  70. size=(pirParams.n[i-1] * size_se);
  71. final_size+=size;
  72. }
  73. std::cout << "measure_size:PIRQueryGenerator_internal:single_query_size:"<< final_size <<":bytes" << std::endl;
  74. std::cout << "other_params:"<<pirParams.d<<":"<<pirParams.alpha<<std::endl;
  75. double end = omp_get_wtime();
  76. gq_end = clock();
  77. delete[] coord;
  78. std::cout << "PIRQueryGenerator_internal: All the queries have been generated, total time is " << end - start << " seconds" << std::endl;
  79. std::cout << "consensgx1 : QueryGeneration time is " << (double)1000 * double(gq_end - gq_start)/(double)CLOCKS_PER_SEC << " ms" << std::endl;
  80. std::cout << "measure_params:PIRQueryGenerator_internal:pirParams.d :" <<pirParams.d<< ":pirParams.alpha:"<<pirParams.alpha<<":pirParams.n[]:" ;
  81. for (unsigned int i = 0 ; i < pirParams.d ; i++)
  82. std::cout << pirParams.n[i] << ", ";
  83. std::cout<<std::endl << std::flush;
  84. }
  85. /**
  86. * Compute coordinates of the chosen file.
  87. **/
  88. void PIRQueryGenerator_internal::computeCoordinates()
  89. {
  90. uint64_t x = chosenElement;
  91. for (unsigned int i = 0 ; i < pirParams.d ; i++)
  92. {
  93. coord[i] = x % pirParams.n[i];
  94. x /= pirParams.n[i];
  95. }
  96. }
  97. /**
  98. * Starts computation in a new thread
  99. **/
  100. void PIRQueryGenerator_internal::startGenerateQuery()
  101. {
  102. queryThread = thread(&PIRQueryGenerator_internal::generateQuery, this);
  103. }
  104. uint64_t PIRQueryGenerator_internal::getChosenElement()
  105. {
  106. return chosenElement;
  107. }
  108. void PIRQueryGenerator_internal::setChosenElement( uint64_t _chosenElement )
  109. {
  110. chosenElement = _chosenElement;
  111. }
  112. void PIRQueryGenerator_internal::setPIRParameters(PIRParameters& pirParams_)
  113. {
  114. pirParams = pirParams_;
  115. }
  116. /**
  117. * Join query thread if it's possible.
  118. **/
  119. void PIRQueryGenerator_internal::joinThread()
  120. {
  121. if(queryThread.joinable()) queryThread.join();
  122. }
  123. void PIRQueryGenerator_internal::cleanQueryBuffer()
  124. {
  125. while (!queryBuffer.empty())
  126. free(queryBuffer.pop_front());
  127. }
  128. PIRQueryGenerator_internal::~PIRQueryGenerator_internal()
  129. {
  130. joinThread();
  131. cleanQueryBuffer();
  132. }