pir_server.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include "pir.hpp"
  3. #include "pir_client.hpp"
  4. #include <map>
  5. #include <memory>
  6. #include <vector>
  7. class PIRServer {
  8. public:
  9. PIRServer(const seal::EncryptionParameters &enc_params,
  10. const PirParams &pir_params);
  11. // NOTE: server takes over ownership of db and frees it when it exits.
  12. // Caller cannot free db
  13. void set_database(std::unique_ptr<std::vector<seal::Plaintext>> &&db);
  14. void set_database(const std::unique_ptr<const std::uint8_t[]> &bytes,
  15. std::uint64_t ele_num, std::uint64_t ele_size);
  16. void preprocess_database();
  17. std::vector<seal::Ciphertext> expand_query(const seal::Ciphertext &encrypted,
  18. std::uint32_t m,
  19. std::uint32_t client_id);
  20. PirQuery deserialize_query(std::stringstream &stream);
  21. PirReply generate_reply(PirQuery &query, std::uint32_t client_id);
  22. // Serializes the reply into the provided stream and returns the number of
  23. // bytes written
  24. int serialize_reply(PirReply &reply, std::stringstream &stream);
  25. void set_galois_key(std::uint32_t client_id, seal::GaloisKeys galkey);
  26. // Below simple operations are for interacting with the database WITHOUT PIR.
  27. // So they can be used to modify a particular element in the database or
  28. // to query a particular element (without privacy guarantees).
  29. void simple_set(std::uint64_t index, seal::Plaintext pt);
  30. seal::Ciphertext simple_query(std::uint64_t index);
  31. void set_one_ct(seal::Ciphertext one);
  32. private:
  33. seal::EncryptionParameters enc_params_; // SEAL parameters
  34. PirParams pir_params_; // PIR parameters
  35. std::unique_ptr<Database> db_;
  36. bool is_db_preprocessed_;
  37. std::map<int, seal::GaloisKeys> galoisKeys_;
  38. std::unique_ptr<seal::Evaluator> evaluator_;
  39. std::unique_ptr<seal::BatchEncoder> encoder_;
  40. std::shared_ptr<seal::SEALContext> context_;
  41. // This is only used for simple_query
  42. seal::Ciphertext one_;
  43. void multiply_power_of_X(const seal::Ciphertext &encrypted,
  44. seal::Ciphertext &destination, std::uint32_t index);
  45. };