simple_query_test.cpp 4.8 KB

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