preproc.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <vector>
  2. #include "types.hpp"
  3. #include "coroutine.hpp"
  4. #include "preproc.hpp"
  5. #include "rdpf.hpp"
  6. // Keep track of open files that coroutines might be writing into
  7. class Openfiles {
  8. std::vector<std::ofstream> files;
  9. public:
  10. class Handle {
  11. Openfiles &parent;
  12. size_t idx;
  13. public:
  14. Handle(Openfiles &parent, size_t idx) :
  15. parent(parent), idx(idx) {}
  16. // Retrieve the ofstream from this Handle
  17. std::ofstream &os() const { return parent.files[idx]; }
  18. };
  19. Handle open(const char *prefix, unsigned player,
  20. unsigned thread_num, nbits_t depth = 0);
  21. void closeall();
  22. };
  23. // Open a file for writing with name the given prefix, and ".pX.tY"
  24. // suffix, where X is the (one-digit) player number and Y is the thread
  25. // number. If depth D is given, use "D.pX.tY" as the suffix.
  26. Openfiles::Handle Openfiles::open(const char *prefix, unsigned player,
  27. unsigned thread_num, nbits_t depth)
  28. {
  29. std::string filename(prefix);
  30. char suffix[20];
  31. if (depth > 0) {
  32. sprintf(suffix, "%02d.p%d.t%u", depth, player%10, thread_num);
  33. } else {
  34. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  35. }
  36. filename.append(suffix);
  37. std::ofstream &f = files.emplace_back(filename);
  38. if (f.fail()) {
  39. std::cerr << "Failed to open " << filename << "\n";
  40. exit(1);
  41. }
  42. return Handle(*this, files.size()-1);
  43. }
  44. // Close all the open files
  45. void Openfiles::closeall()
  46. {
  47. for (auto& f: files) {
  48. f.close();
  49. }
  50. files.clear();
  51. }
  52. // The server-to-computational-peer protocol for sending precomputed
  53. // data is:
  54. //
  55. // One byte: type
  56. // 0x80: Multiplication triple
  57. // 0x81: Multiplication half-triple
  58. // 0x01 to 0x30: RAM DPF of that depth
  59. // 0x40: Comparison DPF
  60. // 0x00: End of preprocessing
  61. //
  62. // Four bytes: number of objects of that type (not sent for type == 0x00)
  63. //
  64. // Then that number of objects
  65. //
  66. // Repeat the whole thing until type == 0x00 is received
  67. void preprocessing_comp(MPCIO &mpcio, const PRACOptions &opts, char **args)
  68. {
  69. int num_threads = opts.num_threads;
  70. boost::asio::thread_pool pool(num_threads);
  71. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  72. boost::asio::post(pool, [&mpcio, &opts, thread_num] {
  73. MPCTIO tio(mpcio, thread_num);
  74. Openfiles ofiles;
  75. std::vector<coro_t> coroutines;
  76. while(1) {
  77. unsigned char type = 0;
  78. unsigned int num = 0;
  79. size_t res = tio.recv_server(&type, 1);
  80. if (res < 1 || type == 0) break;
  81. tio.recv_server(&num, 4);
  82. if (type == 0x80) {
  83. // Multiplication triples
  84. auto tripfile = ofiles.open("triples",
  85. mpcio.player, thread_num);
  86. MultTriple T;
  87. for (unsigned int i=0; i<num; ++i) {
  88. T = tio.triple();
  89. tripfile.os() << T;
  90. }
  91. } else if (type == 0x81) {
  92. // Multiplication half triples
  93. auto halffile = ofiles.open("halves",
  94. mpcio.player, thread_num);
  95. HalfTriple H;
  96. for (unsigned int i=0; i<num; ++i) {
  97. H = tio.halftriple();
  98. halffile.os() << H;
  99. }
  100. } else if (type >= 0x01 && type <= 0x30) {
  101. // RAM DPFs
  102. auto tripfile = ofiles.open("rdpf",
  103. mpcio.player, thread_num, type);
  104. for (unsigned int i=0; i<num; ++i) {
  105. coroutines.emplace_back(
  106. [&, tripfile, type](yield_t &yield) {
  107. RDPFTriple rdpftrip(tio, yield, type,
  108. opts.expand_rdpfs);
  109. printf("usi0 = %016lx\n", rdpftrip.dpf[0].unit_sum_inverse);
  110. printf("sxr0 = %016lx\n", rdpftrip.dpf[0].scaled_xor.xshare);
  111. printf("usi1 = %016lx\n", rdpftrip.dpf[1].unit_sum_inverse);
  112. printf("sxr1 = %016lx\n", rdpftrip.dpf[1].scaled_xor.xshare);
  113. printf("usi2 = %016lx\n", rdpftrip.dpf[2].unit_sum_inverse);
  114. printf("sxr2 = %016lx\n", rdpftrip.dpf[2].scaled_xor.xshare);
  115. tio.iostream_server() <<
  116. rdpftrip.dpf[(mpcio.player == 0) ? 1 : 2];
  117. tripfile.os() << rdpftrip;
  118. });
  119. }
  120. }
  121. }
  122. run_coroutines(tio, coroutines);
  123. ofiles.closeall();
  124. });
  125. }
  126. pool.join();
  127. }
  128. void preprocessing_server(MPCServerIO &mpcsrvio, const PRACOptions &opts, char **args)
  129. {
  130. int num_threads = opts.num_threads;
  131. boost::asio::thread_pool pool(num_threads);
  132. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  133. boost::asio::post(pool, [&mpcsrvio, &opts, thread_num, args] {
  134. char **threadargs = args;
  135. MPCTIO stio(mpcsrvio, thread_num);
  136. Openfiles ofiles;
  137. std::vector<coro_t> coroutines;
  138. if (*threadargs && threadargs[0][0] == 'T') {
  139. // Per-thread initialization. The args look like:
  140. // T0 t:50 h:10 T1 t:20 h:30 T2 h:20
  141. // Skip to the arg marking our thread
  142. char us[20];
  143. sprintf(us, "T%u", thread_num);
  144. while (*threadargs && strcmp(*threadargs, us)) {
  145. ++threadargs;
  146. }
  147. // Now skip to the next arg if there is one
  148. if (*threadargs) {
  149. ++threadargs;
  150. }
  151. }
  152. // Stop scanning for args when we get to the end or when we
  153. // get to another per-thread initialization marker
  154. while (*threadargs && threadargs[0][0] != 'T') {
  155. char *arg = strdup(*threadargs);
  156. char *colon = strchr(arg, ':');
  157. if (!colon) {
  158. std::cerr << "Args must be type:num\n";
  159. ++threadargs;
  160. free(arg);
  161. continue;
  162. }
  163. unsigned num = atoi(colon+1);
  164. *colon = '\0';
  165. char *type = arg;
  166. if (!strcmp(type, "t")) {
  167. unsigned char typetag = 0x80;
  168. stio.queue_p0(&typetag, 1);
  169. stio.queue_p0(&num, 4);
  170. stio.queue_p1(&typetag, 1);
  171. stio.queue_p1(&num, 4);
  172. for (unsigned int i=0; i<num; ++i) {
  173. stio.triple();
  174. }
  175. } else if (!strcmp(type, "h")) {
  176. unsigned char typetag = 0x81;
  177. stio.queue_p0(&typetag, 1);
  178. stio.queue_p0(&num, 4);
  179. stio.queue_p1(&typetag, 1);
  180. stio.queue_p1(&num, 4);
  181. for (unsigned int i=0; i<num; ++i) {
  182. stio.halftriple();
  183. }
  184. } else if (type[0] == 'r') {
  185. int depth = atoi(type+1);
  186. if (depth < 1 || depth > 48) {
  187. std::cerr << "Invalid DPF depth\n";
  188. } else {
  189. unsigned char typetag = depth;
  190. stio.queue_p0(&typetag, 1);
  191. stio.queue_p0(&num, 4);
  192. stio.queue_p1(&typetag, 1);
  193. stio.queue_p1(&num, 4);
  194. auto pairfile = ofiles.open("rdpf",
  195. mpcsrvio.player, thread_num, depth);
  196. for (unsigned int i=0; i<num; ++i) {
  197. coroutines.emplace_back(
  198. [&, pairfile, depth](yield_t &yield) {
  199. RDPFTriple rdpftrip(stio, yield, depth);
  200. RDPFPair rdpfpair;
  201. stio.iostream_p0() >> rdpfpair.dpf[0];
  202. stio.iostream_p1() >> rdpfpair.dpf[1];
  203. printf("usi0 = %016lx\n", rdpfpair.dpf[0].unit_sum_inverse);
  204. printf("sxr0 = %016lx\n", rdpfpair.dpf[0].scaled_xor.xshare);
  205. printf("dep0 = %d\n", rdpfpair.dpf[0].depth());
  206. printf("usi1 = %016lx\n", rdpfpair.dpf[1].unit_sum_inverse);
  207. printf("sxr1 = %016lx\n", rdpfpair.dpf[1].scaled_xor.xshare);
  208. printf("dep1 = %d\n", rdpfpair.dpf[1].depth());
  209. if (opts.expand_rdpfs) {
  210. printf("Expanding\n");
  211. rdpfpair.dpf[0].expand(stio.aes_ops());
  212. rdpfpair.dpf[1].expand(stio.aes_ops());
  213. printf("Expanded\n");
  214. }
  215. pairfile.os() << rdpfpair;
  216. });
  217. }
  218. }
  219. }
  220. free(arg);
  221. ++threadargs;
  222. }
  223. // That's all
  224. unsigned char typetag = 0x00;
  225. stio.queue_p0(&typetag, 1);
  226. stio.queue_p1(&typetag, 1);
  227. run_coroutines(stio, coroutines);
  228. ofiles.closeall();
  229. });
  230. }
  231. pool.join();
  232. }