pir_client.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "pir_client.hpp"
  2. using namespace std;
  3. using namespace seal;
  4. using namespace seal::util;
  5. PIRClient::PIRClient(const EncryptionParameters &enc_params,
  6. const PirParams &pir_params) :
  7. enc_params_(enc_params),
  8. pir_params_(pir_params){
  9. context_ = make_shared<SEALContext>(enc_params, true);
  10. keygen_ = make_unique<KeyGenerator>(*context_);
  11. PublicKey public_key;
  12. keygen_->create_public_key(public_key);
  13. encryptor_ = make_unique<Encryptor>(*context_, public_key);
  14. SecretKey secret_key = keygen_->secret_key();
  15. decryptor_ = make_unique<Decryptor>(*context_, secret_key);
  16. evaluator_ = make_unique<Evaluator>(*context_);
  17. }
  18. PirQuery PIRClient::generate_query(uint64_t desiredIndex) {
  19. indices_ = compute_indices(desiredIndex, pir_params_.nvec);
  20. vector<vector<Ciphertext> > result(pir_params_.d);
  21. int N = enc_params_.poly_modulus_degree();
  22. Plaintext pt(enc_params_.poly_modulus_degree());
  23. for (uint32_t i = 0; i < indices_.size(); i++) {
  24. uint32_t num_ptxts = ceil( (pir_params_.nvec[i] + 0.0) / N);
  25. // initialize result.
  26. cout << "Client: index " << i + 1 << "/ " << indices_.size() << " = " << indices_[i] << endl;
  27. cout << "Client: number of ctxts needed for query = " << num_ptxts << endl;
  28. for (uint32_t j =0; j < num_ptxts; j++){
  29. pt.set_zero();
  30. if (indices_[i] >= N*j && indices_[i] <= N*(j+1)){
  31. uint64_t real_index = indices_[i] - N*j;
  32. uint64_t n_i = pir_params_.nvec[i];
  33. uint64_t total = N;
  34. if (j == num_ptxts - 1){
  35. total = n_i % N;
  36. }
  37. uint64_t log_total = ceil(log2(total));
  38. cout << "Client: Inverting " << pow(2, log_total) << endl;
  39. pt[real_index] = invert_mod(pow(2, log_total), enc_params_.plain_modulus());
  40. }
  41. Ciphertext dest;
  42. encryptor_->encrypt(pt, dest);
  43. result[i].push_back(dest);
  44. }
  45. }
  46. return result;
  47. }
  48. uint64_t PIRClient::get_fv_index(uint64_t element_idx, uint64_t ele_size) {
  49. auto N = enc_params_.poly_modulus_degree();
  50. auto logt = floor(log2(enc_params_.plain_modulus().value()));
  51. auto ele_per_ptxt = elements_per_ptxt(logt, 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 = enc_params_.poly_modulus_degree();
  56. uint32_t logt = floor(log2(enc_params_.plain_modulus().value()));
  57. uint64_t ele_per_ptxt = elements_per_ptxt(logt, 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. uint64_t t = enc_params_.plain_modulus().value();
  65. for (uint32_t i = 0; i < recursion_level; i++) {
  66. cout << "Client: " << i + 1 << "/ " << recursion_level << "-th decryption layer started." << endl;
  67. vector<Ciphertext> newtemp;
  68. vector<Plaintext> tempplain;
  69. for (uint32_t j = 0; j < temp.size(); j++) {
  70. Plaintext ptxt;
  71. decryptor_->decrypt(temp[j], ptxt);
  72. #ifdef DEBUG
  73. cout << "Client: reply noise budget = " << decryptor_->invariant_noise_budget(temp[j]) << endl;
  74. #endif
  75. //cout << "decoded (and scaled) plaintext = " << ptxt.to_string() << endl;
  76. tempplain.push_back(ptxt);
  77. #ifdef DEBUG
  78. cout << "recursion level : " << i << " noise budget : ";
  79. cout << decryptor_->invariant_noise_budget(temp[j]) << endl;
  80. #endif
  81. if ((j + 1) % exp_ratio == 0 && j > 0) {
  82. // Combine into one ciphertext.
  83. Ciphertext combined = compose_to_ciphertext(tempplain);
  84. newtemp.push_back(combined);
  85. tempplain.clear();
  86. // cout << "Client: const term of ciphertext = " << combined[0] << endl;
  87. }
  88. }
  89. cout << "Client: done." << endl;
  90. cout << endl;
  91. if (i == recursion_level - 1) {
  92. assert(temp.size() == 1);
  93. return tempplain[0];
  94. } else {
  95. tempplain.clear();
  96. temp = newtemp;
  97. }
  98. }
  99. // This should never be called
  100. assert(0);
  101. Plaintext fail;
  102. return fail;
  103. }
  104. GaloisKeys PIRClient::generate_galois_keys() {
  105. // Generate the Galois keys needed for coeff_select.
  106. vector<uint32_t> galois_elts;
  107. int N = enc_params_.poly_modulus_degree();
  108. int logN = get_power_of_two(N);
  109. //cout << "printing galois elements...";
  110. for (int i = 0; i < logN; i++) {
  111. galois_elts.push_back((N + exponentiate_uint(2, i)) / exponentiate_uint(2, i));
  112. //#ifdef DEBUG
  113. // cout << galois_elts.back() << ", ";
  114. //#endif
  115. }
  116. GaloisKeys gal_keys;
  117. keygen_->create_galois_keys(galois_elts, gal_keys);
  118. return gal_keys;
  119. }
  120. Ciphertext PIRClient::compose_to_ciphertext(vector<Plaintext> plains) {
  121. size_t encrypted_count = 2;
  122. auto coeff_count = enc_params_.poly_modulus_degree();
  123. auto coeff_mod_count = enc_params_.coeff_modulus().size();
  124. uint64_t plainMod = enc_params_.plain_modulus().value();
  125. int logt = floor(log2(plainMod));
  126. Ciphertext result(*context_);
  127. result.resize(encrypted_count);
  128. // A triple for loop. Going over polys, moduli, and decomposed index.
  129. for (int i = 0; i < encrypted_count; i++) {
  130. uint64_t *encrypted_pointer = result.data(i);
  131. for (int j = 0; j < coeff_mod_count; j++) {
  132. // populate one poly at a time.
  133. // create a polynomial to store the current decomposition value
  134. // which will be copied into the array to populate it at the current
  135. // index.
  136. double logqj = log2(enc_params_.coeff_modulus()[j].value());
  137. int expansion_ratio = ceil(logqj / logt);
  138. uint64_t cur = 1;
  139. // cout << "Client: expansion_ratio = " << expansion_ratio << endl;
  140. for (int k = 0; k < expansion_ratio; k++) {
  141. // Compose here
  142. const uint64_t *plain_coeff =
  143. plains[k + j * (expansion_ratio) + i * (coeff_mod_count * expansion_ratio)]
  144. .data();
  145. for (int m = 0; m < coeff_count; m++) {
  146. if (k == 0) {
  147. *(encrypted_pointer + m + j * coeff_count) = *(plain_coeff + m) * cur;
  148. } else {
  149. *(encrypted_pointer + m + j * coeff_count) += *(plain_coeff + m) * cur;
  150. }
  151. }
  152. cur <<= logt;
  153. }
  154. }
  155. }
  156. return result;
  157. }