mpcio.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. #include <sys/time.h> // getrusage
  2. #include <sys/resource.h> // getrusage
  3. #include "mpcio.hpp"
  4. #include "rdpf.hpp"
  5. #include "cdpf.hpp"
  6. #include "bitutils.hpp"
  7. // T is the type being stored
  8. // N is a type whose "name" static member is a string naming the type
  9. // so that we can report something useful to the user if they try
  10. // to read a type that we don't have any more values for
  11. template<typename T, typename N>
  12. PreCompStorage<T,N>::PreCompStorage(unsigned player, bool preprocessing,
  13. const char *filenameprefix, unsigned thread_num) :
  14. name(N::name), depth(0)
  15. {
  16. init(player, preprocessing, filenameprefix, thread_num);
  17. }
  18. template<typename T, typename N>
  19. void PreCompStorage<T,N>::init(unsigned player, bool preprocessing,
  20. const char *filenameprefix, unsigned thread_num, nbits_t depth)
  21. {
  22. if (preprocessing) return;
  23. std::string filename(filenameprefix);
  24. char suffix[20];
  25. if (depth) {
  26. this->depth = depth;
  27. sprintf(suffix, "%02d.p%d.t%u", depth, player%10, thread_num);
  28. } else {
  29. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  30. }
  31. filename.append(suffix);
  32. storage.open(filename);
  33. // It's OK if not every file exists; so don't worry about checking
  34. // for errors here. We'll report an error in get() if we actually
  35. // try to use a value for which we don't have a precomputed file.
  36. count = 0;
  37. }
  38. template<typename T, typename N>
  39. void PreCompStorage<T,N>::get(T& nextval)
  40. {
  41. storage >> nextval;
  42. if (!storage.good()) {
  43. std::cerr << "Failed to read precomputed value from " << name;
  44. if (depth) {
  45. std::cerr << (int)depth;
  46. }
  47. std::cerr << " storage\n";
  48. exit(1);
  49. }
  50. ++count;
  51. }
  52. void MPCSingleIO::async_send_from_msgqueue()
  53. {
  54. #ifdef SEND_LAMPORT_CLOCKS
  55. std::vector<boost::asio::const_buffer> tosend;
  56. tosend.push_back(boost::asio::buffer(messagequeue.front().header));
  57. tosend.push_back(boost::asio::buffer(messagequeue.front().message));
  58. #endif
  59. boost::asio::async_write(sock,
  60. #ifdef SEND_LAMPORT_CLOCKS
  61. tosend,
  62. #else
  63. boost::asio::buffer(messagequeue.front()),
  64. #endif
  65. [&](boost::system::error_code ec, std::size_t amt){
  66. messagequeuelock.lock();
  67. messagequeue.pop();
  68. if (messagequeue.size() > 0) {
  69. async_send_from_msgqueue();
  70. }
  71. messagequeuelock.unlock();
  72. });
  73. }
  74. size_t MPCSingleIO::queue(const void *data, size_t len, lamport_t lamport)
  75. {
  76. // Is this a new message?
  77. size_t newmsg = 0;
  78. dataqueue.append((const char *)data, len);
  79. // If this is the first queue() since the last explicit send(),
  80. // which we'll know because message_lamport will be nullopt, set
  81. // message_lamport to the current Lamport clock. Note that the
  82. // boolean test tests whether message_lamport is nullopt, not
  83. // whether its value is zero.
  84. if (!message_lamport) {
  85. message_lamport = lamport;
  86. newmsg = 1;
  87. }
  88. // If we already have some full packets worth of data, may as
  89. // well send it.
  90. if (dataqueue.size() > 28800) {
  91. send(true);
  92. }
  93. return newmsg;
  94. }
  95. void MPCSingleIO::send(bool implicit_send)
  96. {
  97. size_t thissize = dataqueue.size();
  98. // Ignore spurious calls to send(), except for resetting
  99. // message_lamport if this was an explicit send().
  100. if (thissize == 0) {
  101. #ifdef SEND_LAMPORT_CLOCKS
  102. // If this was an explicit send(), reset the message_lamport so
  103. // that it gets updated at the next queue().
  104. if (!implicit_send) {
  105. message_lamport.reset();
  106. }
  107. #endif
  108. return;
  109. }
  110. #ifdef RECORD_IOTRACE
  111. iotrace.push_back(thissize);
  112. #endif
  113. messagequeuelock.lock();
  114. // Move the current message to send into the message queue (this
  115. // moves a pointer to the data, not copying the data itself)
  116. #ifdef SEND_LAMPORT_CLOCKS
  117. messagequeue.emplace(std::move(dataqueue),
  118. message_lamport.value());
  119. // If this was an explicit send(), reset the message_lamport so
  120. // that it gets updated at the next queue().
  121. if (!implicit_send) {
  122. message_lamport.reset();
  123. }
  124. #else
  125. messagequeue.emplace(std::move(dataqueue));
  126. #endif
  127. // If this is now the first thing in the message queue, launch
  128. // an async_write to write it
  129. if (messagequeue.size() == 1) {
  130. async_send_from_msgqueue();
  131. }
  132. messagequeuelock.unlock();
  133. }
  134. size_t MPCSingleIO::recv(void *data, size_t len, lamport_t &lamport)
  135. {
  136. #ifdef SEND_LAMPORT_CLOCKS
  137. char *cdata = (char *)data;
  138. size_t res = 0;
  139. while (len > 0) {
  140. while (recvdataremain == 0) {
  141. // Read a new header
  142. char hdr[sizeof(uint32_t) + sizeof(lamport_t)];
  143. uint32_t datalen;
  144. lamport_t recv_lamport;
  145. boost::asio::read(sock, boost::asio::buffer(hdr, sizeof(hdr)));
  146. memmove(&datalen, hdr, sizeof(datalen));
  147. memmove(&recv_lamport, hdr+sizeof(datalen), sizeof(lamport_t));
  148. lamport_t new_lamport = recv_lamport + 1;
  149. if (lamport < new_lamport) {
  150. lamport = new_lamport;
  151. }
  152. if (datalen > 0) {
  153. recvdata.resize(datalen, '\0');
  154. boost::asio::read(sock, boost::asio::buffer(recvdata));
  155. recvdataremain = datalen;
  156. }
  157. }
  158. size_t amttoread = len;
  159. if (amttoread > recvdataremain) {
  160. amttoread = recvdataremain;
  161. }
  162. memmove(cdata, recvdata.data()+recvdata.size()-recvdataremain,
  163. amttoread);
  164. cdata += amttoread;
  165. len -= amttoread;
  166. recvdataremain -= amttoread;
  167. res += amttoread;
  168. }
  169. #else
  170. size_t res = boost::asio::read(sock, boost::asio::buffer(data, len));
  171. #endif
  172. #ifdef RECORD_IOTRACE
  173. iotrace.push_back(-(ssize_t(res)));
  174. #endif
  175. return res;
  176. }
  177. #ifdef RECORD_IOTRACE
  178. void MPCSingleIO::dumptrace(std::ostream &os, const char *label)
  179. {
  180. if (label) {
  181. os << label << " ";
  182. }
  183. os << "IO trace:";
  184. for (auto& s: iotrace) {
  185. os << " " << s;
  186. }
  187. os << "\n";
  188. }
  189. #endif
  190. void MPCIO::reset_stats()
  191. {
  192. msgs_sent.clear();
  193. msg_bytes_sent.clear();
  194. aes_ops.clear();
  195. for (size_t i=0; i<num_threads; ++i) {
  196. msgs_sent.push_back(0);
  197. msg_bytes_sent.push_back(0);
  198. aes_ops.push_back(0);
  199. }
  200. steady_start = boost::chrono::steady_clock::now();
  201. cpu_start = boost::chrono::process_cpu_clock::now();
  202. }
  203. // Report the memory usage
  204. void MPCIO::dump_memusage(std::ostream &os)
  205. {
  206. struct rusage ru;
  207. getrusage(RUSAGE_SELF, &ru);
  208. os << "Mem: " << ru.ru_maxrss << " KiB\n";
  209. }
  210. void MPCIO::dump_stats(std::ostream &os)
  211. {
  212. size_t tot_msgs_sent = 0;
  213. size_t tot_msg_bytes_sent = 0;
  214. size_t tot_aes_ops = 0;
  215. for (auto& n : msgs_sent) {
  216. tot_msgs_sent += n;
  217. }
  218. for (auto& n : msg_bytes_sent) {
  219. tot_msg_bytes_sent += n;
  220. }
  221. for (auto& n : aes_ops) {
  222. tot_aes_ops += n;
  223. }
  224. auto steady_elapsed =
  225. boost::chrono::steady_clock::now() - steady_start;
  226. auto cpu_elapsed =
  227. boost::chrono::process_cpu_clock::now() - cpu_start;
  228. os << tot_msgs_sent << " messages sent\n";
  229. os << tot_msg_bytes_sent << " message bytes sent\n";
  230. os << lamport << " Lamport clock (latencies)\n";
  231. os << tot_aes_ops << " local AES operations\n";
  232. os << boost::chrono::duration_cast
  233. <boost::chrono::milliseconds>(steady_elapsed) <<
  234. " wall clock time\n";
  235. os << cpu_elapsed << " {real;user;system}\n";
  236. dump_memusage(os);
  237. }
  238. MPCPeerIO::MPCPeerIO(unsigned player, bool preprocessing,
  239. std::deque<tcp::socket> &peersocks,
  240. std::deque<tcp::socket> &serversocks) :
  241. MPCIO(player, preprocessing, peersocks.size())
  242. {
  243. unsigned num_threads = unsigned(peersocks.size());
  244. for (unsigned i=0; i<num_threads; ++i) {
  245. triples.emplace_back(player, preprocessing, "triples", i);
  246. }
  247. for (unsigned i=0; i<num_threads; ++i) {
  248. halftriples.emplace_back(player, preprocessing, "halves", i);
  249. }
  250. rdpftriples.resize(num_threads);
  251. for (unsigned i=0; i<num_threads; ++i) {
  252. for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  253. rdpftriples[i][depth-1].init(player, preprocessing,
  254. "rdpf", i, depth);
  255. }
  256. }
  257. for (unsigned i=0; i<num_threads; ++i) {
  258. cdpfs.emplace_back(player, preprocessing, "cdpf", i);
  259. }
  260. for (auto &&sock : peersocks) {
  261. peerios.emplace_back(std::move(sock));
  262. }
  263. for (auto &&sock : serversocks) {
  264. serverios.emplace_back(std::move(sock));
  265. }
  266. }
  267. void MPCPeerIO::dump_precomp_stats(std::ostream &os)
  268. {
  269. for (size_t i=0; i<triples.size(); ++i) {
  270. if (i > 0) {
  271. os << " ";
  272. }
  273. os << "T" << i << " t:" << triples[i].get_stats() <<
  274. " h:" << halftriples[i].get_stats();
  275. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  276. size_t cnt = rdpftriples[i][depth-1].get_stats();
  277. if (cnt > 0) {
  278. os << " r" << int(depth) << ":" << cnt;
  279. }
  280. }
  281. }
  282. os << "\n";
  283. }
  284. void MPCPeerIO::reset_precomp_stats()
  285. {
  286. for (size_t i=0; i<triples.size(); ++i) {
  287. triples[i].reset_stats();
  288. halftriples[i].reset_stats();
  289. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  290. rdpftriples[i][depth-1].reset_stats();
  291. }
  292. }
  293. }
  294. void MPCPeerIO::dump_stats(std::ostream &os)
  295. {
  296. MPCIO::dump_stats(os);
  297. os << "Precomputed values used: ";
  298. dump_precomp_stats(os);
  299. }
  300. MPCServerIO::MPCServerIO(bool preprocessing,
  301. std::deque<tcp::socket> &p0socks,
  302. std::deque<tcp::socket> &p1socks) :
  303. MPCIO(2, preprocessing, p0socks.size())
  304. {
  305. rdpfpairs.resize(num_threads);
  306. for (unsigned i=0; i<num_threads; ++i) {
  307. for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  308. rdpfpairs[i][depth-1].init(player, preprocessing,
  309. "rdpf", i, depth);
  310. }
  311. }
  312. for (auto &&sock : p0socks) {
  313. p0ios.emplace_back(std::move(sock));
  314. }
  315. for (auto &&sock : p1socks) {
  316. p1ios.emplace_back(std::move(sock));
  317. }
  318. }
  319. void MPCServerIO::dump_precomp_stats(std::ostream &os)
  320. {
  321. for (size_t i=0; i<rdpfpairs.size(); ++i) {
  322. if (i > 0) {
  323. os << " ";
  324. }
  325. os << "T" << i;
  326. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  327. size_t cnt = rdpfpairs[i][depth-1].get_stats();
  328. if (cnt > 0) {
  329. os << " r" << int(depth) << ":" << cnt;
  330. }
  331. }
  332. }
  333. os << "\n";
  334. }
  335. void MPCServerIO::reset_precomp_stats()
  336. {
  337. for (size_t i=0; i<rdpfpairs.size(); ++i) {
  338. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  339. rdpfpairs[i][depth-1].reset_stats();
  340. }
  341. }
  342. }
  343. void MPCServerIO::dump_stats(std::ostream &os)
  344. {
  345. MPCIO::dump_stats(os);
  346. os << "Precomputed values used: ";
  347. dump_precomp_stats(os);
  348. }
  349. MPCTIO::MPCTIO(MPCIO &mpcio, int thread_num) :
  350. thread_num(thread_num), thread_lamport(mpcio.lamport),
  351. mpcio(mpcio)
  352. {
  353. if (mpcio.player < 2) {
  354. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  355. peer_iostream.emplace(mpcpio.peerios[thread_num],
  356. thread_lamport, mpcpio.msgs_sent[thread_num],
  357. mpcpio.msg_bytes_sent[thread_num]);
  358. server_iostream.emplace(mpcpio.serverios[thread_num],
  359. thread_lamport, mpcpio.msgs_sent[thread_num],
  360. mpcpio.msg_bytes_sent[thread_num]);
  361. } else {
  362. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  363. p0_iostream.emplace(mpcsrvio.p0ios[thread_num],
  364. thread_lamport, mpcsrvio.msgs_sent[thread_num],
  365. mpcsrvio.msg_bytes_sent[thread_num]);
  366. p1_iostream.emplace(mpcsrvio.p1ios[thread_num],
  367. thread_lamport, mpcsrvio.msgs_sent[thread_num],
  368. mpcsrvio.msg_bytes_sent[thread_num]);
  369. }
  370. }
  371. // Sync our per-thread lamport clock with the master one in the
  372. // mpcio. You only need to call this explicitly if your MPCTIO
  373. // outlives your thread (in which case call it after the join), or
  374. // if your threads do interthread communication amongst themselves
  375. // (in which case call it in the sending thread before the send, and
  376. // call it in the receiving thread after the receive).
  377. void MPCTIO::sync_lamport()
  378. {
  379. // Update the mpcio Lamport time to be max of the thread Lamport
  380. // time and what we thought it was before. We use this
  381. // compare_exchange construction in order to atomically
  382. // do the comparison, computation, and replacement
  383. lamport_t old_lamport = mpcio.lamport;
  384. lamport_t new_lamport = thread_lamport;
  385. do {
  386. if (new_lamport < old_lamport) {
  387. new_lamport = old_lamport;
  388. }
  389. // The next line atomically checks if lamport still has
  390. // the value old_lamport; if so, it changes its value to
  391. // new_lamport and returns true (ending the loop). If
  392. // not, it sets old_lamport to the current value of
  393. // lamport, and returns false (continuing the loop so
  394. // that new_lamport can be recomputed based on this new
  395. // value).
  396. } while (!mpcio.lamport.compare_exchange_weak(
  397. old_lamport, new_lamport));
  398. thread_lamport = new_lamport;
  399. }
  400. // Queue up data to the peer or to the server
  401. void MPCTIO::queue_peer(const void *data, size_t len)
  402. {
  403. if (mpcio.player < 2) {
  404. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  405. size_t newmsg = mpcpio.peerios[thread_num].queue(data, len, thread_lamport);
  406. mpcpio.msgs_sent[thread_num] += newmsg;
  407. mpcpio.msg_bytes_sent[thread_num] += len;
  408. }
  409. }
  410. void MPCTIO::queue_server(const void *data, size_t len)
  411. {
  412. if (mpcio.player < 2) {
  413. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  414. size_t newmsg = mpcpio.serverios[thread_num].queue(data, len, thread_lamport);
  415. mpcpio.msgs_sent[thread_num] += newmsg;
  416. mpcpio.msg_bytes_sent[thread_num] += len;
  417. }
  418. }
  419. // Receive data from the peer or to the server
  420. size_t MPCTIO::recv_peer(void *data, size_t len)
  421. {
  422. if (mpcio.player < 2) {
  423. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  424. return mpcpio.peerios[thread_num].recv(data, len, thread_lamport);
  425. }
  426. return 0;
  427. }
  428. size_t MPCTIO::recv_server(void *data, size_t len)
  429. {
  430. if (mpcio.player < 2) {
  431. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  432. return mpcpio.serverios[thread_num].recv(data, len, thread_lamport);
  433. }
  434. return 0;
  435. }
  436. // Queue up data to p0 or p1
  437. void MPCTIO::queue_p0(const void *data, size_t len)
  438. {
  439. if (mpcio.player == 2) {
  440. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  441. size_t newmsg = mpcsrvio.p0ios[thread_num].queue(data, len, thread_lamport);
  442. mpcsrvio.msgs_sent[thread_num] += newmsg;
  443. mpcsrvio.msg_bytes_sent[thread_num] += len;
  444. }
  445. }
  446. void MPCTIO::queue_p1(const void *data, size_t len)
  447. {
  448. if (mpcio.player == 2) {
  449. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  450. size_t newmsg = mpcsrvio.p1ios[thread_num].queue(data, len, thread_lamport);
  451. mpcsrvio.msgs_sent[thread_num] += newmsg;
  452. mpcsrvio.msg_bytes_sent[thread_num] += len;
  453. }
  454. }
  455. // Receive data from p0 or p1
  456. size_t MPCTIO::recv_p0(void *data, size_t len)
  457. {
  458. if (mpcio.player == 2) {
  459. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  460. return mpcsrvio.p0ios[thread_num].recv(data, len, thread_lamport);
  461. }
  462. return 0;
  463. }
  464. size_t MPCTIO::recv_p1(void *data, size_t len)
  465. {
  466. if (mpcio.player == 2) {
  467. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  468. return mpcsrvio.p1ios[thread_num].recv(data, len, thread_lamport);
  469. }
  470. return 0;
  471. }
  472. // Send all queued data for this thread
  473. void MPCTIO::send()
  474. {
  475. if (mpcio.player < 2) {
  476. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  477. mpcpio.peerios[thread_num].send();
  478. mpcpio.serverios[thread_num].send();
  479. } else {
  480. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  481. mpcsrvio.p0ios[thread_num].send();
  482. mpcsrvio.p1ios[thread_num].send();
  483. }
  484. }
  485. // Functions to get precomputed values. If we're in the online
  486. // phase, get them from PreCompStorage. If we're in the
  487. // preprocessing phase, read them from the server.
  488. MultTriple MPCTIO::triple()
  489. {
  490. MultTriple val;
  491. if (mpcio.player < 2) {
  492. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  493. if (mpcpio.preprocessing) {
  494. recv_server(&val, sizeof(val));
  495. } else {
  496. mpcpio.triples[thread_num].get(val);
  497. }
  498. } else if (mpcio.preprocessing) {
  499. // Create triples (X0,Y0,Z0),(X1,Y1,Z1) such that
  500. // (X0*Y1 + Y0*X1) = (Z0+Z1)
  501. value_t X0, Y0, Z0, X1, Y1, Z1;
  502. arc4random_buf(&X0, sizeof(X0));
  503. arc4random_buf(&Y0, sizeof(Y0));
  504. arc4random_buf(&Z0, sizeof(Z0));
  505. arc4random_buf(&X1, sizeof(X1));
  506. arc4random_buf(&Y1, sizeof(Y1));
  507. Z1 = X0 * Y1 + X1 * Y0 - Z0;
  508. MultTriple T0, T1;
  509. T0 = std::make_tuple(X0, Y0, Z0);
  510. T1 = std::make_tuple(X1, Y1, Z1);
  511. queue_p0(&T0, sizeof(T0));
  512. queue_p1(&T1, sizeof(T1));
  513. }
  514. return val;
  515. }
  516. HalfTriple MPCTIO::halftriple()
  517. {
  518. HalfTriple val;
  519. if (mpcio.player < 2) {
  520. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  521. if (mpcpio.preprocessing) {
  522. recv_server(&val, sizeof(val));
  523. } else {
  524. mpcpio.halftriples[thread_num].get(val);
  525. }
  526. } else if (mpcio.preprocessing) {
  527. // Create half-triples (X0,Z0),(Y1,Z1) such that
  528. // X0*Y1 = Z0 + Z1
  529. value_t X0, Z0, Y1, Z1;
  530. arc4random_buf(&X0, sizeof(X0));
  531. arc4random_buf(&Z0, sizeof(Z0));
  532. arc4random_buf(&Y1, sizeof(Y1));
  533. Z1 = X0 * Y1 - Z0;
  534. HalfTriple H0, H1;
  535. H0 = std::make_tuple(X0, Z0);
  536. H1 = std::make_tuple(Y1, Z1);
  537. queue_p0(&H0, sizeof(H0));
  538. queue_p1(&H1, sizeof(H1));
  539. }
  540. return val;
  541. }
  542. SelectTriple MPCTIO::selecttriple()
  543. {
  544. SelectTriple val;
  545. if (mpcio.player < 2) {
  546. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  547. if (mpcpio.preprocessing) {
  548. uint8_t Xbyte;
  549. recv_server(&Xbyte, sizeof(Xbyte));
  550. val.X = Xbyte & 1;
  551. recv_server(&val.Y, sizeof(val.Y));
  552. recv_server(&val.Z, sizeof(val.Z));
  553. } else {
  554. std::cerr << "Attempted to read SelectTriple in online phase\n";
  555. }
  556. } else if (mpcio.preprocessing) {
  557. // Create triples (X0,Y0,Z0),(X1,Y1,Z1) such that
  558. // (X0*Y1 ^ Y0*X1) = (Z0^Z1)
  559. bit_t X0, X1;
  560. DPFnode Y0, Z0, Y1, Z1;
  561. X0 = arc4random() & 1;
  562. arc4random_buf(&Y0, sizeof(Y0));
  563. arc4random_buf(&Z0, sizeof(Z0));
  564. X1 = arc4random() & 1;
  565. arc4random_buf(&Y1, sizeof(Y1));
  566. DPFnode X0ext, X1ext;
  567. // Sign-extend X0 and X1 (so that 0 -> 0000...0 and
  568. // 1 -> 1111...1)
  569. X0ext = if128_mask[X0];
  570. X1ext = if128_mask[X1];
  571. Z1 = ((X0ext & Y1) ^ (X1ext & Y0)) ^ Z0;
  572. queue_p0(&X0, sizeof(X0));
  573. queue_p0(&Y0, sizeof(Y0));
  574. queue_p0(&Z0, sizeof(Z0));
  575. queue_p1(&X1, sizeof(X1));
  576. queue_p1(&Y1, sizeof(Y1));
  577. queue_p1(&Z1, sizeof(Z1));
  578. }
  579. return val;
  580. }
  581. RDPFTriple MPCTIO::rdpftriple(nbits_t depth)
  582. {
  583. RDPFTriple val;
  584. if (!mpcio.preprocessing && mpcio.player <= 2) {
  585. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  586. mpcpio.rdpftriples[thread_num][depth-1].get(val);
  587. }
  588. return val;
  589. }
  590. RDPFPair MPCTIO::rdpfpair(nbits_t depth)
  591. {
  592. RDPFPair val;
  593. if (!mpcio.preprocessing && mpcio.player == 2) {
  594. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  595. mpcsrvio.rdpfpairs[thread_num][depth-1].get(val);
  596. }
  597. return val;
  598. }
  599. CDPF MPCTIO::cdpf()
  600. {
  601. CDPF val;
  602. if (mpcio.player < 2) {
  603. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  604. if (mpcpio.preprocessing) {
  605. iostream_server() >> val;
  606. } else {
  607. mpcpio.cdpfs[thread_num].get(val);
  608. }
  609. } else if (mpcio.preprocessing) {
  610. auto [ cdpf0, cdpf1 ] = CDPF::generate(aes_ops());
  611. iostream_p0() << cdpf0;
  612. iostream_p1() << cdpf1;
  613. }
  614. return val;
  615. }
  616. // The port number for the P1 -> P0 connection
  617. static const unsigned short port_p1_p0 = 2115;
  618. // The port number for the P2 -> P0 connection
  619. static const unsigned short port_p2_p0 = 2116;
  620. // The port number for the P2 -> P1 connection
  621. static const unsigned short port_p2_p1 = 2117;
  622. void mpcio_setup_computational(unsigned player,
  623. boost::asio::io_context &io_context,
  624. const char *p0addr, // can be NULL when player=0
  625. int num_threads,
  626. std::deque<tcp::socket> &peersocks,
  627. std::deque<tcp::socket> &serversocks)
  628. {
  629. if (player == 0) {
  630. // Listen for connections from P1 and from P2
  631. tcp::acceptor acceptor_p1(io_context,
  632. tcp::endpoint(tcp::v4(), port_p1_p0));
  633. tcp::acceptor acceptor_p2(io_context,
  634. tcp::endpoint(tcp::v4(), port_p2_p0));
  635. peersocks.clear();
  636. serversocks.clear();
  637. for (int i=0;i<num_threads;++i) {
  638. peersocks.emplace_back(io_context);
  639. serversocks.emplace_back(io_context);
  640. }
  641. for (int i=0;i<num_threads;++i) {
  642. tcp::socket peersock = acceptor_p1.accept();
  643. // Read 2 bytes from the socket, which will be the thread
  644. // number
  645. unsigned short thread_num;
  646. boost::asio::read(peersock,
  647. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  648. if (thread_num >= num_threads) {
  649. std::cerr << "Received bad thread number from peer\n";
  650. } else {
  651. peersocks[thread_num] = std::move(peersock);
  652. }
  653. }
  654. for (int i=0;i<num_threads;++i) {
  655. tcp::socket serversock = acceptor_p2.accept();
  656. // Read 2 bytes from the socket, which will be the thread
  657. // number
  658. unsigned short thread_num;
  659. boost::asio::read(serversock,
  660. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  661. if (thread_num >= num_threads) {
  662. std::cerr << "Received bad thread number from server\n";
  663. } else {
  664. serversocks[thread_num] = std::move(serversock);
  665. }
  666. }
  667. } else if (player == 1) {
  668. // Listen for connections from P2, make num_threads connections to P0
  669. tcp::acceptor acceptor_p2(io_context,
  670. tcp::endpoint(tcp::v4(), port_p2_p1));
  671. tcp::resolver resolver(io_context);
  672. boost::system::error_code err;
  673. peersocks.clear();
  674. serversocks.clear();
  675. for (int i=0;i<num_threads;++i) {
  676. serversocks.emplace_back(io_context);
  677. }
  678. for (unsigned short thread_num = 0; thread_num < num_threads; ++thread_num) {
  679. tcp::socket peersock(io_context);
  680. while(1) {
  681. boost::asio::connect(peersock,
  682. resolver.resolve(p0addr, std::to_string(port_p1_p0)), err);
  683. if (!err) break;
  684. std::cerr << "Connection to p0 refused, will retry.\n";
  685. sleep(1);
  686. }
  687. // Write 2 bytes to the socket indicating which thread
  688. // number this socket is for
  689. boost::asio::write(peersock,
  690. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  691. peersocks.push_back(std::move(peersock));
  692. }
  693. for (int i=0;i<num_threads;++i) {
  694. tcp::socket serversock = acceptor_p2.accept();
  695. // Read 2 bytes from the socket, which will be the thread
  696. // number
  697. unsigned short thread_num;
  698. boost::asio::read(serversock,
  699. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  700. if (thread_num >= num_threads) {
  701. std::cerr << "Received bad thread number from server\n";
  702. } else {
  703. serversocks[thread_num] = std::move(serversock);
  704. }
  705. }
  706. } else {
  707. std::cerr << "Invalid player number passed to mpcio_setup_computational\n";
  708. }
  709. }
  710. void mpcio_setup_server(boost::asio::io_context &io_context,
  711. const char *p0addr, const char *p1addr, int num_threads,
  712. std::deque<tcp::socket> &p0socks,
  713. std::deque<tcp::socket> &p1socks)
  714. {
  715. // Make connections to P0 and P1
  716. tcp::resolver resolver(io_context);
  717. boost::system::error_code err;
  718. p0socks.clear();
  719. p1socks.clear();
  720. for (unsigned short thread_num = 0; thread_num < num_threads; ++thread_num) {
  721. tcp::socket p0sock(io_context);
  722. while(1) {
  723. boost::asio::connect(p0sock,
  724. resolver.resolve(p0addr, std::to_string(port_p2_p0)), err);
  725. if (!err) break;
  726. std::cerr << "Connection to p0 refused, will retry.\n";
  727. sleep(1);
  728. }
  729. // Write 2 bytes to the socket indicating which thread
  730. // number this socket is for
  731. boost::asio::write(p0sock,
  732. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  733. p0socks.push_back(std::move(p0sock));
  734. }
  735. for (unsigned short thread_num = 0; thread_num < num_threads; ++thread_num) {
  736. tcp::socket p1sock(io_context);
  737. while(1) {
  738. boost::asio::connect(p1sock,
  739. resolver.resolve(p1addr, std::to_string(port_p2_p1)), err);
  740. if (!err) break;
  741. std::cerr << "Connection to p1 refused, will retry.\n";
  742. sleep(1);
  743. }
  744. // Write 2 bytes to the socket indicating which thread
  745. // number this socket is for
  746. boost::asio::write(p1sock,
  747. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  748. p1socks.push_back(std::move(p1sock));
  749. }
  750. }