simplePIR.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "libpir.hpp"
  2. #include "apps/server/DBDirectoryProcessor.hpp"
  3. bool run(DBHandler *db, uint64_t chosen_element, PIRParameters params){
  4. /******************************************************************************
  5. * PIR and Crypto Setup (must be done by both the client and the server)
  6. * In a real application the client and server must agree on the parameters
  7. * For example the client chooses and sends them to the server (or inversely)
  8. ******************************************************************************/
  9. HomomorphicCrypto *crypto = HomomorphicCryptoFactory::getCryptoMethod(params.crypto_params);
  10. // Absorption capacity of an LWE encryption scheme depends on the number of sums that are going
  11. // to be done in the PIR protocol, it must therefore be initialized
  12. // Warning here we suppose the biggest dimension is in d[0]
  13. // otherwise absorbtion needs to be computed accordingly
  14. crypto->setandgetAbsBitPerCiphertext(params.n[0]);
  15. /******************************************************************************
  16. * Query generation phase (client-side)
  17. ******************************************************************************/
  18. // Create the query generator object
  19. PIRQueryGenerator q_generator(params,*crypto);
  20. std::cout << "SimplePIR: Generating query ..." << std::endl;
  21. // Generate a query to get the FOURTH element in the database (indexes begin at 0)
  22. // Warning : if we had set params.alpha=2 elements would be aggregated 2 by 2 and
  23. // generatequery would only accept as input 0 (the two first elements) or 1 (the other two)
  24. q_generator.generateQuery(chosen_element);
  25. std::cout << "SimplePIR: Query generated" << std::endl;
  26. /******************************************************************************
  27. * Reply generation phase (server-side)
  28. ******************************************************************************/
  29. // Create the reply generator object
  30. // We could have also defined PIRReplyGenerator *r_generator(params,*crypto,db);
  31. // But we prefer a pointer to show (below) how to use multiple generators for a given db
  32. PIRReplyGenerator *r_generator = new PIRReplyGenerator(params,*crypto,db);
  33. // In a real application the client would pop the queries from q with popQuery and
  34. // send them through the network and the server would receive and push them into s
  35. // using pushQuery
  36. char* query_element;
  37. while (q_generator.popQuery(&query_element))
  38. {
  39. r_generator->pushQuery(query_element);
  40. }
  41. // Import database
  42. // This could have been done on the "Database setup" phase if:
  43. // - the contents are static
  44. // - AND the imported database fits in RAM
  45. // - AND the server knows in advance the PIR and crypto parameters (e.g. chosen by him)
  46. std::cout << "SimplePIR: Importing database ..." << std::endl;
  47. // Warning aggregation is dealt with internally the bytes_per_db_element parameter here
  48. // is to be given WITHOUT multiplying it by params.alpha
  49. imported_database* imported_db = r_generator->importData(/* uint64_t offset*/ 0, /*uint64_t
  50. bytes_per_db_element */ db->getmaxFileBytesize());
  51. std::cout << "SimplePIR: Database imported" << std::endl;
  52. // Once the query is known and the database imported launch the reply generation
  53. std::cout << "SimplePIR: Generating reply ..." << std::endl;
  54. double start = omp_get_wtime();
  55. r_generator->generateReply(imported_db);
  56. double end = omp_get_wtime();
  57. std::cout << "SimplePIR: Reply generated in " << end-start << " seconds" << std::endl;
  58. /********************************************************************************
  59. * Advanced example: uncomment it to test
  60. * The object imported_db is separated from r_generator in purpose
  61. * Here is an example on how to use the same imported_db for multiple queries
  62. * DO NOT try to use the same reply generator more than once, this causes issues
  63. * ******************************************************************************/
  64. #if 0
  65. // Generate 3 replies from 3 queries
  66. for (int i = 0 ; i < 3 ; i++){
  67. // Pop (and drop for this simple example) the generated reply
  68. char* reply_element_tmp;
  69. while (r_generator->popReply(&reply_element_tmp)){
  70. free(reply_element_tmp);
  71. }
  72. // If you are unable to reuse a r_generator object (e.g. if you want
  73. // to change the crypto object) you can always recreate a new generator
  74. //delete r_generator;
  75. //r_generator = new PIRReplyGenerator(params,*crypto,db);
  76. // In this example we want to use the same generator for
  77. // multiply queries. Before giving a new query to r_generator
  78. // we must free the previous one.
  79. r_generator->freeQueries();
  80. // It is also possible to change the pir parameters with the
  81. // (unexposed) setPirParams(PIRParameters newparams) function
  82. // Generate a new query
  83. q_generator.generateQuery(chosen_element);
  84. // Push it to the reply generator
  85. while (q_generator.popQuery(&query_element))
  86. {
  87. r_generator->pushQuery(query_element);
  88. }
  89. // Generate again the reply
  90. r_generator->generateReply(imported_db);
  91. }
  92. #endif
  93. /******************************************************************************
  94. * Reply extraction phase (client-side)
  95. ******************************************************************************/
  96. PIRReplyExtraction *r_extractor=new PIRReplyExtraction(params,*crypto);
  97. // In a real application the server would pop the replies from s with popReply and
  98. // send them through the network together with nbRepliesGenerated and aggregated_maxFileSize
  99. // and the client would receive the replies and push them into r using pushEncryptedReply
  100. std::cout << "SimplePIR: "<< r_generator->getnbRepliesGenerated()<< " Replies generated " << std::endl;
  101. uint64_t clientside_maxFileBytesize = db->getmaxFileBytesize();
  102. char* reply_element;
  103. while (r_generator->popReply(&reply_element))
  104. {
  105. r_extractor->pushEncryptedReply(reply_element);
  106. }
  107. std::cout << "SimplePIR: Extracting reply ..." << std::endl;
  108. r_extractor->extractReply(clientside_maxFileBytesize);
  109. std::cout << "SimplePIR: Reply extracted" << std::endl;
  110. // In a real application instead of writing to a buffer we could write to an output file
  111. char *outptr, *result, *tmp;
  112. outptr = result = (char*)calloc(r_extractor->getnbPlaintextReplies(clientside_maxFileBytesize)*r_extractor->getPlaintextReplyBytesize(), sizeof(char));
  113. while (r_extractor->popPlaintextResult(&tmp))
  114. {
  115. memcpy(outptr, tmp, r_extractor->getPlaintextReplyBytesize());
  116. outptr+=r_extractor->getPlaintextReplyBytesize();
  117. free(tmp);
  118. }
  119. // Result is in ... result
  120. /******************************************************************************
  121. * Test correctness
  122. ******************************************************************************/
  123. char *db_element = (char*)calloc(clientside_maxFileBytesize*params.alpha, sizeof(char));
  124. bool fail = false;
  125. db->readAggregatedStream(chosen_element, params.alpha, 0, clientside_maxFileBytesize, db_element);
  126. if (memcmp(result, db_element, clientside_maxFileBytesize*params.alpha))
  127. {
  128. std::cout << "SimplePIR: Test failed, the retrieved element is not correct" << std::endl;
  129. fail = true;
  130. }
  131. else
  132. {
  133. std::cout << "SimplePIR: Test succeeded !!!!!!!!!!!!!!!!!!!!!!!!" << std::endl<< std::endl;
  134. fail = false;
  135. }
  136. /******************************************************************************
  137. * Cleanup
  138. ******************************************************************************/
  139. delete imported_db;
  140. r_generator->freeQueries();
  141. delete r_generator;
  142. delete r_extractor;
  143. delete crypto;
  144. free(result);
  145. free(db_element);
  146. return fail;
  147. }
  148. int main(int argc, char * argv[]) {
  149. uint64_t database_size, nb_files, chosen_element, maxFileBytesize;
  150. PIRParameters params;
  151. bool tests_failed = false;
  152. /******************************************************************************
  153. * Database setup (server-side)
  154. ******************************************************************************/
  155. // To Create the database generator object
  156. // it can be a DBGenerator that simulate nb_files files of size streamBytesize
  157. // database_size = 1ULL<<25; nb_files = 4; maxFileBytesize = database_size/nb_files;
  158. // DBGenerator db(nb_files, maxFileBytesize, /*bool silent*/ false);
  159. //
  160. // OR it can be a DBDirectoryProcessor that reads a real file in the ./db directory
  161. // and splits it into nb_files virtual files
  162. // nb_files = 4;
  163. // DBDirectoryProcessor db(nb_files);
  164. // database_size = db.getDBSizeinbits();maxFileBytesize = database_size/nb_files;
  165. //
  166. // OR it can be a DBDirectoryProcessor that reads the real files in the ./db directory
  167. // DBDirectoryProcessor db;
  168. // nb_files=db.getNbStream();database_size = db.getDBSizeinbits();
  169. // maxFileBytesize = database_size/nb_files;
  170. #if 0
  171. // Simple test
  172. std::cout << "======================================================================" << std::endl;
  173. std::cout << "Test 1/7: database_size = 1ULL<<30; nb_files = 20;" << std::endl;
  174. std::cout << "params.alpha = 1; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl;
  175. std::cout << "======================================================================" << std::endl;
  176. database_size = 1ULL<<20; nb_files = 20; maxFileBytesize = database_size/nb_files;
  177. DBGenerator db(nb_files, maxFileBytesize, /*bool silent*/ false);
  178. chosen_element = 3;
  179. params.alpha = 1; params.d = 1; params.n[0] = nb_files;
  180. // The crypto parameters can be set to other values
  181. // You can get a list of all available cryptographic parameters with this function call
  182. // HomomorphicCryptoFactory::printAllCryptoParams();
  183. params.crypto_params = "LWE:80:2048:120";
  184. tests_failed |= run(&db, chosen_element, params);
  185. // Test with aggregation
  186. // WARNING we must provide the representation of the database GIVEN recursion and aggregation
  187. // as here we have 100 elements and aggregate them in a unique group we have params.n[0]=1
  188. std::cout << "======================================================================" << std::endl;
  189. std::cout << "Test 2/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
  190. std::cout << "params.alpha = 100; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl;
  191. std::cout << "======================================================================" << std::endl;
  192. database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
  193. DBGenerator db2(nb_files, maxFileBytesize, /*bool silent*/ false);
  194. chosen_element = 0;
  195. params.alpha = 100; params.d = 1; params.n[0] = 1;
  196. params.crypto_params = "LWE:80:2048:120";
  197. tests_failed |= run(&db2, chosen_element, params);
  198. #endif
  199. // Test with recursion 2
  200. std::cout << "======================================================================" << std::endl;
  201. std::cout << "Test 3/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
  202. std::cout << "params.alpha = 1; params.d = 2; crypto_params = LWE:80:2048:120;" << std::endl;
  203. std::cout << "======================================================================" << std::endl;
  204. database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
  205. DBGenerator db3(nb_files, maxFileBytesize, /*bool silent*/ false);
  206. chosen_element = 3;
  207. params.alpha = 1; params.d = 2; params.n[0] = 50; params.n[1] = 2;
  208. params.crypto_params = "LWE:80:2048:120";
  209. tests_failed |= run(&db3, chosen_element, params);
  210. #if 0
  211. // Test with recursion 2 and aggregation
  212. std::cout << "======================================================================" << std::endl;
  213. std::cout << "Test 4/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
  214. std::cout << "params.alpha = 2; params.d = 2; crypto_params = LWE:80:2048:120;" << std::endl;
  215. std::cout << "======================================================================" << std::endl;
  216. database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
  217. DBGenerator db4(nb_files, maxFileBytesize, /*bool silent*/ false);
  218. chosen_element = 3;
  219. params.alpha = 2; params.d = 2; params.n[0] = 25; params.n[1] = 2;
  220. params.crypto_params = "LWE:80:2048:120";
  221. tests_failed |= run(&db4, chosen_element, params);
  222. // Test with recursion 3
  223. std::cout << "======================================================================" << std::endl;
  224. std::cout << "Test 5/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
  225. std::cout << "params.alpha = 1; params.d = 3; crypto_params = LWE:80:2048:120;" << std::endl;
  226. std::cout << "======================================================================" << std::endl;
  227. database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
  228. DBGenerator db5(nb_files, maxFileBytesize, /*bool silent*/ false);
  229. chosen_element = 3;
  230. params.alpha = 1; params.d = 3; params.n[0] = 5; params.n[1] = 5; params.n[2] = 4;
  231. params.crypto_params = "LWE:80:2048:120";
  232. tests_failed |= run(&db5, chosen_element, params);
  233. // Test with a DBDirectoryProcessor splitting a big real file
  234. std::cout << "======================================================================" << std::endl;
  235. std::cout << "Test 6/7: DBDirectoryProcessor with split; database_size = 1ULL<<25; nb_files = 4;" << std::endl;
  236. std::cout << "params.alpha = 1; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl;
  237. std::cout << "======================================================================" << std::endl;
  238. database_size = 1ULL<<25; nb_files = 4; maxFileBytesize = database_size/nb_files;
  239. DBDirectoryProcessor db6(/*split the first file in*/ nb_files /*files*/);
  240. if (db6.getErrorStatus()==true){
  241. std::cout << "SimplePIR : Error with db directory skipping test ..." << std::endl << std::endl;
  242. } else {
  243. chosen_element = 3;
  244. params.alpha = 1; params.d = 1; params.n[0] = nb_files;
  245. params.crypto_params = "LWE:80:2048:120";
  246. tests_failed |= run(&db6, chosen_element, params);
  247. }
  248. // Test with a DBDirectoryProcessor reading real files
  249. std::cout << "======================================================================" << std::endl;
  250. std::cout << "Test 7/7: DBDirectoryProcessor without split;" << std::endl;
  251. std::cout << "params.alpha = 1; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl;
  252. std::cout << "======================================================================" << std::endl;
  253. DBDirectoryProcessor db7;
  254. if (db6.getErrorStatus()==true){
  255. std::cout << "SimplePIR : Error with db directory skipping test ..." << std::endl << std::endl;
  256. } else {
  257. database_size = db7.getDBSizeBits()/8; nb_files = db7.getNbStream();
  258. maxFileBytesize = database_size/nb_files;
  259. chosen_element = 0;
  260. params.alpha = 1; params.d = 1; params.n[0] = nb_files;
  261. params.crypto_params = "LWE:80:2048:120";
  262. tests_failed |= run(&db7, chosen_element, params);
  263. }
  264. #endif
  265. if (tests_failed)
  266. {
  267. std::cout << "WARNING : at least one tests failed" << std::endl;
  268. return 1;
  269. }
  270. else
  271. {
  272. std::cout << "All tests succeeded" << std::endl;
  273. return 0;
  274. }
  275. }