pir.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "pir.hpp"
  2. using namespace std;
  3. using namespace seal;
  4. using namespace seal::util;
  5. std::vector<std::uint64_t> get_dimensions(std::uint64_t num_of_plaintexts, std::uint32_t d) {
  6. assert(d > 0);
  7. assert(num_of_plaintexts > 0);
  8. std::uint64_t root = max(static_cast<uint32_t>(2),static_cast<uint32_t>(floor(pow(num_of_plaintexts, 1.0/d))));
  9. std::vector<std::uint64_t> dimensions(d, root);
  10. for(int i = 0; i < d; i++){
  11. if(accumulate(dimensions.begin(), dimensions.end(), 1, multiplies<uint64_t>()) > num_of_plaintexts){
  12. break;
  13. }
  14. dimensions[i] += 1;
  15. }
  16. std::uint32_t prod = accumulate(dimensions.begin(), dimensions.end(), 1, multiplies<uint64_t>());
  17. cout << "Total:" << num_of_plaintexts << endl << "Prod: "
  18. << prod << endl;
  19. assert(prod > num_of_plaintexts);
  20. return dimensions;
  21. }
  22. void gen_encryption_params(std::uint32_t N, std::uint32_t logt,
  23. seal::EncryptionParameters &enc_params){
  24. enc_params.set_poly_modulus_degree(N);
  25. enc_params.set_coeff_modulus(CoeffModulus::BFVDefault(N));
  26. enc_params.set_plain_modulus(PlainModulus::Batching(N, logt));
  27. }
  28. void verify_encryption_params(const seal::EncryptionParameters &enc_params){
  29. SEALContext context(enc_params, true);
  30. if(!context.parameters_set()){
  31. throw invalid_argument("SEAL parameters not valid.");
  32. }
  33. if(!context.using_keyswitching()){
  34. throw invalid_argument("SEAL parameters do not support key switching.");
  35. }
  36. if(!context.first_context_data()->qualifiers().using_batching){
  37. throw invalid_argument("SEAL parameters do not support batching.");
  38. }
  39. return;
  40. }
  41. void gen_pir_params(uint64_t ele_num, uint64_t ele_size, uint32_t d,
  42. const EncryptionParameters &enc_params, PirParams &pir_params,
  43. bool enable_symmetric, bool enable_batching){
  44. std::uint32_t N = enc_params.poly_modulus_degree();
  45. Modulus t = enc_params.plain_modulus();
  46. std::uint32_t logt = floor(log2(t.value()));
  47. cout << "logt: " << logt << endl << "N: " << N << endl <<
  48. "ele_num: " << ele_num << endl << "ele_size: " << ele_size << endl;
  49. std::uint64_t elements_per_plaintext;
  50. std::uint64_t num_of_plaintexts;
  51. if(enable_batching){
  52. elements_per_plaintext = elements_per_ptxt(logt, N, ele_size);
  53. num_of_plaintexts = plaintexts_per_db(logt, N, ele_num, ele_size);
  54. }
  55. else{
  56. elements_per_plaintext = 1;
  57. num_of_plaintexts = ele_num;
  58. }
  59. vector<uint64_t> nvec = get_dimensions(num_of_plaintexts, d);
  60. uint32_t expansion_ratio = 0;
  61. for (uint32_t i = 0; i < enc_params.coeff_modulus().size(); ++i) {
  62. double logqi = log2(enc_params.coeff_modulus()[i].value());
  63. cout << "PIR: logqi = " << logqi << endl;
  64. expansion_ratio += ceil(logqi / logt);
  65. }
  66. if(!enable_symmetric){
  67. expansion_ratio = expansion_ratio << 1;
  68. }
  69. pir_params.enable_symmetric = enable_symmetric;
  70. pir_params.enable_batching = enable_batching;
  71. pir_params.ele_num = ele_num;
  72. pir_params.ele_size = ele_size;
  73. pir_params.elements_per_plaintext = elements_per_plaintext;
  74. pir_params.num_of_plaintexts = num_of_plaintexts;
  75. pir_params.d = d;
  76. pir_params.expansion_ratio = expansion_ratio;
  77. pir_params.nvec = nvec;
  78. pir_params.dbc = 6;
  79. pir_params.n = num_of_plaintexts;
  80. }
  81. void print_pir_params(const PirParams &pir_params){
  82. cout << "Pir Params: " << endl;
  83. cout << "num_of_elements: " << pir_params.ele_num << endl;
  84. cout << "ele_size: " << pir_params.ele_size << endl;
  85. cout << "elements_per_plaintext: " << pir_params.elements_per_plaintext << endl;
  86. cout << "num_of_plaintexts: " << pir_params.num_of_plaintexts << endl;
  87. cout << "dimension: " << pir_params.d << endl;
  88. cout << "expansion ratio: " << pir_params.expansion_ratio << endl;
  89. cout << "dbc: " << pir_params.dbc << endl;
  90. cout << "n: " << pir_params.n << endl;
  91. }
  92. void gen_params(uint64_t ele_num, uint64_t ele_size, uint32_t N, uint32_t logt,
  93. uint32_t d, EncryptionParameters &params,
  94. PirParams &pir_params) {
  95. params.set_poly_modulus_degree(N);
  96. params.set_coeff_modulus(CoeffModulus::BFVDefault(N));
  97. params.set_plain_modulus(PlainModulus::Batching(N, logt));
  98. logt = floor(log2(params.plain_modulus().value()));
  99. cout << "logt: " << logt << endl << "N: " << N << endl <<
  100. "ele_num: " << ele_num << endl << "ele_size: " << ele_size << endl;
  101. uint64_t plaintext_num = plaintexts_per_db(logt, N, ele_num, ele_size);
  102. vector<uint64_t> nvec = get_dimensions(plaintext_num, d);
  103. uint32_t expansion_ratio = 0;
  104. for (uint32_t i = 0; i < params.coeff_modulus().size(); ++i) {
  105. double logqi = log2(params.coeff_modulus()[i].value());
  106. cout << "PIR: logqi = " << logqi << endl;
  107. expansion_ratio += ceil(logqi / logt);
  108. }
  109. pir_params.d = d;
  110. pir_params.dbc = 6;
  111. pir_params.n = plaintext_num;
  112. pir_params.nvec = nvec;
  113. pir_params.expansion_ratio = expansion_ratio << 1; // because one ciphertext = two polys
  114. }
  115. uint32_t plainmod_after_expansion(uint32_t logt, uint32_t N, uint32_t d,
  116. uint64_t ele_num, uint64_t ele_size) {
  117. // Goal: find max logtp such that logtp + ceil(log(ceil(d_root(n)))) <= logt
  118. // where n = ceil(ele_num / floor(N*logtp / ele_size *8))
  119. for (uint32_t logtp = logt; logtp >= 2; logtp--) {
  120. uint64_t n = plaintexts_per_db(logtp, N, ele_num, ele_size);
  121. if (logtp == logt && n == 1) {
  122. return logtp - 1;
  123. }
  124. if ((double)logtp + ceil(log2(ceil(pow(n, 1.0/(double)d)))) <= logt) {
  125. return logtp;
  126. }
  127. }
  128. assert(0); // this should never happen
  129. return logt;
  130. }
  131. // Number of coefficients needed to represent a database element
  132. uint64_t coefficients_per_element(uint32_t logtp, uint64_t ele_size) {
  133. return ceil(8 * ele_size / (double)logtp);
  134. }
  135. // Number of database elements that can fit in a single FV plaintext
  136. uint64_t elements_per_ptxt(uint32_t logt, uint64_t N, uint64_t ele_size) {
  137. uint64_t coeff_per_ele = coefficients_per_element(logt, ele_size);
  138. uint64_t ele_per_ptxt = N / coeff_per_ele;
  139. assert(ele_per_ptxt > 0);
  140. return ele_per_ptxt;
  141. }
  142. // Number of FV plaintexts needed to represent the database
  143. uint64_t plaintexts_per_db(uint32_t logtp, uint64_t N, uint64_t ele_num, uint64_t ele_size) {
  144. uint64_t ele_per_ptxt = elements_per_ptxt(logtp, N, ele_size);
  145. return ceil((double)ele_num / ele_per_ptxt);
  146. }
  147. vector<uint64_t> bytes_to_coeffs(uint32_t limit, const uint8_t *bytes, uint64_t size) {
  148. uint64_t size_out = coefficients_per_element(limit, size);
  149. vector<uint64_t> output(size_out);
  150. uint32_t room = limit;
  151. uint64_t *target = &output[0];
  152. for (uint32_t i = 0; i < size; i++) {
  153. uint8_t src = bytes[i];
  154. uint32_t rest = 8;
  155. while (rest) {
  156. if (room == 0) {
  157. target++;
  158. room = limit;
  159. }
  160. uint32_t shift = rest;
  161. if (room < rest) {
  162. shift = room;
  163. }
  164. *target = *target << shift;
  165. *target = *target | (src >> (8 - shift));
  166. src = src << shift;
  167. room -= shift;
  168. rest -= shift;
  169. }
  170. }
  171. *target = *target << room;
  172. return output;
  173. }
  174. void coeffs_to_bytes(uint32_t limit, const Plaintext &coeffs, uint8_t *output, uint32_t size_out) {
  175. uint32_t room = 8;
  176. uint32_t j = 0;
  177. uint8_t *target = output;
  178. for (uint32_t i = 0; i < coeffs.coeff_count(); i++) {
  179. uint64_t src = coeffs[i];
  180. uint32_t rest = limit;
  181. while (rest && j < size_out) {
  182. uint32_t shift = rest;
  183. if (room < rest) {
  184. shift = room;
  185. }
  186. target[j] = target[j] << shift;
  187. target[j] = target[j] | (src >> (limit - shift));
  188. src = src << shift;
  189. room -= shift;
  190. rest -= shift;
  191. if (room == 0) {
  192. j++;
  193. room = 8;
  194. }
  195. }
  196. }
  197. }
  198. void vector_to_plaintext(const vector<uint64_t> &coeffs, Plaintext &plain) {
  199. uint32_t coeff_count = coeffs.size();
  200. plain.resize(coeff_count);
  201. util::set_uint(coeffs.data(), coeff_count, plain.data());
  202. }
  203. vector<uint64_t> compute_indices(uint64_t desiredIndex, vector<uint64_t> Nvec) {
  204. uint32_t num = Nvec.size();
  205. uint64_t product = 1;
  206. for (uint32_t i = 0; i < num; i++) {
  207. product *= Nvec[i];
  208. }
  209. uint64_t j = desiredIndex;
  210. vector<uint64_t> result;
  211. for (uint32_t i = 0; i < num; i++) {
  212. product /= Nvec[i];
  213. uint64_t ji = j / product;
  214. result.push_back(ji);
  215. j -= ji * product;
  216. }
  217. return result;
  218. }
  219. uint64_t invert_mod(uint64_t m, const seal::Modulus& mod) {
  220. if (mod.uint64_count() > 1) {
  221. cout << "Mod too big to invert";
  222. }
  223. uint64_t inverse = 0;
  224. if (!seal::util::try_invert_uint_mod(m, mod.value(), inverse)) {
  225. cout << "Could not invert value";
  226. }
  227. return inverse;
  228. }
  229. inline Ciphertext deserialize_ciphertext(string s, shared_ptr<SEALContext> context) {
  230. Ciphertext c;
  231. std::istringstream input(s);
  232. c.unsafe_load(*context, input);
  233. return c;
  234. }
  235. vector<Ciphertext> deserialize_ciphertexts(uint32_t count, string s, uint32_t len_ciphertext,
  236. shared_ptr<SEALContext> context) {
  237. vector<Ciphertext> c;
  238. for (uint32_t i = 0; i < count; i++) {
  239. c.push_back(deserialize_ciphertext(s.substr(i * len_ciphertext, len_ciphertext), context));
  240. }
  241. return c;
  242. }
  243. PirQuery deserialize_query(uint32_t d, uint32_t count, string s, uint32_t len_ciphertext,
  244. shared_ptr<SEALContext> context) {
  245. vector<vector<Ciphertext>> c;
  246. for (uint32_t i = 0; i < d; i++) {
  247. c.push_back(deserialize_ciphertexts(
  248. count,
  249. s.substr(i * count * len_ciphertext, count * len_ciphertext),
  250. len_ciphertext, context)
  251. );
  252. }
  253. return c;
  254. }
  255. inline string serialize_ciphertext(Ciphertext c) {
  256. std::ostringstream output;
  257. c.save(output);
  258. return output.str();
  259. }
  260. string serialize_ciphertexts(vector<Ciphertext> c) {
  261. string s;
  262. for (uint32_t i = 0; i < c.size(); i++) {
  263. s.append(serialize_ciphertext(c[i]));
  264. }
  265. return s;
  266. }
  267. string serialize_query(vector<vector<Ciphertext>> c) {
  268. string s;
  269. for (uint32_t i = 0; i < c.size(); i++) {
  270. for (uint32_t j = 0; j < c[i].size(); j++) {
  271. s.append(serialize_ciphertext(c[i][j]));
  272. }
  273. }
  274. return s;
  275. }
  276. string serialize_galoiskeys(GaloisKeys g) {
  277. std::ostringstream output;
  278. g.save(output);
  279. return output.str();
  280. }
  281. GaloisKeys *deserialize_galoiskeys(string s, shared_ptr<SEALContext> context) {
  282. GaloisKeys *g = new GaloisKeys();
  283. std::istringstream input(s);
  284. g->unsafe_load(*context, input);
  285. return g;
  286. }