main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "pir.hpp"
  2. #include "pir_client.hpp"
  3. #include "pir_server.hpp"
  4. #include <seal/seal.h>
  5. #include <chrono>
  6. #include <memory>
  7. #include <random>
  8. #include <cstdint>
  9. #include <cstddef>
  10. using namespace std::chrono;
  11. using namespace std;
  12. using namespace seal;
  13. int main(int argc, char *argv[]) {
  14. uint64_t number_of_items = 1 << 12;
  15. uint64_t size_per_item = 288; // in bytes
  16. uint32_t N = 4096;
  17. // Recommended values: (logt, d) = (12, 2) or (8, 1).
  18. uint32_t logt = 20;
  19. uint32_t d = 2;
  20. EncryptionParameters enc_params(scheme_type::bfv);
  21. PirParams pir_params;
  22. // Generates all parameters
  23. cout << "Main: Generating SEAL parameters" << endl;
  24. gen_encryption_params(N, logt, enc_params);
  25. cout << "Main: Verifying SEAL parameters" << endl;
  26. verify_encryption_params(enc_params);
  27. cout << "Main: SEAL parameters are good" << endl;
  28. cout << "Main: Generating PIR parameters" << endl;
  29. gen_pir_params(number_of_items, size_per_item, d, enc_params, pir_params);
  30. //gen_params(number_of_items, size_per_item, N, logt, d, enc_params, pir_params);
  31. print_pir_params(pir_params);
  32. cout << "Main: Initializing the database (this may take some time) ..." << endl;
  33. // Create test database
  34. auto db(make_unique<uint8_t[]>(number_of_items * size_per_item));
  35. // Copy of the database. We use this at the end to make sure we retrieved
  36. // the correct element.
  37. auto db_copy(make_unique<uint8_t[]>(number_of_items * size_per_item));
  38. random_device rd;
  39. for (uint64_t i = 0; i < number_of_items; i++) {
  40. for (uint64_t j = 0; j < size_per_item; j++) {
  41. uint8_t val = rd() % 256;
  42. db.get()[(i * size_per_item) + j] = val;
  43. db_copy.get()[(i * size_per_item) + j] = val;
  44. }
  45. }
  46. // Initialize PIR Server
  47. cout << "Main: Initializing server and client" << endl;
  48. PIRServer server(enc_params, pir_params);
  49. // Initialize PIR client....
  50. PIRClient client(enc_params, pir_params);
  51. GaloisKeys galois_keys = client.generate_galois_keys();
  52. // Set galois key for client with id 0
  53. cout << "Main: Setting Galois keys...";
  54. server.set_galois_key(0, galois_keys);
  55. // Measure database setup
  56. auto time_pre_s = high_resolution_clock::now();
  57. server.set_database(move(db), number_of_items, size_per_item);
  58. server.preprocess_database();
  59. cout << "Main: database pre processed " << endl;
  60. auto time_pre_e = high_resolution_clock::now();
  61. auto time_pre_us = duration_cast<microseconds>(time_pre_e - time_pre_s).count();
  62. // Choose an index of an element in the DB
  63. uint64_t ele_index = rd() % number_of_items; // element in DB at random position
  64. uint64_t index = client.get_fv_index(ele_index); // index of FV plaintext
  65. uint64_t offset = client.get_fv_offset(ele_index); // offset in FV plaintext
  66. cout << "Main: element index = " << ele_index << " from [0, " << number_of_items -1 << "]" << endl;
  67. cout << "Main: FV index = " << index << ", FV offset = " << offset << endl;
  68. // Measure query generation
  69. auto time_query_s = high_resolution_clock::now();
  70. PirQuery query = client.generate_query(index);
  71. auto time_query_e = high_resolution_clock::now();
  72. auto time_query_us = duration_cast<microseconds>(time_query_e - time_query_s).count();
  73. cout << "Main: query generated" << endl;
  74. //To marshall query to send over the network, you can use serialize/deserialize:
  75. //std::string query_ser = serialize_query(query);
  76. //PirQuery query2 = deserialize_query(d, 1, query_ser, CIPHER_SIZE);
  77. // Measure query processing (including expansion)
  78. auto time_server_s = high_resolution_clock::now();
  79. PirReply reply = server.generate_reply(query, 0);
  80. auto time_server_e = high_resolution_clock::now();
  81. auto time_server_us = duration_cast<microseconds>(time_server_e - time_server_s).count();
  82. // Measure response extraction
  83. auto time_decode_s = chrono::high_resolution_clock::now();
  84. vector<uint8_t> elems = client.decode_reply(reply, offset);
  85. auto time_decode_e = chrono::high_resolution_clock::now();
  86. auto time_decode_us = duration_cast<microseconds>(time_decode_e - time_decode_s).count();
  87. assert(elems.size() == size_per_item);
  88. bool failed = false;
  89. // Check that we retrieved the correct element
  90. for (uint32_t i = 0; i < size_per_item; i++) {
  91. if (elems[i] != db_copy.get()[(ele_index * size_per_item) + i]) {
  92. cout << "Main: elems " << (int)elems[i] << ", db "
  93. << (int) db_copy.get()[(ele_index * size_per_item) + i] << endl;
  94. cout << "Main: PIR result wrong at " << i << endl;
  95. failed = true;
  96. }
  97. }
  98. if(failed){
  99. return -1;
  100. }
  101. // Output results
  102. cout << "Main: PIR result correct!" << endl;
  103. cout << "Main: PIRServer pre-processing time: " << time_pre_us / 1000 << " ms" << endl;
  104. cout << "Main: PIRClient query generation time: " << time_query_us / 1000 << " ms" << endl;
  105. cout << "Main: PIRServer reply generation time: " << time_server_us / 1000 << " ms" << endl;
  106. cout << "Main: PIRClient answer decode time: " << time_decode_us / 1000 << " ms" << endl;
  107. cout << "Main: Reply num ciphertexts: " << reply.size() << endl;
  108. return 0;
  109. }