online.cpp 46 KB

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