PIRQueryGenerator_internal.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. void PIRQueryGenerator_internal::generateQuery()
  36. {
  37. double start = omp_get_wtime();
  38. coord = new unsigned int[pirParams.d]();
  39. computeCoordinates();
  40. for (unsigned int j = 0 ; j < pirParams.d ; j++)
  41. {
  42. for (unsigned int i = 0 ; i < pirParams.n[j] ; i++)
  43. {
  44. if (i == coord[j]) queryBuffer.push(cryptoMethod.encrypt(1, j + 1 ));
  45. else queryBuffer.push(cryptoMethod.encrypt(0, j + 1));
  46. }
  47. std::cout << "PIRQueryGenerator_internal: Generated a " << pirParams.n[j] << " element query" << std::endl;
  48. }
  49. double end = omp_get_wtime();
  50. std::cout << "PIRQueryGenerator_internal: All the queries have been generated, total time is " << end - start << " seconds" << std::endl;
  51. }
  52. /**
  53. * Compute coordinates of the chosen file.
  54. **/
  55. void PIRQueryGenerator_internal::computeCoordinates()
  56. {
  57. uint64_t x = chosenElement;
  58. for (unsigned int i = 0 ; i < pirParams.d ; i++)
  59. {
  60. coord[i] = x % pirParams.n[i];
  61. x /= pirParams.n[i];
  62. }
  63. }
  64. /**
  65. * Starts computation in a new thread
  66. **/
  67. void PIRQueryGenerator_internal::startGenerateQuery()
  68. {
  69. queryThread = thread(&PIRQueryGenerator_internal::generateQuery, this);
  70. }
  71. uint64_t PIRQueryGenerator_internal::getChosenElement()
  72. {
  73. return chosenElement;
  74. }
  75. void PIRQueryGenerator_internal::setChosenElement( uint64_t _chosenElement )
  76. {
  77. chosenElement = _chosenElement;
  78. }
  79. void PIRQueryGenerator_internal::setPIRParameters(PIRParameters& pirParams_)
  80. {
  81. pirParams = pirParams_;
  82. }
  83. /**
  84. * Join query thread if it's possible.
  85. **/
  86. void PIRQueryGenerator_internal::joinThread()
  87. {
  88. if(queryThread.joinable()) queryThread.join();
  89. }
  90. PIRQueryGenerator_internal::~PIRQueryGenerator_internal()
  91. {
  92. joinThread();
  93. cleanQueryBuffer();
  94. delete[] coord;
  95. }
  96. void PIRQueryGenerator_internal::cleanQueryBuffer()
  97. {
  98. while (!queryBuffer.empty())
  99. free(queryBuffer.pop_front());
  100. }