online.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. #include <bsd/stdlib.h> // arc4random_buf
  2. #include "online.hpp"
  3. #include "mpcops.hpp"
  4. #include "rdpf.hpp"
  5. #include "duoram.hpp"
  6. #include "cdpf.hpp"
  7. #include "cell.hpp"
  8. #include "bst.hpp"
  9. #include "avl.hpp"
  10. static void online_test(MPCIO &mpcio,
  11. const PRACOptions &opts, char **args)
  12. {
  13. nbits_t nbits = VALUE_BITS;
  14. if (*args) {
  15. nbits = atoi(*args);
  16. }
  17. size_t as_memsize = 9;
  18. size_t xs_memsize = 3;
  19. MPCTIO tio(mpcio, 0);
  20. bool is_server = (mpcio.player == 2);
  21. RegAS *A = new RegAS[as_memsize];
  22. RegXS *AX = new RegXS[xs_memsize];
  23. value_t V;
  24. RegBS F0, F1, F2;
  25. RegBS FA, FO, FS;
  26. RegXS X;
  27. if (!is_server) {
  28. A[0].randomize();
  29. A[1].randomize();
  30. F0.randomize();
  31. A[4].randomize();
  32. F1.randomize();
  33. F2.randomize();
  34. A[6].randomize();
  35. A[7].randomize();
  36. X.randomize();
  37. AX[0].randomize();
  38. AX[1].randomize();
  39. arc4random_buf(&V, sizeof(V));
  40. printf("A:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  41. printf("AX:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, AX[i].xshare);
  42. printf("V : %016lX\n", V);
  43. printf("F0 : %01X\n", F0.bshare);
  44. printf("F1 : %01X\n", F1.bshare);
  45. printf("F2 : %01X\n", F2.bshare);
  46. printf("X : %016lX\n", X.xshare);
  47. }
  48. std::vector<coro_t> coroutines;
  49. coroutines.emplace_back(
  50. [&tio, &A, nbits](yield_t &yield) {
  51. mpc_mul(tio, yield, A[2], A[0], A[1], nbits);
  52. });
  53. coroutines.emplace_back(
  54. [&tio, &A, V, nbits](yield_t &yield) {
  55. mpc_valuemul(tio, yield, A[3], V, nbits);
  56. });
  57. coroutines.emplace_back(
  58. [&tio, &A, &F0, nbits](yield_t &yield) {
  59. mpc_flagmult(tio, yield, A[5], F0, A[4], nbits);
  60. });
  61. coroutines.emplace_back(
  62. [&tio, &A, &F1, nbits](yield_t &yield) {
  63. mpc_oswap(tio, yield, A[6], A[7], F1, nbits);
  64. });
  65. coroutines.emplace_back(
  66. [&tio, &A, &X, nbits](yield_t &yield) {
  67. mpc_xs_to_as(tio, yield, A[8], X, nbits);
  68. });
  69. coroutines.emplace_back(
  70. [&tio, &AX, &F0, nbits](yield_t &yield) {
  71. mpc_select(tio, yield, AX[2], F0, AX[0], AX[1], nbits);
  72. });
  73. coroutines.emplace_back(
  74. [&tio, &FA, &F0, &F1](yield_t &yield) {
  75. mpc_and(tio, yield, FA, F0, F1);
  76. });
  77. coroutines.emplace_back(
  78. [&tio, &FO, &F0, &F1](yield_t &yield) {
  79. mpc_or(tio, yield, FO, F0, F1);
  80. });
  81. coroutines.emplace_back(
  82. [&tio, &FS, &F0, &F1, &F2](yield_t &yield) {
  83. mpc_select(tio, yield, FS, F2, F0, F1);
  84. });
  85. run_coroutines(tio, coroutines);
  86. if (!is_server) {
  87. printf("\n");
  88. printf("A:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  89. printf("AX:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, AX[i].xshare);
  90. }
  91. // Check the answers
  92. if (mpcio.player == 1) {
  93. tio.queue_peer(A, as_memsize*sizeof(RegAS));
  94. tio.queue_peer(AX, xs_memsize*sizeof(RegXS));
  95. tio.queue_peer(&V, sizeof(V));
  96. tio.queue_peer(&F0, sizeof(RegBS));
  97. tio.queue_peer(&F1, sizeof(RegBS));
  98. tio.queue_peer(&F2, sizeof(RegBS));
  99. tio.queue_peer(&FA, sizeof(RegBS));
  100. tio.queue_peer(&FO, sizeof(RegBS));
  101. tio.queue_peer(&FS, sizeof(RegBS));
  102. tio.queue_peer(&X, sizeof(RegXS));
  103. tio.send();
  104. } else if (mpcio.player == 0) {
  105. RegAS *B = new RegAS[as_memsize];
  106. RegXS *BAX = new RegXS[xs_memsize];
  107. RegBS BF0, BF1, BF2;
  108. RegBS BFA, BFO, BFS;
  109. RegXS BX;
  110. value_t BV;
  111. value_t *S = new value_t[as_memsize];
  112. value_t *Y = new value_t[xs_memsize];
  113. bit_t SF0, SF1, SF2;
  114. bit_t SFA, SFO, SFS;
  115. value_t SX;
  116. tio.recv_peer(B, as_memsize*sizeof(RegAS));
  117. tio.recv_peer(BAX, xs_memsize*sizeof(RegXS));
  118. tio.recv_peer(&BV, sizeof(BV));
  119. tio.recv_peer(&BF0, sizeof(RegBS));
  120. tio.recv_peer(&BF1, sizeof(RegBS));
  121. tio.recv_peer(&BF2, sizeof(RegBS));
  122. tio.recv_peer(&BFA, sizeof(RegBS));
  123. tio.recv_peer(&BFO, sizeof(RegBS));
  124. tio.recv_peer(&BFS, sizeof(RegBS));
  125. tio.recv_peer(&BX, sizeof(RegXS));
  126. for(size_t i=0; i<as_memsize; ++i) S[i] = A[i].ashare+B[i].ashare;
  127. for(size_t i=0; i<xs_memsize; ++i) Y[i] = AX[i].xshare^BAX[i].xshare;
  128. SF0 = F0.bshare ^ BF0.bshare;
  129. SF1 = F1.bshare ^ BF1.bshare;
  130. SF2 = F2.bshare ^ BF2.bshare;
  131. SFA = FA.bshare ^ BFA.bshare;
  132. SFO = FO.bshare ^ BFO.bshare;
  133. SFS = FS.bshare ^ BFS.bshare;
  134. SX = X.xshare ^ BX.xshare;
  135. printf("S:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, S[i]);
  136. printf("Y:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, Y[i]);
  137. printf("SF0: %01X\n", SF0);
  138. printf("SF1: %01X\n", SF1);
  139. printf("SF2: %01X\n", SF2);
  140. printf("SFA: %01X\n", SFA);
  141. printf("SFO: %01X\n", SFO);
  142. printf("SFS: %01X\n", SFS);
  143. printf("SX : %016lX\n", SX);
  144. printf("\n%016lx\n", S[0]*S[1]-S[2]);
  145. printf("%016lx\n", (V*BV)-S[3]);
  146. printf("%016lx\n", (SF0*S[4])-S[5]);
  147. printf("%016lx\n", S[8]-SX);
  148. delete[] B;
  149. delete[] S;
  150. }
  151. delete[] A;
  152. delete[] AX;
  153. }
  154. static void lamport_test(MPCIO &mpcio,
  155. const PRACOptions &opts, char **args)
  156. {
  157. // Create a bunch of threads and send a bunch of data to the other
  158. // peer, and receive their data. If an arg is specified, repeat
  159. // that many times. The Lamport clock at the end should be just the
  160. // number of repetitions. Subsequent args are the chunk size and
  161. // the number of chunks per message
  162. size_t niters = 1;
  163. size_t chunksize = 1<<20;
  164. size_t numchunks = 1;
  165. if (*args) {
  166. niters = atoi(*args);
  167. ++args;
  168. }
  169. if (*args) {
  170. chunksize = atoi(*args);
  171. ++args;
  172. }
  173. if (*args) {
  174. numchunks = atoi(*args);
  175. ++args;
  176. }
  177. int num_threads = opts.num_threads;
  178. boost::asio::thread_pool pool(num_threads);
  179. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  180. boost::asio::post(pool, [&mpcio, thread_num, niters, chunksize, numchunks] {
  181. MPCTIO tio(mpcio, thread_num);
  182. char *sendbuf = new char[chunksize];
  183. char *recvbuf = new char[chunksize*numchunks];
  184. for (size_t i=0; i<niters; ++i) {
  185. for (size_t chunk=0; chunk<numchunks; ++chunk) {
  186. arc4random_buf(sendbuf, chunksize);
  187. tio.queue_peer(sendbuf, chunksize);
  188. }
  189. tio.send();
  190. tio.recv_peer(recvbuf, chunksize*numchunks);
  191. }
  192. delete[] recvbuf;
  193. delete[] sendbuf;
  194. });
  195. }
  196. pool.join();
  197. }
  198. template <nbits_t WIDTH>
  199. static void rdpf_test(MPCIO &mpcio,
  200. const PRACOptions &opts, char **args)
  201. {
  202. nbits_t depth=6;
  203. size_t num_iters = 1;
  204. if (*args) {
  205. depth = atoi(*args);
  206. ++args;
  207. }
  208. if (*args) {
  209. num_iters = atoi(*args);
  210. ++args;
  211. }
  212. MPCTIO tio(mpcio, 0, opts.num_threads);
  213. run_coroutines(tio, [&tio, depth, num_iters] (yield_t &yield) {
  214. size_t &aes_ops = tio.aes_ops();
  215. for (size_t iter=0; iter < num_iters; ++iter) {
  216. if (tio.player() == 2) {
  217. RDPFPair<WIDTH> dp = tio.rdpfpair<WIDTH>(yield, depth);
  218. for (int i=0;i<2;++i) {
  219. const RDPF<WIDTH> &dpf = dp.dpf[i];
  220. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  221. typename RDPF<WIDTH>::LeafNode leaf = dpf.leaf(x, aes_ops);
  222. RegBS ub = dpf.unit_bs(leaf);
  223. RegAS ua = dpf.unit_as(leaf);
  224. typename RDPF<WIDTH>::RegXSW sx = dpf.scaled_xs(leaf);
  225. typename RDPF<WIDTH>::RegASW sa = dpf.scaled_as(leaf);
  226. printf("%04x %x %016lx", x, ub.bshare, ua.ashare);
  227. for (nbits_t j=0;j<WIDTH;++j) {
  228. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  229. }
  230. printf("\n");
  231. }
  232. printf("\n");
  233. }
  234. } else {
  235. RDPFTriple<WIDTH> dt = tio.rdpftriple<WIDTH>(yield, depth);
  236. for (int i=0;i<3;++i) {
  237. const RDPF<WIDTH> &dpf = dt.dpf[i];
  238. typename RDPF<WIDTH>::RegXSW peer_scaled_xor;
  239. typename RDPF<WIDTH>::RegASW peer_scaled_sum;
  240. if (tio.player() == 1) {
  241. tio.iostream_peer() << dpf.li[0].scaled_xor << dpf.li[0].scaled_sum;
  242. } else {
  243. tio.iostream_peer() >> peer_scaled_xor >> peer_scaled_sum;
  244. peer_scaled_sum += dpf.li[0].scaled_sum;
  245. peer_scaled_xor ^= dpf.li[0].scaled_xor;
  246. }
  247. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  248. typename RDPF<WIDTH>::LeafNode leaf = dpf.leaf(x, aes_ops);
  249. RegBS ub = dpf.unit_bs(leaf);
  250. RegAS ua = dpf.unit_as(leaf);
  251. typename RDPF<WIDTH>::RegXSW sx = dpf.scaled_xs(leaf);
  252. typename RDPF<WIDTH>::RegASW sa = dpf.scaled_as(leaf);
  253. printf("%04x %x %016lx", x, ub.bshare, ua.ashare);
  254. for (nbits_t j=0;j<WIDTH;++j) {
  255. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  256. }
  257. printf("\n");
  258. if (tio.player() == 1) {
  259. tio.iostream_peer() << ub << ua << sx << sa;
  260. } else {
  261. RegBS peer_ub;
  262. RegAS peer_ua;
  263. typename RDPF<WIDTH>::RegXSW peer_sx;
  264. typename RDPF<WIDTH>::RegASW peer_sa;
  265. tio.iostream_peer() >> peer_ub >> peer_ua >>
  266. peer_sx >> peer_sa;
  267. ub ^= peer_ub;
  268. ua += peer_ua;
  269. sx ^= peer_sx;
  270. sa += peer_sa;
  271. bool is_nonzero = ub.bshare || ua.ashare;
  272. for (nbits_t j=0;j<WIDTH;++j) {
  273. is_nonzero |= (sx[j].xshare || sa[j].ashare);
  274. }
  275. if (is_nonzero) {
  276. printf("**** %x %016lx", ub.bshare, ua.ashare);
  277. for (nbits_t j=0;j<WIDTH;++j) {
  278. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  279. }
  280. printf("\nSCALE ");
  281. for (nbits_t j=0;j<WIDTH;++j) {
  282. printf(" %016lx %016lx",
  283. peer_scaled_xor[j].xshare,
  284. peer_scaled_sum[j].ashare);
  285. }
  286. printf("\n");
  287. }
  288. }
  289. }
  290. printf("\n");
  291. }
  292. }
  293. }
  294. });
  295. }
  296. static void rdpf_timing(MPCIO &mpcio,
  297. const PRACOptions &opts, char **args)
  298. {
  299. nbits_t depth=6;
  300. if (*args) {
  301. depth = atoi(*args);
  302. ++args;
  303. }
  304. int num_threads = opts.num_threads;
  305. boost::asio::thread_pool pool(num_threads);
  306. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  307. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  308. MPCTIO tio(mpcio, thread_num);
  309. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  310. size_t &aes_ops = tio.aes_ops();
  311. if (tio.player() == 2) {
  312. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  313. for (int i=0;i<2;++i) {
  314. RDPF<1> &dpf = dp.dpf[i];
  315. dpf.expand(aes_ops);
  316. RDPF<1>::RegXSW scaled_xor;
  317. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  318. RDPF<1>::LeafNode leaf = dpf.leaf(x, aes_ops);
  319. RDPF<1>::RegXSW sx = dpf.scaled_xs(leaf);
  320. scaled_xor ^= sx;
  321. }
  322. printf("%016lx\n%016lx\n", scaled_xor[0].xshare,
  323. dpf.li[0].scaled_xor[0].xshare);
  324. printf("\n");
  325. }
  326. } else {
  327. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  328. for (int i=0;i<3;++i) {
  329. RDPF<1> &dpf = dt.dpf[i];
  330. dpf.expand(aes_ops);
  331. RDPF<1>::RegXSW scaled_xor;
  332. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  333. RDPF<1>::LeafNode leaf = dpf.leaf(x, aes_ops);
  334. RDPF<1>::RegXSW sx = dpf.scaled_xs(leaf);
  335. scaled_xor ^= sx;
  336. }
  337. printf("%016lx\n%016lx\n", scaled_xor[0].xshare,
  338. dpf.li[0].scaled_xor[0].xshare);
  339. printf("\n");
  340. }
  341. }
  342. });
  343. });
  344. }
  345. pool.join();
  346. }
  347. static value_t parallel_streameval_rdpf(MPCIO &mpcio, const RDPF<1> &dpf,
  348. address_t start, int num_threads)
  349. {
  350. RDPF<1>::RegXSW scaled_xor[num_threads];
  351. boost::asio::thread_pool pool(num_threads);
  352. address_t totsize = (address_t(1)<<dpf.depth());
  353. address_t threadstart = start;
  354. address_t threadchunk = totsize / num_threads;
  355. address_t threadextra = totsize % num_threads;
  356. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  357. address_t threadsize = threadchunk + (address_t(thread_num) < threadextra);
  358. boost::asio::post(pool,
  359. [&mpcio, &dpf, &scaled_xor, thread_num, threadstart, threadsize] {
  360. MPCTIO tio(mpcio, thread_num);
  361. //printf("Thread %d from %X for %X\n", thread_num, threadstart, threadsize);
  362. RDPF<1>::RegXSW local_xor;
  363. size_t local_aes_ops = 0;
  364. auto ev = StreamEval(dpf, threadstart, 0, local_aes_ops);
  365. for (address_t x=0;x<threadsize;++x) {
  366. //if (x%0x10000 == 0) printf("%d", thread_num);
  367. RDPF<1>::LeafNode leaf = ev.next();
  368. local_xor ^= dpf.scaled_xs(leaf);
  369. }
  370. scaled_xor[thread_num] = local_xor;
  371. tio.aes_ops() += local_aes_ops;
  372. //printf("Thread %d complete\n", thread_num);
  373. });
  374. threadstart = (threadstart + threadsize) % totsize;
  375. }
  376. pool.join();
  377. RDPF<1>::RegXSW res;
  378. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  379. res ^= scaled_xor[thread_num];
  380. }
  381. return res[0].xshare;
  382. }
  383. static void rdpfeval_timing(MPCIO &mpcio,
  384. const PRACOptions &opts, char **args)
  385. {
  386. nbits_t depth=6;
  387. address_t start=0;
  388. if (*args) {
  389. depth = atoi(*args);
  390. ++args;
  391. }
  392. if (*args) {
  393. start = strtoull(*args, NULL, 16);
  394. ++args;
  395. }
  396. int num_threads = opts.num_threads;
  397. MPCTIO tio(mpcio, 0, num_threads);
  398. run_coroutines(tio, [&mpcio, &tio, depth, start, num_threads] (yield_t &yield) {
  399. if (tio.player() == 2) {
  400. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  401. for (int i=0;i<2;++i) {
  402. RDPF<1> &dpf = dp.dpf[i];
  403. value_t scaled_xor =
  404. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  405. printf("%016lx\n%016lx\n", scaled_xor,
  406. dpf.li[0].scaled_xor[0].xshare);
  407. printf("\n");
  408. }
  409. } else {
  410. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  411. for (int i=0;i<3;++i) {
  412. RDPF<1> &dpf = dt.dpf[i];
  413. value_t scaled_xor =
  414. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  415. printf("%016lx\n%016lx\n", scaled_xor,
  416. dpf.li[0].scaled_xor[0].xshare);
  417. printf("\n");
  418. }
  419. }
  420. });
  421. }
  422. static void par_rdpfeval_timing(MPCIO &mpcio,
  423. const PRACOptions &opts, char **args)
  424. {
  425. nbits_t depth=6;
  426. address_t start=0;
  427. if (*args) {
  428. depth = atoi(*args);
  429. ++args;
  430. }
  431. if (*args) {
  432. start = strtoull(*args, NULL, 16);
  433. ++args;
  434. }
  435. int num_threads = opts.num_threads;
  436. MPCTIO tio(mpcio, 0, num_threads);
  437. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  438. if (tio.player() == 2) {
  439. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  440. for (int i=0;i<2;++i) {
  441. RDPF<1> &dpf = dp.dpf[i];
  442. nbits_t depth = dpf.depth();
  443. auto pe = ParallelEval(dpf, start, 0,
  444. address_t(1)<<depth, num_threads, tio.aes_ops());
  445. RDPF<1>::RegXSW result, init;
  446. result = pe.reduce(init, [&dpf] (int thread_num,
  447. address_t i, const RDPF<1>::LeafNode &leaf) {
  448. return dpf.scaled_xs(leaf);
  449. });
  450. printf("%016lx\n%016lx\n", result[0].xshare,
  451. dpf.li[0].scaled_xor[0].xshare);
  452. printf("\n");
  453. }
  454. } else {
  455. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  456. for (int i=0;i<3;++i) {
  457. RDPF<1> &dpf = dt.dpf[i];
  458. nbits_t depth = dpf.depth();
  459. auto pe = ParallelEval(dpf, start, 0,
  460. address_t(1)<<depth, num_threads, tio.aes_ops());
  461. RDPF<1>::RegXSW result, init;
  462. result = pe.reduce(init, [&dpf] (int thread_num,
  463. address_t i, const RDPF<1>::LeafNode &leaf) {
  464. return dpf.scaled_xs(leaf);
  465. });
  466. printf("%016lx\n%016lx\n", result[0].xshare,
  467. dpf.li[0].scaled_xor[0].xshare);
  468. printf("\n");
  469. }
  470. }
  471. });
  472. }
  473. static void tupleeval_timing(MPCIO &mpcio,
  474. const PRACOptions &opts, char **args)
  475. {
  476. nbits_t depth=6;
  477. address_t start=0;
  478. if (*args) {
  479. depth = atoi(*args);
  480. ++args;
  481. }
  482. if (*args) {
  483. start = atoi(*args);
  484. ++args;
  485. }
  486. int num_threads = opts.num_threads;
  487. MPCTIO tio(mpcio, 0, num_threads);
  488. run_coroutines(tio, [&tio, depth, start] (yield_t &yield) {
  489. size_t &aes_ops = tio.aes_ops();
  490. if (tio.player() == 2) {
  491. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  492. RDPF<1>::RegXSW scaled_xor0, scaled_xor1;
  493. auto ev = StreamEval(dp, start, 0, aes_ops, false);
  494. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  495. auto [L0, L1] = ev.next();
  496. RDPF<1>::RegXSW sx0 = dp.dpf[0].scaled_xs(L0);
  497. RDPF<1>::RegXSW sx1 = dp.dpf[1].scaled_xs(L1);
  498. scaled_xor0 ^= sx0;
  499. scaled_xor1 ^= sx1;
  500. }
  501. printf("%016lx\n%016lx\n", scaled_xor0[0].xshare,
  502. dp.dpf[0].li[0].scaled_xor[0].xshare);
  503. printf("\n");
  504. printf("%016lx\n%016lx\n", scaled_xor1[0].xshare,
  505. dp.dpf[1].li[0].scaled_xor[0].xshare);
  506. printf("\n");
  507. } else {
  508. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  509. RDPF<1>::RegXSW scaled_xor0, scaled_xor1, scaled_xor2;
  510. auto ev = StreamEval(dt, start, 0, aes_ops, false);
  511. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  512. auto [L0, L1, L2] = ev.next();
  513. RDPF<1>::RegXSW sx0 = dt.dpf[0].scaled_xs(L0);
  514. RDPF<1>::RegXSW sx1 = dt.dpf[1].scaled_xs(L1);
  515. RDPF<1>::RegXSW sx2 = dt.dpf[2].scaled_xs(L2);
  516. scaled_xor0 ^= sx0;
  517. scaled_xor1 ^= sx1;
  518. scaled_xor2 ^= sx2;
  519. }
  520. printf("%016lx\n%016lx\n", scaled_xor0[0].xshare,
  521. dt.dpf[0].li[0].scaled_xor[0].xshare);
  522. printf("\n");
  523. printf("%016lx\n%016lx\n", scaled_xor1[0].xshare,
  524. dt.dpf[1].li[0].scaled_xor[0].xshare);
  525. printf("\n");
  526. printf("%016lx\n%016lx\n", scaled_xor2[0].xshare,
  527. dt.dpf[2].li[0].scaled_xor[0].xshare);
  528. printf("\n");
  529. }
  530. });
  531. }
  532. static void par_tupleeval_timing(MPCIO &mpcio,
  533. const PRACOptions &opts, char **args)
  534. {
  535. nbits_t depth=6;
  536. address_t start=0;
  537. if (*args) {
  538. depth = atoi(*args);
  539. ++args;
  540. }
  541. if (*args) {
  542. start = atoi(*args);
  543. ++args;
  544. }
  545. int num_threads = opts.num_threads;
  546. MPCTIO tio(mpcio, 0, num_threads);
  547. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  548. size_t &aes_ops = tio.aes_ops();
  549. if (tio.player() == 2) {
  550. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  551. auto pe = ParallelEval(dp, start, 0, address_t(1)<<depth,
  552. num_threads, aes_ops);
  553. RDPFPair<1>::RegXSWP result, init;
  554. result = pe.reduce(init, [&dp] (int thread_num, address_t i,
  555. const RDPFPair<1>::LeafNode &leaf) {
  556. RDPFPair<1>::RegXSWP scaled;
  557. dp.scaled(scaled, leaf);
  558. return scaled;
  559. });
  560. printf("%016lx\n%016lx\n", std::get<0>(result)[0].xshare,
  561. dp.dpf[0].li[0].scaled_xor[0].xshare);
  562. printf("\n");
  563. printf("%016lx\n%016lx\n", std::get<1>(result)[0].xshare,
  564. dp.dpf[1].li[0].scaled_xor[0].xshare);
  565. printf("\n");
  566. } else {
  567. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  568. auto pe = ParallelEval(dt, start, 0, address_t(1)<<depth,
  569. num_threads, aes_ops);
  570. RDPFTriple<1>::RegXSWT result, init;
  571. result = pe.reduce(init, [&dt] (int thread_num, address_t i,
  572. const RDPFTriple<1>::LeafNode &leaf) {
  573. RDPFTriple<1>::RegXSWT scaled;
  574. dt.scaled(scaled, leaf);
  575. return scaled;
  576. });
  577. printf("%016lx\n%016lx\n", std::get<0>(result)[0].xshare,
  578. dt.dpf[0].li[0].scaled_xor[0].xshare);
  579. printf("\n");
  580. printf("%016lx\n%016lx\n", std::get<1>(result)[0].xshare,
  581. dt.dpf[1].li[0].scaled_xor[0].xshare);
  582. printf("\n");
  583. printf("%016lx\n%016lx\n", std::get<2>(result)[0].xshare,
  584. dt.dpf[2].li[0].scaled_xor[0].xshare);
  585. printf("\n");
  586. }
  587. });
  588. }
  589. // T is RegAS or RegXS for additive or XOR shared database respectively
  590. template <typename T>
  591. static void duoram_test(MPCIO &mpcio,
  592. const PRACOptions &opts, char **args)
  593. {
  594. nbits_t depth=6;
  595. address_t share=arc4random();
  596. if (*args) {
  597. depth = atoi(*args);
  598. ++args;
  599. }
  600. if (*args) {
  601. share = atoi(*args);
  602. ++args;
  603. }
  604. share &= ((address_t(1)<<depth)-1);
  605. MPCTIO tio(mpcio, 0, opts.num_threads);
  606. run_coroutines(tio, [&tio, depth, share] (yield_t &yield) {
  607. size_t size = size_t(1)<<depth;
  608. // size_t &aes_ops = tio.aes_ops();
  609. Duoram<T> oram(tio.player(), size);
  610. auto A = oram.flat(tio, yield);
  611. RegAS aidx, aidx2, aidx3;
  612. aidx.ashare = share;
  613. aidx2.ashare = share + tio.player();
  614. aidx3.ashare = share + 1;
  615. T M;
  616. if (tio.player() == 0) {
  617. M.set(0xbabb0000);
  618. } else {
  619. M.set(0x0000a66e);
  620. }
  621. RegXS xidx;
  622. xidx.xshare = share;
  623. T N;
  624. if (tio.player() == 0) {
  625. N.set(0xdead0000);
  626. } else {
  627. N.set(0x0000beef);
  628. }
  629. // Writing and reading with additively shared indices
  630. printf("Additive Updating\n");
  631. A[aidx] += M;
  632. printf("Additive Reading\n");
  633. T Aa = A[aidx];
  634. // Writing and reading with XOR shared indices
  635. printf("XOR Updating\n");
  636. A[xidx] += N;
  637. printf("XOR Reading\n");
  638. T Ax = A[xidx];
  639. T Ae;
  640. // Writing and reading with explicit indices
  641. if (depth > 2) {
  642. printf("Explicit Updating\n");
  643. A[5] += Aa;
  644. printf("Explicit Reading\n");
  645. Ae = A[6];
  646. }
  647. // Simultaneous independent reads
  648. printf("3 independent reading\n");
  649. std::vector<T> Av = A[std::array {
  650. aidx, aidx2, aidx3
  651. }];
  652. // Simultaneous independent updates
  653. T Aw1, Aw2, Aw3;
  654. Aw1.set(0x101010101010101 * tio.player());
  655. Aw2.set(0x202020202020202 * tio.player());
  656. Aw3.set(0x303030303030303 * tio.player());
  657. printf("3 independent updating\n");
  658. A[std::array { aidx, aidx2, aidx3 }] -=
  659. std::array { Aw1, Aw2, Aw3 };
  660. if (depth <= 10) {
  661. oram.dump();
  662. auto check = A.reconstruct();
  663. if (tio.player() == 0) {
  664. for (address_t i=0;i<size;++i) {
  665. printf("%04x %016lx\n", i, check[i].share());
  666. }
  667. }
  668. }
  669. auto checkread = A.reconstruct(Aa);
  670. auto checkreade = A.reconstruct(Ae);
  671. auto checkreadx = A.reconstruct(Ax);
  672. if (tio.player() == 0) {
  673. printf("Read AS value = %016lx\n", checkread.share());
  674. printf("Read AX value = %016lx\n", checkreadx.share());
  675. printf("Read Ex value = %016lx\n", checkreade.share());
  676. }
  677. for (auto &v : Av) {
  678. auto checkv = A.reconstruct(v);
  679. if (tio.player() == 0) {
  680. printf("Read Av value = %016lx\n", checkv.share());
  681. }
  682. }
  683. });
  684. }
  685. // This measures the same things as the Duoram paper: dependent and
  686. // independent reads, updates, writes, and interleaves
  687. // T is RegAS or RegXS for additive or XOR shared database respectively
  688. template <typename T>
  689. static void duoram(MPCIO &mpcio,
  690. const PRACOptions &opts, char **args)
  691. {
  692. nbits_t depth = 6;
  693. int items = 4;
  694. if (*args) {
  695. depth = atoi(*args);
  696. ++args;
  697. }
  698. if (*args) {
  699. items = atoi(*args);
  700. ++args;
  701. }
  702. MPCTIO tio(mpcio, 0, opts.num_threads);
  703. run_coroutines(tio, [&mpcio, &tio, depth, items] (yield_t &yield) {
  704. size_t size = size_t(1)<<depth;
  705. address_t mask = (depth < ADDRESS_MAX_BITS ?
  706. ((address_t(1)<<depth) - 1) : ~0);
  707. Duoram<T> oram(tio.player(), size);
  708. auto A = oram.flat(tio, yield);
  709. std::cout << "===== DEPENDENT UPDATES =====\n";
  710. mpcio.reset_stats();
  711. tio.reset_lamport();
  712. // Make a linked list of length items
  713. std::vector<T> list_indices;
  714. T prev_index, next_index;
  715. prev_index.randomize(depth);
  716. for (int i=0;i<items;++i) {
  717. next_index.randomize(depth);
  718. A[next_index] += prev_index;
  719. list_indices.push_back(next_index);
  720. prev_index = next_index;
  721. }
  722. tio.sync_lamport();
  723. mpcio.dump_stats(std::cout);
  724. std::cout << "\n===== DEPENDENT READS =====\n";
  725. mpcio.reset_stats();
  726. tio.reset_lamport();
  727. // Read the linked list starting with prev_index
  728. T cur_index = prev_index;
  729. for (int i=0;i<items;++i) {
  730. cur_index = A[cur_index];
  731. }
  732. tio.sync_lamport();
  733. mpcio.dump_stats(std::cout);
  734. std::cout << "\n===== INDEPENDENT READS =====\n";
  735. mpcio.reset_stats();
  736. tio.reset_lamport();
  737. // Read all the entries in the list at once
  738. std::vector<T> read_outputs = A[list_indices];
  739. tio.sync_lamport();
  740. mpcio.dump_stats(std::cout);
  741. std::cout << "\n===== INDEPENDENT UPDATES =====\n";
  742. mpcio.reset_stats();
  743. tio.reset_lamport();
  744. // Make a vector of indices 1 larger than those in list_indices,
  745. // and a vector of values 1 larger than those in outputs
  746. std::vector<T> indep_indices, indep_values;
  747. T one;
  748. one.set(tio.player()); // Sets the shared value to 1
  749. for (int i=0;i<items;++i) {
  750. indep_indices.push_back(list_indices[i]+one);
  751. indep_values.push_back(read_outputs[i]+one);
  752. }
  753. // Update all the indices at once
  754. A[indep_indices] += indep_values;
  755. tio.sync_lamport();
  756. mpcio.dump_stats(std::cout);
  757. std::cout << "\n===== DEPENDENT WRITES =====\n";
  758. mpcio.reset_stats();
  759. tio.reset_lamport();
  760. T two;
  761. two.set(2*tio.player()); // Sets the shared value to 2
  762. // For each address addr that's number i from the end of the
  763. // linked list, write i+1 into location addr+2
  764. for (int i=0;i<items;++i) {
  765. T val;
  766. val.set((i+1)*tio.player());
  767. A[list_indices[i]+two] = val;
  768. }
  769. tio.sync_lamport();
  770. mpcio.dump_stats(std::cout);
  771. std::cout << "\n===== DEPENDENT INTERLEAVED =====\n";
  772. mpcio.reset_stats();
  773. tio.reset_lamport();
  774. T three;
  775. three.set(3*tio.player()); // Sets the shared value to 3
  776. // Follow the linked list and whenever A[addr]=val, set
  777. // A[addr+3]=val+3
  778. cur_index = prev_index;
  779. for (int i=0;i<items;++i) {
  780. T next_index = A[cur_index];
  781. A[cur_index+three] = next_index+three;
  782. cur_index = next_index;
  783. }
  784. tio.sync_lamport();
  785. mpcio.dump_stats(std::cout);
  786. std::cout << "\n";
  787. mpcio.reset_stats();
  788. tio.reset_lamport();
  789. if (depth <= 30) {
  790. auto check = A.reconstruct();
  791. auto head = A.reconstruct(prev_index);
  792. if (tio.player() == 0) {
  793. int width = (depth+3)/4;
  794. printf("Head of linked list: %0*lx\n\n", width,
  795. head.share() & mask);
  796. std::cout << "Non-zero reconstructed database entries:\n";
  797. for (address_t i=0;i<size;++i) {
  798. value_t share = check[i].share() & mask;
  799. if (share) printf("%0*x: %0*lx\n", width, i, width, share);
  800. }
  801. }
  802. }
  803. });
  804. }
  805. static void cdpf_test(MPCIO &mpcio,
  806. const PRACOptions &opts, char **args)
  807. {
  808. value_t query, target;
  809. int iters = 1;
  810. arc4random_buf(&query, sizeof(query));
  811. arc4random_buf(&target, sizeof(target));
  812. if (*args) {
  813. query = strtoull(*args, NULL, 16);
  814. ++args;
  815. }
  816. if (*args) {
  817. target = strtoull(*args, NULL, 16);
  818. ++args;
  819. }
  820. if (*args) {
  821. iters = atoi(*args);
  822. ++args;
  823. }
  824. int num_threads = opts.num_threads;
  825. boost::asio::thread_pool pool(num_threads);
  826. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  827. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  828. MPCTIO tio(mpcio, thread_num);
  829. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  830. size_t &aes_ops = tio.aes_ops();
  831. for (int i=0;i<iters;++i) {
  832. if (tio.player() == 2) {
  833. tio.cdpf(yield);
  834. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  835. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  836. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  837. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  838. dump_node(leaf0 ^ leaf1);
  839. } else {
  840. CDPF dpf = tio.cdpf(yield);
  841. printf("ashare = %016lX\nxshare = %016lX\n",
  842. dpf.as_target.ashare, dpf.xs_target.xshare);
  843. DPFnode leaf = dpf.leaf(query, aes_ops);
  844. printf("DPF(%016lx) = ", query);
  845. dump_node(leaf);
  846. if (tio.player() == 1) {
  847. tio.iostream_peer() << leaf;
  848. } else {
  849. DPFnode peerleaf;
  850. tio.iostream_peer() >> peerleaf;
  851. printf("XOR = ");
  852. dump_node(leaf ^ peerleaf);
  853. }
  854. }
  855. }
  856. });
  857. });
  858. }
  859. pool.join();
  860. }
  861. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  862. value_t target, value_t x)
  863. {
  864. int player = tio.player();
  865. size_t &aes_ops = tio.aes_ops();
  866. int res = 1;
  867. if (player == 2) {
  868. // Create a CDPF pair with the given target
  869. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  870. // Send it and a share of x to the computational parties
  871. RegAS x0, x1;
  872. x0.randomize();
  873. x1.set(x-x0.share());
  874. tio.iostream_p0() << dpf0 << x0;
  875. tio.iostream_p1() << dpf1 << x1;
  876. } else {
  877. CDPF dpf;
  878. RegAS xsh;
  879. tio.iostream_server() >> dpf >> xsh;
  880. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  881. RegBS eeq = dpf.is_zero(tio, yield, xsh, aes_ops);
  882. printf("%016lx %016lx %d %d %d %d ", target, x, lt.bshare,
  883. eq.bshare, gt.bshare, eeq.bshare);
  884. // Check the answer
  885. if (player == 1) {
  886. tio.iostream_peer() << xsh << lt << eq << gt << eeq;
  887. } else {
  888. RegAS peer_xsh;
  889. RegBS peer_lt, peer_eq, peer_gt, peer_eeq;
  890. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >>
  891. peer_gt >> peer_eeq;
  892. lt ^= peer_lt;
  893. eq ^= peer_eq;
  894. gt ^= peer_gt;
  895. eeq ^= peer_eeq;
  896. xsh += peer_xsh;
  897. int lti = int(lt.bshare);
  898. int eqi = int(eq.bshare);
  899. int gti = int(gt.bshare);
  900. int eeqi = int(eeq.bshare);
  901. x = xsh.share();
  902. printf(": %d %d %d %d ", lti, eqi, gti, eeqi);
  903. bool signbit = (x >> 63);
  904. if (lti + eqi + gti != 1 || eqi != eeqi) {
  905. printf("INCONSISTENT");
  906. res = 0;
  907. } else if (x == 0 && eqi) {
  908. printf("=");
  909. } else if (!signbit && gti) {
  910. printf(">");
  911. } else if (signbit && lti) {
  912. printf("<");
  913. } else {
  914. printf("INCORRECT");
  915. res = 0;
  916. }
  917. }
  918. printf("\n");
  919. }
  920. return res;
  921. }
  922. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  923. value_t target, value_t x)
  924. {
  925. int res = 1;
  926. res &= compare_test_one(tio, yield, target, x);
  927. res &= compare_test_one(tio, yield, target, 0);
  928. res &= compare_test_one(tio, yield, target, 1);
  929. res &= compare_test_one(tio, yield, target, 15);
  930. res &= compare_test_one(tio, yield, target, 16);
  931. res &= compare_test_one(tio, yield, target, 17);
  932. res &= compare_test_one(tio, yield, target, -1);
  933. res &= compare_test_one(tio, yield, target, -15);
  934. res &= compare_test_one(tio, yield, target, -16);
  935. res &= compare_test_one(tio, yield, target, -17);
  936. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  937. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  938. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  939. return res;
  940. }
  941. static void compare_test(MPCIO &mpcio,
  942. const PRACOptions &opts, char **args)
  943. {
  944. value_t target, x;
  945. arc4random_buf(&target, sizeof(target));
  946. arc4random_buf(&x, sizeof(x));
  947. if (*args) {
  948. target = strtoull(*args, NULL, 16);
  949. ++args;
  950. }
  951. if (*args) {
  952. x = strtoull(*args, NULL, 16);
  953. ++args;
  954. }
  955. int num_threads = opts.num_threads;
  956. boost::asio::thread_pool pool(num_threads);
  957. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  958. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  959. MPCTIO tio(mpcio, thread_num);
  960. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  961. int res = 1;
  962. res &= compare_test_target(tio, yield, target, x);
  963. res &= compare_test_target(tio, yield, 0, x);
  964. res &= compare_test_target(tio, yield, 1, x);
  965. res &= compare_test_target(tio, yield, 15, x);
  966. res &= compare_test_target(tio, yield, 16, x);
  967. res &= compare_test_target(tio, yield, 17, x);
  968. res &= compare_test_target(tio, yield, -1, x);
  969. res &= compare_test_target(tio, yield, -15, x);
  970. res &= compare_test_target(tio, yield, -16, x);
  971. res &= compare_test_target(tio, yield, -17, x);
  972. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  973. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  974. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  975. if (tio.player() == 0) {
  976. if (res == 1) {
  977. printf("All tests passed!\n");
  978. } else {
  979. printf("TEST FAILURES\n");
  980. }
  981. }
  982. });
  983. });
  984. }
  985. pool.join();
  986. }
  987. static void sort_test(MPCIO &mpcio,
  988. const PRACOptions &opts, char **args)
  989. {
  990. nbits_t depth=6;
  991. if (*args) {
  992. depth = atoi(*args);
  993. ++args;
  994. }
  995. int num_threads = opts.num_threads;
  996. boost::asio::thread_pool pool(num_threads);
  997. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  998. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  999. MPCTIO tio(mpcio, thread_num);
  1000. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  1001. address_t size = address_t(1)<<depth;
  1002. // size_t &aes_ops = tio.aes_ops();
  1003. Duoram<RegAS> oram(tio.player(), size);
  1004. auto A = oram.flat(tio, yield);
  1005. A.explicitonly(true);
  1006. // Initialize the memory to random values in parallel
  1007. std::vector<coro_t> coroutines;
  1008. for (address_t i=0; i<size; ++i) {
  1009. coroutines.emplace_back(
  1010. [&A, i](yield_t &yield) {
  1011. auto Acoro = A.context(yield);
  1012. RegAS v;
  1013. v.randomize(62);
  1014. Acoro[i] += v;
  1015. });
  1016. }
  1017. run_coroutines(yield, coroutines);
  1018. A.bitonic_sort(0, depth);
  1019. if (depth <= 10) {
  1020. oram.dump();
  1021. auto check = A.reconstruct();
  1022. if (tio.player() == 0) {
  1023. for (address_t i=0;i<size;++i) {
  1024. printf("%04x %016lx\n", i, check[i].share());
  1025. }
  1026. }
  1027. }
  1028. });
  1029. });
  1030. }
  1031. pool.join();
  1032. }
  1033. static void bsearch_test(MPCIO &mpcio,
  1034. const PRACOptions &opts, char **args)
  1035. {
  1036. value_t target;
  1037. arc4random_buf(&target, sizeof(target));
  1038. target >>= 1;
  1039. nbits_t depth=6;
  1040. if (*args) {
  1041. depth = atoi(*args);
  1042. ++args;
  1043. }
  1044. if (*args) {
  1045. target = strtoull(*args, NULL, 16);
  1046. ++args;
  1047. }
  1048. int num_threads = opts.num_threads;
  1049. boost::asio::thread_pool pool(num_threads);
  1050. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  1051. boost::asio::post(pool, [&mpcio, thread_num, depth, target] {
  1052. MPCTIO tio(mpcio, thread_num);
  1053. run_coroutines(tio, [&tio, depth, target] (yield_t &yield) {
  1054. address_t size = address_t(1)<<depth;
  1055. RegAS tshare;
  1056. if (tio.player() == 2) {
  1057. // Send shares of the target to the computational
  1058. // players
  1059. RegAS tshare0, tshare1;
  1060. tshare0.randomize();
  1061. tshare1.set(target-tshare0.share());
  1062. tio.iostream_p0() << tshare0;
  1063. tio.iostream_p1() << tshare1;
  1064. printf("Using target = %016lx\n", target);
  1065. yield();
  1066. } else {
  1067. // Get the share of the target
  1068. tio.iostream_server() >> tshare;
  1069. }
  1070. // Create a random database and sort it
  1071. // size_t &aes_ops = tio.aes_ops();
  1072. Duoram<RegAS> oram(tio.player(), size);
  1073. auto A = oram.flat(tio, yield);
  1074. A.explicitonly(true);
  1075. // Initialize the memory to random values in parallel
  1076. std::vector<coro_t> coroutines;
  1077. for (address_t i=0; i<size; ++i) {
  1078. coroutines.emplace_back(
  1079. [&A, i](yield_t &yield) {
  1080. auto Acoro = A.context(yield);
  1081. RegAS v;
  1082. v.randomize(62);
  1083. Acoro[i] += v;
  1084. });
  1085. }
  1086. run_coroutines(yield, coroutines);
  1087. A.bitonic_sort(0, depth);
  1088. // Binary search for the target
  1089. RegAS tindex = A.obliv_binary_search(tshare);
  1090. // Check the answer
  1091. if (tio.player() == 1) {
  1092. tio.iostream_peer() << tindex;
  1093. } else if (tio.player() == 0) {
  1094. RegAS peer_tindex;
  1095. tio.iostream_peer() >> peer_tindex;
  1096. tindex += peer_tindex;
  1097. }
  1098. if (depth <= 10) {
  1099. auto check = A.reconstruct();
  1100. if (tio.player() == 0) {
  1101. for (address_t i=0;i<size;++i) {
  1102. printf("%04x %016lx\n", i, check[i].share());
  1103. }
  1104. }
  1105. }
  1106. if (tio.player() == 0) {
  1107. printf("Found index = %lx\n", tindex.share());
  1108. }
  1109. });
  1110. });
  1111. }
  1112. pool.join();
  1113. }
  1114. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  1115. {
  1116. MPCTIO tio(mpcio, 0);
  1117. if (!*args) {
  1118. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  1119. return;
  1120. } else if (!strcmp(*args, "test")) {
  1121. ++args;
  1122. online_test(mpcio, opts, args);
  1123. } else if (!strcmp(*args, "lamporttest")) {
  1124. ++args;
  1125. lamport_test(mpcio, opts, args);
  1126. } else if (!strcmp(*args, "rdpftest")) {
  1127. ++args;
  1128. rdpf_test<1>(mpcio, opts, args);
  1129. } else if (!strcmp(*args, "rdpftest2")) {
  1130. ++args;
  1131. rdpf_test<2>(mpcio, opts, args);
  1132. } else if (!strcmp(*args, "rdpftest3")) {
  1133. ++args;
  1134. rdpf_test<3>(mpcio, opts, args);
  1135. } else if (!strcmp(*args, "rdpftest4")) {
  1136. ++args;
  1137. rdpf_test<4>(mpcio, opts, args);
  1138. } else if (!strcmp(*args, "rdpftest5")) {
  1139. ++args;
  1140. rdpf_test<5>(mpcio, opts, args);
  1141. } else if (!strcmp(*args, "rdpftime")) {
  1142. ++args;
  1143. rdpf_timing(mpcio, opts, args);
  1144. } else if (!strcmp(*args, "evaltime")) {
  1145. ++args;
  1146. rdpfeval_timing(mpcio, opts, args);
  1147. } else if (!strcmp(*args, "parevaltime")) {
  1148. ++args;
  1149. par_rdpfeval_timing(mpcio, opts, args);
  1150. } else if (!strcmp(*args, "tupletime")) {
  1151. ++args;
  1152. tupleeval_timing(mpcio, opts, args);
  1153. } else if (!strcmp(*args, "partupletime")) {
  1154. ++args;
  1155. par_tupleeval_timing(mpcio, opts, args);
  1156. } else if (!strcmp(*args, "duotest")) {
  1157. ++args;
  1158. if (opts.use_xor_db) {
  1159. duoram_test<RegXS>(mpcio, opts, args);
  1160. } else {
  1161. duoram_test<RegAS>(mpcio, opts, args);
  1162. }
  1163. } else if (!strcmp(*args, "cdpftest")) {
  1164. ++args;
  1165. cdpf_test(mpcio, opts, args);
  1166. } else if (!strcmp(*args, "cmptest")) {
  1167. ++args;
  1168. compare_test(mpcio, opts, args);
  1169. } else if (!strcmp(*args, "sorttest")) {
  1170. ++args;
  1171. sort_test(mpcio, opts, args);
  1172. } else if (!strcmp(*args, "bsearch")) {
  1173. ++args;
  1174. bsearch_test(mpcio, opts, args);
  1175. } else if (!strcmp(*args, "duoram")) {
  1176. ++args;
  1177. if (opts.use_xor_db) {
  1178. duoram<RegXS>(mpcio, opts, args);
  1179. } else {
  1180. duoram<RegAS>(mpcio, opts, args);
  1181. }
  1182. } else if (!strcmp(*args, "cell")) {
  1183. ++args;
  1184. cell(mpcio, opts, args);
  1185. } else if (!strcmp(*args, "bst")) {
  1186. ++args;
  1187. bst(mpcio, opts, args);
  1188. } else if (!strcmp(*args, "avl")) {
  1189. ++args;
  1190. avl(mpcio, opts, args);
  1191. } else if (!strcmp(*args, "avl_tests")) {
  1192. ++args;
  1193. avl_tests(mpcio, opts, args);
  1194. } else {
  1195. std::cerr << "Unknown mode " << *args << "\n";
  1196. }
  1197. }