query_test.cpp 5.7 KB

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