mpcio.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #ifndef __MCPIO_HPP__
  2. #define __MCPIO_HPP__
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <array>
  7. #include <deque>
  8. #include <queue>
  9. #include <string>
  10. #include <atomic>
  11. #include <optional>
  12. #include <bsd/stdlib.h> // arc4random_buf
  13. #include <boost/asio.hpp>
  14. #include <boost/thread.hpp>
  15. #include <boost/chrono.hpp>
  16. #include "types.hpp"
  17. #include "corotypes.hpp"
  18. using boost::asio::ip::tcp;
  19. // Classes to represent stored precomputed data (e.g., multiplication triples)
  20. template<typename T, typename N>
  21. class PreCompStorage {
  22. public:
  23. PreCompStorage() : name(N::name), depth(0), count(0) {}
  24. PreCompStorage(unsigned player, ProcessingMode mode,
  25. const char *filenameprefix, unsigned thread_num);
  26. void init(unsigned player, ProcessingMode mode,
  27. const char *filenameprefix, unsigned thread_num, nbits_t depth = 0);
  28. void get(T& nextval);
  29. inline void inc() { ++count; }
  30. inline size_t get_stats() { return count; }
  31. inline void reset_stats() { count = 0; }
  32. private:
  33. std::ifstream storage;
  34. std::string name;
  35. nbits_t depth;
  36. size_t count;
  37. };
  38. // If we want to send Lamport clocks in messages, define this. It adds
  39. // an 8-byte header to each message (length and Lamport clock), so it
  40. // has a small network cost. We always define and pass the Lamport
  41. // clock member of MPCIO to the IO functions for simplicity, but they're
  42. // ignored if this isn't defined
  43. #define SEND_LAMPORT_CLOCKS
  44. using lamport_t = uint32_t;
  45. using atomic_lamport_t = std::atomic<lamport_t>;
  46. using opt_lamport_t = std::optional<lamport_t>;
  47. #ifdef SEND_LAMPORT_CLOCKS
  48. struct MessageWithHeader {
  49. std::string header;
  50. std::string message;
  51. MessageWithHeader(std::string &&msg, lamport_t lamport) :
  52. message(std::move(msg)) {
  53. char hdr[sizeof(uint32_t) + sizeof(lamport_t)];
  54. uint32_t msglen = uint32_t(message.size());
  55. memmove(hdr, &msglen, sizeof(msglen));
  56. memmove(hdr+sizeof(msglen), &lamport, sizeof(lamport));
  57. header.assign(hdr, sizeof(hdr));
  58. }
  59. };
  60. #endif
  61. // A class to wrap a socket to another MPC party. This wrapping allows
  62. // us to do some useful logging, and perform async_writes transparently
  63. // to the application.
  64. class MPCSingleIO {
  65. tcp::socket sock;
  66. size_t totread, totwritten;
  67. #ifdef RECORD_IOTRACE
  68. std::vector<ssize_t> iotrace;
  69. #endif
  70. // To avoid blocking if both we and our peer are trying to send
  71. // something very large, and neither side is receiving, we will send
  72. // with async_write. But this has a number of implications:
  73. // - The data to be sent has to be copied into this MPCSingleIO,
  74. // since asio::buffer pointers are not guaranteed to remain valid
  75. // after the end of the coroutine that created them
  76. // - We have to keep a queue of messages to be sent, in case
  77. // coroutines call send() before the previous message has finished
  78. // being sent
  79. // - This queue may be accessed from the async_write thread as well
  80. // as the work thread that uses this MPCSingleIO directly (there
  81. // should be only one of the latter), so we need some locking
  82. // This is where we accumulate data passed in queue()
  83. std::string dataqueue;
  84. // When send() is called, the above dataqueue is appended to this
  85. // messagequeue, and the dataqueue is reset. If messagequeue was
  86. // empty before this append, launch async_write to write the first
  87. // thing in the messagequeue. When async_write completes, it will
  88. // delete the first thing in the messagequeue, and see if there are
  89. // any more elements. If so, it will start another async_write.
  90. // The invariant is that there is an async_write currently running
  91. // iff messagequeue is nonempty.
  92. #ifdef SEND_LAMPORT_CLOCKS
  93. std::queue<MessageWithHeader> messagequeue;
  94. #else
  95. std::queue<std::string> messagequeue;
  96. #endif
  97. // If a single message is broken into chunks in order to get the
  98. // first part of it out on the wire while the rest of it is still
  99. // being computed, we want the Lamport clock of all the chunks to be
  100. // that of when the message is first created. This value will be
  101. // nullopt when there has been no queue() since the last explicit
  102. // send() (as opposed to the implicit send() called by queue()
  103. // itself if it wants to get a chunk on its way), and will be set to
  104. // the current lamport clock when that first queue() after each
  105. // explicit send() happens.
  106. opt_lamport_t message_lamport;
  107. #ifdef SEND_LAMPORT_CLOCKS
  108. // If Lamport clocks are being sent, then the data stream is divided
  109. // into chunks, each with a header containing the length of the
  110. // chunk and the Lamport clock. So when we read, we'll read a whole
  111. // chunk, and store it here. Then calls to recv() will read pieces
  112. // of this buffer until it has all been read, and then read the next
  113. // header and chunk.
  114. std::string recvdata;
  115. size_t recvdataremain;
  116. #endif
  117. // Never touch the above messagequeue without holding this lock (you
  118. // _can_ touch the strings it contains, though, if you looked one up
  119. // while holding the lock).
  120. boost::mutex messagequeuelock;
  121. // Asynchronously send the first message from the message queue.
  122. // * The messagequeuelock must be held when this is called! *
  123. // This method may be called from either thread (the work thread or
  124. // the async_write handler thread).
  125. void async_send_from_msgqueue();
  126. public:
  127. MPCSingleIO(tcp::socket &&sock) :
  128. sock(std::move(sock)), totread(0), totwritten(0)
  129. #ifdef SEND_LAMPORT_CLOCKS
  130. , recvdataremain(0)
  131. #endif
  132. {}
  133. // Returns 1 if a new message is started, 0 otherwise
  134. size_t queue(const void *data, size_t len, lamport_t lamport);
  135. void send(bool implicit_send = false);
  136. size_t recv(void *data, size_t len, lamport_t &lamport);
  137. #ifdef RECORD_IOTRACE
  138. void dumptrace(std::ostream &os, const char *label = NULL);
  139. void resettrace() {
  140. iotrace.clear();
  141. }
  142. #endif
  143. };
  144. // A base class to represent all of a computation peer or server's IO,
  145. // either to other parties or to local storage (the computation and
  146. // server cases are separate subclasses below).
  147. struct MPCIO {
  148. int player;
  149. ProcessingMode mode;
  150. size_t num_threads;
  151. atomic_lamport_t lamport;
  152. std::vector<size_t> msgs_sent;
  153. std::vector<size_t> msg_bytes_sent;
  154. std::vector<size_t> aes_ops;
  155. boost::chrono::steady_clock::time_point steady_start;
  156. boost::chrono::process_cpu_clock::time_point cpu_start;
  157. MPCIO(int player, ProcessingMode mode, size_t num_threads) :
  158. player(player), mode(mode),
  159. num_threads(num_threads), lamport(0)
  160. {
  161. reset_stats();
  162. }
  163. void reset_stats();
  164. static void dump_memusage(std::ostream &os);
  165. void dump_stats(std::ostream &os);
  166. };
  167. // A class to represent all of a computation peer's IO, either to other
  168. // parties or to local storage
  169. struct MPCPeerIO : public MPCIO {
  170. // We use a deque here instead of a vector because you can't have a
  171. // vector of a type without a copy constructor (tcp::socket is the
  172. // culprit), but you can have a deque of those for some reason.
  173. std::deque<MPCSingleIO> peerios;
  174. std::deque<MPCSingleIO> serverios;
  175. std::vector<PreCompStorage<MultTriple, MultTripleName>> triples;
  176. std::vector<PreCompStorage<HalfTriple, HalfTripleName>> halftriples;
  177. std::vector<PreCompStorage<CDPF, CDPFName>> cdpfs;
  178. // The outer vector is (like above) one item per thread
  179. // The inner array is indexed by DPF depth (depth d is at entry d-1)
  180. std::vector<std::array<PreCompStorage<RDPFTriple, RDPFTripleName>,ADDRESS_MAX_BITS>> rdpftriples;
  181. MPCPeerIO(unsigned player, ProcessingMode mode,
  182. std::deque<tcp::socket> &peersocks,
  183. std::deque<tcp::socket> &serversocks);
  184. void dump_precomp_stats(std::ostream &os);
  185. void reset_precomp_stats();
  186. void dump_stats(std::ostream &os);
  187. };
  188. // A class to represent all of the server party's IO, either to
  189. // computational parties or to local storage
  190. struct MPCServerIO : public MPCIO {
  191. std::deque<MPCSingleIO> p0ios;
  192. std::deque<MPCSingleIO> p1ios;
  193. // The outer vector is (like above) one item per thread
  194. // The inner array is indexed by DPF depth (depth d is at entry d-1)
  195. std::vector<std::array<PreCompStorage<RDPFPair, RDPFPairName>,ADDRESS_MAX_BITS>> rdpfpairs;
  196. MPCServerIO(ProcessingMode mode,
  197. std::deque<tcp::socket> &p0socks,
  198. std::deque<tcp::socket> &p1socks);
  199. void dump_precomp_stats(std::ostream &os);
  200. void reset_precomp_stats();
  201. void dump_stats(std::ostream &os);
  202. };
  203. class MPCSingleIOStream {
  204. MPCSingleIO &sio;
  205. lamport_t &lamport;
  206. size_t &msgs_sent;
  207. size_t &msg_bytes_sent;
  208. public:
  209. MPCSingleIOStream(MPCSingleIO &sio, lamport_t &lamport,
  210. size_t &msgs_sent, size_t &msg_bytes_sent) :
  211. sio(sio), lamport(lamport), msgs_sent(msgs_sent),
  212. msg_bytes_sent(msg_bytes_sent) {}
  213. MPCSingleIOStream& write(const char *data, std::streamsize len) {
  214. size_t newmsg = sio.queue(data, len, lamport);
  215. msgs_sent += newmsg;
  216. msg_bytes_sent += len;
  217. return *this;
  218. }
  219. MPCSingleIOStream& read(char *data, std::streamsize len) {
  220. sio.recv(data, len, lamport);
  221. return *this;
  222. }
  223. };
  224. // A handle to one thread's sockets and streams in a MPCIO
  225. class MPCTIO {
  226. int thread_num;
  227. lamport_t thread_lamport;
  228. MPCIO &mpcio;
  229. std::optional<MPCSingleIOStream> peer_iostream;
  230. std::optional<MPCSingleIOStream> server_iostream;
  231. std::optional<MPCSingleIOStream> p0_iostream;
  232. std::optional<MPCSingleIOStream> p1_iostream;
  233. public:
  234. MPCTIO(MPCIO &mpcio, int thread_num);
  235. // Sync our per-thread lamport clock with the master one in the
  236. // mpcio. You only need to call this explicitly if your MPCTIO
  237. // outlives your thread (in which case call it after the join), or
  238. // if your threads do interthread communication amongst themselves
  239. // (in which case call it in the sending thread before the send, and
  240. // call it in the receiving thread after the receive).
  241. void sync_lamport();
  242. // The normal case, where the MPCIO is created inside the thread,
  243. // and so destructed when the thread ends, is handled automatically
  244. // here.
  245. ~MPCTIO() {
  246. sync_lamport();
  247. }
  248. // Computational peers use these functions:
  249. // Queue up data to the peer or to the server
  250. void queue_peer(const void *data, size_t len);
  251. void queue_server(const void *data, size_t len);
  252. // Receive data from the peer or to the server
  253. size_t recv_peer(void *data, size_t len);
  254. size_t recv_server(void *data, size_t len);
  255. // Or get these MPCSingleIOStreams
  256. MPCSingleIOStream& iostream_peer() { return peer_iostream.value(); }
  257. MPCSingleIOStream& iostream_server() { return server_iostream.value(); }
  258. // The server uses these functions:
  259. // Queue up data to p0 or p1
  260. void queue_p0(const void *data, size_t len);
  261. void queue_p1(const void *data, size_t len);
  262. // Receive data from p0 or p1
  263. size_t recv_p0(void *data, size_t len);
  264. size_t recv_p1(void *data, size_t len);
  265. // Or get these MPCSingleIOStreams
  266. MPCSingleIOStream& iostream_p0() { return p0_iostream.value(); }
  267. MPCSingleIOStream& iostream_p1() { return p1_iostream.value(); }
  268. // Everyone can use the remaining functions.
  269. // Send all queued data for this thread
  270. void send();
  271. // Functions to get precomputed values. If we're in the online
  272. // phase, get them from PreCompStorage. If we're in the
  273. // preprocessing phase, read them from the server.
  274. MultTriple triple();
  275. HalfTriple halftriple();
  276. SelectTriple selecttriple();
  277. // These ones only work during the online phase
  278. // Computational peers call:
  279. RDPFTriple rdpftriple(yield_t &yield, nbits_t depth,
  280. bool keep_expansion = true);
  281. // The server calls:
  282. RDPFPair rdpfpair(yield_t &yield, nbits_t depth);
  283. // Anyone can call:
  284. CDPF cdpf();
  285. // Accessors
  286. inline int player() { return mpcio.player; }
  287. inline bool preprocessing() { return mpcio.mode == MODE_PREPROCESSING; }
  288. inline bool is_server() { return mpcio.player == 2; }
  289. inline size_t& aes_ops() { return mpcio.aes_ops[thread_num]; }
  290. inline size_t msgs_sent() { return mpcio.msgs_sent[thread_num]; }
  291. };
  292. // Set up the socket connections between the two computational parties
  293. // (P0 and P1) and the server party (P2). For each connection, the
  294. // lower-numbered party does the accept() and the higher-numbered party
  295. // does the connect().
  296. // Computational parties call this version with player=0 or 1
  297. void mpcio_setup_computational(unsigned player,
  298. boost::asio::io_context &io_context,
  299. const char *p0addr, // can be NULL when player=0
  300. int num_threads,
  301. std::deque<tcp::socket> &peersocks,
  302. std::deque<tcp::socket> &serversocks);
  303. // Server calls this version
  304. void mpcio_setup_server(boost::asio::io_context &io_context,
  305. const char *p0addr, const char *p1addr, int num_threads,
  306. std::deque<tcp::socket> &p0socks,
  307. std::deque<tcp::socket> &p1socks);
  308. #endif