mpcio.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. #ifndef __MCPIO_HPP__
  2. #define __MCPIO_HPP__
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <deque>
  7. #include <queue>
  8. #include <string>
  9. #include <atomic>
  10. #include <bsd/stdlib.h> // arc4random_buf
  11. #include <boost/asio.hpp>
  12. #include <boost/thread.hpp>
  13. #include "types.hpp"
  14. using boost::asio::ip::tcp;
  15. // Classes to represent stored precomputed data (e.g., multiplication triples)
  16. template<typename T>
  17. class PreCompStorage {
  18. public:
  19. PreCompStorage(unsigned player, bool preprocessing,
  20. const char *filenameprefix, unsigned thread_num);
  21. void get(T& nextval);
  22. inline size_t get_stats() { return count; }
  23. inline void reset_stats() { count = 0; }
  24. private:
  25. std::ifstream storage;
  26. size_t count;
  27. };
  28. template<typename T>
  29. PreCompStorage<T>::PreCompStorage(unsigned player, bool preprocessing,
  30. const char *filenameprefix, unsigned thread_num) {
  31. if (preprocessing) return;
  32. std::string filename(filenameprefix);
  33. char suffix[20];
  34. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  35. filename.append(suffix);
  36. storage.open(filename);
  37. if (storage.fail()) {
  38. std::cerr << "Failed to open " << filename << "\n";
  39. exit(1);
  40. }
  41. count = 0;
  42. }
  43. template<typename T>
  44. void PreCompStorage<T>::get(T& nextval) {
  45. storage.read((char *)&nextval, sizeof(T));
  46. if (storage.gcount() != sizeof(T)) {
  47. std::cerr << "Failed to read precomputed value from storage\n";
  48. exit(1);
  49. }
  50. ++count;
  51. }
  52. // If we want to send Lamport clocks in messages, define this. It adds
  53. // an 8-byte header to each message (length and Lamport clock), so it
  54. // has a small network cost. We always define and pass the Lamport
  55. // clock member of MPCIO to the IO functions for simplicity, but they're
  56. // ignored if this isn't defined
  57. #define SEND_LAMPORT_CLOCKS
  58. using lamport_t = uint32_t;
  59. using atomic_lamport_t = std::atomic<lamport_t>;
  60. #ifdef SEND_LAMPORT_CLOCKS
  61. struct MessageWithHeader {
  62. std::string header;
  63. std::string message;
  64. MessageWithHeader(std::string &&msg, lamport_t lamport) :
  65. message(std::move(msg)) {
  66. char hdr[sizeof(uint32_t) + sizeof(lamport_t)];
  67. uint32_t msglen = uint32_t(message.size());
  68. memmove(hdr, &msglen, sizeof(msglen));
  69. memmove(hdr+sizeof(msglen), &lamport, sizeof(lamport));
  70. header.assign(hdr, sizeof(hdr));
  71. }
  72. };
  73. #endif
  74. // A class to wrap a socket to another MPC party. This wrapping allows
  75. // us to do some useful logging, and perform async_writes transparently
  76. // to the application.
  77. class MPCSingleIO {
  78. tcp::socket sock;
  79. size_t totread, totwritten;
  80. #ifdef RECORD_IOTRACE
  81. std::vector<ssize_t> iotrace;
  82. #endif
  83. // To avoid blocking if both we and our peer are trying to send
  84. // something very large, and neither side is receiving, we will send
  85. // with async_write. But this has a number of implications:
  86. // - The data to be sent has to be copied into this MPCSingleIO,
  87. // since asio::buffer pointers are not guaranteed to remain valid
  88. // after the end of the coroutine that created them
  89. // - We have to keep a queue of messages to be sent, in case
  90. // coroutines call send() before the previous message has finished
  91. // being sent
  92. // - This queue may be accessed from the async_write thread as well
  93. // as the work thread that uses this MPCSingleIO directly (there
  94. // should be only one of the latter), so we need some locking
  95. // This is where we accumulate data passed in queue()
  96. std::string dataqueue;
  97. // When send() is called, the above dataqueue is appended to this
  98. // messagequeue, and the dataqueue is reset. If messagequeue was
  99. // empty before this append, launch async_write to write the first
  100. // thing in the messagequeue. When async_write completes, it will
  101. // delete the first thing in the messagequeue, and see if there are
  102. // any more elements. If so, it will start another async_write.
  103. // The invariant is that there is an async_write currently running
  104. // iff messagequeue is nonempty.
  105. #ifdef SEND_LAMPORT_CLOCKS
  106. std::queue<MessageWithHeader> messagequeue;
  107. #else
  108. std::queue<std::string> messagequeue;
  109. #endif
  110. #ifdef SEND_LAMPORT_CLOCKS
  111. // If Lamport clocks are being sent, then the data stream is divided
  112. // into chunks, each with a header containing the length of the
  113. // chunk and the Lamport clock. So when we read, we'll read a whole
  114. // chunk, and store it here. Then calls to recv() will read pieces
  115. // of this buffer until it has all been read, and then read the next
  116. // header and chunk.
  117. std::string recvdata;
  118. size_t recvdataremain;
  119. #endif
  120. // Never touch the above messagequeue without holding this lock (you
  121. // _can_ touch the strings it contains, though, if you looked one up
  122. // while holding the lock).
  123. boost::mutex messagequeuelock;
  124. // Asynchronously send the first message from the message queue.
  125. // * The messagequeuelock must be held when this is called! *
  126. // This method may be called from either thread (the work thread or
  127. // the async_write handler thread).
  128. void async_send_from_msgqueue() {
  129. #ifdef SEND_LAMPORT_CLOCKS
  130. std::vector<boost::asio::const_buffer> tosend;
  131. tosend.push_back(boost::asio::buffer(messagequeue.front().header));
  132. tosend.push_back(boost::asio::buffer(messagequeue.front().message));
  133. #endif
  134. boost::asio::async_write(sock,
  135. #ifdef SEND_LAMPORT_CLOCKS
  136. tosend,
  137. #else
  138. boost::asio::buffer(messagequeue.front()),
  139. #endif
  140. [&](boost::system::error_code ec, std::size_t amt){
  141. messagequeuelock.lock();
  142. messagequeue.pop();
  143. if (messagequeue.size() > 0) {
  144. async_send_from_msgqueue();
  145. }
  146. messagequeuelock.unlock();
  147. });
  148. }
  149. public:
  150. MPCSingleIO(tcp::socket &&sock) :
  151. sock(std::move(sock)), totread(0), totwritten(0) {}
  152. void queue(const void *data, size_t len, lamport_t lamport) {
  153. dataqueue.append((const char *)data, len);
  154. // If we already have some full packets worth of data, may as
  155. // well send it.
  156. if (dataqueue.size() > 28800) {
  157. send(lamport);
  158. }
  159. }
  160. void send(lamport_t lamport) {
  161. size_t thissize = dataqueue.size();
  162. // Ignore spurious calls to send()
  163. if (thissize == 0) return;
  164. #ifdef RECORD_IOTRACE
  165. iotrace.push_back(thissize);
  166. #endif
  167. messagequeuelock.lock();
  168. // Move the current message to send into the message queue (this
  169. // moves a pointer to the data, not copying the data itself)
  170. #ifdef SEND_LAMPORT_CLOCKS
  171. messagequeue.emplace(std::move(dataqueue), lamport);
  172. #else
  173. messagequeue.emplace(std::move(dataqueue));
  174. #endif
  175. // If this is now the first thing in the message queue, launch
  176. // an async_write to write it
  177. if (messagequeue.size() == 1) {
  178. async_send_from_msgqueue();
  179. }
  180. messagequeuelock.unlock();
  181. }
  182. size_t recv(void *data, size_t len, atomic_lamport_t &lamport) {
  183. #ifdef SEND_LAMPORT_CLOCKS
  184. char *cdata = (char *)data;
  185. size_t res = 0;
  186. while (len > 0) {
  187. while (recvdataremain == 0) {
  188. // Read a new header
  189. char hdr[sizeof(uint32_t) + sizeof(lamport_t)];
  190. uint32_t datalen;
  191. lamport_t recv_lamport;
  192. boost::asio::read(sock, boost::asio::buffer(hdr, sizeof(hdr)));
  193. memmove(&datalen, hdr, sizeof(datalen));
  194. memmove(&recv_lamport, hdr+sizeof(datalen), sizeof(lamport_t));
  195. // Update our Lamport time to be max of recv_lamport+1
  196. // and what we thought it was before. We use this
  197. // compare_exchange construction in order to atomically
  198. // do the comparison, computation, and replacement
  199. lamport_t old_lamport = lamport;
  200. lamport_t new_lamport = recv_lamport + 1;
  201. do {
  202. if (new_lamport < old_lamport) {
  203. new_lamport = old_lamport;
  204. }
  205. // The next line atomically checks if lamport still has
  206. // the value old_lamport; if so, it changes its value to
  207. // new_lamport and returns true (ending the loop). If
  208. // not, it sets old_lamport to the current value of
  209. // lamport, and returns false (continuing the loop so
  210. // that new_lamport can be recomputed based on this new
  211. // value).
  212. } while (!lamport.compare_exchange_weak(
  213. old_lamport, new_lamport));
  214. if (datalen > 0) {
  215. recvdata.resize(datalen, '\0');
  216. boost::asio::read(sock, boost::asio::buffer(recvdata));
  217. recvdataremain = datalen;
  218. }
  219. }
  220. size_t amttoread = len;
  221. if (amttoread > recvdataremain) {
  222. amttoread = recvdataremain;
  223. }
  224. memmove(cdata, recvdata.data()+recvdata.size()-recvdataremain,
  225. amttoread);
  226. cdata += amttoread;
  227. len -= amttoread;
  228. recvdataremain -= amttoread;
  229. res += amttoread;
  230. }
  231. return res;
  232. #else
  233. size_t res = boost::asio::read(sock, boost::asio::buffer(data, len));
  234. #ifdef RECORD_IOTRACE
  235. iotrace.push_back(-(ssize_t(res)));
  236. #endif
  237. return res;
  238. #endif
  239. }
  240. #ifdef RECORD_IOTRACE
  241. void dumptrace(std::ostream &os, const char *label = NULL) {
  242. if (label) {
  243. os << label << " ";
  244. }
  245. os << "IO trace:";
  246. for (auto& s: iotrace) {
  247. os << " " << s;
  248. }
  249. os << "\n";
  250. }
  251. void resettrace() {
  252. iotrace.clear();
  253. }
  254. #endif
  255. };
  256. // A base class to represent all of a computation peer or server's IO,
  257. // either to other parties or to local storage (the computation and
  258. // server cases are separate subclasses below).
  259. struct MPCIO {
  260. int player;
  261. bool preprocessing;
  262. atomic_lamport_t lamport;
  263. MPCIO(int player, bool preprocessing) :
  264. player(player), preprocessing(preprocessing), lamport(0) {}
  265. };
  266. // A class to represent all of a computation peer's IO, either to other
  267. // parties or to local storage
  268. struct MPCPeerIO : public MPCIO {
  269. // We use a deque here instead of a vector because you can't have a
  270. // vector of a type without a copy constructor (tcp::socket is the
  271. // culprit), but you can have a deque of those for some reason.
  272. std::deque<MPCSingleIO> peerios;
  273. std::deque<MPCSingleIO> serverios;
  274. std::vector<PreCompStorage<MultTriple>> triples;
  275. std::vector<PreCompStorage<HalfTriple>> halftriples;
  276. MPCPeerIO(unsigned player, bool preprocessing,
  277. std::deque<tcp::socket> &peersocks,
  278. std::deque<tcp::socket> &serversocks) :
  279. MPCIO(player, preprocessing)
  280. {
  281. unsigned num_threads = unsigned(peersocks.size());
  282. for (unsigned i=0; i<num_threads; ++i) {
  283. triples.emplace_back(player, preprocessing, "triples", i);
  284. }
  285. for (unsigned i=0; i<num_threads; ++i) {
  286. halftriples.emplace_back(player, preprocessing, "halves", i);
  287. }
  288. for (auto &&sock : peersocks) {
  289. peerios.emplace_back(std::move(sock));
  290. }
  291. for (auto &&sock : serversocks) {
  292. serverios.emplace_back(std::move(sock));
  293. }
  294. }
  295. void dump_precomp_stats(std::ostream &os)
  296. {
  297. for (size_t i=0; i<triples.size(); ++i) {
  298. if (i > 0) {
  299. os << " ";
  300. }
  301. os << "T" << i << " t:" << triples[i].get_stats() <<
  302. " h:" << halftriples[i].get_stats();
  303. }
  304. os << "\n";
  305. }
  306. void reset_precomp_stats()
  307. {
  308. for (size_t i=0; i<triples.size(); ++i) {
  309. triples[i].reset_stats();
  310. halftriples[i].reset_stats();
  311. }
  312. }
  313. };
  314. // A class to represent all of the server party's IO, either to
  315. // computational parties or to local storage
  316. struct MPCServerIO : public MPCIO {
  317. std::deque<MPCSingleIO> p0ios;
  318. std::deque<MPCSingleIO> p1ios;
  319. MPCServerIO(bool preprocessing,
  320. std::deque<tcp::socket> &p0socks,
  321. std::deque<tcp::socket> &p1socks) :
  322. MPCIO(2, preprocessing)
  323. {
  324. for (auto &&sock : p0socks) {
  325. p0ios.emplace_back(std::move(sock));
  326. }
  327. for (auto &&sock : p1socks) {
  328. p1ios.emplace_back(std::move(sock));
  329. }
  330. }
  331. };
  332. // A handle to one thread's sockets and streams in a MPCIO
  333. class MPCTIO {
  334. int thread_num;
  335. MPCIO &mpcio;
  336. public:
  337. MPCTIO(MPCIO &mpcio, int thread_num):
  338. thread_num(thread_num), mpcio(mpcio) {}
  339. // Queue up data to the peer or to the server
  340. void queue_peer(const void *data, size_t len) {
  341. if (mpcio.player < 2) {
  342. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  343. mpcpio.peerios[thread_num].queue(data, len, mpcio.lamport);
  344. }
  345. }
  346. void queue_server(const void *data, size_t len) {
  347. if (mpcio.player < 2) {
  348. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  349. mpcpio.serverios[thread_num].queue(data, len, mpcio.lamport);
  350. }
  351. }
  352. // Receive data from the peer or to the server
  353. size_t recv_peer(void *data, size_t len) {
  354. if (mpcio.player < 2) {
  355. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  356. return mpcpio.peerios[thread_num].recv(data, len, mpcio.lamport);
  357. }
  358. return 0;
  359. }
  360. size_t recv_server(void *data, size_t len) {
  361. if (mpcio.player < 2) {
  362. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  363. return mpcpio.serverios[thread_num].recv(data, len, mpcio.lamport);
  364. }
  365. return 0;
  366. }
  367. // Queue up data to p0 or p1
  368. void queue_p0(const void *data, size_t len) {
  369. if (mpcio.player == 2) {
  370. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  371. mpcsrvio.p0ios[thread_num].queue(data, len, mpcio.lamport);
  372. }
  373. }
  374. void queue_p1(const void *data, size_t len) {
  375. if (mpcio.player == 2) {
  376. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  377. mpcsrvio.p1ios[thread_num].queue(data, len, mpcio.lamport);
  378. }
  379. }
  380. // Receive data from p0 or p1
  381. size_t recv_p0(void *data, size_t len) {
  382. if (mpcio.player == 2) {
  383. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  384. return mpcsrvio.p0ios[thread_num].recv(data, len, mpcio.lamport);
  385. }
  386. return 0;
  387. }
  388. size_t recv_p1(void *data, size_t len) {
  389. if (mpcio.player == 2) {
  390. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  391. return mpcsrvio.p1ios[thread_num].recv(data, len, mpcio.lamport);
  392. }
  393. return 0;
  394. }
  395. // Send all queued data for this thread
  396. void send() {
  397. if (mpcio.player < 2) {
  398. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  399. mpcpio.peerios[thread_num].send(mpcio.lamport);
  400. mpcpio.serverios[thread_num].send(mpcio.lamport);
  401. } else {
  402. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  403. mpcsrvio.p0ios[thread_num].send(mpcio.lamport);
  404. mpcsrvio.p1ios[thread_num].send(mpcio.lamport);
  405. }
  406. }
  407. // Functions to get precomputed values. If we're in the online
  408. // phase, get them from PreCompStorage. If we're in the
  409. // preprocessing phase, read them from the server.
  410. MultTriple triple() {
  411. MultTriple val;
  412. if (mpcio.player < 2) {
  413. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  414. if (mpcpio.preprocessing) {
  415. recv_server(&val, sizeof(val));
  416. } else {
  417. mpcpio.triples[thread_num].get(val);
  418. }
  419. } else if (mpcio.preprocessing) {
  420. // Create triples (X0,Y0,Z0),(X1,Y1,Z1) such that
  421. // (X0*Y1 + Y0*X1) = (Z0+Z1)
  422. value_t X0, Y0, Z0, X1, Y1, Z1;
  423. arc4random_buf(&X0, sizeof(X0));
  424. arc4random_buf(&Y0, sizeof(Y0));
  425. arc4random_buf(&Z0, sizeof(Z0));
  426. arc4random_buf(&X1, sizeof(X1));
  427. arc4random_buf(&Y1, sizeof(Y1));
  428. Z1 = X0 * Y1 + X1 * Y0 - Z0;
  429. MultTriple T0, T1;
  430. T0 = std::make_tuple(X0, Y0, Z0);
  431. T1 = std::make_tuple(X1, Y1, Z1);
  432. queue_p0(&T0, sizeof(T0));
  433. queue_p1(&T1, sizeof(T1));
  434. }
  435. return val;
  436. }
  437. HalfTriple halftriple() {
  438. HalfTriple val;
  439. if (mpcio.player < 2) {
  440. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  441. if (mpcpio.preprocessing) {
  442. recv_server(&val, sizeof(val));
  443. } else {
  444. mpcpio.halftriples[thread_num].get(val);
  445. }
  446. } else if (mpcio.preprocessing) {
  447. // Create half-triples (X0,Z0),(Y1,Z1) such that
  448. // X0*Y1 = Z0 + Z1
  449. value_t X0, Z0, Y1, Z1;
  450. arc4random_buf(&X0, sizeof(X0));
  451. arc4random_buf(&Z0, sizeof(Z0));
  452. arc4random_buf(&Y1, sizeof(Y1));
  453. Z1 = X0 * Y1 - Z0;
  454. HalfTriple H0, H1;
  455. H0 = std::make_tuple(X0, Z0);
  456. H1 = std::make_tuple(Y1, Z1);
  457. queue_p0(&H0, sizeof(H0));
  458. queue_p1(&H1, sizeof(H1));
  459. }
  460. return val;
  461. }
  462. // Accessors
  463. inline int player() { return mpcio.player; }
  464. inline bool preprocessing() { return mpcio.preprocessing; }
  465. inline bool is_server() { return mpcio.player == 2; }
  466. };
  467. // Set up the socket connections between the two computational parties
  468. // (P0 and P1) and the server party (P2). For each connection, the
  469. // lower-numbered party does the accept() and the higher-numbered party
  470. // does the connect().
  471. // Computational parties call this version with player=0 or 1
  472. void mpcio_setup_computational(unsigned player,
  473. boost::asio::io_context &io_context,
  474. const char *p0addr, // can be NULL when player=0
  475. int num_threads,
  476. std::deque<tcp::socket> &peersocks,
  477. std::deque<tcp::socket> &serversocks);
  478. // Server calls this version
  479. void mpcio_setup_server(boost::asio::io_context &io_context,
  480. const char *p0addr, const char *p1addr, int num_threads,
  481. std::deque<tcp::socket> &p0socks,
  482. std::deque<tcp::socket> &p1socks);
  483. #endif