online.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. scaled_xor.xshare = 0;
  239. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  240. DPFnode leaf = dpf.leaf(x, op_counter);
  241. RegXS sx = dpf.scaled_xs(leaf);
  242. scaled_xor ^= sx;
  243. }
  244. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  245. dpf.scaled_xor.xshare);
  246. printf("\n");
  247. }
  248. } else {
  249. RDPFTriple dt = tio.rdpftriple(depth);
  250. for (int i=0;i<3;++i) {
  251. RDPF &dpf = dt.dpf[i];
  252. dpf.expand(op_counter);
  253. RegXS scaled_xor;
  254. scaled_xor.xshare = 0;
  255. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  256. DPFnode leaf = dpf.leaf(x, op_counter);
  257. RegXS sx = dpf.scaled_xs(leaf);
  258. scaled_xor ^= sx;
  259. }
  260. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  261. dpf.scaled_xor.xshare);
  262. printf("\n");
  263. }
  264. }
  265. tio.send();
  266. });
  267. }
  268. pool.join();
  269. }
  270. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  271. {
  272. if (!*args) {
  273. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  274. return;
  275. } else if (!strcmp(*args, "test")) {
  276. ++args;
  277. online_test(mpcio, opts, args);
  278. } else if (!strcmp(*args, "lamporttest")) {
  279. ++args;
  280. lamport_test(mpcio, opts, args);
  281. } else if (!strcmp(*args, "rdpftest")) {
  282. ++args;
  283. rdpf_test(mpcio, opts, args);
  284. } else if (!strcmp(*args, "rdpftime")) {
  285. ++args;
  286. rdpf_timing(mpcio, opts, args);
  287. } else {
  288. std::cerr << "Unknown mode " << *args << "\n";
  289. }
  290. }