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