pir.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, uint64_t ele_size, uint32_t d,
  28. const seal::EncryptionParameters &enc_params,
  29. PirParams &pir_params, bool enable_symmetric = false,
  30. bool enable_batching = true, bool enable_mswitching = true);
  31. void gen_params(uint64_t ele_num, uint64_t ele_size, uint32_t N, uint32_t logt,
  32. uint32_t d, seal::EncryptionParameters &params,
  33. PirParams &pir_params);
  34. void verify_encryption_params(const seal::EncryptionParameters &enc_params);
  35. void print_pir_params(const PirParams &pir_params);
  36. void print_seal_params(const seal::EncryptionParameters &enc_params);
  37. // returns the number of plaintexts that the database can hold
  38. std::uint64_t plaintexts_per_db(std::uint32_t logt, std::uint64_t N,
  39. std::uint64_t ele_num, std::uint64_t ele_size);
  40. // returns the number of elements that a single FV plaintext can hold
  41. std::uint64_t elements_per_ptxt(std::uint32_t logt, std::uint64_t N,
  42. std::uint64_t ele_size);
  43. // returns the number of coefficients needed to store one element
  44. std::uint64_t coefficients_per_element(std::uint32_t logt,
  45. std::uint64_t ele_size);
  46. // Converts an array of bytes to a vector of coefficients, each of which is less
  47. // than the plaintext modulus
  48. std::vector<std::uint64_t> bytes_to_coeffs(std::uint32_t limit,
  49. const std::uint8_t *bytes,
  50. std::uint64_t size);
  51. // Converts an array of coefficients into an array of bytes
  52. void coeffs_to_bytes(std::uint32_t limit,
  53. const std::vector<std::uint64_t> &coeffs,
  54. std::uint8_t *output, std::uint32_t size_out,
  55. std::uint32_t ele_size);
  56. // Takes a vector of coefficients and returns the corresponding FV plaintext
  57. void vector_to_plaintext(const std::vector<std::uint64_t> &coeffs,
  58. seal::Plaintext &plain);
  59. // Since the database has d dimensions, and an item is a particular cell
  60. // in the d-dimensional hypercube, this function computes the corresponding
  61. // index for each of the d dimensions
  62. std::vector<std::uint64_t> compute_indices(std::uint64_t desiredIndex,
  63. std::vector<std::uint64_t> nvec);
  64. uint64_t invert_mod(uint64_t m, const seal::Modulus &mod);
  65. uint32_t compute_expansion_ratio(seal::EncryptionParameters params);
  66. std::vector<seal::Plaintext>
  67. decompose_to_plaintexts(seal::EncryptionParameters params,
  68. const seal::Ciphertext &ct);
  69. // We need the returned ciphertext to be initialized by Context so the caller
  70. // will pass it in
  71. void compose_to_ciphertext(seal::EncryptionParameters params,
  72. const std::vector<seal::Plaintext> &pts,
  73. seal::Ciphertext &ct);
  74. void compose_to_ciphertext(seal::EncryptionParameters params,
  75. std::vector<seal::Plaintext>::const_iterator pt_iter,
  76. seal::Ciphertext &ct);
  77. // Serialize and deserialize galois keys to send them over the network
  78. std::string serialize_galoiskeys(seal::Serializable<seal::GaloisKeys> g);
  79. seal::GaloisKeys *
  80. deserialize_galoiskeys(std::string s,
  81. std::shared_ptr<seal::SEALContext> context);