pir_client.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_index) {
  49. return static_cast<uint64_t>(element_index / pir_params_.elements_per_plaintext);
  50. }
  51. uint64_t PIRClient::get_fv_offset(uint64_t element_index) {
  52. return element_index % pir_params_.elements_per_plaintext;
  53. }
  54. Plaintext PIRClient::decode_reply(PirReply reply) {
  55. uint32_t exp_ratio = pir_params_.expansion_ratio;
  56. uint32_t recursion_level = pir_params_.d;
  57. vector<Ciphertext> temp = reply;
  58. uint64_t t = enc_params_.plain_modulus().value();
  59. for (uint32_t i = 0; i < recursion_level; i++) {
  60. cout << "Client: " << i + 1 << "/ " << recursion_level << "-th decryption layer started." << endl;
  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. #ifdef DEBUG
  67. cout << "Client: reply noise budget = " << decryptor_->invariant_noise_budget(temp[j]) << endl;
  68. #endif
  69. //cout << "decoded (and scaled) plaintext = " << ptxt.to_string() << endl;
  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. tempplain.clear();
  80. // cout << "Client: const term of ciphertext = " << combined[0] << endl;
  81. }
  82. }
  83. cout << "Client: done." << endl;
  84. cout << endl;
  85. if (i == recursion_level - 1) {
  86. assert(temp.size() == 1);
  87. return tempplain[0];
  88. } else {
  89. tempplain.clear();
  90. temp = newtemp;
  91. }
  92. }
  93. // This should never be called
  94. assert(0);
  95. Plaintext fail;
  96. return fail;
  97. }
  98. GaloisKeys PIRClient::generate_galois_keys() {
  99. // Generate the Galois keys needed for coeff_select.
  100. vector<uint32_t> galois_elts;
  101. int N = enc_params_.poly_modulus_degree();
  102. int logN = get_power_of_two(N);
  103. //cout << "printing galois elements...";
  104. for (int i = 0; i < logN; i++) {
  105. galois_elts.push_back((N + exponentiate_uint(2, i)) / exponentiate_uint(2, i));
  106. //#ifdef DEBUG
  107. // cout << galois_elts.back() << ", ";
  108. //#endif
  109. }
  110. GaloisKeys gal_keys;
  111. keygen_->create_galois_keys(galois_elts, gal_keys);
  112. return gal_keys;
  113. }
  114. Ciphertext PIRClient::compose_to_ciphertext(vector<Plaintext> plains) {
  115. size_t encrypted_count = 2;
  116. auto coeff_count = enc_params_.poly_modulus_degree();
  117. auto coeff_mod_count = enc_params_.coeff_modulus().size();
  118. uint64_t plainMod = enc_params_.plain_modulus().value();
  119. int logt = floor(log2(plainMod));
  120. Ciphertext result(*context_);
  121. result.resize(encrypted_count);
  122. // A triple for loop. Going over polys, moduli, and decomposed index.
  123. for (int i = 0; i < encrypted_count; i++) {
  124. uint64_t *encrypted_pointer = result.data(i);
  125. for (int j = 0; j < coeff_mod_count; j++) {
  126. // populate one poly at a time.
  127. // create a polynomial to store the current decomposition value
  128. // which will be copied into the array to populate it at the current
  129. // index.
  130. double logqj = log2(enc_params_.coeff_modulus()[j].value());
  131. int expansion_ratio = ceil(logqj / logt);
  132. uint64_t cur = 1;
  133. // cout << "Client: expansion_ratio = " << expansion_ratio << endl;
  134. for (int k = 0; k < expansion_ratio; k++) {
  135. // Compose here
  136. const uint64_t *plain_coeff =
  137. plains[k + j * (expansion_ratio) + i * (coeff_mod_count * expansion_ratio)]
  138. .data();
  139. for (int m = 0; m < coeff_count; m++) {
  140. if (k == 0) {
  141. *(encrypted_pointer + m + j * coeff_count) = *(plain_coeff + m) * cur;
  142. } else {
  143. *(encrypted_pointer + m + j * coeff_count) += *(plain_coeff + m) * cur;
  144. }
  145. }
  146. cur <<= logt;
  147. }
  148. }
  149. }
  150. return result;
  151. }