online.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include <bsd/stdlib.h> // arc4random_buf
  2. #include "online.hpp"
  3. #include "mpcops.hpp"
  4. #include "rdpf.hpp"
  5. static void online_test(MPCIO &mpcio, const PRACOptions &opts, char **args)
  6. {
  7. nbits_t nbits = VALUE_BITS;
  8. if (*args) {
  9. nbits = atoi(*args);
  10. }
  11. size_t memsize = 9;
  12. MPCTIO tio(mpcio, 0);
  13. bool is_server = (mpcio.player == 2);
  14. RegAS *A = new RegAS[memsize];
  15. value_t V;
  16. RegBS F0, F1;
  17. RegXS X;
  18. if (!is_server) {
  19. A[0].randomize();
  20. A[1].randomize();
  21. F0.randomize();
  22. A[4].randomize();
  23. F1.randomize();
  24. A[6].randomize();
  25. A[7].randomize();
  26. X.randomize();
  27. arc4random_buf(&V, sizeof(V));
  28. printf("A:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  29. printf("V : %016lX\n", V);
  30. printf("F0 : %01X\n", F0.bshare);
  31. printf("F1 : %01X\n", F1.bshare);
  32. printf("X : %016lX\n", X.xshare);
  33. }
  34. std::vector<coro_t> coroutines;
  35. coroutines.emplace_back(
  36. [&](yield_t &yield) {
  37. mpc_mul(tio, yield, A[2], A[0], A[1], nbits);
  38. });
  39. coroutines.emplace_back(
  40. [&](yield_t &yield) {
  41. mpc_valuemul(tio, yield, A[3], V, nbits);
  42. });
  43. coroutines.emplace_back(
  44. [&](yield_t &yield) {
  45. mpc_flagmult(tio, yield, A[5], F0, A[4], nbits);
  46. });
  47. coroutines.emplace_back(
  48. [&](yield_t &yield) {
  49. mpc_oswap(tio, yield, A[6], A[7], F1, nbits);
  50. });
  51. coroutines.emplace_back(
  52. [&](yield_t &yield) {
  53. mpc_xs_to_as(tio, yield, A[8], X, nbits);
  54. });
  55. run_coroutines(tio, coroutines);
  56. if (!is_server) {
  57. printf("\n");
  58. printf("A:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  59. }
  60. // Check the answers
  61. if (mpcio.player == 1) {
  62. tio.queue_peer(A, memsize*sizeof(RegAS));
  63. tio.queue_peer(&V, sizeof(V));
  64. tio.queue_peer(&F0, sizeof(RegBS));
  65. tio.queue_peer(&F1, sizeof(RegBS));
  66. tio.queue_peer(&X, sizeof(RegXS));
  67. tio.send();
  68. } else if (mpcio.player == 0) {
  69. RegAS *B = new RegAS[memsize];
  70. RegBS BF0, BF1;
  71. RegXS BX;
  72. value_t BV;
  73. value_t *S = new value_t[memsize];
  74. bit_t SF0, SF1;
  75. value_t SX;
  76. tio.recv_peer(B, memsize*sizeof(RegAS));
  77. tio.recv_peer(&BV, sizeof(BV));
  78. tio.recv_peer(&BF0, sizeof(RegBS));
  79. tio.recv_peer(&BF1, sizeof(RegBS));
  80. tio.recv_peer(&BX, sizeof(RegXS));
  81. for(size_t i=0; i<memsize; ++i) S[i] = A[i].ashare+B[i].ashare;
  82. SF0 = F0.bshare ^ BF0.bshare;
  83. SF1 = F1.bshare ^ BF1.bshare;
  84. SX = X.xshare ^ BX.xshare;
  85. printf("S:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, S[i]);
  86. printf("SF0: %01X\n", SF0);
  87. printf("SF1: %01X\n", SF1);
  88. printf("SX : %016lX\n", SX);
  89. printf("\n%016lx\n", S[0]*S[1]-S[2]);
  90. printf("%016lx\n", (V*BV)-S[3]);
  91. printf("%016lx\n", (SF0*S[4])-S[5]);
  92. printf("%016lx\n", S[8]-SX);
  93. delete[] B;
  94. delete[] S;
  95. }
  96. delete[] A;
  97. }
  98. static void lamport_test(MPCIO &mpcio, const PRACOptions &opts, char **args)
  99. {
  100. // Create a bunch of threads and send a bunch of data to the other
  101. // peer, and receive their data. If an arg is specified, repeat
  102. // that many times. The Lamport clock at the end should be just the
  103. // number of repetitions. Subsequent args are the chunk size and
  104. // the number of chunks per message
  105. size_t niters = 1;
  106. size_t chunksize = 1<<20;
  107. size_t numchunks = 1;
  108. if (*args) {
  109. niters = atoi(*args);
  110. ++args;
  111. }
  112. if (*args) {
  113. chunksize = atoi(*args);
  114. ++args;
  115. }
  116. if (*args) {
  117. numchunks = atoi(*args);
  118. ++args;
  119. }
  120. int num_threads = opts.num_threads;
  121. boost::asio::thread_pool pool(num_threads);
  122. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  123. boost::asio::post(pool, [&mpcio, thread_num, niters, chunksize, numchunks] {
  124. MPCTIO tio(mpcio, thread_num);
  125. char *sendbuf = new char[chunksize];
  126. char *recvbuf = new char[chunksize*numchunks];
  127. for (size_t i=0; i<niters; ++i) {
  128. for (size_t chunk=0; chunk<numchunks; ++chunk) {
  129. arc4random_buf(sendbuf, chunksize);
  130. tio.queue_peer(sendbuf, chunksize);
  131. }
  132. tio.send();
  133. tio.recv_peer(recvbuf, chunksize*numchunks);
  134. }
  135. delete[] recvbuf;
  136. delete[] sendbuf;
  137. });
  138. }
  139. pool.join();
  140. }
  141. static void rdpf_test(MPCIO &mpcio, const PRACOptions &opts, char **args)
  142. {
  143. nbits_t depth=6;
  144. if (*args) {
  145. depth = atoi(*args);
  146. ++args;
  147. }
  148. int num_threads = opts.num_threads;
  149. boost::asio::thread_pool pool(num_threads);
  150. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  151. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  152. MPCTIO tio(mpcio, thread_num);
  153. size_t &op_counter = tio.aes_ops();
  154. if (mpcio.player == 2) {
  155. RDPFPair dp = tio.rdpfpair(depth);
  156. for (int i=0;i<2;++i) {
  157. const RDPF &dpf = dp.dpf[i];
  158. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  159. DPFnode leaf = dpf.leaf(x, op_counter);
  160. RegBS ub = dpf.unit_bs(leaf);
  161. RegAS ua = dpf.unit_as(leaf);
  162. RegXS sx = dpf.scaled_xs(leaf);
  163. RegAS sa = dpf.scaled_as(leaf);
  164. printf("%04x %x %016lx %016lx %016lx\n", x,
  165. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  166. }
  167. printf("\n");
  168. }
  169. } else {
  170. RDPFTriple dt = tio.rdpftriple(depth);
  171. for (int i=0;i<3;++i) {
  172. const RDPF &dpf = dt.dpf[i];
  173. RegXS peer_scaled_xor;
  174. RegAS peer_scaled_sum;
  175. if (mpcio.player == 1) {
  176. tio.iostream_peer() << dpf.scaled_xor << dpf.scaled_sum;
  177. } else {
  178. tio.iostream_peer() >> peer_scaled_xor >> peer_scaled_sum;
  179. peer_scaled_sum += dpf.scaled_sum;
  180. peer_scaled_xor ^= dpf.scaled_xor;
  181. }
  182. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  183. DPFnode leaf = dpf.leaf(x, op_counter);
  184. RegBS ub = dpf.unit_bs(leaf);
  185. RegAS ua = dpf.unit_as(leaf);
  186. RegXS sx = dpf.scaled_xs(leaf);
  187. RegAS sa = dpf.scaled_as(leaf);
  188. printf("%04x %x %016lx %016lx %016lx\n", x,
  189. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  190. if (mpcio.player == 1) {
  191. tio.iostream_peer() << ub << ua << sx << sa;
  192. } else {
  193. RegBS peer_ub;
  194. RegAS peer_ua;
  195. RegXS peer_sx;
  196. RegAS peer_sa;
  197. tio.iostream_peer() >> peer_ub >> peer_ua >>
  198. peer_sx >> peer_sa;
  199. ub ^= peer_ub;
  200. ua += peer_ua;
  201. sx ^= peer_sx;
  202. sa += peer_sa;
  203. if (ub.bshare || ua.ashare || sx.xshare || sa.ashare) {
  204. printf("**** %x %016lx %016lx %016lx\n",
  205. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  206. printf("SCALE %016lx %016lx\n",
  207. peer_scaled_xor.xshare, peer_scaled_sum.ashare);
  208. }
  209. }
  210. }
  211. printf("\n");
  212. }
  213. }
  214. tio.send();
  215. });
  216. }
  217. pool.join();
  218. }
  219. static void rdpf_timing(MPCIO &mpcio, const PRACOptions &opts, char **args)
  220. {
  221. nbits_t depth=6;
  222. if (*args) {
  223. depth = atoi(*args);
  224. ++args;
  225. }
  226. int num_threads = opts.num_threads;
  227. boost::asio::thread_pool pool(num_threads);
  228. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  229. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  230. MPCTIO tio(mpcio, thread_num);
  231. size_t &op_counter = tio.aes_ops();
  232. if (mpcio.player == 2) {
  233. RDPFPair dp = tio.rdpfpair(depth);
  234. for (int i=0;i<2;++i) {
  235. RDPF &dpf = dp.dpf[i];
  236. dpf.expand(op_counter);
  237. RegXS scaled_xor;
  238. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  239. DPFnode leaf = dpf.leaf(x, op_counter);
  240. RegXS sx = dpf.scaled_xs(leaf);
  241. scaled_xor ^= sx;
  242. }
  243. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  244. dpf.scaled_xor.xshare);
  245. printf("\n");
  246. }
  247. } else {
  248. RDPFTriple dt = tio.rdpftriple(depth);
  249. for (int i=0;i<3;++i) {
  250. RDPF &dpf = dt.dpf[i];
  251. dpf.expand(op_counter);
  252. RegXS scaled_xor;
  253. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  254. DPFnode leaf = dpf.leaf(x, op_counter);
  255. RegXS sx = dpf.scaled_xs(leaf);
  256. scaled_xor ^= sx;
  257. }
  258. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  259. dpf.scaled_xor.xshare);
  260. printf("\n");
  261. }
  262. }
  263. tio.send();
  264. });
  265. }
  266. pool.join();
  267. }
  268. static void rdpfeval_timing(MPCIO &mpcio, const PRACOptions &opts, char **args)
  269. {
  270. nbits_t depth=6;
  271. address_t start=0;
  272. if (*args) {
  273. depth = atoi(*args);
  274. ++args;
  275. }
  276. if (*args) {
  277. start = atoi(*args);
  278. ++args;
  279. }
  280. int num_threads = opts.num_threads;
  281. boost::asio::thread_pool pool(num_threads);
  282. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  283. boost::asio::post(pool, [&mpcio, thread_num, depth, start] {
  284. MPCTIO tio(mpcio, thread_num);
  285. size_t &op_counter = tio.aes_ops();
  286. if (mpcio.player == 2) {
  287. RDPFPair dp = tio.rdpfpair(depth);
  288. for (int i=0;i<2;++i) {
  289. RDPF &dpf = dp.dpf[i];
  290. RegXS scaled_xor;
  291. auto ev = StreamEval(dpf, start, op_counter, false);
  292. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  293. DPFnode leaf = ev.next();
  294. RegXS sx = dpf.scaled_xs(leaf);
  295. scaled_xor ^= sx;
  296. }
  297. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  298. dpf.scaled_xor.xshare);
  299. printf("\n");
  300. }
  301. } else {
  302. RDPFTriple dt = tio.rdpftriple(depth);
  303. for (int i=0;i<3;++i) {
  304. RDPF &dpf = dt.dpf[i];
  305. RegXS scaled_xor;
  306. auto ev = StreamEval(dpf, start, op_counter, false);
  307. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  308. DPFnode leaf = ev.next();
  309. RegXS sx = dpf.scaled_xs(leaf);
  310. scaled_xor ^= sx;
  311. }
  312. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  313. dpf.scaled_xor.xshare);
  314. printf("\n");
  315. }
  316. }
  317. tio.send();
  318. });
  319. }
  320. pool.join();
  321. }
  322. static void tupleeval_timing(MPCIO &mpcio, const PRACOptions &opts, char **args)
  323. {
  324. nbits_t depth=6;
  325. address_t start=0;
  326. if (*args) {
  327. depth = atoi(*args);
  328. ++args;
  329. }
  330. if (*args) {
  331. start = atoi(*args);
  332. ++args;
  333. }
  334. int num_threads = opts.num_threads;
  335. boost::asio::thread_pool pool(num_threads);
  336. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  337. boost::asio::post(pool, [&mpcio, thread_num, depth, start] {
  338. MPCTIO tio(mpcio, thread_num);
  339. size_t &op_counter = tio.aes_ops();
  340. if (mpcio.player == 2) {
  341. RDPFPair dp = tio.rdpfpair(depth);
  342. RegXS scaled_xor0, scaled_xor1;
  343. auto ev = StreamEval(dp, start, op_counter, false);
  344. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  345. auto [L0, L1] = ev.next();
  346. RegXS sx0 = dp.dpf[0].scaled_xs(L0);
  347. RegXS sx1 = dp.dpf[1].scaled_xs(L1);
  348. scaled_xor0 ^= sx0;
  349. scaled_xor1 ^= sx1;
  350. }
  351. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  352. dp.dpf[0].scaled_xor.xshare);
  353. printf("\n");
  354. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  355. dp.dpf[1].scaled_xor.xshare);
  356. printf("\n");
  357. } else {
  358. RDPFTriple dt = tio.rdpftriple(depth);
  359. RegXS scaled_xor0, scaled_xor1, scaled_xor2;
  360. auto ev = StreamEval(dt, start, op_counter, false);
  361. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  362. auto [L0, L1, L2] = ev.next();
  363. RegXS sx0 = dt.dpf[0].scaled_xs(L0);
  364. RegXS sx1 = dt.dpf[1].scaled_xs(L1);
  365. RegXS sx2 = dt.dpf[2].scaled_xs(L2);
  366. scaled_xor0 ^= sx0;
  367. scaled_xor1 ^= sx1;
  368. scaled_xor2 ^= sx2;
  369. }
  370. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  371. dt.dpf[0].scaled_xor.xshare);
  372. printf("\n");
  373. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  374. dt.dpf[1].scaled_xor.xshare);
  375. printf("\n");
  376. printf("%016lx\n%016lx\n", scaled_xor2.xshare,
  377. dt.dpf[2].scaled_xor.xshare);
  378. printf("\n");
  379. }
  380. tio.send();
  381. });
  382. }
  383. pool.join();
  384. }
  385. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  386. {
  387. if (!*args) {
  388. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  389. return;
  390. } else if (!strcmp(*args, "test")) {
  391. ++args;
  392. online_test(mpcio, opts, args);
  393. } else if (!strcmp(*args, "lamporttest")) {
  394. ++args;
  395. lamport_test(mpcio, opts, args);
  396. } else if (!strcmp(*args, "rdpftest")) {
  397. ++args;
  398. rdpf_test(mpcio, opts, args);
  399. } else if (!strcmp(*args, "rdpftime")) {
  400. ++args;
  401. rdpf_timing(mpcio, opts, args);
  402. } else if (!strcmp(*args, "evaltime")) {
  403. ++args;
  404. rdpfeval_timing(mpcio, opts, args);
  405. } else if (!strcmp(*args, "tupletime")) {
  406. ++args;
  407. tupleeval_timing(mpcio, opts, args);
  408. } else {
  409. std::cerr << "Unknown mode " << *args << "\n";
  410. }
  411. }