online.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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, int num_threads, 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, int num_threads, 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. boost::asio::thread_pool pool(num_threads);
  121. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  122. boost::asio::post(pool, [&mpcio, thread_num, niters, chunksize, numchunks] {
  123. MPCTIO tio(mpcio, thread_num);
  124. char *sendbuf = new char[chunksize];
  125. char *recvbuf = new char[chunksize*numchunks];
  126. for (size_t i=0; i<niters; ++i) {
  127. for (size_t chunk=0; chunk<numchunks; ++chunk) {
  128. arc4random_buf(sendbuf, chunksize);
  129. tio.queue_peer(sendbuf, chunksize);
  130. }
  131. tio.send();
  132. tio.recv_peer(recvbuf, chunksize*numchunks);
  133. }
  134. delete[] recvbuf;
  135. delete[] sendbuf;
  136. });
  137. }
  138. pool.join();
  139. }
  140. static void rdpf_test(MPCIO &mpcio, int num_threads, char **args)
  141. {
  142. nbits_t depth=6;
  143. if (*args) {
  144. depth = atoi(*args);
  145. ++args;
  146. }
  147. boost::asio::thread_pool pool(num_threads);
  148. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  149. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  150. MPCTIO tio(mpcio, thread_num);
  151. size_t &op_counter = tio.aes_ops();
  152. if (mpcio.player == 2) {
  153. RDPFPair dp = tio.rdpfpair(depth);
  154. for (int i=0;i<2;++i) {
  155. const RDPF &dpf = dp.dpf[i];
  156. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  157. DPFnode leaf = dpf.leaf(x, op_counter);
  158. RegBS ub = dpf.unit_bs(leaf);
  159. RegAS ua = dpf.unit_as(leaf);
  160. RegXS sx = dpf.scaled_xs(leaf);
  161. RegAS sa = dpf.scaled_as(leaf);
  162. printf("%04x %x %016lx %016lx %016lx\n", x,
  163. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  164. }
  165. }
  166. } else {
  167. RDPFTriple dt = tio.rdpftriple(depth);
  168. for (int i=0;i<3;++i) {
  169. const RDPF &dpf = dt.dpf[i];
  170. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  171. DPFnode leaf = dpf.leaf(x, op_counter);
  172. RegBS ub = dpf.unit_bs(leaf);
  173. RegAS ua = dpf.unit_as(leaf);
  174. RegXS sx = dpf.scaled_xs(leaf);
  175. RegAS sa = dpf.scaled_as(leaf);
  176. printf("%04x %x %016lx %016lx %016lx\n", x,
  177. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  178. }
  179. }
  180. }
  181. });
  182. }
  183. pool.join();
  184. }
  185. void online_main(MPCIO &mpcio, int num_threads, char **args)
  186. {
  187. if (!*args) {
  188. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  189. return;
  190. } else if (!strcmp(*args, "test")) {
  191. ++args;
  192. online_test(mpcio, num_threads, args);
  193. } else if (!strcmp(*args, "lamporttest")) {
  194. ++args;
  195. lamport_test(mpcio, num_threads, args);
  196. } else if (!strcmp(*args, "rdpftest")) {
  197. ++args;
  198. rdpf_test(mpcio, num_threads, args);
  199. } else {
  200. std::cerr << "Unknown mode " << *args << "\n";
  201. }
  202. }