pir_client.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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), pir_params_(pir_params) {
  8. context_ = make_shared<SEALContext>(enc_params, true);
  9. keygen_ = make_unique<KeyGenerator>(*context_);
  10. PublicKey public_key;
  11. keygen_->create_public_key(public_key);
  12. SecretKey secret_key = keygen_->secret_key();
  13. if (pir_params_.enable_symmetric) {
  14. encryptor_ = make_unique<Encryptor>(*context_, secret_key);
  15. } else {
  16. encryptor_ = make_unique<Encryptor>(*context_, public_key);
  17. }
  18. decryptor_ = make_unique<Decryptor>(*context_, secret_key);
  19. evaluator_ = make_unique<Evaluator>(*context_);
  20. encoder_ = make_unique<BatchEncoder>(*context_);
  21. }
  22. int PIRClient::generate_serialized_query(uint64_t desiredIndex,
  23. std::stringstream &stream) {
  24. int N = enc_params_.poly_modulus_degree();
  25. int output_size = 0;
  26. indices_ = compute_indices(desiredIndex, pir_params_.nvec);
  27. Plaintext pt(enc_params_.poly_modulus_degree());
  28. for (uint32_t i = 0; i < indices_.size(); i++) {
  29. uint32_t num_ptxts = ceil((pir_params_.nvec[i] + 0.0) / N);
  30. // initialize result.
  31. cout << "Client: index " << i + 1 << "/ " << indices_.size() << " = "
  32. << 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] =
  46. invert_mod(pow(2, log_total), enc_params_.plain_modulus());
  47. }
  48. if (pir_params_.enable_symmetric) {
  49. output_size += encryptor_->encrypt_symmetric(pt).save(stream);
  50. } else {
  51. output_size += encryptor_->encrypt(pt).save(stream);
  52. }
  53. }
  54. }
  55. return output_size;
  56. }
  57. PirQuery PIRClient::generate_query(uint64_t desiredIndex) {
  58. indices_ = compute_indices(desiredIndex, pir_params_.nvec);
  59. PirQuery result(pir_params_.d);
  60. int N = enc_params_.poly_modulus_degree();
  61. Plaintext pt(enc_params_.poly_modulus_degree());
  62. for (uint32_t i = 0; i < indices_.size(); i++) {
  63. uint32_t num_ptxts = ceil((pir_params_.nvec[i] + 0.0) / N);
  64. // initialize result.
  65. cout << "Client: index " << i + 1 << "/ " << indices_.size() << " = "
  66. << indices_[i] << endl;
  67. cout << "Client: number of ctxts needed for query = " << num_ptxts << endl;
  68. for (uint32_t j = 0; j < num_ptxts; j++) {
  69. pt.set_zero();
  70. if (indices_[i] >= N * j && indices_[i] <= N * (j + 1)) {
  71. uint64_t real_index = indices_[i] - N * j;
  72. uint64_t n_i = pir_params_.nvec[i];
  73. uint64_t total = N;
  74. if (j == num_ptxts - 1) {
  75. total = n_i % N;
  76. }
  77. uint64_t log_total = ceil(log2(total));
  78. cout << "Client: Inverting " << pow(2, log_total) << endl;
  79. pt[real_index] =
  80. invert_mod(pow(2, log_total), enc_params_.plain_modulus());
  81. }
  82. Ciphertext dest;
  83. if (pir_params_.enable_symmetric) {
  84. encryptor_->encrypt_symmetric(pt, dest);
  85. } else {
  86. encryptor_->encrypt(pt, dest);
  87. }
  88. result[i].push_back(dest);
  89. }
  90. }
  91. return result;
  92. }
  93. uint64_t PIRClient::get_fv_index(uint64_t element_index) {
  94. return static_cast<uint64_t>(element_index /
  95. pir_params_.elements_per_plaintext);
  96. }
  97. uint64_t PIRClient::get_fv_offset(uint64_t element_index) {
  98. return element_index % pir_params_.elements_per_plaintext;
  99. }
  100. Plaintext PIRClient::decrypt(Ciphertext ct) {
  101. Plaintext pt;
  102. decryptor_->decrypt(ct, pt);
  103. return pt;
  104. }
  105. vector<uint8_t> PIRClient::decode_reply(PirReply &reply, uint64_t offset) {
  106. Plaintext result = decode_reply(reply);
  107. return extract_bytes(result, offset);
  108. }
  109. vector<uint64_t> PIRClient::extract_coeffs(Plaintext pt) {
  110. vector<uint64_t> coeffs;
  111. encoder_->decode(pt, coeffs);
  112. return coeffs;
  113. }
  114. std::vector<uint64_t> PIRClient::extract_coeffs(seal::Plaintext pt,
  115. uint64_t offset) {
  116. vector<uint64_t> coeffs;
  117. encoder_->decode(pt, coeffs);
  118. uint32_t logt = floor(log2(enc_params_.plain_modulus().value()));
  119. uint64_t coeffs_per_element =
  120. coefficients_per_element(logt, pir_params_.ele_size);
  121. return std::vector<uint64_t>(coeffs.begin() + offset * coeffs_per_element,
  122. coeffs.begin() +
  123. (offset + 1) * coeffs_per_element);
  124. }
  125. std::vector<uint8_t> PIRClient::extract_bytes(seal::Plaintext pt,
  126. uint64_t offset) {
  127. uint32_t N = enc_params_.poly_modulus_degree();
  128. uint32_t logt = floor(log2(enc_params_.plain_modulus().value()));
  129. uint32_t bytes_per_ptxt =
  130. pir_params_.elements_per_plaintext * pir_params_.ele_size;
  131. // Convert from FV plaintext (polynomial) to database element at the client
  132. vector<uint8_t> elems(bytes_per_ptxt);
  133. vector<uint64_t> coeffs;
  134. encoder_->decode(pt, coeffs);
  135. coeffs_to_bytes(logt, coeffs, elems.data(), bytes_per_ptxt,
  136. pir_params_.ele_size);
  137. return std::vector<uint8_t>(elems.begin() + offset * pir_params_.ele_size,
  138. elems.begin() +
  139. (offset + 1) * pir_params_.ele_size);
  140. }
  141. Plaintext PIRClient::decode_reply(PirReply &reply) {
  142. EncryptionParameters parms;
  143. parms_id_type parms_id;
  144. if (pir_params_.enable_mswitching) {
  145. parms = context_->last_context_data()->parms();
  146. parms_id = context_->last_parms_id();
  147. } else {
  148. parms = context_->first_context_data()->parms();
  149. parms_id = context_->first_parms_id();
  150. }
  151. uint32_t exp_ratio = compute_expansion_ratio(parms);
  152. uint32_t recursion_level = pir_params_.d;
  153. vector<Ciphertext> temp = reply;
  154. uint32_t ciphertext_size = temp[0].size();
  155. uint64_t t = enc_params_.plain_modulus().value();
  156. for (uint32_t i = 0; i < recursion_level; i++) {
  157. cout << "Client: " << i + 1 << "/ " << recursion_level
  158. << "-th decryption layer started." << endl;
  159. vector<Ciphertext> newtemp;
  160. vector<Plaintext> tempplain;
  161. for (uint32_t j = 0; j < temp.size(); j++) {
  162. Plaintext ptxt;
  163. decryptor_->decrypt(temp[j], ptxt);
  164. #ifdef DEBUG
  165. cout << "Client: reply noise budget = "
  166. << decryptor_->invariant_noise_budget(temp[j]) << endl;
  167. #endif
  168. // cout << "decoded (and scaled) plaintext = " << ptxt.to_string() <<
  169. // endl;
  170. tempplain.push_back(ptxt);
  171. #ifdef DEBUG
  172. cout << "recursion level : " << i << " noise budget : ";
  173. cout << decryptor_->invariant_noise_budget(temp[j]) << endl;
  174. #endif
  175. if ((j + 1) % (exp_ratio * ciphertext_size) == 0 && j > 0) {
  176. // Combine into one ciphertext.
  177. Ciphertext combined(*context_, parms_id);
  178. compose_to_ciphertext(parms, tempplain, combined);
  179. newtemp.push_back(combined);
  180. tempplain.clear();
  181. // cout << "Client: const term of ciphertext = " << combined[0] << endl;
  182. }
  183. }
  184. cout << "Client: done." << endl;
  185. cout << endl;
  186. if (i == recursion_level - 1) {
  187. assert(temp.size() == 1);
  188. return tempplain[0];
  189. } else {
  190. tempplain.clear();
  191. temp = newtemp;
  192. }
  193. }
  194. // This should never be called
  195. assert(0);
  196. Plaintext fail;
  197. return fail;
  198. }
  199. GaloisKeys PIRClient::generate_galois_keys() {
  200. // Generate the Galois keys needed for coeff_select.
  201. vector<uint32_t> galois_elts;
  202. int N = enc_params_.poly_modulus_degree();
  203. int logN = get_power_of_two(N);
  204. // cout << "printing galois elements...";
  205. for (int i = 0; i < logN; i++) {
  206. galois_elts.push_back((N + exponentiate_uint(2, i)) /
  207. exponentiate_uint(2, i));
  208. //#ifdef DEBUG
  209. // cout << galois_elts.back() << ", ";
  210. //#endif
  211. }
  212. GaloisKeys gal_keys;
  213. keygen_->create_galois_keys(galois_elts, gal_keys);
  214. return gal_keys;
  215. }
  216. Plaintext PIRClient::replace_element(Plaintext pt, vector<uint64_t> new_element,
  217. uint64_t offset) {
  218. vector<uint64_t> coeffs = extract_coeffs(pt);
  219. uint32_t logt = floor(log2(enc_params_.plain_modulus().value()));
  220. uint64_t coeffs_per_element =
  221. coefficients_per_element(logt, pir_params_.ele_size);
  222. assert(new_element.size() == coeffs_per_element);
  223. for (uint64_t i = 0; i < coeffs_per_element; i++) {
  224. coeffs[i + offset * coeffs_per_element] = new_element[i];
  225. }
  226. Plaintext new_pt;
  227. encoder_->encode(coeffs, new_pt);
  228. return new_pt;
  229. }
  230. Ciphertext PIRClient::get_one() {
  231. Plaintext pt("1");
  232. Ciphertext ct;
  233. if (pir_params_.enable_symmetric) {
  234. encryptor_->encrypt_symmetric(pt, ct);
  235. } else {
  236. encryptor_->encrypt(pt, ct);
  237. }
  238. return ct;
  239. }