online.cpp 44 KB

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