pir.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef SEAL_PIR_H
  2. #define SEAL_PIR_H
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <math.h>
  6. #include <chrono>
  7. #include "seal/memorypoolhandle.h"
  8. #include "seal/encryptor.h"
  9. #include "seal/decryptor.h"
  10. #include "seal/encryptionparams.h"
  11. #include "seal/publickey.h"
  12. #include "seal/secretkey.h"
  13. #include "seal/evaluationkeys.h"
  14. #include "seal/galoiskeys.h"
  15. #include "seal/seal.h"
  16. #include "math.h"
  17. #include "seal/util/polyarith.h"
  18. #include "seal/util/uintarith.h"
  19. #include "seal/util/polyarithsmallmod.h"
  20. using namespace std;
  21. using namespace seal;
  22. using namespace seal::util;
  23. typedef std::vector<Plaintext> dataBase;
  24. typedef std::vector<Ciphertext> pirQuery;
  25. typedef std::vector<Ciphertext> pirReply;
  26. vector<Ciphertext> deserialize_ciphertexts(int count, string s, int len_ciphertext);
  27. string serialize_ciphertexts(vector<Ciphertext> c);
  28. string serialize_plaintext(Plaintext p);
  29. string serialize_plaintexts(vector<Plaintext> p);
  30. struct pirParams {
  31. int N;
  32. int size;
  33. int alpha;
  34. int d;
  35. vector<int> Nvec;
  36. int expansion_ratio_;
  37. int dbc;
  38. };
  39. void vector_to_plaintext(const std::vector<std::uint64_t> &coeffs, Plaintext &plain);
  40. vector<int> compute_indices(int desiredIndex, vector<int> Nvec);
  41. class PIRClient {
  42. public:
  43. PIRClient(const seal::EncryptionParameters & parms, pirParams & pirparms);
  44. pirQuery generate_query(int desiredIndex);
  45. Plaintext decode_reply(pirReply reply);
  46. GaloisKeys generate_galois_keys();
  47. void print_info(Ciphertext &encrypted);
  48. EncryptionParameters get_new_parms() {
  49. return newparms_;
  50. }
  51. pirParams get_pir_parms() {
  52. return pirparms_;
  53. }
  54. Ciphertext compose_to_ciphertext(vector<Plaintext> plains);
  55. private:
  56. EncryptionParameters parms_;
  57. EncryptionParameters newparms_;
  58. pirParams pirparms_;
  59. unique_ptr<Encryptor> encryptor_;
  60. unique_ptr<Decryptor> decryptor_;
  61. unique_ptr<Evaluator> evaluator_;
  62. unique_ptr<KeyGenerator> keygen_;
  63. };
  64. class PIRServer
  65. {
  66. public:
  67. PIRServer(const seal::EncryptionParameters &parms, const pirParams &pirparams);
  68. // Reads the database from file.
  69. void read_database_from_file(string file);
  70. // Preprocess the databse
  71. void preprocess_database();
  72. void set_database(vector<Plaintext> *db);
  73. pirReply generate_reply(pirQuery query, int client_id);
  74. vector<Ciphertext> expand_query(const Ciphertext & encrypted, int d, const GaloisKeys &galkey);
  75. void set_galois_key(int client_id, GaloisKeys galkey) {
  76. galoisKeys_[client_id] = galkey;
  77. }
  78. void multiply_power_of_X(const Ciphertext &encrypted, Ciphertext & destination, int index);
  79. void decompose_to_plaintexts_ptr(const Ciphertext & encrypted, uint64_t* plain_ptr);
  80. vector<Plaintext> decompose_to_plaintexts(const Ciphertext &encrypted);
  81. private:
  82. EncryptionParameters parms_;
  83. pirParams pirparams_;
  84. map<int, GaloisKeys> galoisKeys_;
  85. dataBase *dataBase_ = nullptr;
  86. unique_ptr<Evaluator> evaluator_;
  87. bool is_db_preprocessed_;
  88. };
  89. extern "C" {
  90. void cpp_buffer_free(char* buf);
  91. // client-specific methods
  92. void* cpp_client_setup(uint64_t len_db_total_bytes, uint64_t num_db_entries);
  93. char* cpp_client_generate_galois_keys(void *pir);
  94. char* cpp_client_generate_query(void* pir, uint64_t chosen_idx, uint64_t* rlen_query_total_bytes, uint64_t* rnum_query_slots);
  95. char* cpp_client_process_reply(void* pir, char* r, uint64_t len_response_total_bytes, uint64_t num_response_slots, uint64_t* rlen_answer_total_bytes);
  96. void cpp_client_update_db_params(void* pir, uint64_t len_db_total_bytes, uint64_t num_db_entries, uint64_t alpha, uint64_t d);
  97. void cpp_client_free(void* pir);
  98. // server-specific methods
  99. void* cpp_server_setup(uint64_t len_db_total_bytes, char *db, uint64_t num_db_entries);
  100. char* cpp_server_process_query(void* pir, char* q, uint64_t len_query_total_bytes, uint64_t num_query_slots, uint64_t* rlen_response_total_bytes, uint64_t* rnum_response_slots);
  101. void cpp_server_set_galois_keys(void *pir, char *q, uint64_t len_total_bytes, int client_id);
  102. void cpp_server_free(void* pir);
  103. }
  104. #endif