preproc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include <vector>
  2. #include "types.hpp"
  3. #include "coroutine.hpp"
  4. #include "preproc.hpp"
  5. #include "rdpf.hpp"
  6. #include "cdpf.hpp"
  7. // Keep track of open files that coroutines might be writing into
  8. class Openfiles {
  9. bool append_mode;
  10. std::vector<std::ofstream> files;
  11. public:
  12. Openfiles(bool append_mode = false) : append_mode(append_mode) {}
  13. class Handle {
  14. Openfiles &parent;
  15. size_t idx;
  16. public:
  17. Handle(Openfiles &parent, size_t idx) :
  18. parent(parent), idx(idx) {}
  19. // Retrieve the ofstream from this Handle
  20. std::ofstream &os() const { return parent.files[idx]; }
  21. };
  22. Handle open(const char *prefix, unsigned player,
  23. unsigned thread_num, nbits_t depth = 0);
  24. void closeall();
  25. };
  26. // Open a file for writing with name the given prefix, and ".pX.tY"
  27. // suffix, where X is the (one-digit) player number and Y is the thread
  28. // number. If depth D is given, use "D.pX.tY" as the suffix.
  29. Openfiles::Handle Openfiles::open(const char *prefix, unsigned player,
  30. unsigned thread_num, nbits_t depth)
  31. {
  32. std::string filename(prefix);
  33. char suffix[20];
  34. if (depth > 0) {
  35. sprintf(suffix, "%02d.p%d.t%u", depth, player%10, thread_num);
  36. } else {
  37. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  38. }
  39. filename.append(suffix);
  40. std::ofstream &f = files.emplace_back(filename,
  41. append_mode ? std::ios_base::app : std::ios_base::out);
  42. if (f.fail()) {
  43. std::cerr << "Failed to open " << filename << "\n";
  44. exit(1);
  45. }
  46. return Handle(*this, files.size()-1);
  47. }
  48. // Close all the open files
  49. void Openfiles::closeall()
  50. {
  51. for (auto& f: files) {
  52. f.close();
  53. }
  54. files.clear();
  55. }
  56. // The server-to-computational-peer protocol for sending precomputed
  57. // data is:
  58. //
  59. // One byte: type
  60. // 0x01 to 0x30: RAM DPF of that depth
  61. // 0x40: Comparison DPF
  62. // 0x80: Multiplication triple
  63. // 0x81: Multiplication half-triple
  64. // 0x82: AND triple
  65. // 0x83: Select triple
  66. // 0x8e: Counter (for testing)
  67. // 0x8f: Set number of CPU threads for this communication thread
  68. // 0x00: End of preprocessing
  69. //
  70. // Four bytes: number of objects of that type (not sent for type == 0x00)
  71. //
  72. // Then that number of objects
  73. //
  74. // Repeat the whole thing until type == 0x00 is received
  75. void preprocessing_comp(MPCIO &mpcio, const PRACOptions &opts, char **args)
  76. {
  77. int num_threads = opts.num_threads;
  78. boost::asio::thread_pool pool(num_threads);
  79. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  80. boost::asio::post(pool, [&mpcio, &opts, thread_num] {
  81. MPCTIO tio(mpcio, thread_num);
  82. Openfiles ofiles(opts.append_to_files);
  83. std::vector<coro_t> coroutines;
  84. while(1) {
  85. unsigned char type = 0;
  86. unsigned int num = 0;
  87. size_t res = tio.recv_server(&type, 1);
  88. if (res < 1 || type == 0) break;
  89. tio.recv_server(&num, 4);
  90. if (type == 0x80) {
  91. // Multiplication triples
  92. auto tripfile = ofiles.open("mults",
  93. mpcio.player, thread_num);
  94. for (unsigned int i=0; i<num; ++i) {
  95. coroutines.emplace_back(
  96. [&tio, tripfile](yield_t &yield) {
  97. yield();
  98. MultTriple T = tio.multtriple(yield);
  99. tripfile.os() << T;
  100. });
  101. }
  102. } else if (type == 0x81) {
  103. // Multiplication half triples
  104. auto halffile = ofiles.open("halves",
  105. mpcio.player, thread_num);
  106. for (unsigned int i=0; i<num; ++i) {
  107. coroutines.emplace_back(
  108. [&tio, halffile](yield_t &yield) {
  109. yield();
  110. HalfTriple H = tio.halftriple(yield);
  111. halffile.os() << H;
  112. });
  113. }
  114. } else if (type == 0x82) {
  115. // AND triples
  116. auto andfile = ofiles.open("ands",
  117. mpcio.player, thread_num);
  118. for (unsigned int i=0; i<num; ++i) {
  119. coroutines.emplace_back(
  120. [&tio, andfile](yield_t &yield) {
  121. yield();
  122. AndTriple A = tio.andtriple(yield);
  123. andfile.os() << A;
  124. });
  125. }
  126. } else if (type == 0x83) {
  127. // Select triples
  128. auto selfile = ofiles.open("selects",
  129. mpcio.player, thread_num);
  130. for (unsigned int i=0; i<num; ++i) {
  131. coroutines.emplace_back(
  132. [&tio, selfile](yield_t &yield) {
  133. yield();
  134. SelectTriple<value_t> S =
  135. tio.valselecttriple(yield);
  136. selfile.os() << S;
  137. });
  138. }
  139. } else if (type >= 0x01 && type <= 0x30) {
  140. // RAM DPFs
  141. auto tripfile = ofiles.open("rdpf",
  142. mpcio.player, thread_num, type);
  143. for (unsigned int i=0; i<num; ++i) {
  144. coroutines.emplace_back(
  145. [&tio, &opts, tripfile, type](yield_t &yield) {
  146. yield();
  147. RDPFTriple rdpftrip =
  148. tio.rdpftriple(yield, type, opts.expand_rdpfs);
  149. printf("dep = %d\n", type);
  150. printf("usi0 = %016lx\n", rdpftrip.dpf[0].unit_sum_inverse);
  151. printf("sxr0 = %016lx\n", rdpftrip.dpf[0].scaled_xor.xshare);
  152. printf("usi1 = %016lx\n", rdpftrip.dpf[1].unit_sum_inverse);
  153. printf("sxr1 = %016lx\n", rdpftrip.dpf[1].scaled_xor.xshare);
  154. printf("usi2 = %016lx\n", rdpftrip.dpf[2].unit_sum_inverse);
  155. printf("sxr2 = %016lx\n", rdpftrip.dpf[2].scaled_xor.xshare);
  156. tripfile.os() << rdpftrip;
  157. });
  158. }
  159. } else if (type == 0x40) {
  160. // Comparison DPFs
  161. auto cdpffile = ofiles.open("cdpf",
  162. mpcio.player, thread_num);
  163. for (unsigned int i=0; i<num; ++i) {
  164. coroutines.emplace_back(
  165. [&tio, cdpffile](yield_t &yield) {
  166. yield();
  167. CDPF C = tio.cdpf(yield);
  168. cdpffile.os() << C;
  169. });
  170. }
  171. } else if (type == 0x8e) {
  172. coroutines.emplace_back(
  173. [&tio, num](yield_t &yield) {
  174. yield();
  175. unsigned int istart = 0x31415080;
  176. for (unsigned int i=istart; i<istart+num; ++i) {
  177. tio.queue_peer(&i, sizeof(i));
  178. tio.queue_server(&i, sizeof(i));
  179. yield();
  180. unsigned int peeri, srvi;
  181. tio.recv_peer(&peeri, sizeof(peeri));
  182. tio.recv_server(&srvi, sizeof(srvi));
  183. if (peeri != i || srvi != i) {
  184. printf("Incorrect counter received: "
  185. "peer=%08x srv=%08x\n", peeri,
  186. srvi);
  187. }
  188. }
  189. });
  190. } else if (type == 0x8f) {
  191. tio.cpu_nthreads(num);
  192. }
  193. }
  194. run_coroutines(tio, coroutines);
  195. ofiles.closeall();
  196. });
  197. }
  198. pool.join();
  199. }
  200. void preprocessing_server(MPCServerIO &mpcsrvio, const PRACOptions &opts, char **args)
  201. {
  202. int num_threads = opts.num_threads;
  203. boost::asio::thread_pool pool(num_threads);
  204. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  205. boost::asio::post(pool, [&mpcsrvio, &opts, thread_num, args] {
  206. char **threadargs = args;
  207. MPCTIO stio(mpcsrvio, thread_num);
  208. Openfiles ofiles(opts.append_to_files);
  209. std::vector<coro_t> coroutines;
  210. if (*threadargs && threadargs[0][0] == 'T') {
  211. // Per-thread initialization. The args look like:
  212. // T0 t:50 h:10 T1 t:20 h:30 T2 h:20
  213. // Skip to the arg marking our thread
  214. char us[20];
  215. sprintf(us, "T%u", thread_num);
  216. while (*threadargs && strcmp(*threadargs, us)) {
  217. ++threadargs;
  218. }
  219. // Now skip to the next arg if there is one
  220. if (*threadargs) {
  221. ++threadargs;
  222. }
  223. }
  224. // Stop scanning for args when we get to the end or when we
  225. // get to another per-thread initialization marker
  226. while (*threadargs && threadargs[0][0] != 'T') {
  227. char *arg = strdup(*threadargs);
  228. char *colon = strchr(arg, ':');
  229. if (!colon) {
  230. std::cerr << "Args must be type:num\n";
  231. ++threadargs;
  232. free(arg);
  233. continue;
  234. }
  235. unsigned num = atoi(colon+1);
  236. *colon = '\0';
  237. char *type = arg;
  238. if (!strcmp(type, "m")) {
  239. unsigned char typetag = 0x80;
  240. stio.queue_p0(&typetag, 1);
  241. stio.queue_p0(&num, 4);
  242. stio.queue_p1(&typetag, 1);
  243. stio.queue_p1(&num, 4);
  244. for (unsigned int i=0; i<num; ++i) {
  245. coroutines.emplace_back(
  246. [&stio](yield_t &yield) {
  247. yield();
  248. stio.multtriple(yield);
  249. });
  250. }
  251. } else if (!strcmp(type, "h")) {
  252. unsigned char typetag = 0x81;
  253. stio.queue_p0(&typetag, 1);
  254. stio.queue_p0(&num, 4);
  255. stio.queue_p1(&typetag, 1);
  256. stio.queue_p1(&num, 4);
  257. for (unsigned int i=0; i<num; ++i) {
  258. coroutines.emplace_back(
  259. [&stio](yield_t &yield) {
  260. yield();
  261. stio.halftriple(yield);
  262. });
  263. }
  264. } else if (!strcmp(type, "a")) {
  265. unsigned char typetag = 0x82;
  266. stio.queue_p0(&typetag, 1);
  267. stio.queue_p0(&num, 4);
  268. stio.queue_p1(&typetag, 1);
  269. stio.queue_p1(&num, 4);
  270. for (unsigned int i=0; i<num; ++i) {
  271. coroutines.emplace_back(
  272. [&stio](yield_t &yield) {
  273. yield();
  274. stio.andtriple(yield);
  275. });
  276. }
  277. } else if (!strcmp(type, "s")) {
  278. unsigned char typetag = 0x83;
  279. stio.queue_p0(&typetag, 1);
  280. stio.queue_p0(&num, 4);
  281. stio.queue_p1(&typetag, 1);
  282. stio.queue_p1(&num, 4);
  283. for (unsigned int i=0; i<num; ++i) {
  284. coroutines.emplace_back(
  285. [&stio](yield_t &yield) {
  286. yield();
  287. stio.valselecttriple(yield);
  288. });
  289. }
  290. } else if (type[0] == 'r') {
  291. int depth = atoi(type+1);
  292. if (depth < 1 || depth > 48) {
  293. std::cerr << "Invalid DPF depth\n";
  294. } else {
  295. unsigned char typetag = depth;
  296. stio.queue_p0(&typetag, 1);
  297. stio.queue_p0(&num, 4);
  298. stio.queue_p1(&typetag, 1);
  299. stio.queue_p1(&num, 4);
  300. auto pairfile = ofiles.open("rdpf",
  301. mpcsrvio.player, thread_num, depth);
  302. for (unsigned int i=0; i<num; ++i) {
  303. coroutines.emplace_back(
  304. [&stio, &opts, pairfile, depth](yield_t &yield) {
  305. yield();
  306. RDPFPair rdpfpair = stio.rdpfpair(yield, depth);
  307. printf("usi0 = %016lx\n", rdpfpair.dpf[0].unit_sum_inverse);
  308. printf("sxr0 = %016lx\n", rdpfpair.dpf[0].scaled_xor.xshare);
  309. printf("dep0 = %d\n", rdpfpair.dpf[0].depth());
  310. printf("usi1 = %016lx\n", rdpfpair.dpf[1].unit_sum_inverse);
  311. printf("sxr1 = %016lx\n", rdpfpair.dpf[1].scaled_xor.xshare);
  312. printf("dep1 = %d\n", rdpfpair.dpf[1].depth());
  313. if (opts.expand_rdpfs) {
  314. rdpfpair.dpf[0].expand(stio.aes_ops());
  315. rdpfpair.dpf[1].expand(stio.aes_ops());
  316. }
  317. pairfile.os() << rdpfpair;
  318. });
  319. }
  320. }
  321. } else if (!strcmp(type, "c")) {
  322. unsigned char typetag = 0x40;
  323. stio.queue_p0(&typetag, 1);
  324. stio.queue_p0(&num, 4);
  325. stio.queue_p1(&typetag, 1);
  326. stio.queue_p1(&num, 4);
  327. for (unsigned int i=0; i<num; ++i) {
  328. coroutines.emplace_back(
  329. [&stio](yield_t &yield) {
  330. yield();
  331. stio.cdpf(yield);
  332. });
  333. }
  334. } else if (!strcmp(type, "i")) {
  335. unsigned char typetag = 0x8e;
  336. stio.queue_p0(&typetag, 1);
  337. stio.queue_p0(&num, 4);
  338. stio.queue_p1(&typetag, 1);
  339. stio.queue_p1(&num, 4);
  340. coroutines.emplace_back(
  341. [&stio, num] (yield_t &yield) {
  342. unsigned int istart = 0x31415080;
  343. yield();
  344. for (unsigned int i=istart; i<istart+num; ++i) {
  345. stio.queue_p0(&i, sizeof(i));
  346. stio.queue_p1(&i, sizeof(i));
  347. yield();
  348. unsigned int p0i, p1i;
  349. stio.recv_p0(&p0i, sizeof(p0i));
  350. stio.recv_p1(&p1i, sizeof(p1i));
  351. if (p0i != i || p1i != i) {
  352. printf("Incorrect counter received: "
  353. "p0=%08x p1=%08x\n", p0i,
  354. p1i);
  355. }
  356. }
  357. });
  358. } else if (!strcmp(type, "p")) {
  359. unsigned char typetag = 0x8f;
  360. stio.queue_p0(&typetag, 1);
  361. stio.queue_p0(&num, 4);
  362. stio.queue_p1(&typetag, 1);
  363. stio.queue_p1(&num, 4);
  364. stio.cpu_nthreads(num);
  365. }
  366. free(arg);
  367. ++threadargs;
  368. }
  369. // That's all
  370. unsigned char typetag = 0x00;
  371. stio.queue_p0(&typetag, 1);
  372. stio.queue_p1(&typetag, 1);
  373. run_coroutines(stio, coroutines);
  374. ofiles.closeall();
  375. });
  376. }
  377. pool.join();
  378. }