mpcio.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 (auto &&sock : peersocks) {
  258. peerios.emplace_back(std::move(sock));
  259. }
  260. for (auto &&sock : serversocks) {
  261. serverios.emplace_back(std::move(sock));
  262. }
  263. }
  264. void MPCPeerIO::dump_precomp_stats(std::ostream &os)
  265. {
  266. for (size_t i=0; i<triples.size(); ++i) {
  267. if (i > 0) {
  268. os << " ";
  269. }
  270. os << "T" << i << " t:" << triples[i].get_stats() <<
  271. " h:" << halftriples[i].get_stats();
  272. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  273. size_t cnt = rdpftriples[i][depth-1].get_stats();
  274. if (cnt > 0) {
  275. os << " r" << int(depth) << ":" << cnt;
  276. }
  277. }
  278. }
  279. os << "\n";
  280. }
  281. void MPCPeerIO::reset_precomp_stats()
  282. {
  283. for (size_t i=0; i<triples.size(); ++i) {
  284. triples[i].reset_stats();
  285. halftriples[i].reset_stats();
  286. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  287. rdpftriples[i][depth-1].reset_stats();
  288. }
  289. }
  290. }
  291. void MPCPeerIO::dump_stats(std::ostream &os)
  292. {
  293. MPCIO::dump_stats(os);
  294. os << "Precomputed values used: ";
  295. dump_precomp_stats(os);
  296. }
  297. MPCServerIO::MPCServerIO(bool preprocessing,
  298. std::deque<tcp::socket> &p0socks,
  299. std::deque<tcp::socket> &p1socks) :
  300. MPCIO(2, preprocessing, p0socks.size())
  301. {
  302. rdpfpairs.resize(num_threads);
  303. for (unsigned i=0; i<num_threads; ++i) {
  304. for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  305. rdpfpairs[i][depth-1].init(player, preprocessing,
  306. "rdpf", i, depth);
  307. }
  308. }
  309. for (auto &&sock : p0socks) {
  310. p0ios.emplace_back(std::move(sock));
  311. }
  312. for (auto &&sock : p1socks) {
  313. p1ios.emplace_back(std::move(sock));
  314. }
  315. }
  316. void MPCServerIO::dump_precomp_stats(std::ostream &os)
  317. {
  318. for (size_t i=0; i<rdpfpairs.size(); ++i) {
  319. if (i > 0) {
  320. os << " ";
  321. }
  322. os << "T" << i;
  323. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  324. size_t cnt = rdpfpairs[i][depth-1].get_stats();
  325. if (cnt > 0) {
  326. os << " r" << int(depth) << ":" << cnt;
  327. }
  328. }
  329. }
  330. os << "\n";
  331. }
  332. void MPCServerIO::reset_precomp_stats()
  333. {
  334. for (size_t i=0; i<rdpfpairs.size(); ++i) {
  335. for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
  336. rdpfpairs[i][depth-1].reset_stats();
  337. }
  338. }
  339. }
  340. void MPCServerIO::dump_stats(std::ostream &os)
  341. {
  342. MPCIO::dump_stats(os);
  343. os << "Precomputed values used: ";
  344. dump_precomp_stats(os);
  345. }
  346. MPCTIO::MPCTIO(MPCIO &mpcio, int thread_num) :
  347. thread_num(thread_num), thread_lamport(mpcio.lamport),
  348. mpcio(mpcio)
  349. {
  350. if (mpcio.player < 2) {
  351. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  352. peer_iostream.emplace(mpcpio.peerios[thread_num],
  353. thread_lamport, mpcpio.msgs_sent[thread_num],
  354. mpcpio.msg_bytes_sent[thread_num]);
  355. server_iostream.emplace(mpcpio.serverios[thread_num],
  356. thread_lamport, mpcpio.msgs_sent[thread_num],
  357. mpcpio.msg_bytes_sent[thread_num]);
  358. } else {
  359. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  360. p0_iostream.emplace(mpcsrvio.p0ios[thread_num],
  361. thread_lamport, mpcsrvio.msgs_sent[thread_num],
  362. mpcsrvio.msg_bytes_sent[thread_num]);
  363. p1_iostream.emplace(mpcsrvio.p1ios[thread_num],
  364. thread_lamport, mpcsrvio.msgs_sent[thread_num],
  365. mpcsrvio.msg_bytes_sent[thread_num]);
  366. }
  367. }
  368. // Sync our per-thread lamport clock with the master one in the
  369. // mpcio. You only need to call this explicitly if your MPCTIO
  370. // outlives your thread (in which case call it after the join), or
  371. // if your threads do interthread communication amongst themselves
  372. // (in which case call it in the sending thread before the send, and
  373. // call it in the receiving thread after the receive).
  374. void MPCTIO::sync_lamport()
  375. {
  376. // Update the mpcio Lamport time to be max of the thread Lamport
  377. // time and what we thought it was before. We use this
  378. // compare_exchange construction in order to atomically
  379. // do the comparison, computation, and replacement
  380. lamport_t old_lamport = mpcio.lamport;
  381. lamport_t new_lamport = thread_lamport;
  382. do {
  383. if (new_lamport < old_lamport) {
  384. new_lamport = old_lamport;
  385. }
  386. // The next line atomically checks if lamport still has
  387. // the value old_lamport; if so, it changes its value to
  388. // new_lamport and returns true (ending the loop). If
  389. // not, it sets old_lamport to the current value of
  390. // lamport, and returns false (continuing the loop so
  391. // that new_lamport can be recomputed based on this new
  392. // value).
  393. } while (!mpcio.lamport.compare_exchange_weak(
  394. old_lamport, new_lamport));
  395. thread_lamport = new_lamport;
  396. }
  397. // Queue up data to the peer or to the server
  398. void MPCTIO::queue_peer(const void *data, size_t len)
  399. {
  400. if (mpcio.player < 2) {
  401. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  402. size_t newmsg = mpcpio.peerios[thread_num].queue(data, len, thread_lamport);
  403. mpcpio.msgs_sent[thread_num] += newmsg;
  404. mpcpio.msg_bytes_sent[thread_num] += len;
  405. }
  406. }
  407. void MPCTIO::queue_server(const void *data, size_t len)
  408. {
  409. if (mpcio.player < 2) {
  410. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  411. size_t newmsg = mpcpio.serverios[thread_num].queue(data, len, thread_lamport);
  412. mpcpio.msgs_sent[thread_num] += newmsg;
  413. mpcpio.msg_bytes_sent[thread_num] += len;
  414. }
  415. }
  416. // Receive data from the peer or to the server
  417. size_t MPCTIO::recv_peer(void *data, size_t len)
  418. {
  419. if (mpcio.player < 2) {
  420. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  421. return mpcpio.peerios[thread_num].recv(data, len, thread_lamport);
  422. }
  423. return 0;
  424. }
  425. size_t MPCTIO::recv_server(void *data, size_t len)
  426. {
  427. if (mpcio.player < 2) {
  428. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  429. return mpcpio.serverios[thread_num].recv(data, len, thread_lamport);
  430. }
  431. return 0;
  432. }
  433. // Queue up data to p0 or p1
  434. void MPCTIO::queue_p0(const void *data, size_t len)
  435. {
  436. if (mpcio.player == 2) {
  437. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  438. size_t newmsg = mpcsrvio.p0ios[thread_num].queue(data, len, thread_lamport);
  439. mpcsrvio.msgs_sent[thread_num] += newmsg;
  440. mpcsrvio.msg_bytes_sent[thread_num] += len;
  441. }
  442. }
  443. void MPCTIO::queue_p1(const void *data, size_t len)
  444. {
  445. if (mpcio.player == 2) {
  446. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  447. size_t newmsg = mpcsrvio.p1ios[thread_num].queue(data, len, thread_lamport);
  448. mpcsrvio.msgs_sent[thread_num] += newmsg;
  449. mpcsrvio.msg_bytes_sent[thread_num] += len;
  450. }
  451. }
  452. // Receive data from p0 or p1
  453. size_t MPCTIO::recv_p0(void *data, size_t len)
  454. {
  455. if (mpcio.player == 2) {
  456. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  457. return mpcsrvio.p0ios[thread_num].recv(data, len, thread_lamport);
  458. }
  459. return 0;
  460. }
  461. size_t MPCTIO::recv_p1(void *data, size_t len)
  462. {
  463. if (mpcio.player == 2) {
  464. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  465. return mpcsrvio.p1ios[thread_num].recv(data, len, thread_lamport);
  466. }
  467. return 0;
  468. }
  469. // Send all queued data for this thread
  470. void MPCTIO::send()
  471. {
  472. if (mpcio.player < 2) {
  473. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  474. mpcpio.peerios[thread_num].send();
  475. mpcpio.serverios[thread_num].send();
  476. } else {
  477. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  478. mpcsrvio.p0ios[thread_num].send();
  479. mpcsrvio.p1ios[thread_num].send();
  480. }
  481. }
  482. // Functions to get precomputed values. If we're in the online
  483. // phase, get them from PreCompStorage. If we're in the
  484. // preprocessing phase, read them from the server.
  485. MultTriple MPCTIO::triple()
  486. {
  487. MultTriple val;
  488. if (mpcio.player < 2) {
  489. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  490. if (mpcpio.preprocessing) {
  491. recv_server(&val, sizeof(val));
  492. } else {
  493. mpcpio.triples[thread_num].get(val);
  494. }
  495. } else if (mpcio.preprocessing) {
  496. // Create triples (X0,Y0,Z0),(X1,Y1,Z1) such that
  497. // (X0*Y1 + Y0*X1) = (Z0+Z1)
  498. value_t X0, Y0, Z0, X1, Y1, Z1;
  499. arc4random_buf(&X0, sizeof(X0));
  500. arc4random_buf(&Y0, sizeof(Y0));
  501. arc4random_buf(&Z0, sizeof(Z0));
  502. arc4random_buf(&X1, sizeof(X1));
  503. arc4random_buf(&Y1, sizeof(Y1));
  504. Z1 = X0 * Y1 + X1 * Y0 - Z0;
  505. MultTriple T0, T1;
  506. T0 = std::make_tuple(X0, Y0, Z0);
  507. T1 = std::make_tuple(X1, Y1, Z1);
  508. queue_p0(&T0, sizeof(T0));
  509. queue_p1(&T1, sizeof(T1));
  510. }
  511. return val;
  512. }
  513. HalfTriple MPCTIO::halftriple()
  514. {
  515. HalfTriple val;
  516. if (mpcio.player < 2) {
  517. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  518. if (mpcpio.preprocessing) {
  519. recv_server(&val, sizeof(val));
  520. } else {
  521. mpcpio.halftriples[thread_num].get(val);
  522. }
  523. } else if (mpcio.preprocessing) {
  524. // Create half-triples (X0,Z0),(Y1,Z1) such that
  525. // X0*Y1 = Z0 + Z1
  526. value_t X0, Z0, Y1, Z1;
  527. arc4random_buf(&X0, sizeof(X0));
  528. arc4random_buf(&Z0, sizeof(Z0));
  529. arc4random_buf(&Y1, sizeof(Y1));
  530. Z1 = X0 * Y1 - Z0;
  531. HalfTriple H0, H1;
  532. H0 = std::make_tuple(X0, Z0);
  533. H1 = std::make_tuple(Y1, Z1);
  534. queue_p0(&H0, sizeof(H0));
  535. queue_p1(&H1, sizeof(H1));
  536. }
  537. return val;
  538. }
  539. SelectTriple MPCTIO::selecttriple()
  540. {
  541. SelectTriple val;
  542. if (mpcio.player < 2) {
  543. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  544. if (mpcpio.preprocessing) {
  545. uint8_t Xbyte;
  546. recv_server(&Xbyte, sizeof(Xbyte));
  547. val.X = Xbyte & 1;
  548. recv_server(&val.Y, sizeof(val.Y));
  549. recv_server(&val.Z, sizeof(val.Z));
  550. } else {
  551. std::cerr << "Attempted to read SelectTriple in online phase\n";
  552. }
  553. } else if (mpcio.preprocessing) {
  554. // Create triples (X0,Y0,Z0),(X1,Y1,Z1) such that
  555. // (X0*Y1 ^ Y0*X1) = (Z0^Z1)
  556. bit_t X0, X1;
  557. DPFnode Y0, Z0, Y1, Z1;
  558. X0 = arc4random() & 1;
  559. arc4random_buf(&Y0, sizeof(Y0));
  560. arc4random_buf(&Z0, sizeof(Z0));
  561. X1 = arc4random() & 1;
  562. arc4random_buf(&Y1, sizeof(Y1));
  563. DPFnode X0ext, X1ext;
  564. // Sign-extend X0 and X1 (so that 0 -> 0000...0 and
  565. // 1 -> 1111...1)
  566. X0ext = if128_mask[X0];
  567. X1ext = if128_mask[X1];
  568. Z1 = ((X0ext & Y1) ^ (X1ext & Y0)) ^ Z0;
  569. queue_p0(&X0, sizeof(X0));
  570. queue_p0(&Y0, sizeof(Y0));
  571. queue_p0(&Z0, sizeof(Z0));
  572. queue_p1(&X1, sizeof(X1));
  573. queue_p1(&Y1, sizeof(Y1));
  574. queue_p1(&Z1, sizeof(Z1));
  575. }
  576. return val;
  577. }
  578. RDPFTriple MPCTIO::rdpftriple(nbits_t depth)
  579. {
  580. RDPFTriple val;
  581. if (!mpcio.preprocessing && mpcio.player <= 2) {
  582. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  583. mpcpio.rdpftriples[thread_num][depth-1].get(val);
  584. }
  585. return val;
  586. }
  587. RDPFPair MPCTIO::rdpfpair(nbits_t depth)
  588. {
  589. RDPFPair val;
  590. if (!mpcio.preprocessing && mpcio.player == 2) {
  591. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  592. mpcsrvio.rdpfpairs[thread_num][depth-1].get(val);
  593. }
  594. return val;
  595. }
  596. CDPF MPCTIO::cdpf()
  597. {
  598. CDPF val;
  599. if (mpcio.player < 2) {
  600. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  601. if (mpcpio.preprocessing) {
  602. iostream_server() >> val;
  603. } else {
  604. mpcpio.cdpfs[thread_num].get(val);
  605. }
  606. } else if (mpcio.preprocessing) {
  607. auto [ cdpf0, cdpf1 ] = CDPF::generate(aes_ops());
  608. iostream_p0() << cdpf0;
  609. iostream_p1() << cdpf1;
  610. }
  611. return val;
  612. }
  613. // The port number for the P1 -> P0 connection
  614. static const unsigned short port_p1_p0 = 2115;
  615. // The port number for the P2 -> P0 connection
  616. static const unsigned short port_p2_p0 = 2116;
  617. // The port number for the P2 -> P1 connection
  618. static const unsigned short port_p2_p1 = 2117;
  619. void mpcio_setup_computational(unsigned player,
  620. boost::asio::io_context &io_context,
  621. const char *p0addr, // can be NULL when player=0
  622. int num_threads,
  623. std::deque<tcp::socket> &peersocks,
  624. std::deque<tcp::socket> &serversocks)
  625. {
  626. if (player == 0) {
  627. // Listen for connections from P1 and from P2
  628. tcp::acceptor acceptor_p1(io_context,
  629. tcp::endpoint(tcp::v4(), port_p1_p0));
  630. tcp::acceptor acceptor_p2(io_context,
  631. tcp::endpoint(tcp::v4(), port_p2_p0));
  632. peersocks.clear();
  633. serversocks.clear();
  634. for (int i=0;i<num_threads;++i) {
  635. peersocks.emplace_back(io_context);
  636. serversocks.emplace_back(io_context);
  637. }
  638. for (int i=0;i<num_threads;++i) {
  639. tcp::socket peersock = acceptor_p1.accept();
  640. // Read 2 bytes from the socket, which will be the thread
  641. // number
  642. unsigned short thread_num;
  643. boost::asio::read(peersock,
  644. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  645. if (thread_num >= num_threads) {
  646. std::cerr << "Received bad thread number from peer\n";
  647. } else {
  648. peersocks[thread_num] = std::move(peersock);
  649. }
  650. }
  651. for (int i=0;i<num_threads;++i) {
  652. tcp::socket serversock = acceptor_p2.accept();
  653. // Read 2 bytes from the socket, which will be the thread
  654. // number
  655. unsigned short thread_num;
  656. boost::asio::read(serversock,
  657. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  658. if (thread_num >= num_threads) {
  659. std::cerr << "Received bad thread number from server\n";
  660. } else {
  661. serversocks[thread_num] = std::move(serversock);
  662. }
  663. }
  664. } else if (player == 1) {
  665. // Listen for connections from P2, make num_threads connections to P0
  666. tcp::acceptor acceptor_p2(io_context,
  667. tcp::endpoint(tcp::v4(), port_p2_p1));
  668. tcp::resolver resolver(io_context);
  669. boost::system::error_code err;
  670. peersocks.clear();
  671. serversocks.clear();
  672. for (int i=0;i<num_threads;++i) {
  673. serversocks.emplace_back(io_context);
  674. }
  675. for (unsigned short thread_num = 0; thread_num < num_threads; ++thread_num) {
  676. tcp::socket peersock(io_context);
  677. while(1) {
  678. boost::asio::connect(peersock,
  679. resolver.resolve(p0addr, std::to_string(port_p1_p0)), err);
  680. if (!err) break;
  681. std::cerr << "Connection to p0 refused, will retry.\n";
  682. sleep(1);
  683. }
  684. // Write 2 bytes to the socket indicating which thread
  685. // number this socket is for
  686. boost::asio::write(peersock,
  687. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  688. peersocks.push_back(std::move(peersock));
  689. }
  690. for (int i=0;i<num_threads;++i) {
  691. tcp::socket serversock = acceptor_p2.accept();
  692. // Read 2 bytes from the socket, which will be the thread
  693. // number
  694. unsigned short thread_num;
  695. boost::asio::read(serversock,
  696. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  697. if (thread_num >= num_threads) {
  698. std::cerr << "Received bad thread number from server\n";
  699. } else {
  700. serversocks[thread_num] = std::move(serversock);
  701. }
  702. }
  703. } else {
  704. std::cerr << "Invalid player number passed to mpcio_setup_computational\n";
  705. }
  706. }
  707. void mpcio_setup_server(boost::asio::io_context &io_context,
  708. const char *p0addr, const char *p1addr, int num_threads,
  709. std::deque<tcp::socket> &p0socks,
  710. std::deque<tcp::socket> &p1socks)
  711. {
  712. // Make connections to P0 and P1
  713. tcp::resolver resolver(io_context);
  714. boost::system::error_code err;
  715. p0socks.clear();
  716. p1socks.clear();
  717. for (unsigned short thread_num = 0; thread_num < num_threads; ++thread_num) {
  718. tcp::socket p0sock(io_context);
  719. while(1) {
  720. boost::asio::connect(p0sock,
  721. resolver.resolve(p0addr, std::to_string(port_p2_p0)), err);
  722. if (!err) break;
  723. std::cerr << "Connection to p0 refused, will retry.\n";
  724. sleep(1);
  725. }
  726. // Write 2 bytes to the socket indicating which thread
  727. // number this socket is for
  728. boost::asio::write(p0sock,
  729. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  730. p0socks.push_back(std::move(p0sock));
  731. }
  732. for (unsigned short thread_num = 0; thread_num < num_threads; ++thread_num) {
  733. tcp::socket p1sock(io_context);
  734. while(1) {
  735. boost::asio::connect(p1sock,
  736. resolver.resolve(p1addr, std::to_string(port_p2_p1)), err);
  737. if (!err) break;
  738. std::cerr << "Connection to p1 refused, will retry.\n";
  739. sleep(1);
  740. }
  741. // Write 2 bytes to the socket indicating which thread
  742. // number this socket is for
  743. boost::asio::write(p1sock,
  744. boost::asio::buffer(&thread_num, sizeof(thread_num)));
  745. p1socks.push_back(std::move(p1sock));
  746. }
  747. }