pir_server.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "pir_server.hpp"
  2. #include "pir_client.hpp"
  3. using namespace std;
  4. using namespace seal;
  5. using namespace seal::util;
  6. PIRServer::PIRServer(const EncryptionParameters &enc_params, const PirParams &pir_params) :
  7. enc_params_(enc_params),
  8. pir_params_(pir_params),
  9. is_db_preprocessed_(false)
  10. {
  11. context_ = make_shared<SEALContext>(enc_params, true);
  12. evaluator_ = make_unique<Evaluator>(*context_);
  13. encoder_ = make_unique<BatchEncoder>(*context_);
  14. }
  15. void PIRServer::preprocess_database() {
  16. if (!is_db_preprocessed_) {
  17. for (uint32_t i = 0; i < db_->size(); i++) {
  18. evaluator_->transform_to_ntt_inplace(
  19. db_->operator[](i), context_->first_parms_id());
  20. }
  21. is_db_preprocessed_ = true;
  22. }
  23. }
  24. // Server takes over ownership of db and will free it when it exits
  25. void PIRServer::set_database(unique_ptr<vector<Plaintext>> &&db) {
  26. if (!db) {
  27. throw invalid_argument("db cannot be null");
  28. }
  29. db_ = move(db);
  30. is_db_preprocessed_ = false;
  31. }
  32. void PIRServer::set_database(const std::unique_ptr<const uint8_t[]> &bytes,
  33. uint64_t ele_num, uint64_t ele_size) {
  34. uint32_t logt = floor(log2(enc_params_.plain_modulus().value()));
  35. uint32_t N = enc_params_.poly_modulus_degree();
  36. // number of FV plaintexts needed to represent all elements
  37. uint64_t num_of_plaintexts = pir_params_.num_of_plaintexts;
  38. // number of FV plaintexts needed to create the d-dimensional matrix
  39. uint64_t prod = 1;
  40. for (uint32_t i = 0; i < pir_params_.nvec.size(); i++) {
  41. prod *= pir_params_.nvec[i];
  42. }
  43. uint64_t matrix_plaintexts = prod;
  44. assert(num_of_plaintexts <= matrix_plaintexts);
  45. auto result = make_unique<vector<Plaintext>>();
  46. result->reserve(matrix_plaintexts);
  47. uint64_t ele_per_ptxt = pir_params_.elements_per_plaintext;
  48. uint64_t bytes_per_ptxt = ele_per_ptxt * ele_size;
  49. uint64_t db_size = ele_num * ele_size;
  50. uint64_t coeff_per_ptxt = ele_per_ptxt * coefficients_per_element(logt, ele_size);
  51. assert(coeff_per_ptxt <= N);
  52. uint32_t offset = 0;
  53. for (uint64_t i = 0; i < num_of_plaintexts; i++) {
  54. uint64_t process_bytes = 0;
  55. if (db_size <= offset) {
  56. break;
  57. } else if (db_size < offset + bytes_per_ptxt) {
  58. process_bytes = db_size - offset;
  59. } else {
  60. process_bytes = bytes_per_ptxt;
  61. }
  62. // Get the coefficients of the elements that will be packed in plaintext i
  63. vector<uint64_t> coefficients = bytes_to_coeffs(logt, bytes.get() + offset, process_bytes);
  64. offset += process_bytes;
  65. uint64_t used = coefficients.size();
  66. assert(used <= coeff_per_ptxt);
  67. // Pad the rest with 1s
  68. for (uint64_t j = 0; j < (pir_params_.slot_count - used); j++) {
  69. coefficients.push_back(1);
  70. }
  71. Plaintext plain;
  72. encoder_->encode(coefficients, plain);
  73. // cout << i << "-th encoded plaintext = " << plain.to_string() << endl;
  74. result->push_back(move(plain));
  75. }
  76. // Add padding to make database a matrix
  77. uint64_t current_plaintexts = result->size();
  78. assert(current_plaintexts <= num_of_plaintexts);
  79. #ifdef DEBUG
  80. cout << "adding: " << matrix_plaintexts - current_plaintexts
  81. << " FV plaintexts of padding (equivalent to: "
  82. << (matrix_plaintexts - current_plaintexts) * elements_per_ptxt(logtp, N, ele_size)
  83. << " elements)" << endl;
  84. #endif
  85. vector<uint64_t> padding(N, 1);
  86. for (uint64_t i = 0; i < (matrix_plaintexts - current_plaintexts); i++) {
  87. Plaintext plain;
  88. vector_to_plaintext(padding, plain);
  89. result->push_back(plain);
  90. }
  91. set_database(move(result));
  92. }
  93. void PIRServer::set_galois_key(uint32_t client_id, seal::GaloisKeys galkey) {
  94. galoisKeys_[client_id] = galkey;
  95. }
  96. PirQuery PIRServer::deserialize_query(stringstream &stream) {
  97. PirQuery q;
  98. for (uint32_t i = 0; i < pir_params_.d; i++) {
  99. // number of ciphertexts needed to encode the index for dimension i
  100. // keeping into account that each ciphertext can encode up to poly_modulus_degree indexes
  101. // In most cases this is usually 1.
  102. uint32_t ctx_per_dimension = ceil((pir_params_.nvec[i] + 0.0) / enc_params_.poly_modulus_degree());
  103. vector<Ciphertext> cs;
  104. for (uint32_t j = 0; j < ctx_per_dimension; j++) {
  105. Ciphertext c;
  106. c.load(*context_, stream);
  107. cs.push_back(c);
  108. }
  109. q.push_back(cs);
  110. }
  111. return q;
  112. }
  113. int PIRServer::serialize_reply(PirReply &reply, stringstream &stream) {
  114. int output_size = 0;
  115. for (int i = 0; i < reply.size(); i++){
  116. output_size += reply[i].save(stream);
  117. }
  118. return output_size;
  119. }
  120. PirReply PIRServer::generate_reply(PirQuery &query, uint32_t client_id) {
  121. vector<uint64_t> nvec = pir_params_.nvec;
  122. uint64_t product = 1;
  123. for (uint32_t i = 0; i < nvec.size(); i++) {
  124. product *= nvec[i];
  125. }
  126. auto coeff_count = enc_params_.poly_modulus_degree();
  127. vector<Plaintext> *cur = db_.get();
  128. vector<Plaintext> intermediate_plain; // decompose....
  129. auto pool = MemoryManager::GetPool();
  130. int N = enc_params_.poly_modulus_degree();
  131. int logt = floor(log2(enc_params_.plain_modulus().value()));
  132. for (uint32_t i = 0; i < nvec.size(); i++) {
  133. cout << "Server: " << i + 1 << "-th recursion level started " << endl;
  134. vector<Ciphertext> expanded_query;
  135. uint64_t n_i = nvec[i];
  136. cout << "Server: n_i = " << n_i << endl;
  137. cout << "Server: expanding " << query[i].size() << " query ctxts" << endl;
  138. for (uint32_t j = 0; j < query[i].size(); j++){
  139. uint64_t total = N;
  140. if (j == query[i].size() - 1){
  141. total = n_i % N;
  142. }
  143. cout << "-- expanding one query ctxt into " << total << " ctxts "<< endl;
  144. vector<Ciphertext> expanded_query_part = expand_query(query[i][j], total, client_id);
  145. expanded_query.insert(expanded_query.end(), std::make_move_iterator(expanded_query_part.begin()),
  146. std::make_move_iterator(expanded_query_part.end()));
  147. expanded_query_part.clear();
  148. }
  149. cout << "Server: expansion done " << endl;
  150. if (expanded_query.size() != n_i) {
  151. cout << " size mismatch!!! " << expanded_query.size() << ", " << n_i << endl;
  152. }
  153. // Transform expanded query to NTT, and ...
  154. for (uint32_t jj = 0; jj < expanded_query.size(); jj++) {
  155. evaluator_->transform_to_ntt_inplace(expanded_query[jj]);
  156. }
  157. // Transform plaintext to NTT. If database is pre-processed, can skip
  158. if ((!is_db_preprocessed_) || i > 0) {
  159. for (uint32_t jj = 0; jj < cur->size(); jj++) {
  160. evaluator_->transform_to_ntt_inplace((*cur)[jj], context_->first_parms_id());
  161. }
  162. }
  163. for (uint64_t k = 0; k < product; k++) {
  164. if ((*cur)[k].is_zero()){
  165. cout << k + 1 << "/ " << product << "-th ptxt = 0 " << endl;
  166. }
  167. }
  168. product /= n_i;
  169. vector<Ciphertext> intermediateCtxts(product);
  170. Ciphertext temp;
  171. for (uint64_t k = 0; k < product; k++) {
  172. evaluator_->multiply_plain(expanded_query[0], (*cur)[k], intermediateCtxts[k]);
  173. for (uint64_t j = 1; j < n_i; j++) {
  174. evaluator_->multiply_plain(expanded_query[j], (*cur)[k + j * product], temp);
  175. evaluator_->add_inplace(intermediateCtxts[k], temp); // Adds to first component.
  176. }
  177. }
  178. for (uint32_t jj = 0; jj < intermediateCtxts.size(); jj++) {
  179. evaluator_->transform_from_ntt_inplace(intermediateCtxts[jj]);
  180. // print intermediate ctxts?
  181. //cout << "const term of ctxt " << jj << " = " << intermediateCtxts[jj][0] << endl;
  182. }
  183. if (i == nvec.size() - 1) {
  184. return intermediateCtxts;
  185. } else {
  186. intermediate_plain.clear();
  187. intermediate_plain.reserve(pir_params_.expansion_ratio * product);
  188. cur = &intermediate_plain;
  189. auto tempplain = util::allocate<Plaintext>(
  190. pir_params_.expansion_ratio * product,
  191. pool, coeff_count);
  192. for (uint64_t rr = 0; rr < product; rr++) {
  193. decompose_to_plaintexts_ptr(intermediateCtxts[rr],
  194. tempplain.get() + rr * pir_params_.expansion_ratio, logt);
  195. for (uint32_t jj = 0; jj < pir_params_.expansion_ratio; jj++) {
  196. auto offset = rr * pir_params_.expansion_ratio + jj;
  197. intermediate_plain.emplace_back(tempplain[offset]);
  198. }
  199. }
  200. product *= pir_params_.expansion_ratio; // multiply by expansion rate.
  201. }
  202. cout << "Server: " << i + 1 << "-th recursion level finished " << endl;
  203. cout << endl;
  204. }
  205. cout << "reply generated! " << endl;
  206. // This should never get here
  207. assert(0);
  208. vector<Ciphertext> fail(1);
  209. return fail;
  210. }
  211. inline vector<Ciphertext> PIRServer::expand_query(const Ciphertext &encrypted, uint32_t m,
  212. uint32_t client_id) {
  213. GaloisKeys &galkey = galoisKeys_[client_id];
  214. // Assume that m is a power of 2. If not, round it to the next power of 2.
  215. uint32_t logm = ceil(log2(m));
  216. Plaintext two("2");
  217. vector<int> galois_elts;
  218. auto n = enc_params_.poly_modulus_degree();
  219. if (logm > ceil(log2(n))){
  220. throw logic_error("m > n is not allowed.");
  221. }
  222. for (int i = 0; i < ceil(log2(n)); i++) {
  223. galois_elts.push_back((n + exponentiate_uint(2, i)) / exponentiate_uint(2, i));
  224. }
  225. vector<Ciphertext> temp;
  226. temp.push_back(encrypted);
  227. Ciphertext tempctxt;
  228. Ciphertext tempctxt_rotated;
  229. Ciphertext tempctxt_shifted;
  230. Ciphertext tempctxt_rotatedshifted;
  231. for (uint32_t i = 0; i < logm - 1; i++) {
  232. vector<Ciphertext> newtemp(temp.size() << 1);
  233. // temp[a] = (j0 = a (mod 2**i) ? ) : Enc(x^{j0 - a}) else Enc(0). With
  234. // some scaling....
  235. int index_raw = (n << 1) - (1 << i);
  236. int index = (index_raw * galois_elts[i]) % (n << 1);
  237. for (uint32_t a = 0; a < temp.size(); a++) {
  238. evaluator_->apply_galois(temp[a], galois_elts[i], galkey, tempctxt_rotated);
  239. //cout << "rotate " << client.decryptor_->invariant_noise_budget(tempctxt_rotated) << ", ";
  240. evaluator_->add(temp[a], tempctxt_rotated, newtemp[a]);
  241. multiply_power_of_X(temp[a], tempctxt_shifted, index_raw);
  242. //cout << "mul by x^pow: " << client.decryptor_->invariant_noise_budget(tempctxt_shifted) << ", ";
  243. multiply_power_of_X(tempctxt_rotated, tempctxt_rotatedshifted, index);
  244. // cout << "mul by x^pow: " << client.decryptor_->invariant_noise_budget(tempctxt_rotatedshifted) << ", ";
  245. // Enc(2^i x^j) if j = 0 (mod 2**i).
  246. evaluator_->add(tempctxt_shifted, tempctxt_rotatedshifted, newtemp[a + temp.size()]);
  247. }
  248. temp = newtemp;
  249. /*
  250. cout << "end: ";
  251. for (int h = 0; h < temp.size();h++){
  252. cout << client.decryptor_->invariant_noise_budget(temp[h]) << ", ";
  253. }
  254. cout << endl;
  255. */
  256. }
  257. // Last step of the loop
  258. vector<Ciphertext> newtemp(temp.size() << 1);
  259. int index_raw = (n << 1) - (1 << (logm - 1));
  260. int index = (index_raw * galois_elts[logm - 1]) % (n << 1);
  261. for (uint32_t a = 0; a < temp.size(); a++) {
  262. if (a >= (m - (1 << (logm - 1)))) { // corner case.
  263. evaluator_->multiply_plain(temp[a], two, newtemp[a]); // plain multiplication by 2.
  264. // cout << client.decryptor_->invariant_noise_budget(newtemp[a]) << ", ";
  265. } else {
  266. evaluator_->apply_galois(temp[a], galois_elts[logm - 1], galkey, tempctxt_rotated);
  267. evaluator_->add(temp[a], tempctxt_rotated, newtemp[a]);
  268. multiply_power_of_X(temp[a], tempctxt_shifted, index_raw);
  269. multiply_power_of_X(tempctxt_rotated, tempctxt_rotatedshifted, index);
  270. evaluator_->add(tempctxt_shifted, tempctxt_rotatedshifted, newtemp[a + temp.size()]);
  271. }
  272. }
  273. vector<Ciphertext>::const_iterator first = newtemp.begin();
  274. vector<Ciphertext>::const_iterator last = newtemp.begin() + m;
  275. vector<Ciphertext> newVec(first, last);
  276. return newVec;
  277. }
  278. inline void PIRServer::multiply_power_of_X(const Ciphertext &encrypted, Ciphertext &destination,
  279. uint32_t index) {
  280. auto coeff_mod_count = enc_params_.coeff_modulus().size() - 1;
  281. auto coeff_count = enc_params_.poly_modulus_degree();
  282. auto encrypted_count = encrypted.size();
  283. //cout << "coeff mod count for power of X = " << coeff_mod_count << endl;
  284. //cout << "coeff count for power of X = " << coeff_count << endl;
  285. // First copy over.
  286. destination = encrypted;
  287. // Prepare for destination
  288. // Multiply X^index for each ciphertext polynomial
  289. for (int i = 0; i < encrypted_count; i++) {
  290. for (int j = 0; j < coeff_mod_count; j++) {
  291. negacyclic_shift_poly_coeffmod(encrypted.data(i) + (j * coeff_count),
  292. coeff_count, index,
  293. enc_params_.coeff_modulus()[j],
  294. destination.data(i) + (j * coeff_count));
  295. }
  296. }
  297. }
  298. inline void PIRServer::decompose_to_plaintexts_ptr(const Ciphertext &encrypted, Plaintext *plain_ptr, int logt) {
  299. vector<Plaintext> result;
  300. auto coeff_count = enc_params_.poly_modulus_degree();
  301. auto coeff_mod_count = enc_params_.coeff_modulus().size();
  302. auto encrypted_count = encrypted.size();
  303. uint64_t t1 = 1 << logt; // t1 <= t.
  304. uint64_t t1minusone = t1 -1;
  305. // A triple for loop. Going over polys, moduli, and decomposed index.
  306. for (int i = 0; i < encrypted_count; i++) {
  307. const uint64_t *encrypted_pointer = encrypted.data(i);
  308. for (int j = 0; j < coeff_mod_count; j++) {
  309. // populate one poly at a time.
  310. // create a polynomial to store the current decomposition value
  311. // which will be copied into the array to populate it at the current
  312. // index.
  313. double logqj = log2(enc_params_.coeff_modulus()[j].value());
  314. //int expansion_ratio = ceil(logqj + exponent - 1) / exponent;
  315. int expansion_ratio = ceil(logqj / logt);
  316. // cout << "local expansion ratio = " << expansion_ratio << endl;
  317. uint64_t curexp = 0;
  318. for (int k = 0; k < expansion_ratio; k++) {
  319. // Decompose here
  320. for (int m = 0; m < coeff_count; m++) {
  321. plain_ptr[i * coeff_mod_count * expansion_ratio
  322. + j * expansion_ratio + k][m] =
  323. (*(encrypted_pointer + m + (j * coeff_count)) >> curexp) & t1minusone;
  324. }
  325. curexp += logt;
  326. }
  327. }
  328. }
  329. }
  330. vector<Plaintext> PIRServer::decompose_to_plaintexts(const Ciphertext &encrypted) {
  331. vector<Plaintext> result;
  332. auto coeff_count = enc_params_.poly_modulus_degree();
  333. auto coeff_mod_count = enc_params_.coeff_modulus().size();
  334. auto plain_bit_count = enc_params_.plain_modulus().bit_count();
  335. auto encrypted_count = encrypted.size();
  336. // Generate powers of t.
  337. uint64_t plainMod = enc_params_.plain_modulus().value();
  338. // A triple for loop. Going over polys, moduli, and decomposed index.
  339. for (int i = 0; i < encrypted_count; i++) {
  340. const uint64_t *encrypted_pointer = encrypted.data(i);
  341. for (int j = 0; j < coeff_mod_count; j++) {
  342. // populate one poly at a time.
  343. // create a polynomial to store the current decomposition value
  344. // which will be copied into the array to populate it at the current
  345. // index.
  346. int logqj = log2(enc_params_.coeff_modulus()[j].value());
  347. int expansion_ratio = ceil(logqj / log2(plainMod));
  348. // cout << "expansion ratio = " << expansion_ratio << endl;
  349. uint64_t cur = 1;
  350. for (int k = 0; k < expansion_ratio; k++) {
  351. // Decompose here
  352. Plaintext temp(coeff_count);
  353. transform(encrypted_pointer + (j * coeff_count),
  354. encrypted_pointer + ((j + 1) * coeff_count),
  355. temp.data(),
  356. [cur, &plainMod](auto &in) { return (in / cur) % plainMod; }
  357. );
  358. result.emplace_back(move(temp));
  359. cur *= plainMod;
  360. }
  361. }
  362. }
  363. return result;
  364. }