pir.hpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 slot_count;
  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. void print_seal_params(const seal::EncryptionParameters &enc_params);
  43. // returns the number of plaintexts that the database can hold
  44. std::uint64_t plaintexts_per_db(std::uint32_t logt, std::uint64_t N, std::uint64_t ele_num,
  45. std::uint64_t ele_size);
  46. // returns the number of elements that a single FV plaintext can hold
  47. std::uint64_t elements_per_ptxt(std::uint32_t logt, std::uint64_t N, std::uint64_t ele_size);
  48. // returns the number of coefficients needed to store one element
  49. std::uint64_t coefficients_per_element(std::uint32_t logt, std::uint64_t ele_size);
  50. // Converts an array of bytes to a vector of coefficients, each of which is less
  51. // than the plaintext modulus
  52. std::vector<std::uint64_t> bytes_to_coeffs(std::uint32_t limit, const std::uint8_t *bytes,
  53. std::uint64_t size);
  54. // Converts an array of coefficients into an array of bytes
  55. void coeffs_to_bytes(std::uint32_t logtp, const std::vector<std::uint64_t> &coeffs, std::uint8_t *output,
  56. std::uint32_t size_out);
  57. // Takes a vector of coefficients and returns the corresponding FV plaintext
  58. void vector_to_plaintext(const std::vector<std::uint64_t> &coeffs, 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. // Serialize and deserialize ciphertexts to send them over the network
  66. PirQuery deserialize_query(std::uint32_t d, uint32_t count, std::string s, std::uint32_t len_ciphertext,
  67. std::shared_ptr<seal::SEALContext> context);
  68. std::vector<seal::Ciphertext> deserialize_ciphertexts(std::uint32_t count, std::string s,
  69. std::uint32_t len_ciphertext, std::shared_ptr<seal::SEALContext> context);
  70. std::string serialize_ciphertexts(std::vector<seal::Ciphertext> c);
  71. std::string serialize_query(std::vector<std::vector<seal::Ciphertext>> c);
  72. // Serialize and deserialize galois keys to send them over the network
  73. std::string serialize_galoiskeys(seal::GaloisKeys g);
  74. seal::GaloisKeys *deserialize_galoiskeys(std::string s, std::shared_ptr<seal::SEALContext> context);