libpir.hpp 7.3 KB

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