online.cpp 44 KB

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