pir.hpp 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include "seal/seal.h"
  3. #include "seal/util/polyarithsmallmod.h"
  4. #include <cassert>
  5. #include <cmath>
  6. #include <string>
  7. #include <vector>
  8. typedef std::vector<seal::Plaintext> Database;
  9. typedef std::vector<std::vector<seal::Ciphertext>> PirQuery;
  10. typedef std::vector<seal::Ciphertext> PirReply;
  11. struct PirParams {
  12. bool enable_symmetric;
  13. bool enable_batching;
  14. bool enable_mswitching;
  15. std::uint64_t ele_num;
  16. std::uint64_t ele_size;
  17. std::uint64_t elements_per_plaintext;
  18. std::uint64_t num_of_plaintexts; // number of plaintexts in database
  19. std::uint32_t d; // number of dimensions for the database
  20. std::uint32_t expansion_ratio; // ratio of ciphertext to plaintext
  21. std::vector<std::uint64_t> nvec; // size of each of the d dimensions
  22. std::uint32_t slot_count;
  23. };
  24. void gen_encryption_params(std::uint32_t N, // degree of polynomial
  25. std::uint32_t logt, // bits of plaintext coefficient
  26. seal::EncryptionParameters &enc_params);
  27. void gen_pir_params(uint64_t ele_num,
  28. uint64_t ele_size,
  29. uint32_t d,
  30. const seal::EncryptionParameters &enc_params,
  31. PirParams &pir_params,
  32. bool enable_symmetric = false,
  33. bool enable_batching = true,
  34. bool enable_mswitching = true);
  35. void gen_params(uint64_t ele_num,
  36. uint64_t ele_size,
  37. uint32_t N,
  38. uint32_t logt,
  39. uint32_t d,
  40. seal::EncryptionParameters &params,
  41. PirParams &pir_params);
  42. void verify_encryption_params(const seal::EncryptionParameters &enc_params);
  43. void print_pir_params(const PirParams &pir_params);
  44. void print_seal_params(const seal::EncryptionParameters &enc_params);
  45. // returns the number of plaintexts that the database can hold
  46. std::uint64_t plaintexts_per_db(std::uint32_t logt, std::uint64_t N, std::uint64_t ele_num,
  47. std::uint64_t ele_size);
  48. // returns the number of elements that a single FV plaintext can hold
  49. std::uint64_t elements_per_ptxt(std::uint32_t logt, std::uint64_t N, std::uint64_t ele_size);
  50. // returns the number of coefficients needed to store one element
  51. std::uint64_t coefficients_per_element(std::uint32_t logt, std::uint64_t ele_size);
  52. // Converts an array of bytes to a vector of coefficients, each of which is less
  53. // than the plaintext modulus
  54. std::vector<std::uint64_t> bytes_to_coeffs(std::uint32_t limit, const std::uint8_t *bytes,
  55. std::uint64_t size);
  56. // Converts an array of coefficients into an array of bytes
  57. void coeffs_to_bytes(std::uint32_t limit, const std::vector<std::uint64_t> &coeffs, std::uint8_t *output,
  58. std::uint32_t size_out, std::uint32_t ele_size);
  59. // Takes a vector of coefficients and returns the corresponding FV plaintext
  60. void vector_to_plaintext(const std::vector<std::uint64_t> &coeffs, seal::Plaintext &plain);
  61. // Since the database has d dimensions, and an item is a particular cell
  62. // in the d-dimensional hypercube, this function computes the corresponding
  63. // index for each of the d dimensions
  64. std::vector<std::uint64_t> compute_indices(std::uint64_t desiredIndex,
  65. std::vector<std::uint64_t> nvec);
  66. uint64_t invert_mod(uint64_t m, const seal::Modulus& mod);
  67. uint32_t compute_expansion_ratio(seal::EncryptionParameters params);
  68. std::vector<seal::Plaintext> decompose_to_plaintexts(seal::EncryptionParameters params,
  69. const seal::Ciphertext& ct);
  70. //We need the returned ciphertext to be initialized by Context so the caller will pass it in
  71. void compose_to_ciphertext(seal::EncryptionParameters params,
  72. const std::vector<seal::Plaintext>& pts, seal::Ciphertext& ct);
  73. void compose_to_ciphertext(seal::EncryptionParameters params,
  74. std::vector<seal::Plaintext>::const_iterator pt_iter, seal::Ciphertext& ct);
  75. // Serialize and deserialize galois keys to send them over the network
  76. std::string serialize_galoiskeys(seal::Serializable<seal::GaloisKeys> g);
  77. seal::GaloisKeys *deserialize_galoiskeys(std::string s, std::shared_ptr<seal::SEALContext> context);