libpir.hpp 7.2 KB

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