online.cpp 43 KB

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