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