pir_server.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "pir_server.hpp"
  2. using namespace std;
  3. using namespace seal;
  4. using namespace seal::util;
  5. PIRServer::PIRServer(const EncryptionParameters &expanded_params, const PirParams &pir_params) {
  6. expanded_params_ = expanded_params;
  7. pir_params_ = pir_params;
  8. SEALContext context(expanded_params);
  9. evaluator_.reset(new Evaluator(context));
  10. is_db_preprocessed_ = false;
  11. }
  12. PIRServer::~PIRServer() {
  13. delete db_;
  14. }
  15. void PIRServer::update_parameters(const EncryptionParameters &expanded_params,
  16. const PirParams &pir_params) {
  17. // The only thing that can change is the plaintext modulus and pir_params
  18. assert(expanded_params.poly_modulus() == expanded_params_.poly_modulus());
  19. assert(expanded_params.coeff_modulus() == expanded_params_.coeff_modulus());
  20. expanded_params_ = expanded_params;
  21. pir_params_ = pir_params;
  22. SEALContext context(expanded_params);
  23. evaluator_.reset(new Evaluator(context));
  24. is_db_preprocessed_ = false;
  25. // Update all the galois keys
  26. for (std::pair<const int, GaloisKeys> &key : galoisKeys_) {
  27. key.second.mutable_hash_block() = expanded_params_.hash_block();
  28. }
  29. }
  30. void PIRServer::preprocess_database() {
  31. if (!is_db_preprocessed_) {
  32. for (uint32_t i = 0; i < db_->size(); i++) {
  33. evaluator_->transform_to_ntt(db_->operator[](i));
  34. }
  35. is_db_preprocessed_ = true;
  36. }
  37. }
  38. // Server takes over ownership of db and will free it when it exits
  39. void PIRServer::set_database(vector<Plaintext> *db) {
  40. if (db == nullptr) {
  41. throw invalid_argument("db cannot be null");
  42. }
  43. db_ = db;
  44. is_db_preprocessed_ = false;
  45. }
  46. void PIRServer::set_database(const uint8_t *bytes, uint64_t ele_num, uint64_t ele_size) {
  47. uint32_t logtp = ceil(log2(expanded_params_.plain_modulus().value()));
  48. uint32_t N = expanded_params_.poly_modulus().coeff_count() - 1;
  49. // number of FV plaintexts needed to represent all elements
  50. uint64_t total = plaintexts_per_db(logtp, N, ele_num, ele_size);
  51. // number of FV plaintexts needed to create the d-dimensional matrix
  52. uint64_t prod = 1;
  53. for (uint32_t i = 0; i < pir_params_.nvec.size(); i++) {
  54. prod *= pir_params_.nvec[i];
  55. }
  56. uint64_t matrix_plaintexts = prod;
  57. assert(total <= matrix_plaintexts);
  58. vector<Plaintext> *result = new vector<Plaintext>();
  59. result->reserve(matrix_plaintexts);
  60. uint64_t ele_per_ptxt = elements_per_ptxt(logtp, N, ele_size);
  61. uint64_t bytes_per_ptxt = ele_per_ptxt * ele_size;
  62. uint64_t db_size = ele_num * ele_size;
  63. uint64_t coeff_per_ptxt = ele_per_ptxt * coefficients_per_element(logtp, ele_size);
  64. assert(coeff_per_ptxt <= N);
  65. uint32_t offset = 0;
  66. for (uint64_t i = 0; i < total; i++) {
  67. uint64_t process_bytes = 0;
  68. if (db_size <= offset) {
  69. break;
  70. } else if (db_size < offset + bytes_per_ptxt) {
  71. process_bytes = db_size - offset;
  72. } else {
  73. process_bytes = bytes_per_ptxt;
  74. }
  75. // Get the coefficients of the elements that will be packed in plaintext i
  76. vector<uint64_t> coefficients = bytes_to_coeffs(logtp, bytes + offset, process_bytes);
  77. offset += process_bytes;
  78. uint64_t used = coefficients.size();
  79. assert(used <= coeff_per_ptxt);
  80. // Pad the rest with 1s
  81. for (uint64_t j = 0; j < (N - used); j++) {
  82. coefficients.push_back(1);
  83. }
  84. Plaintext plain;
  85. vector_to_plaintext(coefficients, plain);
  86. result->push_back(plain);
  87. }
  88. // Add padding to make database a matrix
  89. uint64_t current_plaintexts = result->size();
  90. assert(current_plaintexts <= total);
  91. #ifdef DEBUG
  92. cout << "adding: " << matrix_plaintexts - current_plaintexts
  93. << " FV plaintexts of padding (equivalent to: "
  94. << (matrix_plaintexts - current_plaintexts) * elements_per_ptxt(logtp, N, ele_size)
  95. << " elements)" << endl;
  96. #endif
  97. vector<uint64_t> padding(N, 1);
  98. for (uint64_t i = 0; i < (matrix_plaintexts - current_plaintexts); i++) {
  99. Plaintext plain;
  100. vector_to_plaintext(padding, plain);
  101. result->push_back(plain);
  102. }
  103. set_database(result);
  104. }
  105. void PIRServer::set_galois_key(std::uint32_t client_id, seal::GaloisKeys galkey) {
  106. galkey.mutable_hash_block() = expanded_params_.hash_block();
  107. galoisKeys_[client_id] = galkey;
  108. }
  109. PirReply PIRServer::generate_reply(PirQuery query, uint32_t client_id) {
  110. vector<uint64_t> nvec = pir_params_.nvec;
  111. uint64_t product = 1;
  112. for (uint32_t i = 0; i < nvec.size(); i++) {
  113. product *= nvec[i];
  114. }
  115. int coeff_count = expanded_params_.poly_modulus().coeff_count();
  116. vector<Plaintext> *cur = db_;
  117. vector<Plaintext> intermediate_plain; // decompose....
  118. auto my_pool = MemoryPoolHandle::New();
  119. for (uint32_t i = 0; i < nvec.size(); i++) {
  120. uint64_t n_i = nvec[i];
  121. vector<Ciphertext> expanded_query = expand_query(query[i], n_i, client_id);
  122. // Transform expanded query to NTT, and ...
  123. for (uint32_t jj = 0; jj < expanded_query.size(); jj++) {
  124. evaluator_->transform_to_ntt(expanded_query[jj]);
  125. }
  126. // Transform plaintext to NTT. If database is pre-processed, can skip
  127. if ((!is_db_preprocessed_) || i > 0) {
  128. for (uint32_t jj = 0; jj < cur->size(); jj++) {
  129. evaluator_->transform_to_ntt((*cur)[jj]);
  130. }
  131. }
  132. product /= n_i;
  133. vector<Ciphertext> intermediate(product);
  134. Ciphertext temp;
  135. for (uint64_t k = 0; k < product; k++) {
  136. evaluator_->multiply_plain_ntt(expanded_query[0], (*cur)[k], intermediate[k]);
  137. for (uint64_t j = 1; j < n_i; j++) {
  138. evaluator_->multiply_plain_ntt(expanded_query[j], (*cur)[k + j * product], temp);
  139. evaluator_->add(intermediate[k],
  140. temp); // Adds to first component.
  141. }
  142. }
  143. for (uint32_t jj = 0; jj < intermediate.size(); jj++) {
  144. evaluator_->transform_from_ntt(intermediate[jj]);
  145. }
  146. if (i == nvec.size() - 1) {
  147. return intermediate;
  148. } else {
  149. intermediate_plain.clear();
  150. intermediate_plain.reserve(pir_params_.expansion_ratio * product);
  151. cur = &intermediate_plain;
  152. util::Pointer tempplain_ptr(allocate_zero_poly(
  153. pir_params_.expansion_ratio * product, coeff_count, my_pool));
  154. for (uint64_t rr = 0; rr < product; rr++) {
  155. decompose_to_plaintexts_ptr(intermediate[rr],
  156. tempplain_ptr.get() +
  157. rr * pir_params_.expansion_ratio * coeff_count);
  158. for (uint32_t jj = 0; jj < pir_params_.expansion_ratio; jj++) {
  159. int offset = rr * pir_params_.expansion_ratio * coeff_count + jj * coeff_count;
  160. intermediate_plain.emplace_back(coeff_count, tempplain_ptr.get() + offset);
  161. }
  162. }
  163. product *= pir_params_.expansion_ratio; // multiply by expansion rate.
  164. }
  165. }
  166. // This should never get here
  167. assert(0);
  168. vector<Ciphertext> fail(1);
  169. return fail;
  170. }
  171. inline vector<Ciphertext> PIRServer::expand_query(const Ciphertext &encrypted, uint32_t m,
  172. uint32_t client_id) {
  173. #ifdef DEBUG
  174. uint64_t plainMod = expanded_params_.plain_modulus().value();
  175. cout << "PIRServer side plain modulus = " << plainMod << endl;
  176. #endif
  177. GaloisKeys &galkey = galoisKeys_[client_id];
  178. // Assume that m is a power of 2. If not, round it to the next power of 2.
  179. uint32_t logm = ceil(log2(m));
  180. Plaintext two("2");
  181. vector<int> galois_elts;
  182. int n = expanded_params_.poly_modulus().coeff_count() - 1;
  183. for (uint32_t i = 0; i < logm; i++) {
  184. galois_elts.push_back((n + exponentiate_uint64(2, i)) / exponentiate_uint64(2, i));
  185. }
  186. vector<Ciphertext> temp;
  187. temp.push_back(encrypted);
  188. Ciphertext tempctxt;
  189. Ciphertext tempctxt_rotated;
  190. Ciphertext tempctxt_shifted;
  191. Ciphertext tempctxt_rotatedshifted;
  192. for (uint32_t i = 0; i < logm - 1; i++) {
  193. vector<Ciphertext> newtemp(temp.size() << 1);
  194. // temp[a] = (j0 = a (mod 2**i) ? ) : Enc(x^{j0 - a}) else Enc(0). With
  195. // some scaling....
  196. int index_raw = (n << 1) - (1 << i);
  197. int index = (index_raw * galois_elts[i]) % (n << 1);
  198. for (uint32_t a = 0; a < temp.size(); a++) {
  199. evaluator_->apply_galois(temp[a], galois_elts[i], galkey, tempctxt_rotated);
  200. evaluator_->add(temp[a], tempctxt_rotated, newtemp[a]);
  201. multiply_power_of_X(temp[a], tempctxt_shifted, index_raw);
  202. multiply_power_of_X(tempctxt_rotated, tempctxt_rotatedshifted, index);
  203. // Enc(2^i x^j) if j = 0 (mod 2**i).
  204. evaluator_->add(tempctxt_shifted, tempctxt_rotatedshifted, newtemp[a + temp.size()]);
  205. }
  206. temp = newtemp;
  207. }
  208. // Last step of the loop
  209. vector<Ciphertext> newtemp(temp.size() << 1);
  210. int index_raw = (n << 1) - (1 << (logm - 1));
  211. int index = (index_raw * galois_elts[logm - 1]) % (n << 1);
  212. for (uint32_t a = 0; a < temp.size(); a++) {
  213. if (a >= (m - (1 << (logm - 1)))) { // corner case.
  214. evaluator_->multiply_plain(temp[a], two, newtemp[a]); // plain multiplication by 2.
  215. } else {
  216. evaluator_->apply_galois(temp[a], galois_elts[logm - 1], galkey, tempctxt_rotated);
  217. evaluator_->add(temp[a], tempctxt_rotated, newtemp[a]);
  218. multiply_power_of_X(temp[a], tempctxt_shifted, index_raw);
  219. multiply_power_of_X(tempctxt_rotated, tempctxt_rotatedshifted, index);
  220. evaluator_->add(tempctxt_shifted, tempctxt_rotatedshifted, newtemp[a + temp.size()]);
  221. }
  222. }
  223. vector<Ciphertext>::const_iterator first = newtemp.begin();
  224. vector<Ciphertext>::const_iterator last = newtemp.begin() + m;
  225. vector<Ciphertext> newVec(first, last);
  226. return newVec;
  227. }
  228. inline void PIRServer::multiply_power_of_X(const Ciphertext &encrypted, Ciphertext &destination,
  229. uint32_t index) {
  230. int coeff_mod_count = expanded_params_.coeff_modulus().size();
  231. int coeff_count = expanded_params_.poly_modulus().coeff_count();
  232. int encrypted_count = encrypted.size();
  233. // First copy over.
  234. destination = encrypted;
  235. // Prepare for destination
  236. // Multiply X^index for each ciphertext polynomial
  237. for (int i = 0; i < encrypted_count; i++) {
  238. for (int j = 0; j < coeff_mod_count; j++) {
  239. negacyclic_shift_poly_coeffmod(encrypted.pointer(i) + (j * coeff_count),
  240. coeff_count - 1, index,
  241. expanded_params_.coeff_modulus()[j],
  242. destination.mutable_pointer(i) + (j * coeff_count));
  243. }
  244. }
  245. }
  246. inline void PIRServer::decompose_to_plaintexts_ptr(const Ciphertext &encrypted, uint64_t *plain_ptr) {
  247. vector<Plaintext> result;
  248. int coeff_count = expanded_params_.poly_modulus().coeff_count();
  249. int coeff_mod_count = expanded_params_.coeff_modulus().size();
  250. int encrypted_count = encrypted.size();
  251. // Generate powers of t.
  252. uint64_t plainModMinusOne = expanded_params_.plain_modulus().value() - 1;
  253. int exp = ceil(log2(plainModMinusOne + 1));
  254. // A triple for loop. Going over polys, moduli, and decomposed index.
  255. for (int i = 0; i < encrypted_count; i++) {
  256. const uint64_t *encrypted_pointer = encrypted.pointer(i);
  257. for (int j = 0; j < coeff_mod_count; j++) {
  258. // populate one poly at a time.
  259. // create a polynomial to store the current decomposition value
  260. // which will be copied into the array to populate it at the current
  261. // index.
  262. int logqj = log2(expanded_params_.coeff_modulus()[j].value());
  263. int expansion_ratio = ceil(logqj + exp - 1) / exp;
  264. // cout << "expansion ratio = " << expansion_ratio << endl;
  265. uint64_t curexp = 0;
  266. for (int k = 0; k < expansion_ratio; k++) {
  267. // Decompose here
  268. for (int m = 0; m < coeff_count; m++) {
  269. *plain_ptr =
  270. (*(encrypted_pointer + m + (j * coeff_count)) >> curexp) & plainModMinusOne;
  271. plain_ptr++;
  272. }
  273. curexp += exp;
  274. }
  275. }
  276. }
  277. }
  278. vector<Plaintext> PIRServer::decompose_to_plaintexts(const Ciphertext &encrypted) {
  279. vector<Plaintext> result;
  280. int coeff_count = expanded_params_.poly_modulus().coeff_count();
  281. int coeff_mod_count = expanded_params_.coeff_modulus().size();
  282. int plain_bit_count = expanded_params_.plain_modulus().bit_count();
  283. int encrypted_count = encrypted.size();
  284. // Generate powers of t.
  285. uint64_t plainMod = expanded_params_.plain_modulus().value();
  286. // A triple for loop. Going over polys, moduli, and decomposed index.
  287. for (int i = 0; i < encrypted_count; i++) {
  288. const uint64_t *encrypted_pointer = encrypted.pointer(i);
  289. for (int j = 0; j < coeff_mod_count; j++) {
  290. // populate one poly at a time.
  291. // create a polynomial to store the current decomposition value
  292. // which will be copied into the array to populate it at the current
  293. // index.
  294. int logqj = log2(expanded_params_.coeff_modulus()[j].value());
  295. int expansion_ratio = ceil(logqj / log2(plainMod));
  296. // cout << "expansion ratio = " << expansion_ratio << endl;
  297. uint64_t cur = 1;
  298. for (int k = 0; k < expansion_ratio; k++) {
  299. // Decompose here
  300. BigPoly temp;
  301. temp.resize(coeff_count, plain_bit_count);
  302. temp.set_zero();
  303. uint64_t *plain_coeff = temp.pointer();
  304. for (int m = 0; m < coeff_count; m++) {
  305. *(plain_coeff + m) =
  306. (*(encrypted_pointer + m + (j * coeff_count)) / cur) % plainMod;
  307. }
  308. result.push_back(Plaintext(temp));
  309. cur *= plainMod;
  310. }
  311. }
  312. }
  313. return result;
  314. }