pir.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. std::uint64_t ele_num;
  15. std::uint64_t ele_size;
  16. std::uint64_t elements_per_plaintext;
  17. std::uint64_t num_of_plaintexts; // number of plaintexts in database
  18. std::uint32_t d; // number of dimensions for the database
  19. std::uint32_t expansion_ratio; // ratio of ciphertext to plaintext
  20. std::vector<std::uint64_t> nvec; // size of each of the d dimensions
  21. std::uint32_t n;
  22. };
  23. void gen_encryption_params(std::uint32_t N, // degree of polynomial
  24. std::uint32_t logt, // bits of plaintext coefficient
  25. seal::EncryptionParameters &enc_params);
  26. void gen_pir_params(uint64_t ele_num,
  27. uint64_t ele_size,
  28. uint32_t d,
  29. const seal::EncryptionParameters &enc_params,
  30. PirParams &pir_params,
  31. bool enable_symmetric = false,
  32. bool enable_batching = true);
  33. void gen_params(uint64_t ele_num,
  34. uint64_t ele_size,
  35. uint32_t N,
  36. uint32_t logt,
  37. uint32_t d,
  38. seal::EncryptionParameters &params,
  39. PirParams &pir_params);
  40. void verify_encryption_params(const seal::EncryptionParameters &enc_params);
  41. void print_pir_params(const PirParams &pir_params);
  42. // returns the plaintext modulus after expansion
  43. std::uint32_t plainmod_after_expansion(std::uint32_t logt, std::uint32_t N,
  44. std::uint32_t d, std::uint64_t ele_num,
  45. std::uint64_t ele_size);
  46. // returns the number of plaintexts that the database can hold
  47. std::uint64_t plaintexts_per_db(std::uint32_t logtp, std::uint64_t N, std::uint64_t ele_num,
  48. std::uint64_t ele_size);
  49. // returns the number of elements that a single FV plaintext can hold
  50. std::uint64_t elements_per_ptxt(std::uint32_t logtp, std::uint64_t N, std::uint64_t ele_size);
  51. // returns the number of coefficients needed to store one element
  52. std::uint64_t coefficients_per_element(std::uint32_t logtp, std::uint64_t ele_size);
  53. // Converts an array of bytes to a vector of coefficients, each of which is less
  54. // than the plaintext modulus
  55. std::vector<std::uint64_t> bytes_to_coeffs(std::uint32_t limit, const std::uint8_t *bytes,
  56. std::uint64_t size);
  57. // Converts an array of coefficients into an array of bytes
  58. void coeffs_to_bytes(std::uint32_t logtp, const seal::Plaintext &coeffs, std::uint8_t *output,
  59. std::uint32_t size_out);
  60. // Takes a vector of coefficients and returns the corresponding FV plaintext
  61. void vector_to_plaintext(const std::vector<std::uint64_t> &coeffs, seal::Plaintext &plain);
  62. // Since the database has d dimensions, and an item is a particular cell
  63. // in the d-dimensional hypercube, this function computes the corresponding
  64. // index for each of the d dimensions
  65. std::vector<std::uint64_t> compute_indices(std::uint64_t desiredIndex,
  66. std::vector<std::uint64_t> nvec);
  67. uint64_t invert_mod(uint64_t m, const seal::Modulus& mod);
  68. // Serialize and deserialize ciphertexts to send them over the network
  69. PirQuery deserialize_query(std::uint32_t d, uint32_t count, std::string s, std::uint32_t len_ciphertext,
  70. std::shared_ptr<seal::SEALContext> context);
  71. std::vector<seal::Ciphertext> deserialize_ciphertexts(std::uint32_t count, std::string s,
  72. std::uint32_t len_ciphertext, std::shared_ptr<seal::SEALContext> context);
  73. std::string serialize_ciphertexts(std::vector<seal::Ciphertext> c);
  74. std::string serialize_query(std::vector<std::vector<seal::Ciphertext>> c);
  75. // Serialize and deserialize galois keys to send them over the network
  76. std::string serialize_galoiskeys(seal::GaloisKeys g);
  77. seal::GaloisKeys *deserialize_galoiskeys(std::string s, std::shared_ptr<seal::SEALContext> context);