online.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. address_t len = (1<<depth);
  621. if (*args) {
  622. len = atoi(*args);
  623. ++args;
  624. }
  625. MPCTIO tio(mpcio, 0, opts.num_threads);
  626. run_coroutines(tio, [&tio, depth, share, len] (yield_t &yield) {
  627. // size_t &aes_ops = tio.aes_ops();
  628. Duoram<T> oram(tio.player(), len);
  629. auto A = oram.flat(tio, yield);
  630. RegAS aidx, aidx2, aidx3;
  631. aidx.ashare = share;
  632. aidx2.ashare = share + tio.player();
  633. aidx3.ashare = share + 1;
  634. T M;
  635. if (tio.player() == 0) {
  636. M.set(0xbabb0000);
  637. } else {
  638. M.set(0x0000a66e);
  639. }
  640. RegXS xidx;
  641. xidx.xshare = share;
  642. T N;
  643. if (tio.player() == 0) {
  644. N.set(0xdead0000);
  645. } else {
  646. N.set(0x0000beef);
  647. }
  648. // Writing and reading with additively shared indices
  649. printf("Additive Updating\n");
  650. A[aidx] += M;
  651. printf("Additive Reading\n");
  652. T Aa = A[aidx];
  653. // Writing and reading with XOR shared indices
  654. printf("XOR Updating\n");
  655. A[xidx] += N;
  656. printf("XOR Reading\n");
  657. T Ax = A[xidx];
  658. T Ae;
  659. // Writing and reading with explicit indices
  660. if (depth > 2) {
  661. printf("Explicit Updating\n");
  662. A[5] += Aa;
  663. printf("Explicit Reading\n");
  664. Ae = A[6];
  665. }
  666. // Simultaneous independent reads
  667. printf("3 independent reading\n");
  668. std::vector<T> Av = A[std::array {
  669. aidx, aidx2, aidx3
  670. }];
  671. // Simultaneous independent updates
  672. T Aw1, Aw2, Aw3;
  673. Aw1.set(0x101010101010101 * tio.player());
  674. Aw2.set(0x202020202020202 * tio.player());
  675. Aw3.set(0x303030303030303 * tio.player());
  676. printf("3 independent updating\n");
  677. A[std::array { aidx, aidx2, aidx3 }] -=
  678. std::array { Aw1, Aw2, Aw3 };
  679. if (depth <= 10) {
  680. oram.dump();
  681. auto check = A.reconstruct();
  682. if (tio.player() == 0) {
  683. for (address_t i=0;i<len;++i) {
  684. printf("%04x %016lx\n", i, check[i].share());
  685. }
  686. }
  687. }
  688. auto checkread = A.reconstruct(Aa);
  689. auto checkreade = A.reconstruct(Ae);
  690. auto checkreadx = A.reconstruct(Ax);
  691. if (tio.player() == 0) {
  692. printf("Read AS value = %016lx\n", checkread.share());
  693. printf("Read AX value = %016lx\n", checkreadx.share());
  694. printf("Read Ex value = %016lx\n", checkreade.share());
  695. }
  696. for (auto &v : Av) {
  697. auto checkv = A.reconstruct(v);
  698. if (tio.player() == 0) {
  699. printf("Read Av value = %016lx\n", checkv.share());
  700. }
  701. }
  702. });
  703. }
  704. // This measures the same things as the Duoram paper: dependent and
  705. // independent reads, updates, writes, and interleaves
  706. // T is RegAS or RegXS for additive or XOR shared database respectively
  707. template <typename T>
  708. static void duoram(MPCIO &mpcio,
  709. const PRACOptions &opts, char **args)
  710. {
  711. nbits_t depth = 6;
  712. int items = 4;
  713. if (*args) {
  714. depth = atoi(*args);
  715. ++args;
  716. }
  717. if (*args) {
  718. items = atoi(*args);
  719. ++args;
  720. }
  721. MPCTIO tio(mpcio, 0, opts.num_threads);
  722. run_coroutines(tio, [&mpcio, &tio, depth, items] (yield_t &yield) {
  723. size_t size = size_t(1)<<depth;
  724. address_t mask = (depth < ADDRESS_MAX_BITS ?
  725. ((address_t(1)<<depth) - 1) : ~0);
  726. Duoram<T> oram(tio.player(), size);
  727. auto A = oram.flat(tio, yield);
  728. std::cout << "===== DEPENDENT UPDATES =====\n";
  729. mpcio.reset_stats();
  730. tio.reset_lamport();
  731. // Make a linked list of length items
  732. std::vector<T> list_indices;
  733. T prev_index, next_index;
  734. prev_index.randomize(depth);
  735. for (int i=0;i<items;++i) {
  736. next_index.randomize(depth);
  737. A[next_index] += prev_index;
  738. list_indices.push_back(next_index);
  739. prev_index = next_index;
  740. }
  741. tio.sync_lamport();
  742. mpcio.dump_stats(std::cout);
  743. std::cout << "\n===== DEPENDENT READS =====\n";
  744. mpcio.reset_stats();
  745. tio.reset_lamport();
  746. // Read the linked list starting with prev_index
  747. T cur_index = prev_index;
  748. for (int i=0;i<items;++i) {
  749. cur_index = A[cur_index];
  750. }
  751. tio.sync_lamport();
  752. mpcio.dump_stats(std::cout);
  753. std::cout << "\n===== INDEPENDENT READS =====\n";
  754. mpcio.reset_stats();
  755. tio.reset_lamport();
  756. // Read all the entries in the list at once
  757. std::vector<T> read_outputs = A[list_indices];
  758. tio.sync_lamport();
  759. mpcio.dump_stats(std::cout);
  760. std::cout << "\n===== INDEPENDENT UPDATES =====\n";
  761. mpcio.reset_stats();
  762. tio.reset_lamport();
  763. // Make a vector of indices 1 larger than those in list_indices,
  764. // and a vector of values 1 larger than those in outputs
  765. std::vector<T> indep_indices, indep_values;
  766. T one;
  767. one.set(tio.player()); // Sets the shared value to 1
  768. for (int i=0;i<items;++i) {
  769. indep_indices.push_back(list_indices[i]+one);
  770. indep_values.push_back(read_outputs[i]+one);
  771. }
  772. // Update all the indices at once
  773. A[indep_indices] += indep_values;
  774. tio.sync_lamport();
  775. mpcio.dump_stats(std::cout);
  776. std::cout << "\n===== DEPENDENT WRITES =====\n";
  777. mpcio.reset_stats();
  778. tio.reset_lamport();
  779. T two;
  780. two.set(2*tio.player()); // Sets the shared value to 2
  781. // For each address addr that's number i from the end of the
  782. // linked list, write i+1 into location addr+2
  783. for (int i=0;i<items;++i) {
  784. T val;
  785. val.set((i+1)*tio.player());
  786. A[list_indices[i]+two] = val;
  787. }
  788. tio.sync_lamport();
  789. mpcio.dump_stats(std::cout);
  790. std::cout << "\n===== DEPENDENT INTERLEAVED =====\n";
  791. mpcio.reset_stats();
  792. tio.reset_lamport();
  793. T three;
  794. three.set(3*tio.player()); // Sets the shared value to 3
  795. // Follow the linked list and whenever A[addr]=val, set
  796. // A[addr+3]=val+3
  797. cur_index = prev_index;
  798. for (int i=0;i<items;++i) {
  799. T next_index = A[cur_index];
  800. A[cur_index+three] = next_index+three;
  801. cur_index = next_index;
  802. }
  803. tio.sync_lamport();
  804. mpcio.dump_stats(std::cout);
  805. std::cout << "\n";
  806. mpcio.reset_stats();
  807. tio.reset_lamport();
  808. if (depth <= 30) {
  809. auto check = A.reconstruct();
  810. auto head = A.reconstruct(prev_index);
  811. if (tio.player() == 0) {
  812. int width = (depth+3)/4;
  813. printf("Head of linked list: %0*lx\n\n", width,
  814. head.share() & mask);
  815. std::cout << "Non-zero reconstructed database entries:\n";
  816. for (address_t i=0;i<size;++i) {
  817. value_t share = check[i].share() & mask;
  818. if (share) printf("%0*x: %0*lx\n", width, i, width, share);
  819. }
  820. }
  821. }
  822. });
  823. }
  824. static void cdpf_test(MPCIO &mpcio,
  825. const PRACOptions &opts, char **args)
  826. {
  827. value_t query, target;
  828. int iters = 1;
  829. arc4random_buf(&query, sizeof(query));
  830. arc4random_buf(&target, sizeof(target));
  831. if (*args) {
  832. query = strtoull(*args, NULL, 16);
  833. ++args;
  834. }
  835. if (*args) {
  836. target = strtoull(*args, NULL, 16);
  837. ++args;
  838. }
  839. if (*args) {
  840. iters = atoi(*args);
  841. ++args;
  842. }
  843. int num_threads = opts.num_threads;
  844. boost::asio::thread_pool pool(num_threads);
  845. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  846. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  847. MPCTIO tio(mpcio, thread_num);
  848. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  849. size_t &aes_ops = tio.aes_ops();
  850. for (int i=0;i<iters;++i) {
  851. if (tio.player() == 2) {
  852. tio.cdpf(yield);
  853. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  854. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  855. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  856. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  857. dump_node(leaf0 ^ leaf1);
  858. } else {
  859. CDPF dpf = tio.cdpf(yield);
  860. printf("ashare = %016lX\nxshare = %016lX\n",
  861. dpf.as_target.ashare, dpf.xs_target.xshare);
  862. DPFnode leaf = dpf.leaf(query, aes_ops);
  863. printf("DPF(%016lx) = ", query);
  864. dump_node(leaf);
  865. if (tio.player() == 1) {
  866. tio.iostream_peer() << leaf;
  867. } else {
  868. DPFnode peerleaf;
  869. tio.iostream_peer() >> peerleaf;
  870. printf("XOR = ");
  871. dump_node(leaf ^ peerleaf);
  872. }
  873. }
  874. }
  875. });
  876. });
  877. }
  878. pool.join();
  879. }
  880. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  881. value_t target, value_t x)
  882. {
  883. int player = tio.player();
  884. size_t &aes_ops = tio.aes_ops();
  885. int res = 1;
  886. if (player == 2) {
  887. // Create a CDPF pair with the given target
  888. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  889. // Send it and a share of x to the computational parties
  890. RegAS x0, x1;
  891. x0.randomize();
  892. x1.set(x-x0.share());
  893. tio.iostream_p0() << dpf0 << x0;
  894. tio.iostream_p1() << dpf1 << x1;
  895. } else {
  896. CDPF dpf;
  897. RegAS xsh;
  898. tio.iostream_server() >> dpf >> xsh;
  899. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  900. RegBS eeq = dpf.is_zero(tio, yield, xsh, aes_ops);
  901. printf("%016lx %016lx %d %d %d %d ", target, x, lt.bshare,
  902. eq.bshare, gt.bshare, eeq.bshare);
  903. // Check the answer
  904. if (player == 1) {
  905. tio.iostream_peer() << xsh << lt << eq << gt << eeq;
  906. } else {
  907. RegAS peer_xsh;
  908. RegBS peer_lt, peer_eq, peer_gt, peer_eeq;
  909. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >>
  910. peer_gt >> peer_eeq;
  911. lt ^= peer_lt;
  912. eq ^= peer_eq;
  913. gt ^= peer_gt;
  914. eeq ^= peer_eeq;
  915. xsh += peer_xsh;
  916. int lti = int(lt.bshare);
  917. int eqi = int(eq.bshare);
  918. int gti = int(gt.bshare);
  919. int eeqi = int(eeq.bshare);
  920. x = xsh.share();
  921. printf(": %d %d %d %d ", lti, eqi, gti, eeqi);
  922. bool signbit = (x >> 63);
  923. if (lti + eqi + gti != 1 || eqi != eeqi) {
  924. printf("INCONSISTENT");
  925. res = 0;
  926. } else if (x == 0 && eqi) {
  927. printf("=");
  928. } else if (!signbit && gti) {
  929. printf(">");
  930. } else if (signbit && lti) {
  931. printf("<");
  932. } else {
  933. printf("INCORRECT");
  934. res = 0;
  935. }
  936. }
  937. printf("\n");
  938. }
  939. return res;
  940. }
  941. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  942. value_t target, value_t x)
  943. {
  944. int res = 1;
  945. res &= compare_test_one(tio, yield, target, x);
  946. res &= compare_test_one(tio, yield, target, 0);
  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, -1);
  952. res &= compare_test_one(tio, yield, target, -15);
  953. res &= compare_test_one(tio, yield, target, -16);
  954. res &= compare_test_one(tio, yield, target, -17);
  955. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  956. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  957. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  958. return res;
  959. }
  960. static void compare_test(MPCIO &mpcio,
  961. const PRACOptions &opts, char **args)
  962. {
  963. value_t target, x;
  964. arc4random_buf(&target, sizeof(target));
  965. arc4random_buf(&x, sizeof(x));
  966. if (*args) {
  967. target = strtoull(*args, NULL, 16);
  968. ++args;
  969. }
  970. if (*args) {
  971. x = strtoull(*args, NULL, 16);
  972. ++args;
  973. }
  974. int num_threads = opts.num_threads;
  975. boost::asio::thread_pool pool(num_threads);
  976. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  977. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  978. MPCTIO tio(mpcio, thread_num);
  979. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  980. int res = 1;
  981. res &= compare_test_target(tio, yield, target, x);
  982. res &= compare_test_target(tio, yield, 0, 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, -1, x);
  988. res &= compare_test_target(tio, yield, -15, x);
  989. res &= compare_test_target(tio, yield, -16, x);
  990. res &= compare_test_target(tio, yield, -17, x);
  991. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  992. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  993. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  994. if (tio.player() == 0) {
  995. if (res == 1) {
  996. printf("All tests passed!\n");
  997. } else {
  998. printf("TEST FAILURES\n");
  999. }
  1000. }
  1001. });
  1002. });
  1003. }
  1004. pool.join();
  1005. }
  1006. static void sort_test(MPCIO &mpcio,
  1007. const PRACOptions &opts, char **args)
  1008. {
  1009. nbits_t depth=6;
  1010. if (*args) {
  1011. depth = atoi(*args);
  1012. ++args;
  1013. }
  1014. address_t len = (1<<depth);
  1015. if (*args) {
  1016. len = atoi(*args);
  1017. ++args;
  1018. }
  1019. int num_threads = opts.num_threads;
  1020. boost::asio::thread_pool pool(num_threads);
  1021. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  1022. boost::asio::post(pool, [&mpcio, thread_num, depth, len] {
  1023. MPCTIO tio(mpcio, thread_num);
  1024. run_coroutines(tio, [&tio, depth, len] (yield_t &yield) {
  1025. address_t size = address_t(1)<<depth;
  1026. // size_t &aes_ops = tio.aes_ops();
  1027. Duoram<RegAS> oram(tio.player(), size);
  1028. auto A = oram.flat(tio, yield);
  1029. A.explicitonly(true);
  1030. // Initialize the memory to random values in parallel
  1031. std::vector<coro_t> coroutines;
  1032. for (address_t i=0; i<size; ++i) {
  1033. coroutines.emplace_back(
  1034. [&A, i](yield_t &yield) {
  1035. auto Acoro = A.context(yield);
  1036. RegAS v;
  1037. v.randomize(62);
  1038. Acoro[i] += v;
  1039. });
  1040. }
  1041. run_coroutines(yield, coroutines);
  1042. A.bitonic_sort(0, len);
  1043. if (depth <= 10) {
  1044. oram.dump();
  1045. }
  1046. auto check = A.reconstruct();
  1047. bool fail = false;
  1048. if (tio.player() == 0) {
  1049. for (address_t i=0;i<size;++i) {
  1050. if (depth <= 10) {
  1051. printf("%04x %016lx\n", i, check[i].share());
  1052. }
  1053. if (i>0 && i<len &&
  1054. check[i].share() < check[i-1].share()) {
  1055. fail = true;
  1056. }
  1057. }
  1058. if (fail) {
  1059. printf("FAIL\n");
  1060. } else {
  1061. printf("PASS\n");
  1062. }
  1063. }
  1064. });
  1065. });
  1066. }
  1067. pool.join();
  1068. }
  1069. static void bsearch_test(MPCIO &mpcio,
  1070. const PRACOptions &opts, char **args)
  1071. {
  1072. value_t target;
  1073. arc4random_buf(&target, sizeof(target));
  1074. target >>= 1;
  1075. nbits_t depth=6;
  1076. if (*args) {
  1077. depth = atoi(*args);
  1078. ++args;
  1079. }
  1080. address_t len = (1<<depth);
  1081. if (*args) {
  1082. len = atoi(*args);
  1083. ++args;
  1084. }
  1085. if (*args) {
  1086. target = strtoull(*args, NULL, 16);
  1087. ++args;
  1088. }
  1089. int num_threads = opts.num_threads;
  1090. boost::asio::thread_pool pool(num_threads);
  1091. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  1092. boost::asio::post(pool, [&mpcio, thread_num, depth, len, target] {
  1093. MPCTIO tio(mpcio, thread_num);
  1094. run_coroutines(tio, [&tio, depth, len, target] (yield_t &yield) {
  1095. address_t size = address_t(1)<<depth;
  1096. RegAS tshare;
  1097. if (tio.player() == 2) {
  1098. // Send shares of the target to the computational
  1099. // players
  1100. RegAS tshare0, tshare1;
  1101. tshare0.randomize();
  1102. tshare1.set(target-tshare0.share());
  1103. tio.iostream_p0() << tshare0;
  1104. tio.iostream_p1() << tshare1;
  1105. printf("Using target = %016lx\n", target);
  1106. yield();
  1107. } else {
  1108. // Get the share of the target
  1109. tio.iostream_server() >> tshare;
  1110. }
  1111. // Create a random database and sort it
  1112. // size_t &aes_ops = tio.aes_ops();
  1113. Duoram<RegAS> oram(tio.player(), size);
  1114. auto A = oram.flat(tio, yield);
  1115. A.explicitonly(true);
  1116. // Initialize the memory to random values in parallel
  1117. std::vector<coro_t> coroutines;
  1118. for (address_t i=0; i<size; ++i) {
  1119. coroutines.emplace_back(
  1120. [&A, i](yield_t &yield) {
  1121. auto Acoro = A.context(yield);
  1122. RegAS v;
  1123. v.randomize(62);
  1124. Acoro[i] += v;
  1125. });
  1126. }
  1127. run_coroutines(yield, coroutines);
  1128. A.bitonic_sort(0, len);
  1129. // Binary search for the target
  1130. RegAS tindex = A.obliv_binary_search(tshare);
  1131. // Check the answer
  1132. if (tio.player() == 1) {
  1133. tio.iostream_peer() << tindex;
  1134. } else if (tio.player() == 0) {
  1135. RegAS peer_tindex;
  1136. tio.iostream_peer() >> peer_tindex;
  1137. tindex += peer_tindex;
  1138. }
  1139. if (depth <= 10) {
  1140. auto check = A.reconstruct();
  1141. if (tio.player() == 0) {
  1142. for (address_t i=0;i<size;++i) {
  1143. printf("%04x %016lx\n", i, check[i].share());
  1144. }
  1145. }
  1146. }
  1147. if (tio.player() == 0) {
  1148. printf("Found index = %lx\n", tindex.share());
  1149. }
  1150. });
  1151. });
  1152. }
  1153. pool.join();
  1154. }
  1155. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  1156. {
  1157. MPCTIO tio(mpcio, 0);
  1158. if (!*args) {
  1159. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  1160. return;
  1161. } else if (!strcmp(*args, "test")) {
  1162. ++args;
  1163. online_test(mpcio, opts, args);
  1164. } else if (!strcmp(*args, "lamporttest")) {
  1165. ++args;
  1166. lamport_test(mpcio, opts, args);
  1167. } else if (!strcmp(*args, "rdpftest")) {
  1168. ++args;
  1169. rdpf_test<1>(mpcio, opts, args, false);
  1170. } else if (!strcmp(*args, "rdpftest2")) {
  1171. ++args;
  1172. rdpf_test<2>(mpcio, opts, args, false);
  1173. } else if (!strcmp(*args, "rdpftest3")) {
  1174. ++args;
  1175. rdpf_test<3>(mpcio, opts, args, false);
  1176. } else if (!strcmp(*args, "rdpftest4")) {
  1177. ++args;
  1178. rdpf_test<4>(mpcio, opts, args, false);
  1179. } else if (!strcmp(*args, "rdpftest5")) {
  1180. ++args;
  1181. rdpf_test<5>(mpcio, opts, args, false);
  1182. } else if (!strcmp(*args, "irdpftest")) {
  1183. ++args;
  1184. rdpf_test<1>(mpcio, opts, args, true);
  1185. } else if (!strcmp(*args, "irdpftest2")) {
  1186. ++args;
  1187. rdpf_test<2>(mpcio, opts, args, true);
  1188. } else if (!strcmp(*args, "irdpftest3")) {
  1189. ++args;
  1190. rdpf_test<3>(mpcio, opts, args, true);
  1191. } else if (!strcmp(*args, "irdpftest4")) {
  1192. ++args;
  1193. rdpf_test<4>(mpcio, opts, args, true);
  1194. } else if (!strcmp(*args, "irdpftest5")) {
  1195. ++args;
  1196. rdpf_test<5>(mpcio, opts, args, true);
  1197. } else if (!strcmp(*args, "rdpftime")) {
  1198. ++args;
  1199. rdpf_timing(mpcio, opts, args);
  1200. } else if (!strcmp(*args, "evaltime")) {
  1201. ++args;
  1202. rdpfeval_timing(mpcio, opts, args);
  1203. } else if (!strcmp(*args, "parevaltime")) {
  1204. ++args;
  1205. par_rdpfeval_timing(mpcio, opts, args);
  1206. } else if (!strcmp(*args, "tupletime")) {
  1207. ++args;
  1208. tupleeval_timing(mpcio, opts, args);
  1209. } else if (!strcmp(*args, "partupletime")) {
  1210. ++args;
  1211. par_tupleeval_timing(mpcio, opts, args);
  1212. } else if (!strcmp(*args, "duotest")) {
  1213. ++args;
  1214. if (opts.use_xor_db) {
  1215. duoram_test<RegXS>(mpcio, opts, args);
  1216. } else {
  1217. duoram_test<RegAS>(mpcio, opts, args);
  1218. }
  1219. } else if (!strcmp(*args, "cdpftest")) {
  1220. ++args;
  1221. cdpf_test(mpcio, opts, args);
  1222. } else if (!strcmp(*args, "cmptest")) {
  1223. ++args;
  1224. compare_test(mpcio, opts, args);
  1225. } else if (!strcmp(*args, "sorttest")) {
  1226. ++args;
  1227. sort_test(mpcio, opts, args);
  1228. } else if (!strcmp(*args, "bsearch")) {
  1229. ++args;
  1230. bsearch_test(mpcio, opts, args);
  1231. } else if (!strcmp(*args, "duoram")) {
  1232. ++args;
  1233. if (opts.use_xor_db) {
  1234. duoram<RegXS>(mpcio, opts, args);
  1235. } else {
  1236. duoram<RegAS>(mpcio, opts, args);
  1237. }
  1238. } else if (!strcmp(*args, "cell")) {
  1239. ++args;
  1240. cell(mpcio, opts, args);
  1241. } else {
  1242. std::cerr << "Unknown mode " << *args << "\n";
  1243. }
  1244. }