expand_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. //For this test, we need the parameters to be such that the number of compressed ciphertexts needed is 1.
  14. int main(int argc, char *argv[]) {
  15. uint64_t number_of_items = 2048;
  16. uint64_t size_per_item = 288; // in bytes
  17. uint32_t N = 4096;
  18. // Recommended values: (logt, d) = (12, 2) or (8, 1).
  19. uint32_t logt = 20;
  20. uint32_t d = 1;
  21. EncryptionParameters enc_params(scheme_type::bfv);
  22. PirParams pir_params;
  23. // Generates all parameters
  24. cout << "Main: Generating SEAL parameters" << endl;
  25. gen_encryption_params(N, logt, enc_params);
  26. cout << "Main: Verifying SEAL parameters" << endl;
  27. verify_encryption_params(enc_params);
  28. cout << "Main: SEAL parameters are good" << endl;
  29. cout << "Main: Generating PIR parameters" << endl;
  30. gen_pir_params(number_of_items, size_per_item, d, enc_params, pir_params);
  31. //gen_params(number_of_items, size_per_item, N, logt, d, enc_params, pir_params);
  32. print_pir_params(pir_params);
  33. // Initialize PIR Server
  34. cout << "Main: Initializing server and client" << endl;
  35. PIRServer server(enc_params, pir_params);
  36. // Initialize PIR client....
  37. PIRClient client(enc_params, pir_params);
  38. GaloisKeys galois_keys = client.generate_galois_keys();
  39. // Set galois key for client with id 0
  40. cout << "Main: Setting Galois keys...";
  41. server.set_galois_key(0, galois_keys);
  42. random_device rd;
  43. // Choose an index of an element in the DB
  44. uint64_t ele_index = rd() % number_of_items; // element in DB at random position
  45. uint64_t index = client.get_fv_index(ele_index); // index of FV plaintext
  46. uint64_t offset = client.get_fv_offset(ele_index); // offset in FV plaintext
  47. cout << "Main: element index = " << ele_index << " from [0, " << number_of_items -1 << "]" << endl;
  48. cout << "Main: FV index = " << index << ", FV offset = " << offset << endl;
  49. // Measure query generation
  50. auto time_query_s = high_resolution_clock::now();
  51. PirQuery query = client.generate_query(index);
  52. auto time_query_e = high_resolution_clock::now();
  53. auto time_query_us = duration_cast<microseconds>(time_query_e - time_query_s).count();
  54. cout << "Main: query generated" << endl;
  55. // Measure query processing (including expansion)
  56. auto time_server_s = high_resolution_clock::now();
  57. uint64_t n_i = pir_params.nvec[0];
  58. vector<Ciphertext> expanded_query = server.expand_query(query[0][0], n_i, 0);
  59. auto time_server_e = high_resolution_clock::now();
  60. auto time_server_us = duration_cast<microseconds>(time_server_e - time_server_s).count();
  61. cout << "Main: query expanded" << endl;
  62. assert(expanded_query.size() == n_i);
  63. cout << "Main: checking expansion" << endl;
  64. for(size_t i = 0; i < expanded_query.size(); i++){
  65. Plaintext decryption = client.decrypt(expanded_query.at(i));
  66. if(decryption.is_zero() && index != i){
  67. continue;
  68. }
  69. else if(decryption.is_zero()){
  70. cout << "Found zero where index should be" << endl;
  71. return -1;
  72. }
  73. else if (std::stoi(decryption.to_string()) != 1) {
  74. cout << "Query vector at index " << index << " should be 1 but is instead " << decryption.to_string() << endl;
  75. return -1;
  76. } else {
  77. cout << "Query vector at index " << index << " is " << decryption.to_string() << endl;
  78. }
  79. }
  80. return 0;
  81. }