libpir.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef DEF_LIBPIR
  2. #define DEF_LIBPIR
  3. #include <pir/shared_queue.hpp>
  4. #include <pir/PIRParameters.hpp>
  5. #include <pir/queryGen/PIRQueryGenerator_internal.hpp>
  6. #include <pir/replyExtraction/PIRReplyExtraction_internal.hpp>
  7. #include <pir/replyGenerator/PIRReplyGeneratorNFL_internal.hpp>
  8. #include <crypto/NFLLWE.hpp>
  9. #include <crypto/HomomorphicCryptoFactory_internal.hpp>
  10. #include <crypto/HomomorphicCrypto.hpp>
  11. #include "pir/dbhandlers/DBGenerator.hpp"
  12. #include "pir/dbhandlers/DBDirectoryProcessor.hpp"
  13. #include <stdint.h>
  14. /**
  15. * Class storing the database (or a chunk of it) after pre-processing
  16. * Type returned by the funcion importData below
  17. **/
  18. class imported_database : public imported_database_t
  19. {
  20. public:
  21. ~imported_database();
  22. };
  23. /**
  24. * HomomorphicCryptoFactory is used to create a generic cryptographic object (Ring-LWE, Paillier,
  25. * mockup-cryptography, etc.). This API only exposes the Ring-LWE cryptosystem, but we still
  26. * use the generic factory to get an instance of this cryptosystem in order to avoid code duplication.
  27. **/
  28. class HomomorphicCryptoFactory : public HomomorphicCryptoFactory_internal
  29. {
  30. public:
  31. /** prints a list of the available crypto parameters
  32. * (CryptoSystem:SecurityMax:PolyDegree:ModulusBitsize)
  33. **/
  34. static void printAllCryptoParams();
  35. /** takes one of the parameters given by the previous
  36. * method as a string and returns an instance of a cryptosystem for the given parameters
  37. **/
  38. static HomomorphicCrypto* getCryptoMethod(std::string cryptoParams);
  39. };
  40. /**
  41. * PIRQueryGenerator is Client side, it initiates the PIR protocol by generating a query
  42. * corresponding to the chosen element
  43. **/
  44. class PIRQueryGenerator : public PIRQueryGenerator_internal
  45. {
  46. public:
  47. /**
  48. * Class constructor
  49. * Params:
  50. * - PIRParameters& pirParameters_ : PIRParameters structure defining the cryptographic
  51. * parameters, aggregation, recursion, and the shape of the database, see the main()
  52. * function of apps/simplepir/simplePIR.cpp for detailed usage examples
  53. * - NFLLWE& cryptoMethod_ : shared_pointer of the cryptographic instance
  54. **/
  55. PIRQueryGenerator(PIRParameters& pirParameters, HomomorphicCrypto& cryptoMethod_);
  56. /**
  57. * Generates asynchronously queries (a set of encryptions of 0 or 1).
  58. * Params:
  59. * - uint64_t _chosenElement : index of the element to be retrieved (indexes start at 0)
  60. * Can be called on a separate thread.
  61. **/
  62. void generateQuery(uint64_t _chosenElement );
  63. /**
  64. * Function to get a pointer to a char* query element.
  65. * Returns false when the queue is over (true otherwise) and waits when it's empty
  66. * Can be called on a separate thread.
  67. **/
  68. bool popQuery(char** query);
  69. /**
  70. * Get the size in bytes of a query element
  71. **/
  72. uint64_t getQueryElementBytesize();
  73. private:
  74. int nbQueries;
  75. };
  76. /**
  77. * PIRReplyGenerator is Server side
  78. * It handles the request generated by the client and generates the reply
  79. **/
  80. class PIRReplyGenerator : public PIRReplyGeneratorNFL_internal
  81. {
  82. public:
  83. /**
  84. * Constructor of the class.
  85. * Params :
  86. * - vector <std::string>& database : reference of vector of the database elements
  87. * - PIRParameters& param : reference to a PIRParameters structure (see the PIRQueryGenerator
  88. * constructor help for more details).
  89. * - DBHandler* db : abstract type for a database handler, at the moment it can be a DBGenerator
  90. * that generates a fake database or a DBDirectoryProcessor that works on real files. See the
  91. * main() function of apps/simplepir/simplePIR.cpp for examples.
  92. **/
  93. PIRReplyGenerator(PIRParameters& param, HomomorphicCrypto& cryptoMethod_, DBHandler *db);
  94. /**
  95. * Feeds the Server with the queries, this includes a pre-computation phase that speeds up
  96. * reply generation (computation of newton coefficients, see the associated paper)
  97. * Can be called on a separate thread but all the query elements must be pushed before
  98. * starting the reply generation.
  99. **/
  100. void pushQuery(char* rawQuery);
  101. /**
  102. * Imports the database into a usable form. If the database is too large to fit in RAM
  103. * (there is an expansion factor of 3 due to pre-processing) it can be cut by reading
  104. * only for each element a chunk of size bytes_per_db_element starting at a given offset
  105. * Can be called on a separate thread but the database must be imported before starting
  106. * the reply generation.
  107. **/
  108. imported_database* importData(uint64_t offset, uint64_t bytes_per_db_element);
  109. /**
  110. * Prepares reply and start absoptions, returns number of chunks.
  111. * Precondition: database is precomputed
  112. *
  113. * The PIRParameters given to the constructor are used and therefore the reply will
  114. * be potentially generated with recursion and aggregation
  115. * Can be called on a separate thread but the database must be imported and the query pushed
  116. * before starting the reply generation.
  117. **/
  118. void generateReply(const imported_database* database);
  119. /**
  120. * Frees the queries allocated
  121. **/
  122. void freeQueries();
  123. /**
  124. * Gets a pointer to a char* reply element.
  125. * Returns false when the queue is over (true otherwise) and waits when it's empty
  126. * Can be called on a separate thread
  127. **/
  128. bool popReply(char** reply);
  129. /**
  130. * Getter for nbRepliesGenerated, the amount or reply elements generated
  131. **/
  132. uint64_t getnbRepliesGenerated();
  133. /**
  134. * Gets the size in bytes of a reply element
  135. **/
  136. uint64_t getReplyElementBytesize();
  137. private:
  138. uint64_t nbRepliesToHandle, nbRepliesGenerated, currentReply;
  139. };
  140. /**
  141. * PIRReplyExtraction is Client side, it extracts the chosen element from the reply of the Server
  142. **/
  143. class PIRReplyExtraction : public PIRReplyExtraction_internal
  144. {
  145. public :
  146. /**
  147. * Class constructor
  148. * Params:
  149. * - PIRParameters& pirParameters : reference to a PIRParameters structure (see the
  150. * PIRQueryGenerator constructor help for more details).
  151. * - NFLLWE& cryptoMethod_ : shared_pointer of the cryptographic instance.
  152. **/
  153. PIRReplyExtraction(PIRParameters& pirParameters, HomomorphicCrypto& cryptoMethod);
  154. /**
  155. * Can be called on a separate thread
  156. * Feed the client with the reply, may wait if the internal queue is full until
  157. * enough replies are decrypted. If such thing happens extractReply should be called
  158. * on a different thread
  159. **/
  160. void pushEncryptedReply(char* rawBytes);
  161. /**
  162. * Can be called on a separate thread
  163. * extract/decrypt the result from the pushed replies, the plaintext results have to be popped.
  164. * May block if the internal queue of plaintext results is full. If such a thing happens
  165. * a thread that calls regularly popPlaintextResult must be used.
  166. **/
  167. void extractReply(uint64_t maxFileBytesize);
  168. /**
  169. * Can be called on a separate thread
  170. * Function to get a pointer to a char* result element
  171. * Returns false when the queue is over (true otherwise) and waits when its empty
  172. **/
  173. bool popPlaintextResult(char** result);
  174. /**
  175. * Get plaintext reply size
  176. **/
  177. uint64_t getPlaintextReplyBytesize();
  178. /**
  179. * Getter for nbPlaintextReplies, the number of plaintext replies that will be generated
  180. * in the extraction processs
  181. **/
  182. uint64_t getnbPlaintextReplies(uint64_t maxFileBytesize);
  183. private:
  184. shared_queue<char*> clearChunks;
  185. uint64_t nbPlaintextReplies;
  186. };
  187. #endif