pir.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "pir.hpp"
  2. using namespace std;
  3. using namespace seal;
  4. using namespace seal::util;
  5. vector<uint64_t> get_dimensions(uint64_t plaintext_num, uint32_t d) {
  6. assert(d > 0);
  7. assert(plaintext_num > 0);
  8. vector<uint64_t> dimensions(d);
  9. for (uint32_t i = 0; i < d; i++) {
  10. dimensions[i] = std::max((uint32_t) 2, (uint32_t) floor(pow(plaintext_num, 1.0/d)));
  11. }
  12. uint32_t product = 1;
  13. uint32_t j = 0;
  14. // if plaintext_num is not a d-power
  15. if ((double) dimensions[0] != pow(plaintext_num, 1.0 / d)) {
  16. while (product < plaintext_num && j < d) {
  17. product = 1;
  18. dimensions[j++]++;
  19. for (uint32_t i = 0; i < d; i++) {
  20. product *= dimensions[i];
  21. }
  22. }
  23. }
  24. return dimensions;
  25. }
  26. void gen_params(uint64_t ele_num, uint64_t ele_size, uint32_t N, uint32_t logt,
  27. uint32_t d, EncryptionParameters &params,
  28. PirParams &pir_params) {
  29. // Determine the maximum size of each dimension
  30. // plain modulus = a power of 2 plus 1
  31. uint64_t plain_mod = (static_cast<uint64_t>(1) << logt) + 1;
  32. uint64_t plaintext_num = plaintexts_per_db(logt, N, ele_num, ele_size);
  33. #ifdef DEBUG
  34. cout << "log(plain mod) before expand = " << logt << endl;
  35. cout << "number of FV plaintexts = " << plaintext_num << endl;
  36. #endif
  37. vector<SmallModulus> coeff_mod_array;
  38. uint32_t logq = 0;
  39. for (uint32_t i = 0; i < 1; i++) {
  40. coeff_mod_array.emplace_back(SmallModulus());
  41. coeff_mod_array[i] = small_mods_60bit(i);
  42. logq += coeff_mod_array[i].bit_count();
  43. }
  44. params.set_poly_modulus_degree(N);
  45. params.set_coeff_modulus(coeff_mod_array);
  46. params.set_plain_modulus(plain_mod);
  47. vector<uint64_t> nvec = get_dimensions(plaintext_num, d);
  48. uint32_t expansion_ratio = 0;
  49. for (uint32_t i = 0; i < params.coeff_modulus().size(); ++i) {
  50. double logqi = log2(params.coeff_modulus()[i].value());
  51. cout << "PIR: logqi = " << logqi << endl;
  52. expansion_ratio += ceil(logqi / logt);
  53. }
  54. pir_params.d = d;
  55. pir_params.dbc = 6;
  56. pir_params.n = plaintext_num;
  57. pir_params.nvec = nvec;
  58. pir_params.expansion_ratio = expansion_ratio << 1; // because one ciphertext = two polys
  59. }
  60. void update_params(uint64_t ele_num, uint64_t ele_size, uint32_t d,
  61. const EncryptionParameters &old_params, EncryptionParameters &expanded_params,
  62. PirParams &pir_params) {
  63. uint32_t logt = ceil(log2(old_params.plain_modulus().value()));
  64. uint32_t N = old_params.poly_modulus_degree();
  65. // Determine the maximum size of each dimension
  66. uint32_t logtp = plainmod_after_expansion(logt, N, d, ele_num, ele_size);
  67. uint64_t expanded_plain_mod = static_cast<uint64_t>(1) << logtp;
  68. uint64_t plaintext_num = plaintexts_per_db(logtp, N, ele_num, ele_size);
  69. #ifdef DEBUG
  70. cout << "log(plain mod) before expand = " << logt << endl;
  71. cout << "log(plain mod) after expand = " << logtp << endl;
  72. cout << "number of FV plaintexts = " << plaintext_num << endl;
  73. #endif
  74. expanded_params.set_poly_modulus_degree(old_params.poly_modulus_degree());
  75. expanded_params.set_coeff_modulus(old_params.coeff_modulus());
  76. expanded_params.set_plain_modulus(expanded_plain_mod);
  77. // Assumes dimension of database is 2
  78. vector<uint64_t> nvec = get_dimensions(plaintext_num, d);
  79. uint32_t expansion_ratio = 0;
  80. for (uint32_t i = 0; i < old_params.coeff_modulus().size(); ++i) {
  81. double logqi = log2(old_params.coeff_modulus()[i].value());
  82. expansion_ratio += ceil(logqi / logtp);
  83. }
  84. pir_params.d = d;
  85. pir_params.dbc = 6;
  86. pir_params.n = plaintext_num;
  87. pir_params.nvec = nvec;
  88. pir_params.expansion_ratio = expansion_ratio << 1;
  89. }
  90. uint32_t plainmod_after_expansion(uint32_t logt, uint32_t N, uint32_t d,
  91. uint64_t ele_num, uint64_t ele_size) {
  92. // Goal: find max logtp such that logtp + ceil(log(ceil(d_root(n)))) <= logt
  93. // where n = ceil(ele_num / floor(N*logtp / ele_size *8))
  94. for (uint32_t logtp = logt; logtp >= 2; logtp--) {
  95. uint64_t n = plaintexts_per_db(logtp, N, ele_num, ele_size);
  96. if (logtp == logt && n == 1) {
  97. return logtp - 1;
  98. }
  99. if ((double)logtp + ceil(log2(ceil(pow(n, 1.0/(double)d)))) <= logt) {
  100. return logtp;
  101. }
  102. }
  103. assert(0); // this should never happen
  104. return logt;
  105. }
  106. // Number of coefficients needed to represent a database element
  107. uint64_t coefficients_per_element(uint32_t logtp, uint64_t ele_size) {
  108. return ceil(8 * ele_size / (double)logtp);
  109. }
  110. // Number of database elements that can fit in a single FV plaintext
  111. uint64_t elements_per_ptxt(uint32_t logt, uint64_t N, uint64_t ele_size) {
  112. uint64_t coeff_per_ele = coefficients_per_element(logt, ele_size);
  113. uint64_t ele_per_ptxt = N / coeff_per_ele;
  114. assert(ele_per_ptxt > 0);
  115. return ele_per_ptxt;
  116. }
  117. // Number of FV plaintexts needed to represent the database
  118. uint64_t plaintexts_per_db(uint32_t logtp, uint64_t N, uint64_t ele_num, uint64_t ele_size) {
  119. uint64_t ele_per_ptxt = elements_per_ptxt(logtp, N, ele_size);
  120. return ceil((double)ele_num / ele_per_ptxt);
  121. }
  122. vector<uint64_t> bytes_to_coeffs(uint32_t limit, const uint8_t *bytes, uint64_t size) {
  123. uint64_t size_out = coefficients_per_element(limit, size);
  124. vector<uint64_t> output(size_out);
  125. uint32_t room = limit;
  126. uint64_t *target = &output[0];
  127. for (uint32_t i = 0; i < size; i++) {
  128. uint8_t src = bytes[i];
  129. uint32_t rest = 8;
  130. while (rest) {
  131. if (room == 0) {
  132. target++;
  133. room = limit;
  134. }
  135. uint32_t shift = rest;
  136. if (room < rest) {
  137. shift = room;
  138. }
  139. *target = *target << shift;
  140. *target = *target | (src >> (8 - shift));
  141. src = src << shift;
  142. room -= shift;
  143. rest -= shift;
  144. }
  145. }
  146. *target = *target << room;
  147. return output;
  148. }
  149. void coeffs_to_bytes(uint32_t limit, const Plaintext &coeffs, uint8_t *output, uint32_t size_out) {
  150. uint32_t room = 8;
  151. uint32_t j = 0;
  152. uint8_t *target = output;
  153. for (uint32_t i = 0; i < coeffs.coeff_count(); i++) {
  154. uint64_t src = coeffs[i];
  155. uint32_t rest = limit;
  156. while (rest && j < size_out) {
  157. uint32_t shift = rest;
  158. if (room < rest) {
  159. shift = room;
  160. }
  161. target[j] = target[j] << shift;
  162. target[j] = target[j] | (src >> (limit - shift));
  163. src = src << shift;
  164. room -= shift;
  165. rest -= shift;
  166. if (room == 0) {
  167. j++;
  168. room = 8;
  169. }
  170. }
  171. }
  172. }
  173. void vector_to_plaintext(const vector<uint64_t> &coeffs, Plaintext &plain) {
  174. uint32_t coeff_count = coeffs.size();
  175. plain.resize(coeff_count);
  176. util::set_uint_uint(coeffs.data(), coeff_count, plain.data());
  177. }
  178. vector<uint64_t> compute_indices(uint64_t desiredIndex, vector<uint64_t> Nvec) {
  179. uint32_t num = Nvec.size();
  180. uint64_t product = 1;
  181. for (uint32_t i = 0; i < num; i++) {
  182. product *= Nvec[i];
  183. }
  184. uint64_t j = desiredIndex;
  185. vector<uint64_t> result;
  186. for (uint32_t i = 0; i < num; i++) {
  187. product /= Nvec[i];
  188. uint64_t ji = j / product;
  189. result.push_back(ji);
  190. j -= ji * product;
  191. }
  192. return result;
  193. }
  194. inline Ciphertext deserialize_ciphertext(string s) {
  195. Ciphertext c;
  196. std::istringstream input(s);
  197. c.unsafe_load(input);
  198. return c;
  199. }
  200. vector<Ciphertext> deserialize_ciphertexts(uint32_t count, string s, uint32_t len_ciphertext) {
  201. vector<Ciphertext> c;
  202. for (uint32_t i = 0; i < count; i++) {
  203. c.push_back(deserialize_ciphertext(s.substr(i * len_ciphertext, len_ciphertext)));
  204. }
  205. return c;
  206. }
  207. inline string serialize_ciphertext(Ciphertext c) {
  208. std::ostringstream output;
  209. c.save(output);
  210. return output.str();
  211. }
  212. string serialize_ciphertexts(vector<Ciphertext> c) {
  213. string s;
  214. for (uint32_t i = 0; i < c.size(); i++) {
  215. s.append(serialize_ciphertext(c[i]));
  216. }
  217. return s;
  218. }
  219. string serialize_galoiskeys(GaloisKeys g) {
  220. std::ostringstream output;
  221. g.save(output);
  222. return output.str();
  223. }
  224. GaloisKeys *deserialize_galoiskeys(string s) {
  225. GaloisKeys *g = new GaloisKeys();
  226. std::istringstream input(s);
  227. g->unsafe_load(input);
  228. return g;
  229. }