pir_client.cpp 6.2 KB

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