prac.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <iostream>
  2. #include <deque>
  3. #include "mpcio.hpp"
  4. #include "preproc.hpp"
  5. #include "online.hpp"
  6. #include "options.hpp"
  7. static void usage(const char *progname)
  8. {
  9. std::cerr << "Usage: " << progname << " [-p] [-t num] player_num player_addrs args ...\n";
  10. std::cerr << "-p: preprocessing mode\n";
  11. std::cerr << "-t num: use num threads\n";
  12. std::cerr << "-c: store DPFs compressed (default is expanded)\n";
  13. std::cerr << "-x: use XOR-shared database (default is additive)\n";
  14. std::cerr << "player_num = 0 or 1 for the computational players\n";
  15. std::cerr << "player_num = 2 for the server player\n";
  16. std::cerr << "player_addrs is omitted for player 0\n";
  17. std::cerr << "player_addrs is p0's hostname for player 1\n";
  18. std::cerr << "player_addrs is p0's hostname followed by p1's hostname for player 2\n";
  19. exit(1);
  20. }
  21. static void comp_player_main(boost::asio::io_context &io_context,
  22. unsigned player, const PRACOptions &opts, const char *p0addr,
  23. char **args)
  24. {
  25. std::deque<tcp::socket> peersocks, serversocks;
  26. mpcio_setup_computational(player, io_context, p0addr,
  27. opts.num_threads, peersocks, serversocks);
  28. MPCPeerIO mpcio(player, opts.preprocessing, peersocks, serversocks);
  29. // Queue up the work to be done
  30. boost::asio::post(io_context, [&]{
  31. if (opts.preprocessing) {
  32. preprocessing_comp(mpcio, opts, args);
  33. } else {
  34. online_main(mpcio, opts, args);
  35. }
  36. });
  37. // Start another thread; one will perform the work and the other
  38. // will execute the async_write handlers
  39. boost::thread t([&]{io_context.run();});
  40. io_context.run();
  41. t.join();
  42. mpcio.dump_stats(std::cout);
  43. }
  44. static void server_player_main(boost::asio::io_context &io_context,
  45. const PRACOptions &opts, const char *p0addr,
  46. const char *p1addr, char **args)
  47. {
  48. std::deque<tcp::socket> p0socks, p1socks;
  49. mpcio_setup_server(io_context, p0addr, p1addr,
  50. opts.num_threads, p0socks, p1socks);
  51. MPCServerIO mpcserverio(opts.preprocessing, p0socks, p1socks);
  52. // Queue up the work to be done
  53. boost::asio::post(io_context, [&]{
  54. if (opts.preprocessing) {
  55. preprocessing_server(mpcserverio, opts, args);
  56. } else {
  57. online_main(mpcserverio, opts, args);
  58. }
  59. });
  60. // Start another thread; one will perform the work and the other
  61. // will execute the async_write handlers
  62. boost::thread t([&]{io_context.run();});
  63. io_context.run();
  64. t.join();
  65. mpcserverio.dump_stats(std::cout);
  66. }
  67. int main(int argc, char **argv)
  68. {
  69. char **args = argv+1; // Skip argv[0] (the program name)
  70. PRACOptions opts;
  71. unsigned player = 0;
  72. const char *p0addr = NULL;
  73. const char *p1addr = NULL;
  74. // Get the options
  75. while (*args && *args[0] == '-') {
  76. if (!strcmp("-p", *args)) {
  77. opts.preprocessing = true;
  78. ++args;
  79. } else if (!strcmp("-t", *args)) {
  80. if (args[1]) {
  81. opts.num_threads = atoi(args[1]);
  82. if (opts.num_threads < 1) {
  83. usage(argv[0]);
  84. }
  85. args += 2;
  86. } else {
  87. usage(argv[0]);
  88. }
  89. } else if (!strcmp("-c", *args)) {
  90. opts.expand_rdpfs = false;
  91. ++args;
  92. } else if (!strcmp("-x", *args)) {
  93. opts.use_xor_db = true;
  94. ++args;
  95. }
  96. }
  97. if (*args == NULL) {
  98. // No arguments?
  99. usage(argv[0]);
  100. } else {
  101. player = atoi(*args);
  102. ++args;
  103. }
  104. if (player > 2) {
  105. usage(argv[0]);
  106. }
  107. if (player > 0) {
  108. if (*args == NULL) {
  109. usage(argv[0]);
  110. } else {
  111. p0addr = *args;
  112. ++args;
  113. }
  114. }
  115. if (player > 1) {
  116. if (*args == NULL) {
  117. usage(argv[0]);
  118. } else {
  119. p1addr = *args;
  120. ++args;
  121. }
  122. }
  123. /*
  124. std::cout << "Preprocessing = " <<
  125. (preprocessing ? "true" : "false") << "\n";
  126. std::cout << "Thread count = " << num_threads << "\n";
  127. std::cout << "Player = " << player << "\n";
  128. if (p0addr) {
  129. std::cout << "Player 0 addr = " << p0addr << "\n";
  130. }
  131. if (p1addr) {
  132. std::cout << "Player 1 addr = " << p1addr << "\n";
  133. }
  134. std::cout << "Args =";
  135. for (char **a = args; *a; ++a) {
  136. std::cout << " " << *a;
  137. }
  138. std::cout << "\n";
  139. */
  140. // Make the network connections
  141. boost::asio::io_context io_context;
  142. if (player < 2) {
  143. comp_player_main(io_context, player, opts, p0addr, args);
  144. } else {
  145. server_player_main(io_context, opts, p0addr, p1addr, args);
  146. }
  147. return 0;
  148. }