pir_client.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "pir_client.hpp"
  2. using namespace std;
  3. using namespace seal;
  4. using namespace seal::util;
  5. PIRClient::PIRClient(const EncryptionParameters &params,
  6. const EncryptionParameters &expanded_params, const PirParams &pir_parms) {
  7. params_ = params;
  8. SEALContext context(params);
  9. expanded_params_ = expanded_params;
  10. SEALContext newcontext(expanded_params);
  11. pir_params_ = pir_parms;
  12. keygen_.reset(new KeyGenerator(context));
  13. encryptor_.reset(new Encryptor(context, keygen_->public_key()));
  14. SecretKey secret_key = keygen_->secret_key();
  15. secret_key.mutable_hash_block() = expanded_params.hash_block();
  16. decryptor_.reset(new Decryptor(newcontext, secret_key));
  17. evaluator_.reset(new Evaluator(newcontext));
  18. }
  19. void PIRClient::update_parameters(const EncryptionParameters &expanded_params,
  20. const PirParams &pir_params) {
  21. // The only thing that can change is the plaintext modulus and pir_params
  22. assert(expanded_params.poly_modulus() == expanded_params_.poly_modulus());
  23. assert(expanded_params.coeff_modulus() == expanded_params_.coeff_modulus());
  24. expanded_params_ = expanded_params;
  25. pir_params_ = pir_params;
  26. SEALContext newcontext(expanded_params);
  27. SecretKey secret_key = keygen_->secret_key();
  28. secret_key.mutable_hash_block() = expanded_params.hash_block();
  29. decryptor_.reset(new Decryptor(newcontext, secret_key));
  30. evaluator_.reset(new Evaluator(newcontext));
  31. }
  32. PirQuery PIRClient::generate_query(uint64_t desiredIndex) {
  33. vector<uint64_t> indices = compute_indices(desiredIndex, pir_params_.nvec);
  34. vector<Ciphertext> result;
  35. for (uint32_t i = 0; i < indices.size(); i++) {
  36. Ciphertext dest;
  37. encryptor_->encrypt(Plaintext("1x^" + std::to_string(indices[i])), dest);
  38. dest.mutable_hash_block() = expanded_params_.hash_block();
  39. result.push_back(dest);
  40. }
  41. return result;
  42. }
  43. uint64_t PIRClient::get_fv_index(uint64_t element_idx, uint64_t ele_size) {
  44. uint32_t N = params_.poly_modulus().coeff_count() - 1;
  45. uint32_t logtp = ceil(log2(expanded_params_.plain_modulus().value()));
  46. uint64_t ele_per_ptxt = elements_per_ptxt(logtp, N, ele_size);
  47. return element_idx / ele_per_ptxt;
  48. }
  49. uint64_t PIRClient::get_fv_offset(uint64_t element_idx, uint64_t ele_size) {
  50. uint32_t N = params_.poly_modulus().coeff_count() - 1;
  51. uint32_t logtp = ceil(log2(expanded_params_.plain_modulus().value()));
  52. uint64_t ele_per_ptxt = elements_per_ptxt(logtp, N, ele_size);
  53. return element_idx % ele_per_ptxt;
  54. }
  55. Plaintext PIRClient::decode_reply(PirReply reply) {
  56. uint32_t exp_ratio = pir_params_.expansion_ratio;
  57. uint32_t recursion_level = pir_params_.d;
  58. vector<Ciphertext> temp = reply;
  59. for (uint32_t i = 0; i < recursion_level; i++) {
  60. vector<Ciphertext> newtemp;
  61. vector<Plaintext> tempplain;
  62. for (uint32_t j = 0; j < temp.size(); j++) {
  63. Plaintext ptxt;
  64. decryptor_->decrypt(temp[j], ptxt);
  65. tempplain.push_back(ptxt);
  66. #ifdef DEBUG
  67. cout << "recursion level : " << i << " noise budget : ";
  68. cout << decryptor_->invariant_noise_budget(temp[j]) << endl;
  69. #endif
  70. if ((j + 1) % exp_ratio == 0 && j > 0) {
  71. // Combine into one ciphertext.
  72. Ciphertext combined = compose_to_ciphertext(tempplain);
  73. newtemp.push_back(combined);
  74. }
  75. }
  76. if (i == recursion_level - 1) {
  77. assert(temp.size() == 1);
  78. return tempplain[0];
  79. } else {
  80. tempplain.clear();
  81. temp = newtemp;
  82. }
  83. }
  84. // This should never be called
  85. assert(0);
  86. Plaintext fail;
  87. return fail;
  88. }
  89. GaloisKeys PIRClient::generate_galois_keys() {
  90. // Generate the Galois keys needed for coeff_select.
  91. vector<uint64_t> galois_elts;
  92. int N = params_.poly_modulus().coeff_count() - 1;
  93. int logN = get_power_of_two(N);
  94. for (int i = 0; i < logN; i++) {
  95. galois_elts.push_back((N + exponentiate_uint64(2, i)) / exponentiate_uint64(2, i));
  96. #ifdef DEBUG
  97. cout << galois_elts.back() << ", ";
  98. #endif
  99. }
  100. GaloisKeys galois_keys;
  101. keygen_->generate_galois_keys(pir_params_.dbc, galois_elts, galois_keys);
  102. return galois_keys;
  103. }
  104. Ciphertext PIRClient::compose_to_ciphertext(vector<Plaintext> plains) {
  105. int encrypted_count = 2;
  106. int coeff_count = expanded_params_.poly_modulus().coeff_count();
  107. int coeff_mod_count = expanded_params_.coeff_modulus().size();
  108. uint64_t plainMod = expanded_params_.plain_modulus().value();
  109. Ciphertext result;
  110. result.reserve(expanded_params_, encrypted_count);
  111. // A triple for loop. Going over polys, moduli, and decomposed index.
  112. for (int i = 0; i < encrypted_count; i++) {
  113. uint64_t *encrypted_pointer = result.mutable_pointer(i);
  114. for (int j = 0; j < coeff_mod_count; j++) {
  115. // populate one poly at a time.
  116. // create a polynomial to store the current decomposition value
  117. // which will be copied into the array to populate it at the current
  118. // index.
  119. double logqj = log2(expanded_params_.coeff_modulus()[j].value());
  120. int expansion_ratio = ceil(logqj / log2(plainMod));
  121. // cout << "expansion ratio = " << expansion_ratio << endl;
  122. uint64_t cur = 1;
  123. for (int k = 0; k < expansion_ratio; k++) {
  124. // Compose here
  125. const uint64_t *plain_coeff =
  126. plains[k + j * (expansion_ratio) + i * (coeff_mod_count * expansion_ratio)]
  127. .pointer();
  128. for (int m = 0; m < coeff_count - 1; m++) {
  129. if (k == 0) {
  130. *(encrypted_pointer + m + j * coeff_count) = *(plain_coeff + m) * cur;
  131. } else {
  132. *(encrypted_pointer + m + j * coeff_count) += *(plain_coeff + m) * cur;
  133. }
  134. }
  135. *(encrypted_pointer + coeff_count - 1 + j * coeff_count) = 0;
  136. cur *= plainMod;
  137. }
  138. // XXX: Reduction modulo qj. This is needed?
  139. /*
  140. for (int m = 0; m < coeff_count; m++) {
  141. *(encrypted_pointer + m + j * coeff_count) %=
  142. expanded_params_.coeff_modulus()[j].value();
  143. }
  144. */
  145. }
  146. }
  147. result.mutable_hash_block() = expanded_params_.hash_block();
  148. return result;
  149. }