simple_query_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 simple_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(simple_query_test(1 << 10, 288, 4096, 20, 1) == 0);
  17. // Forces ciphertext expansion to be the same as the degree
  18. assert(simple_query_test(1 << 20, 288, 4096, 20, 1) == 0);
  19. assert(simple_query_test(1 << 20, 288, 4096, 20, 2) == 0);
  20. }
  21. int simple_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. Ciphertext one_ct = client.get_one();
  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. server.set_one_ct(one_ct);
  65. cout << "Main: database pre processed " << endl;
  66. auto time_pre_e = high_resolution_clock::now();
  67. auto time_pre_us = duration_cast<microseconds>(time_pre_e - time_pre_s).count();
  68. // Choose an index of an element in the DB
  69. uint64_t ele_index = rd() % number_of_items; // element in DB at random position
  70. uint64_t index = client.get_fv_index(ele_index); // index of FV plaintext
  71. uint64_t offset = client.get_fv_offset(ele_index); // offset in FV plaintext
  72. cout << "Main: element index = " << ele_index << " from [0, " << number_of_items -1 << "]" << endl;
  73. cout << "Main: FV index = " << index << ", FV offset = " << offset << endl;
  74. // Measure query processing (including expansion)
  75. auto time_server_s = high_resolution_clock::now();
  76. Ciphertext reply = server.simple_query(index);
  77. auto time_server_e = high_resolution_clock::now();
  78. auto time_server_us = duration_cast<microseconds>(time_server_e - time_server_s).count();
  79. // Measure response extraction
  80. auto time_decode_s = chrono::high_resolution_clock::now();
  81. vector<uint8_t> elems = client.extract_bytes(client.decrypt(reply), offset);
  82. auto time_decode_e = chrono::high_resolution_clock::now();
  83. auto time_decode_us = duration_cast<microseconds>(time_decode_e - time_decode_s).count();
  84. assert(elems.size() == size_per_item);
  85. bool failed = false;
  86. // Check that we retrieved the correct element
  87. for (uint32_t i = 0; i < size_per_item; i++) {
  88. if (elems[i] != db_copy.get()[(ele_index * size_per_item) + i]) {
  89. cout << "Main: elems " << (int)elems[i] << ", db "
  90. << (int) db_copy.get()[(ele_index * size_per_item) + i] << endl;
  91. cout << "Main: PIR result wrong at " << i << endl;
  92. failed = true;
  93. }
  94. }
  95. if(failed){
  96. return -1;
  97. }
  98. // Output results
  99. cout << "Main: PIR result correct!" << endl;
  100. cout << "Main: PIRServer pre-processing time: " << time_pre_us / 1000 << " ms" << endl;
  101. cout << "Main: PIRServer reply generation time: " << time_server_us / 1000 << " ms" << endl;
  102. cout << "Main: PIRClient answer decode time: " << time_decode_us / 1000 << " ms" << endl;
  103. return 0;
  104. }