main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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) = (20, 2).
  18. uint32_t logt = 20;
  19. uint32_t d = 2;
  20. bool use_symmetric = true; // use symmetric encryption instead of public key (recommended)
  21. bool use_batching = true; // pack as many elements as possible into a BFV plaintext (recommended)
  22. EncryptionParameters enc_params(scheme_type::bfv);
  23. PirParams pir_params;
  24. // Generates all parameters
  25. cout << "Main: Generating SEAL parameters" << endl;
  26. gen_encryption_params(N, logt, enc_params);
  27. cout << "Main: Verifying SEAL parameters" << endl;
  28. verify_encryption_params(enc_params);
  29. cout << "Main: SEAL parameters are good" << endl;
  30. cout << "Main: Generating PIR parameters" << endl;
  31. gen_pir_params(number_of_items, size_per_item, d, enc_params, pir_params, use_symmetric, use_batching);
  32. print_seal_params(enc_params);
  33. print_pir_params(pir_params);
  34. // Initialize PIR client....
  35. PIRClient client(enc_params, pir_params);
  36. cout << "Main: Generating galois keys for client" << endl;
  37. GaloisKeys galois_keys = client.generate_galois_keys();
  38. // Initialize PIR Server
  39. cout << "Main: Initializing server" << endl;
  40. PIRServer server(enc_params, pir_params);
  41. // Server maps the galois key to client 0. We only have 1 client,
  42. // which is why we associate it with 0. If there are multiple PIR
  43. // clients, you should have each client generate a galois key,
  44. // and assign each client an index or id, then call the procedure below.
  45. server.set_galois_key(0, galois_keys);
  46. cout << "Main: Creating the database with random data (this may take some time) ..." << endl;
  47. // Create test database
  48. auto db(make_unique<uint8_t[]>(number_of_items * size_per_item));
  49. // Copy of the database. We use this at the end to make sure we retrieved
  50. // the correct element.
  51. auto db_copy(make_unique<uint8_t[]>(number_of_items * size_per_item));
  52. random_device rd;
  53. for (uint64_t i = 0; i < number_of_items; i++) {
  54. for (uint64_t j = 0; j < size_per_item; j++) {
  55. uint8_t val = rd() % 256;
  56. db.get()[(i * size_per_item) + j] = val;
  57. db_copy.get()[(i * size_per_item) + j] = val;
  58. }
  59. }
  60. // Measure database setup
  61. auto time_pre_s = high_resolution_clock::now();
  62. server.set_database(move(db), number_of_items, size_per_item);
  63. server.preprocess_database();
  64. cout << "Main: database pre processed " << endl;
  65. auto time_pre_e = high_resolution_clock::now();
  66. auto time_pre_us = duration_cast<microseconds>(time_pre_e - time_pre_s).count();
  67. // Choose an index of an element in the DB
  68. uint64_t ele_index = rd() % number_of_items; // element in DB at random position
  69. uint64_t index = client.get_fv_index(ele_index); // index of FV plaintext
  70. uint64_t offset = client.get_fv_offset(ele_index); // offset in FV plaintext
  71. cout << "Main: element index = " << ele_index << " from [0, " << number_of_items -1 << "]" << endl;
  72. cout << "Main: FV index = " << index << ", FV offset = " << offset << endl;
  73. // Measure query generation
  74. auto time_query_s = high_resolution_clock::now();
  75. PirQuery query = client.generate_query(index);
  76. auto time_query_e = high_resolution_clock::now();
  77. auto time_query_us = duration_cast<microseconds>(time_query_e - time_query_s).count();
  78. cout << "Main: query generated" << endl;
  79. //To marshall query to send over the network, you can use serialize/deserialize:
  80. //std::string query_ser = serialize_query(query);
  81. //PirQuery query2 = deserialize_query(d, 1, query_ser, CIPHER_SIZE);
  82. // Measure query processing (including expansion)
  83. auto time_server_s = high_resolution_clock::now();
  84. // Answer PIR query form client 0. If there are multiple clients,
  85. // enter the id of the client (to use the associated galois key).
  86. PirReply reply = server.generate_reply(query, 0);
  87. auto time_server_e = high_resolution_clock::now();
  88. auto time_server_us = duration_cast<microseconds>(time_server_e - time_server_s).count();
  89. cout << "Main: reply generated" << endl;
  90. // Measure response extraction
  91. auto time_decode_s = chrono::high_resolution_clock::now();
  92. vector<uint8_t> elems = client.decode_reply(reply, offset);
  93. auto time_decode_e = chrono::high_resolution_clock::now();
  94. auto time_decode_us = duration_cast<microseconds>(time_decode_e - time_decode_s).count();
  95. cout << "Main: reply decoded" << endl;
  96. assert(elems.size() == size_per_item);
  97. bool failed = false;
  98. // Check that we retrieved the correct element
  99. for (uint32_t i = 0; i < size_per_item; i++) {
  100. if (elems[i] != db_copy.get()[(ele_index * size_per_item) + i]) {
  101. cout << "Main: elems " << (int)elems[i] << ", db "
  102. << (int) db_copy.get()[(ele_index * size_per_item) + i] << endl;
  103. cout << "Main: PIR result wrong at " << i << endl;
  104. failed = true;
  105. }
  106. }
  107. if(failed){
  108. return -1;
  109. }
  110. // Output results
  111. cout << "Main: PIR result correct!" << endl;
  112. cout << "Main: PIRServer pre-processing time: " << time_pre_us / 1000 << " ms" << endl;
  113. cout << "Main: PIRClient query generation time: " << time_query_us / 1000 << " ms" << endl;
  114. cout << "Main: PIRServer reply generation time: " << time_server_us / 1000 << " ms" << endl;
  115. cout << "Main: PIRClient answer decode time: " << time_decode_us / 1000 << " ms" << endl;
  116. cout << "Main: Reply num ciphertexts: " << reply.size() << endl;
  117. return 0;
  118. }