pir_client.cpp 7.4 KB

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