123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "pir.hpp"
- #include "pir_client.hpp"
- #include "pir_server.hpp"
- #include <seal/seal.h>
- #include <chrono>
- #include <memory>
- #include <random>
- #include <cstdint>
- #include <cstddef>
- using namespace std::chrono;
- using namespace std;
- using namespace seal;
- //For this test, we need the parameters to be such that the number of compressed ciphertexts needed is 1.
- int main(int argc, char *argv[]) {
- uint64_t number_of_items = 2048;
- uint64_t size_per_item = 288; // in bytes
- uint32_t N = 4096;
- // Recommended values: (logt, d) = (12, 2) or (8, 1).
- uint32_t logt = 20;
- uint32_t d = 1;
- EncryptionParameters enc_params(scheme_type::bfv);
- PirParams pir_params;
- // Generates all parameters
-
- cout << "Main: Generating SEAL parameters" << endl;
- gen_encryption_params(N, logt, enc_params);
-
- cout << "Main: Verifying SEAL parameters" << endl;
- verify_encryption_params(enc_params);
- cout << "Main: SEAL parameters are good" << endl;
- cout << "Main: Generating PIR parameters" << endl;
- gen_pir_params(number_of_items, size_per_item, d, enc_params, pir_params);
-
-
-
- //gen_params(number_of_items, size_per_item, N, logt, d, enc_params, pir_params);
- print_pir_params(pir_params);
- // Initialize PIR Server
- cout << "Main: Initializing server and client" << endl;
- PIRServer server(enc_params, pir_params);
- // Initialize PIR client....
- PIRClient client(enc_params, pir_params);
- GaloisKeys galois_keys = client.generate_galois_keys();
- // Set galois key for client with id 0
- cout << "Main: Setting Galois keys...";
- server.set_galois_key(0, galois_keys);
- random_device rd;
- // Choose an index of an element in the DB
- uint64_t ele_index = rd() % number_of_items; // element in DB at random position
- uint64_t index = client.get_fv_index(ele_index); // index of FV plaintext
- uint64_t offset = client.get_fv_offset(ele_index); // offset in FV plaintext
- cout << "Main: element index = " << ele_index << " from [0, " << number_of_items -1 << "]" << endl;
- cout << "Main: FV index = " << index << ", FV offset = " << offset << endl;
- // Measure query generation
- auto time_query_s = high_resolution_clock::now();
- PirQuery query = client.generate_query(index);
- auto time_query_e = high_resolution_clock::now();
- auto time_query_us = duration_cast<microseconds>(time_query_e - time_query_s).count();
- cout << "Main: query generated" << endl;
- // Measure query processing (including expansion)
- auto time_server_s = high_resolution_clock::now();
- uint64_t n_i = pir_params.nvec[0];
- vector<Ciphertext> expanded_query = server.expand_query(query[0][0], n_i, 0);
- auto time_server_e = high_resolution_clock::now();
- auto time_server_us = duration_cast<microseconds>(time_server_e - time_server_s).count();
- cout << "Main: query expanded" << endl;
- assert(expanded_query.size() == n_i);
- cout << "Main: checking expansion" << endl;
- for(size_t i = 0; i < expanded_query.size(); i++){
- Plaintext decryption = client.decrypt(expanded_query.at(i));
-
- if(decryption.is_zero() && index != i){
- continue;
- }
- else if(decryption.is_zero()){
- cout << "Found zero where index should be" << endl;
- return 1;
- }
- else{
- cout << "Plaintext of query vector at index " << index << " should have value 1 (check below)" << endl;
- cout << "Plaintext of query vector at index " << i << " has value " << decryption.to_string() << endl;
- }
- }
- return 0;
- }
|