online.cpp 46 KB

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