pir_client.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. SecretKey secret_key = keygen_->secret_key();
  14. if(pir_params_.enable_symmetric){
  15. encryptor_ = make_unique<Encryptor>(*context_, secret_key);
  16. }
  17. else{
  18. encryptor_ = make_unique<Encryptor>(*context_, public_key);
  19. }
  20. decryptor_ = make_unique<Decryptor>(*context_, secret_key);
  21. evaluator_ = make_unique<Evaluator>(*context_);
  22. encoder_ = make_unique<BatchEncoder>(*context_);
  23. }
  24. PirQuery PIRClient::generate_query(uint64_t desiredIndex) {
  25. indices_ = compute_indices(desiredIndex, pir_params_.nvec);
  26. vector<vector<Ciphertext> > result(pir_params_.d);
  27. int N = enc_params_.poly_modulus_degree();
  28. Plaintext pt(enc_params_.poly_modulus_degree());
  29. for (uint32_t i = 0; i < indices_.size(); i++) {
  30. uint32_t num_ptxts = ceil( (pir_params_.nvec[i] + 0.0) / N);
  31. // initialize result.
  32. cout << "Client: index " << i + 1 << "/ " << indices_.size() << " = " << indices_[i] << endl;
  33. cout << "Client: number of ctxts needed for query = " << num_ptxts << endl;
  34. for (uint32_t j =0; j < num_ptxts; j++){
  35. pt.set_zero();
  36. if (indices_[i] >= N*j && indices_[i] <= N*(j+1)){
  37. uint64_t real_index = indices_[i] - N*j;
  38. uint64_t n_i = pir_params_.nvec[i];
  39. uint64_t total = N;
  40. if (j == num_ptxts - 1){
  41. total = n_i % N;
  42. }
  43. uint64_t log_total = ceil(log2(total));
  44. cout << "Client: Inverting " << pow(2, log_total) << endl;
  45. pt[real_index] = invert_mod(pow(2, log_total), enc_params_.plain_modulus());
  46. }
  47. Ciphertext dest;
  48. if(pir_params_.enable_symmetric){
  49. encryptor_->encrypt_symmetric(pt, dest);
  50. }
  51. else{
  52. encryptor_->encrypt(pt, dest);
  53. }
  54. result[i].push_back(dest);
  55. }
  56. }
  57. return result;
  58. }
  59. uint64_t PIRClient::get_fv_index(uint64_t element_index) {
  60. return static_cast<uint64_t>(element_index / pir_params_.elements_per_plaintext);
  61. }
  62. uint64_t PIRClient::get_fv_offset(uint64_t element_index) {
  63. return element_index % pir_params_.elements_per_plaintext;
  64. }
  65. Plaintext PIRClient::decrypt(Ciphertext ct){
  66. Plaintext pt;
  67. decryptor_->decrypt(ct, pt);
  68. return pt;
  69. }
  70. vector<uint8_t> PIRClient::decode_reply(PirReply reply, uint64_t offset){
  71. Plaintext result = decode_reply(reply);
  72. uint32_t N = enc_params_.poly_modulus_degree();
  73. uint32_t logt = floor(log2(enc_params_.plain_modulus().value()));
  74. // Convert from FV plaintext (polynomial) to database element at the client
  75. vector<uint8_t> elems(N * logt / 8);
  76. vector<uint64_t> coeffs;
  77. encoder_->decode(result, coeffs);
  78. coeffs_to_bytes(logt, coeffs, elems.data(), (N * logt) / 8);
  79. return std::vector<uint8_t>(elems.begin() + offset * pir_params_.ele_size, elems.begin() + (offset + 1) * pir_params_.ele_size);
  80. }
  81. Plaintext PIRClient::decode_reply(PirReply reply) {
  82. uint32_t exp_ratio = pir_params_.expansion_ratio;
  83. uint32_t recursion_level = pir_params_.d;
  84. vector<Ciphertext> temp = reply;
  85. uint64_t t = enc_params_.plain_modulus().value();
  86. for (uint32_t i = 0; i < recursion_level; i++) {
  87. cout << "Client: " << i + 1 << "/ " << recursion_level << "-th decryption layer started." << endl;
  88. vector<Ciphertext> newtemp;
  89. vector<Plaintext> tempplain;
  90. for (uint32_t j = 0; j < temp.size(); j++) {
  91. Plaintext ptxt;
  92. decryptor_->decrypt(temp[j], ptxt);
  93. #ifdef DEBUG
  94. cout << "Client: reply noise budget = " << decryptor_->invariant_noise_budget(temp[j]) << endl;
  95. #endif
  96. //cout << "decoded (and scaled) plaintext = " << ptxt.to_string() << endl;
  97. tempplain.push_back(ptxt);
  98. #ifdef DEBUG
  99. cout << "recursion level : " << i << " noise budget : ";
  100. cout << decryptor_->invariant_noise_budget(temp[j]) << endl;
  101. #endif
  102. if ((j + 1) % exp_ratio == 0 && j > 0) {
  103. // Combine into one ciphertext.
  104. Ciphertext combined = compose_to_ciphertext(tempplain);
  105. newtemp.push_back(combined);
  106. tempplain.clear();
  107. // cout << "Client: const term of ciphertext = " << combined[0] << endl;
  108. }
  109. }
  110. cout << "Client: done." << endl;
  111. cout << endl;
  112. if (i == recursion_level - 1) {
  113. assert(temp.size() == 1);
  114. return tempplain[0];
  115. } else {
  116. tempplain.clear();
  117. temp = newtemp;
  118. }
  119. }
  120. // This should never be called
  121. assert(0);
  122. Plaintext fail;
  123. return fail;
  124. }
  125. GaloisKeys PIRClient::generate_galois_keys() {
  126. // Generate the Galois keys needed for coeff_select.
  127. vector<uint32_t> galois_elts;
  128. int N = enc_params_.poly_modulus_degree();
  129. int logN = get_power_of_two(N);
  130. //cout << "printing galois elements...";
  131. for (int i = 0; i < logN; i++) {
  132. galois_elts.push_back((N + exponentiate_uint(2, i)) / exponentiate_uint(2, i));
  133. //#ifdef DEBUG
  134. // cout << galois_elts.back() << ", ";
  135. //#endif
  136. }
  137. GaloisKeys gal_keys;
  138. keygen_->create_galois_keys(galois_elts, gal_keys);
  139. return gal_keys;
  140. }
  141. Ciphertext PIRClient::compose_to_ciphertext(vector<Plaintext> plains) {
  142. size_t encrypted_count = 2;
  143. auto coeff_count = enc_params_.poly_modulus_degree();
  144. auto coeff_mod_count = enc_params_.coeff_modulus().size();
  145. uint64_t plainMod = enc_params_.plain_modulus().value();
  146. int logt = floor(log2(plainMod));
  147. Ciphertext result(*context_);
  148. result.resize(encrypted_count);
  149. // A triple for loop. Going over polys, moduli, and decomposed index.
  150. for (int i = 0; i < encrypted_count; i++) {
  151. uint64_t *encrypted_pointer = result.data(i);
  152. for (int j = 0; j < coeff_mod_count; j++) {
  153. // populate one poly at a time.
  154. // create a polynomial to store the current decomposition value
  155. // which will be copied into the array to populate it at the current
  156. // index.
  157. double logqj = log2(enc_params_.coeff_modulus()[j].value());
  158. int expansion_ratio = ceil(logqj / logt);
  159. uint64_t cur = 1;
  160. // cout << "Client: expansion_ratio = " << expansion_ratio << endl;
  161. for (int k = 0; k < expansion_ratio; k++) {
  162. // Compose here
  163. const uint64_t *plain_coeff =
  164. plains[k + j * (expansion_ratio) + i * (coeff_mod_count * expansion_ratio)]
  165. .data();
  166. for (int m = 0; m < coeff_count; m++) {
  167. if (k == 0) {
  168. *(encrypted_pointer + m + j * coeff_count) = *(plain_coeff + m) * cur;
  169. } else {
  170. *(encrypted_pointer + m + j * coeff_count) += *(plain_coeff + m) * cur;
  171. }
  172. }
  173. cur <<= logt;
  174. }
  175. }
  176. }
  177. return result;
  178. }