pir.hpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #define CIPHER_SIZE 32841
  9. typedef std::vector<seal::Plaintext> Database;
  10. typedef std::vector< std::vector< seal::Ciphertext >> PirQuery;
  11. typedef std::vector<seal::Ciphertext> PirReply;
  12. struct PirParams {
  13. std::uint64_t n; // number of plaintexts in database
  14. std::uint32_t d; // number of dimensions for the database (1 or 2)
  15. std::uint32_t expansion_ratio; // ratio of ciphertext to plaintext
  16. std::uint32_t dbc; // decomposition bit count (used by relinearization)
  17. std::vector<std::uint64_t> nvec; // size of each of the d dimensions
  18. };
  19. void gen_params(std::uint64_t ele_num, // number of elements (not FV plaintexts) in database
  20. std::uint64_t ele_size, // size of each element
  21. std::uint32_t N, // degree of polynomial
  22. std::uint32_t logt, // bits of plaintext coefficient
  23. std::uint32_t d, // dimension of database
  24. seal::EncryptionParameters &params,
  25. PirParams &pir_params);
  26. void update_params(std::uint64_t ele_num,
  27. std::uint64_t ele_size,
  28. std::uint32_t d,
  29. const seal::EncryptionParameters &old_params, PirParams &pir_params);
  30. // returns the plaintext modulus after expansion
  31. std::uint32_t plainmod_after_expansion(std::uint32_t logt, std::uint32_t N,
  32. std::uint32_t d, std::uint64_t ele_num,
  33. std::uint64_t ele_size);
  34. // returns the number of plaintexts that the database can hold
  35. std::uint64_t plaintexts_per_db(std::uint32_t logtp, std::uint64_t N, std::uint64_t ele_num,
  36. std::uint64_t ele_size);
  37. // returns the number of elements that a single FV plaintext can hold
  38. std::uint64_t elements_per_ptxt(std::uint32_t logtp, std::uint64_t N, std::uint64_t ele_size);
  39. // returns the number of coefficients needed to store one element
  40. std::uint64_t coefficients_per_element(std::uint32_t logtp, std::uint64_t ele_size);
  41. // Converts an array of bytes to a vector of coefficients, each of which is less
  42. // than the plaintext modulus
  43. std::vector<std::uint64_t> bytes_to_coeffs(std::uint32_t limit, const std::uint8_t *bytes,
  44. std::uint64_t size);
  45. // Converts an array of coefficients into an array of bytes
  46. void coeffs_to_bytes(std::uint32_t logtp, const seal::Plaintext &coeffs, std::uint8_t *output,
  47. std::uint32_t size_out);
  48. // Takes a vector of coefficients and returns the corresponding FV plaintext
  49. void vector_to_plaintext(const std::vector<std::uint64_t> &coeffs, seal::Plaintext &plain);
  50. // Since the database has d dimensions, and an item is a particular cell
  51. // in the d-dimensional hypercube, this function computes the corresponding
  52. // index for each of the d dimensions
  53. std::vector<std::uint64_t> compute_indices(std::uint64_t desiredIndex,
  54. std::vector<std::uint64_t> nvec);
  55. // Serialize and deserialize ciphertexts to send them over the network
  56. std::vector<seal::Ciphertext> deserialize_ciphertexts(std::uint32_t count, std::string s,
  57. std::uint32_t len_ciphertext);
  58. std::string serialize_ciphertexts(std::vector<seal::Ciphertext> c);
  59. // Serialize and deserialize galois keys to send them over the network
  60. std::string serialize_galoiskeys(seal::GaloisKeys g);
  61. seal::GaloisKeys *deserialize_galoiskeys(std::string s);