pir_client.cpp 6.4 KB

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