preproc.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <vector>
  2. #include "types.hpp"
  3. #include "coroutine.hpp"
  4. #include "preproc.hpp"
  5. #include "rdpf.hpp"
  6. // Open a file for writing with name the given prefix, and ".pX.tY"
  7. // suffix, where X is the (one-digit) player number and Y is the thread
  8. // number
  9. static std::ofstream openfile(const char *prefix, unsigned player,
  10. unsigned thread_num)
  11. {
  12. std::string filename(prefix);
  13. char suffix[20];
  14. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  15. filename.append(suffix);
  16. std::ofstream f;
  17. f.open(filename);
  18. if (f.fail()) {
  19. std::cerr << "Failed to open " << filename << "\n";
  20. exit(1);
  21. }
  22. return f;
  23. }
  24. // The server-to-computational-peer protocol for sending precomputed
  25. // data is:
  26. //
  27. // One byte: type
  28. // 0x80: Multiplication triple
  29. // 0x81: Multiplication half-triple
  30. // 0x01 to 0x30: RAM DPF of that depth
  31. // 0x40: Comparison DPF
  32. // 0x00: End of preprocessing
  33. //
  34. // Four bytes: number of objects of that type (not sent for type == 0x00)
  35. //
  36. // Then that number of objects
  37. //
  38. // Repeat the whole thing until type == 0x00 is received
  39. void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args)
  40. {
  41. boost::asio::thread_pool pool(num_threads);
  42. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  43. boost::asio::post(pool, [&mpcio, thread_num] {
  44. MPCTIO tio(mpcio, thread_num);
  45. std::vector<coro_t> coroutines;
  46. while(1) {
  47. unsigned char type = 0;
  48. unsigned int num = 0;
  49. size_t res = tio.recv_server(&type, 1);
  50. if (res < 1 || type == 0) break;
  51. tio.recv_server(&num, 4);
  52. if (type == 0x80) {
  53. // Multiplication triples
  54. std::ofstream tripfile = openfile("triples",
  55. mpcio.player, thread_num);
  56. MultTriple T;
  57. for (unsigned int i=0; i<num; ++i) {
  58. T = tio.triple();
  59. tripfile.write((const char *)&T, sizeof(T));
  60. }
  61. tripfile.close();
  62. } else if (type == 0x81) {
  63. // Multiplication half triples
  64. std::ofstream halffile = openfile("halves",
  65. mpcio.player, thread_num);
  66. HalfTriple H;
  67. for (unsigned int i=0; i<num; ++i) {
  68. res = tio.recv_server(&H, sizeof(H));
  69. if (res < sizeof(H)) break;
  70. halffile.write((const char *)&H, sizeof(H));
  71. }
  72. halffile.close();
  73. } else if (type >= 0x01 && type <= 0x30) {
  74. // RAM DPFs
  75. for (unsigned int i=0; i<num; ++i) {
  76. coroutines.emplace_back(
  77. [&](yield_t &yield) {
  78. RegXS ri;
  79. ri.randomize(type);
  80. RDPF rdpf(tio, yield, ri, type);
  81. });
  82. }
  83. }
  84. }
  85. run_coroutines(tio, coroutines);
  86. });
  87. }
  88. pool.join();
  89. }
  90. void preprocessing_server(MPCServerIO &mpcsrvio, int num_threads, char **args)
  91. {
  92. boost::asio::thread_pool pool(num_threads);
  93. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  94. boost::asio::post(pool, [&mpcsrvio, thread_num, args] {
  95. char **threadargs = args;
  96. MPCTIO stio(mpcsrvio, thread_num);
  97. std::vector<coro_t> coroutines;
  98. if (*threadargs && threadargs[0][0] == 'T') {
  99. // Per-thread initialization. The args look like:
  100. // T0 t:50 h:10 T1 t:20 h:30 T2 h:20
  101. // Skip to the arg marking our thread
  102. char us[20];
  103. sprintf(us, "T%u", thread_num);
  104. while (*threadargs && strcmp(*threadargs, us)) {
  105. ++threadargs;
  106. }
  107. // Now skip to the next arg if there is one
  108. if (*threadargs) {
  109. ++threadargs;
  110. }
  111. }
  112. // Stop scanning for args when we get to the end or when we
  113. // get to another per-thread initialization marker
  114. while (*threadargs && threadargs[0][0] != 'T') {
  115. char *arg = strdup(*threadargs);
  116. char *colon = strchr(arg, ':');
  117. if (!colon) {
  118. std::cerr << "Args must be type:num\n";
  119. ++threadargs;
  120. free(arg);
  121. continue;
  122. }
  123. unsigned num = atoi(colon+1);
  124. *colon = '\0';
  125. char *type = arg;
  126. if (!strcmp(type, "t")) {
  127. unsigned char typetag = 0x80;
  128. stio.queue_p0(&typetag, 1);
  129. stio.queue_p0(&num, 4);
  130. stio.queue_p1(&typetag, 1);
  131. stio.queue_p1(&num, 4);
  132. for (unsigned int i=0; i<num; ++i) {
  133. stio.triple();
  134. }
  135. } else if (!strcmp(type, "h")) {
  136. unsigned char typetag = 0x81;
  137. stio.queue_p0(&typetag, 1);
  138. stio.queue_p0(&num, 4);
  139. stio.queue_p1(&typetag, 1);
  140. stio.queue_p1(&num, 4);
  141. for (unsigned int i=0; i<num; ++i) {
  142. stio.halftriple();
  143. }
  144. } else if (type[0] == 'r') {
  145. int depth = atoi(type+1);
  146. if (depth < 1 || depth > 48) {
  147. std::cerr << "Invalid DPF depth\n";
  148. } else {
  149. unsigned char typetag = depth;
  150. stio.queue_p0(&typetag, 1);
  151. stio.queue_p0(&num, 4);
  152. stio.queue_p1(&typetag, 1);
  153. stio.queue_p1(&num, 4);
  154. for (unsigned int i=0; i<num; ++i) {
  155. coroutines.emplace_back(
  156. [&](yield_t &yield) {
  157. RegXS ri;
  158. RDPF rdpf(stio, yield, ri, depth);
  159. });
  160. }
  161. }
  162. }
  163. free(arg);
  164. ++threadargs;
  165. }
  166. // That's all
  167. unsigned char typetag = 0x00;
  168. stio.queue_p0(&typetag, 1);
  169. stio.queue_p1(&typetag, 1);
  170. run_coroutines(stio, coroutines);
  171. });
  172. }
  173. pool.join();
  174. }