pir_server.hpp 1.9 KB

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