preproc.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. H = tio.halftriple();
  69. halffile.write((const char *)&H, sizeof(H));
  70. }
  71. halffile.close();
  72. } else if (type >= 0x01 && type <= 0x30) {
  73. // RAM DPFs
  74. for (unsigned int i=0; i<num; ++i) {
  75. coroutines.emplace_back(
  76. [&](yield_t &yield) {
  77. RegXS ri;
  78. ri.randomize(type);
  79. RDPF rdpf(tio, yield, ri, type);
  80. });
  81. }
  82. }
  83. }
  84. run_coroutines(tio, coroutines);
  85. });
  86. }
  87. pool.join();
  88. }
  89. void preprocessing_server(MPCServerIO &mpcsrvio, int num_threads, char **args)
  90. {
  91. boost::asio::thread_pool pool(num_threads);
  92. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  93. boost::asio::post(pool, [&mpcsrvio, thread_num, args] {
  94. char **threadargs = args;
  95. MPCTIO stio(mpcsrvio, thread_num);
  96. std::vector<coro_t> coroutines;
  97. if (*threadargs && threadargs[0][0] == 'T') {
  98. // Per-thread initialization. The args look like:
  99. // T0 t:50 h:10 T1 t:20 h:30 T2 h:20
  100. // Skip to the arg marking our thread
  101. char us[20];
  102. sprintf(us, "T%u", thread_num);
  103. while (*threadargs && strcmp(*threadargs, us)) {
  104. ++threadargs;
  105. }
  106. // Now skip to the next arg if there is one
  107. if (*threadargs) {
  108. ++threadargs;
  109. }
  110. }
  111. // Stop scanning for args when we get to the end or when we
  112. // get to another per-thread initialization marker
  113. while (*threadargs && threadargs[0][0] != 'T') {
  114. char *arg = strdup(*threadargs);
  115. char *colon = strchr(arg, ':');
  116. if (!colon) {
  117. std::cerr << "Args must be type:num\n";
  118. ++threadargs;
  119. free(arg);
  120. continue;
  121. }
  122. unsigned num = atoi(colon+1);
  123. *colon = '\0';
  124. char *type = arg;
  125. if (!strcmp(type, "t")) {
  126. unsigned char typetag = 0x80;
  127. stio.queue_p0(&typetag, 1);
  128. stio.queue_p0(&num, 4);
  129. stio.queue_p1(&typetag, 1);
  130. stio.queue_p1(&num, 4);
  131. for (unsigned int i=0; i<num; ++i) {
  132. stio.triple();
  133. }
  134. } else if (!strcmp(type, "h")) {
  135. unsigned char typetag = 0x81;
  136. stio.queue_p0(&typetag, 1);
  137. stio.queue_p0(&num, 4);
  138. stio.queue_p1(&typetag, 1);
  139. stio.queue_p1(&num, 4);
  140. for (unsigned int i=0; i<num; ++i) {
  141. stio.halftriple();
  142. }
  143. } else if (type[0] == 'r') {
  144. int depth = atoi(type+1);
  145. if (depth < 1 || depth > 48) {
  146. std::cerr << "Invalid DPF depth\n";
  147. } else {
  148. unsigned char typetag = depth;
  149. stio.queue_p0(&typetag, 1);
  150. stio.queue_p0(&num, 4);
  151. stio.queue_p1(&typetag, 1);
  152. stio.queue_p1(&num, 4);
  153. for (unsigned int i=0; i<num; ++i) {
  154. coroutines.emplace_back(
  155. [&](yield_t &yield) {
  156. RegXS ri;
  157. RDPF rdpf(stio, yield, ri, depth);
  158. });
  159. }
  160. }
  161. }
  162. free(arg);
  163. ++threadargs;
  164. }
  165. // That's all
  166. unsigned char typetag = 0x00;
  167. stio.queue_p0(&typetag, 1);
  168. stio.queue_p1(&typetag, 1);
  169. run_coroutines(stio, coroutines);
  170. });
  171. }
  172. pool.join();
  173. }