online.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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. #include "shapes.hpp"
  10. #include "bst.hpp"
  11. #include "avl.hpp"
  12. static void online_test(MPCIO &mpcio,
  13. const PRACOptions &opts, char **args)
  14. {
  15. nbits_t nbits = VALUE_BITS;
  16. if (*args) {
  17. nbits = atoi(*args);
  18. }
  19. size_t as_memsize = 9;
  20. size_t xs_memsize = 3;
  21. MPCTIO tio(mpcio, 0);
  22. bool is_server = (mpcio.player == 2);
  23. RegAS *A = new RegAS[as_memsize];
  24. RegXS *AX = new RegXS[xs_memsize];
  25. value_t V;
  26. RegBS F0, F1, F2;
  27. RegBS FA, FO, FS;
  28. RegXS X;
  29. if (!is_server) {
  30. A[0].randomize();
  31. A[1].randomize();
  32. F0.randomize();
  33. A[4].randomize();
  34. F1.randomize();
  35. F2.randomize();
  36. A[6].randomize();
  37. A[7].randomize();
  38. X.randomize();
  39. AX[0].randomize();
  40. AX[1].randomize();
  41. arc4random_buf(&V, sizeof(V));
  42. printf("A:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  43. printf("AX:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, AX[i].xshare);
  44. printf("V : %016lX\n", V);
  45. printf("F0 : %01X\n", F0.bshare);
  46. printf("F1 : %01X\n", F1.bshare);
  47. printf("F2 : %01X\n", F2.bshare);
  48. printf("X : %016lX\n", X.xshare);
  49. }
  50. std::vector<coro_t> coroutines;
  51. coroutines.emplace_back(
  52. [&tio, &A, nbits](yield_t &yield) {
  53. mpc_mul(tio, yield, A[2], A[0], A[1], nbits);
  54. });
  55. coroutines.emplace_back(
  56. [&tio, &A, V, nbits](yield_t &yield) {
  57. mpc_valuemul(tio, yield, A[3], V, nbits);
  58. });
  59. coroutines.emplace_back(
  60. [&tio, &A, &F0, nbits](yield_t &yield) {
  61. mpc_flagmult(tio, yield, A[5], F0, A[4], nbits);
  62. });
  63. coroutines.emplace_back(
  64. [&tio, &A, &F1, nbits](yield_t &yield) {
  65. mpc_oswap(tio, yield, A[6], A[7], F1, nbits);
  66. });
  67. coroutines.emplace_back(
  68. [&tio, &A, &X, nbits](yield_t &yield) {
  69. mpc_xs_to_as(tio, yield, A[8], X, nbits);
  70. });
  71. coroutines.emplace_back(
  72. [&tio, &AX, &F0, nbits](yield_t &yield) {
  73. mpc_select(tio, yield, AX[2], F0, AX[0], AX[1], nbits);
  74. });
  75. coroutines.emplace_back(
  76. [&tio, &FA, &F0, &F1](yield_t &yield) {
  77. mpc_and(tio, yield, FA, F0, F1);
  78. });
  79. coroutines.emplace_back(
  80. [&tio, &FO, &F0, &F1](yield_t &yield) {
  81. mpc_or(tio, yield, FO, F0, F1);
  82. });
  83. coroutines.emplace_back(
  84. [&tio, &FS, &F0, &F1, &F2](yield_t &yield) {
  85. mpc_select(tio, yield, FS, F2, F0, F1);
  86. });
  87. run_coroutines(tio, coroutines);
  88. if (!is_server) {
  89. printf("\n");
  90. printf("A:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  91. printf("AX:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, AX[i].xshare);
  92. }
  93. // Check the answers
  94. if (mpcio.player == 1) {
  95. tio.queue_peer(A, as_memsize*sizeof(RegAS));
  96. tio.queue_peer(AX, xs_memsize*sizeof(RegXS));
  97. tio.queue_peer(&V, sizeof(V));
  98. tio.queue_peer(&F0, sizeof(RegBS));
  99. tio.queue_peer(&F1, sizeof(RegBS));
  100. tio.queue_peer(&F2, sizeof(RegBS));
  101. tio.queue_peer(&FA, sizeof(RegBS));
  102. tio.queue_peer(&FO, sizeof(RegBS));
  103. tio.queue_peer(&FS, sizeof(RegBS));
  104. tio.queue_peer(&X, sizeof(RegXS));
  105. tio.send();
  106. } else if (mpcio.player == 0) {
  107. RegAS *B = new RegAS[as_memsize];
  108. RegXS *BAX = new RegXS[xs_memsize];
  109. RegBS BF0, BF1, BF2;
  110. RegBS BFA, BFO, BFS;
  111. RegXS BX;
  112. value_t BV;
  113. value_t *S = new value_t[as_memsize];
  114. value_t *Y = new value_t[xs_memsize];
  115. bit_t SF0, SF1, SF2;
  116. bit_t SFA, SFO, SFS;
  117. value_t SX;
  118. tio.recv_peer(B, as_memsize*sizeof(RegAS));
  119. tio.recv_peer(BAX, xs_memsize*sizeof(RegXS));
  120. tio.recv_peer(&BV, sizeof(BV));
  121. tio.recv_peer(&BF0, sizeof(RegBS));
  122. tio.recv_peer(&BF1, sizeof(RegBS));
  123. tio.recv_peer(&BF2, sizeof(RegBS));
  124. tio.recv_peer(&BFA, sizeof(RegBS));
  125. tio.recv_peer(&BFO, sizeof(RegBS));
  126. tio.recv_peer(&BFS, sizeof(RegBS));
  127. tio.recv_peer(&BX, sizeof(RegXS));
  128. for(size_t i=0; i<as_memsize; ++i) S[i] = A[i].ashare+B[i].ashare;
  129. for(size_t i=0; i<xs_memsize; ++i) Y[i] = AX[i].xshare^BAX[i].xshare;
  130. SF0 = F0.bshare ^ BF0.bshare;
  131. SF1 = F1.bshare ^ BF1.bshare;
  132. SF2 = F2.bshare ^ BF2.bshare;
  133. SFA = FA.bshare ^ BFA.bshare;
  134. SFO = FO.bshare ^ BFO.bshare;
  135. SFS = FS.bshare ^ BFS.bshare;
  136. SX = X.xshare ^ BX.xshare;
  137. printf("S:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, S[i]);
  138. printf("Y:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, Y[i]);
  139. printf("SF0: %01X\n", SF0);
  140. printf("SF1: %01X\n", SF1);
  141. printf("SF2: %01X\n", SF2);
  142. printf("SFA: %01X\n", SFA);
  143. printf("SFO: %01X\n", SFO);
  144. printf("SFS: %01X\n", SFS);
  145. printf("SX : %016lX\n", SX);
  146. printf("\n%016lx\n", S[0]*S[1]-S[2]);
  147. printf("%016lx\n", (V*BV)-S[3]);
  148. printf("%016lx\n", (SF0*S[4])-S[5]);
  149. printf("%016lx\n", S[8]-SX);
  150. delete[] B;
  151. delete[] S;
  152. }
  153. delete[] A;
  154. delete[] AX;
  155. }
  156. static void lamport_test(MPCIO &mpcio,
  157. const PRACOptions &opts, char **args)
  158. {
  159. // Create a bunch of threads and send a bunch of data to the other
  160. // peer, and receive their data. If an arg is specified, repeat
  161. // that many times. The Lamport clock at the end should be just the
  162. // number of repetitions. Subsequent args are the chunk size and
  163. // the number of chunks per message
  164. size_t niters = 1;
  165. size_t chunksize = 1<<20;
  166. size_t numchunks = 1;
  167. if (*args) {
  168. niters = atoi(*args);
  169. ++args;
  170. }
  171. if (*args) {
  172. chunksize = atoi(*args);
  173. ++args;
  174. }
  175. if (*args) {
  176. numchunks = atoi(*args);
  177. ++args;
  178. }
  179. int num_threads = opts.num_threads;
  180. boost::asio::thread_pool pool(num_threads);
  181. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  182. boost::asio::post(pool, [&mpcio, thread_num, niters, chunksize, numchunks] {
  183. MPCTIO tio(mpcio, thread_num);
  184. char *sendbuf = new char[chunksize];
  185. char *recvbuf = new char[chunksize*numchunks];
  186. for (size_t i=0; i<niters; ++i) {
  187. for (size_t chunk=0; chunk<numchunks; ++chunk) {
  188. arc4random_buf(sendbuf, chunksize);
  189. tio.queue_peer(sendbuf, chunksize);
  190. }
  191. tio.send();
  192. tio.recv_peer(recvbuf, chunksize*numchunks);
  193. }
  194. delete[] recvbuf;
  195. delete[] sendbuf;
  196. });
  197. }
  198. pool.join();
  199. }
  200. template <nbits_t WIDTH>
  201. static void rdpf_test(MPCIO &mpcio,
  202. const PRACOptions &opts, char **args, bool incremental)
  203. {
  204. nbits_t depth=6;
  205. size_t num_iters = 1;
  206. if (*args) {
  207. depth = atoi(*args);
  208. ++args;
  209. }
  210. if (*args) {
  211. num_iters = atoi(*args);
  212. ++args;
  213. }
  214. MPCTIO tio(mpcio, 0, opts.num_threads);
  215. run_coroutines(tio, [&tio, depth, num_iters, incremental] (yield_t &yield) {
  216. size_t &aes_ops = tio.aes_ops();
  217. nbits_t min_level = incremental ? 1 : depth;
  218. for (size_t iter=0; iter < num_iters; ++iter) {
  219. if (tio.player() == 2) {
  220. RDPFPair<WIDTH> dp = tio.rdpfpair<WIDTH>(yield, depth,
  221. incremental);
  222. for (int i=0;i<2;++i) {
  223. RDPF<WIDTH> &dpf = dp.dpf[i];
  224. for (nbits_t level=min_level; level<=depth; ++level) {
  225. if (incremental) {
  226. printf("Level = %u\n\n", level);
  227. dpf.depth(level);
  228. }
  229. for (address_t x=0;x<(address_t(1)<<level);++x) {
  230. typename RDPF<WIDTH>::LeafNode leaf = dpf.leaf(x, aes_ops);
  231. RegBS ub = dpf.unit_bs(leaf);
  232. RegAS ua = dpf.unit_as(leaf);
  233. typename RDPF<WIDTH>::RegXSW sx = dpf.scaled_xs(leaf);
  234. typename RDPF<WIDTH>::RegASW sa = dpf.scaled_as(leaf);
  235. printf("%04x %x %016lx", x, ub.bshare, ua.ashare);
  236. for (nbits_t j=0;j<WIDTH;++j) {
  237. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  238. }
  239. printf("\n");
  240. }
  241. printf("\n");
  242. }
  243. }
  244. } else {
  245. RDPFTriple<WIDTH> dt = tio.rdpftriple<WIDTH>(yield,
  246. depth, incremental);
  247. for (int i=0;i<3;++i) {
  248. RDPF<WIDTH> &dpf = dt.dpf[i];
  249. for (nbits_t level=min_level; level<=depth; ++level) {
  250. if (incremental) {
  251. printf("Level = %u\n", level);
  252. dt.depth(level);
  253. RegXS tshare;
  254. dt.get_target(tshare);
  255. printf("Target share = %lx\n\n", tshare.share());
  256. }
  257. typename RDPF<WIDTH>::RegXSW peer_scaled_xor;
  258. typename RDPF<WIDTH>::RegASW peer_scaled_sum;
  259. if (tio.player() == 1) {
  260. tio.iostream_peer() <<
  261. dpf.li[depth-level].scaled_xor <<
  262. dpf.li[depth-level].scaled_sum;
  263. } else {
  264. tio.iostream_peer() >> peer_scaled_xor >> peer_scaled_sum;
  265. peer_scaled_sum += dpf.li[depth-level].scaled_sum;
  266. peer_scaled_xor ^= dpf.li[depth-level].scaled_xor;
  267. }
  268. for (address_t x=0;x<(address_t(1)<<level);++x) {
  269. typename RDPF<WIDTH>::LeafNode leaf = dpf.leaf(x, aes_ops);
  270. RegBS ub = dpf.unit_bs(leaf);
  271. RegAS ua = dpf.unit_as(leaf);
  272. typename RDPF<WIDTH>::RegXSW sx = dpf.scaled_xs(leaf);
  273. typename RDPF<WIDTH>::RegASW sa = dpf.scaled_as(leaf);
  274. printf("%04x %x %016lx", x, ub.bshare, ua.ashare);
  275. for (nbits_t j=0;j<WIDTH;++j) {
  276. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  277. }
  278. printf("\n");
  279. if (tio.player() == 1) {
  280. tio.iostream_peer() << ub << ua << sx << sa;
  281. } else {
  282. RegBS peer_ub;
  283. RegAS peer_ua;
  284. typename RDPF<WIDTH>::RegXSW peer_sx;
  285. typename RDPF<WIDTH>::RegASW peer_sa;
  286. tio.iostream_peer() >> peer_ub >> peer_ua >>
  287. peer_sx >> peer_sa;
  288. ub ^= peer_ub;
  289. ua += peer_ua;
  290. sx ^= peer_sx;
  291. sa += peer_sa;
  292. bool is_nonzero = ub.bshare || ua.ashare;
  293. for (nbits_t j=0;j<WIDTH;++j) {
  294. is_nonzero |= (sx[j].xshare || sa[j].ashare);
  295. }
  296. if (is_nonzero) {
  297. printf("**** %x %016lx", ub.bshare, ua.ashare);
  298. for (nbits_t j=0;j<WIDTH;++j) {
  299. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  300. }
  301. printf("\nSCALE ");
  302. for (nbits_t j=0;j<WIDTH;++j) {
  303. printf(" %016lx %016lx",
  304. peer_scaled_xor[j].xshare,
  305. peer_scaled_sum[j].ashare);
  306. }
  307. printf("\n");
  308. }
  309. }
  310. }
  311. printf("\n");
  312. }
  313. }
  314. }
  315. }
  316. });
  317. }
  318. static void rdpf_timing(MPCIO &mpcio,
  319. const PRACOptions &opts, char **args)
  320. {
  321. nbits_t depth=6;
  322. if (*args) {
  323. depth = atoi(*args);
  324. ++args;
  325. }
  326. int num_threads = opts.num_threads;
  327. boost::asio::thread_pool pool(num_threads);
  328. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  329. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  330. MPCTIO tio(mpcio, thread_num);
  331. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  332. size_t &aes_ops = tio.aes_ops();
  333. if (tio.player() == 2) {
  334. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  335. for (int i=0;i<2;++i) {
  336. RDPF<1> &dpf = dp.dpf[i];
  337. dpf.expand(aes_ops);
  338. RDPF<1>::RegXSW scaled_xor;
  339. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  340. RDPF<1>::LeafNode leaf = dpf.leaf(x, aes_ops);
  341. RDPF<1>::RegXSW sx = dpf.scaled_xs(leaf);
  342. scaled_xor ^= sx;
  343. }
  344. printf("%016lx\n%016lx\n", scaled_xor[0].xshare,
  345. dpf.li[0].scaled_xor[0].xshare);
  346. printf("\n");
  347. }
  348. } else {
  349. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  350. for (int i=0;i<3;++i) {
  351. RDPF<1> &dpf = dt.dpf[i];
  352. dpf.expand(aes_ops);
  353. RDPF<1>::RegXSW scaled_xor;
  354. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  355. RDPF<1>::LeafNode leaf = dpf.leaf(x, aes_ops);
  356. RDPF<1>::RegXSW sx = dpf.scaled_xs(leaf);
  357. scaled_xor ^= sx;
  358. }
  359. printf("%016lx\n%016lx\n", scaled_xor[0].xshare,
  360. dpf.li[0].scaled_xor[0].xshare);
  361. printf("\n");
  362. }
  363. }
  364. });
  365. });
  366. }
  367. pool.join();
  368. }
  369. static value_t parallel_streameval_rdpf(MPCIO &mpcio, const RDPF<1> &dpf,
  370. address_t start, int num_threads)
  371. {
  372. RDPF<1>::RegXSW scaled_xor[num_threads];
  373. boost::asio::thread_pool pool(num_threads);
  374. address_t totsize = (address_t(1)<<dpf.depth());
  375. address_t threadstart = start;
  376. address_t threadchunk = totsize / num_threads;
  377. address_t threadextra = totsize % num_threads;
  378. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  379. address_t threadsize = threadchunk + (address_t(thread_num) < threadextra);
  380. boost::asio::post(pool,
  381. [&mpcio, &dpf, &scaled_xor, thread_num, threadstart, threadsize] {
  382. MPCTIO tio(mpcio, thread_num);
  383. //printf("Thread %d from %X for %X\n", thread_num, threadstart, threadsize);
  384. RDPF<1>::RegXSW local_xor;
  385. size_t local_aes_ops = 0;
  386. auto ev = StreamEval(dpf, threadstart, 0, local_aes_ops);
  387. for (address_t x=0;x<threadsize;++x) {
  388. //if (x%0x10000 == 0) printf("%d", thread_num);
  389. RDPF<1>::LeafNode leaf = ev.next();
  390. local_xor ^= dpf.scaled_xs(leaf);
  391. }
  392. scaled_xor[thread_num] = local_xor;
  393. tio.aes_ops() += local_aes_ops;
  394. //printf("Thread %d complete\n", thread_num);
  395. });
  396. threadstart = (threadstart + threadsize) % totsize;
  397. }
  398. pool.join();
  399. RDPF<1>::RegXSW res;
  400. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  401. res ^= scaled_xor[thread_num];
  402. }
  403. return res[0].xshare;
  404. }
  405. static void rdpfeval_timing(MPCIO &mpcio,
  406. const PRACOptions &opts, char **args)
  407. {
  408. nbits_t depth=6;
  409. address_t start=0;
  410. if (*args) {
  411. depth = atoi(*args);
  412. ++args;
  413. }
  414. if (*args) {
  415. start = strtoull(*args, NULL, 16);
  416. ++args;
  417. }
  418. int num_threads = opts.num_threads;
  419. MPCTIO tio(mpcio, 0, num_threads);
  420. run_coroutines(tio, [&mpcio, &tio, depth, start, num_threads] (yield_t &yield) {
  421. if (tio.player() == 2) {
  422. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  423. for (int i=0;i<2;++i) {
  424. RDPF<1> &dpf = dp.dpf[i];
  425. value_t scaled_xor =
  426. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  427. printf("%016lx\n%016lx\n", scaled_xor,
  428. dpf.li[0].scaled_xor[0].xshare);
  429. printf("\n");
  430. }
  431. } else {
  432. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  433. for (int i=0;i<3;++i) {
  434. RDPF<1> &dpf = dt.dpf[i];
  435. value_t scaled_xor =
  436. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  437. printf("%016lx\n%016lx\n", scaled_xor,
  438. dpf.li[0].scaled_xor[0].xshare);
  439. printf("\n");
  440. }
  441. }
  442. });
  443. }
  444. static void par_rdpfeval_timing(MPCIO &mpcio,
  445. const PRACOptions &opts, char **args)
  446. {
  447. nbits_t depth=6;
  448. address_t start=0;
  449. if (*args) {
  450. depth = atoi(*args);
  451. ++args;
  452. }
  453. if (*args) {
  454. start = strtoull(*args, NULL, 16);
  455. ++args;
  456. }
  457. int num_threads = opts.num_threads;
  458. MPCTIO tio(mpcio, 0, num_threads);
  459. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  460. if (tio.player() == 2) {
  461. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  462. for (int i=0;i<2;++i) {
  463. RDPF<1> &dpf = dp.dpf[i];
  464. nbits_t depth = dpf.depth();
  465. auto pe = ParallelEval(dpf, start, 0,
  466. address_t(1)<<depth, num_threads, tio.aes_ops());
  467. RDPF<1>::RegXSW result, init;
  468. result = pe.reduce(init, [&dpf] (int thread_num,
  469. address_t i, const RDPF<1>::LeafNode &leaf) {
  470. return dpf.scaled_xs(leaf);
  471. });
  472. printf("%016lx\n%016lx\n", result[0].xshare,
  473. dpf.li[0].scaled_xor[0].xshare);
  474. printf("\n");
  475. }
  476. } else {
  477. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  478. for (int i=0;i<3;++i) {
  479. RDPF<1> &dpf = dt.dpf[i];
  480. nbits_t depth = dpf.depth();
  481. auto pe = ParallelEval(dpf, start, 0,
  482. address_t(1)<<depth, num_threads, tio.aes_ops());
  483. RDPF<1>::RegXSW result, init;
  484. result = pe.reduce(init, [&dpf] (int thread_num,
  485. address_t i, const RDPF<1>::LeafNode &leaf) {
  486. return dpf.scaled_xs(leaf);
  487. });
  488. printf("%016lx\n%016lx\n", result[0].xshare,
  489. dpf.li[0].scaled_xor[0].xshare);
  490. printf("\n");
  491. }
  492. }
  493. });
  494. }
  495. static void tupleeval_timing(MPCIO &mpcio,
  496. const PRACOptions &opts, char **args)
  497. {
  498. nbits_t depth=6;
  499. address_t start=0;
  500. if (*args) {
  501. depth = atoi(*args);
  502. ++args;
  503. }
  504. if (*args) {
  505. start = atoi(*args);
  506. ++args;
  507. }
  508. int num_threads = opts.num_threads;
  509. MPCTIO tio(mpcio, 0, num_threads);
  510. run_coroutines(tio, [&tio, depth, start] (yield_t &yield) {
  511. size_t &aes_ops = tio.aes_ops();
  512. if (tio.player() == 2) {
  513. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  514. RDPF<1>::RegXSW scaled_xor0, scaled_xor1;
  515. auto ev = StreamEval(dp, start, 0, aes_ops, false);
  516. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  517. auto [L0, L1] = ev.next();
  518. RDPF<1>::RegXSW sx0 = dp.dpf[0].scaled_xs(L0);
  519. RDPF<1>::RegXSW sx1 = dp.dpf[1].scaled_xs(L1);
  520. scaled_xor0 ^= sx0;
  521. scaled_xor1 ^= sx1;
  522. }
  523. printf("%016lx\n%016lx\n", scaled_xor0[0].xshare,
  524. dp.dpf[0].li[0].scaled_xor[0].xshare);
  525. printf("\n");
  526. printf("%016lx\n%016lx\n", scaled_xor1[0].xshare,
  527. dp.dpf[1].li[0].scaled_xor[0].xshare);
  528. printf("\n");
  529. } else {
  530. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  531. RDPF<1>::RegXSW scaled_xor0, scaled_xor1, scaled_xor2;
  532. auto ev = StreamEval(dt, start, 0, aes_ops, false);
  533. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  534. auto [L0, L1, L2] = ev.next();
  535. RDPF<1>::RegXSW sx0 = dt.dpf[0].scaled_xs(L0);
  536. RDPF<1>::RegXSW sx1 = dt.dpf[1].scaled_xs(L1);
  537. RDPF<1>::RegXSW sx2 = dt.dpf[2].scaled_xs(L2);
  538. scaled_xor0 ^= sx0;
  539. scaled_xor1 ^= sx1;
  540. scaled_xor2 ^= sx2;
  541. }
  542. printf("%016lx\n%016lx\n", scaled_xor0[0].xshare,
  543. dt.dpf[0].li[0].scaled_xor[0].xshare);
  544. printf("\n");
  545. printf("%016lx\n%016lx\n", scaled_xor1[0].xshare,
  546. dt.dpf[1].li[0].scaled_xor[0].xshare);
  547. printf("\n");
  548. printf("%016lx\n%016lx\n", scaled_xor2[0].xshare,
  549. dt.dpf[2].li[0].scaled_xor[0].xshare);
  550. printf("\n");
  551. }
  552. });
  553. }
  554. static void par_tupleeval_timing(MPCIO &mpcio,
  555. const PRACOptions &opts, char **args)
  556. {
  557. nbits_t depth=6;
  558. address_t start=0;
  559. if (*args) {
  560. depth = atoi(*args);
  561. ++args;
  562. }
  563. if (*args) {
  564. start = atoi(*args);
  565. ++args;
  566. }
  567. int num_threads = opts.num_threads;
  568. MPCTIO tio(mpcio, 0, num_threads);
  569. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  570. size_t &aes_ops = tio.aes_ops();
  571. if (tio.player() == 2) {
  572. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  573. auto pe = ParallelEval(dp, start, 0, address_t(1)<<depth,
  574. num_threads, aes_ops);
  575. RDPFPair<1>::RegXSWP result, init;
  576. result = pe.reduce(init, [&dp] (int thread_num, address_t i,
  577. const RDPFPair<1>::LeafNode &leaf) {
  578. RDPFPair<1>::RegXSWP scaled;
  579. dp.scaled(scaled, leaf);
  580. return scaled;
  581. });
  582. printf("%016lx\n%016lx\n", std::get<0>(result)[0].xshare,
  583. dp.dpf[0].li[0].scaled_xor[0].xshare);
  584. printf("\n");
  585. printf("%016lx\n%016lx\n", std::get<1>(result)[0].xshare,
  586. dp.dpf[1].li[0].scaled_xor[0].xshare);
  587. printf("\n");
  588. } else {
  589. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  590. auto pe = ParallelEval(dt, start, 0, address_t(1)<<depth,
  591. num_threads, aes_ops);
  592. RDPFTriple<1>::RegXSWT result, init;
  593. result = pe.reduce(init, [&dt] (int thread_num, address_t i,
  594. const RDPFTriple<1>::LeafNode &leaf) {
  595. RDPFTriple<1>::RegXSWT scaled;
  596. dt.scaled(scaled, leaf);
  597. return scaled;
  598. });
  599. printf("%016lx\n%016lx\n", std::get<0>(result)[0].xshare,
  600. dt.dpf[0].li[0].scaled_xor[0].xshare);
  601. printf("\n");
  602. printf("%016lx\n%016lx\n", std::get<1>(result)[0].xshare,
  603. dt.dpf[1].li[0].scaled_xor[0].xshare);
  604. printf("\n");
  605. printf("%016lx\n%016lx\n", std::get<2>(result)[0].xshare,
  606. dt.dpf[2].li[0].scaled_xor[0].xshare);
  607. printf("\n");
  608. }
  609. });
  610. }
  611. // T is RegAS or RegXS for additive or XOR shared database respectively
  612. template <typename T>
  613. static void duoram_test(MPCIO &mpcio,
  614. const PRACOptions &opts, char **args)
  615. {
  616. nbits_t depth=6;
  617. address_t share=arc4random();
  618. if (*args) {
  619. depth = atoi(*args);
  620. ++args;
  621. }
  622. if (*args) {
  623. share = atoi(*args);
  624. ++args;
  625. }
  626. share &= ((address_t(1)<<depth)-1);
  627. address_t len = (1<<depth);
  628. if (*args) {
  629. len = atoi(*args);
  630. ++args;
  631. }
  632. MPCTIO tio(mpcio, 0, opts.num_threads);
  633. run_coroutines(tio, [&tio, depth, share, len] (yield_t &yield) {
  634. // size_t &aes_ops = tio.aes_ops();
  635. Duoram<T> oram(tio.player(), len);
  636. auto A = oram.flat(tio, yield);
  637. RegAS aidx, aidx2, aidx3;
  638. aidx.ashare = share;
  639. aidx2.ashare = share + tio.player();
  640. aidx3.ashare = share + 1;
  641. T M;
  642. if (tio.player() == 0) {
  643. M.set(0xbabb0000);
  644. } else {
  645. M.set(0x0000a66e);
  646. }
  647. RegXS xidx;
  648. xidx.xshare = share;
  649. T N;
  650. if (tio.player() == 0) {
  651. N.set(0xdead0000);
  652. } else {
  653. N.set(0x0000beef);
  654. }
  655. RegXS oxidx;
  656. oxidx.xshare = share+3*tio.player();
  657. T O;
  658. if (tio.player() == 0) {
  659. O.set(0x31410000);
  660. } else {
  661. O.set(0x00005926);
  662. }
  663. // Writing and reading with additively shared indices
  664. printf("Additive Updating\n");
  665. A[aidx] += M;
  666. printf("Additive Reading\n");
  667. T Aa = A[aidx];
  668. // Writing and reading with XOR shared indices
  669. printf("XOR Updating\n");
  670. A[xidx] += N;
  671. printf("XOR Reading\n");
  672. T Ax = A[xidx];
  673. // Writing and reading with OblivIndex indices
  674. auto oidx = A.oblivindex(oxidx);
  675. printf("OblivIndex Updating\n");
  676. A[oidx] += O;
  677. printf("OblivIndex Reading\n");
  678. T Ox = A[oidx];
  679. // Writing and reading with explicit indices
  680. T Ae;
  681. if (depth > 2) {
  682. printf("Explicit Updating\n");
  683. A[5] += Aa;
  684. printf("Explicit Reading\n");
  685. Ae = A[6];
  686. }
  687. // Simultaneous independent reads
  688. printf("3 independent reading\n");
  689. std::vector<T> Av = A[std::array {
  690. aidx, aidx2, aidx3
  691. }];
  692. // Simultaneous independent updates
  693. T Aw1, Aw2, Aw3;
  694. Aw1.set(0x101010101010101 * tio.player());
  695. Aw2.set(0x202020202020202 * tio.player());
  696. Aw3.set(0x303030303030303 * tio.player());
  697. printf("3 independent updating\n");
  698. A[std::array { aidx, aidx2, aidx3 }] -=
  699. std::array { Aw1, Aw2, Aw3 };
  700. if (depth <= 10) {
  701. oram.dump();
  702. auto check = A.reconstruct();
  703. if (tio.player() == 0) {
  704. for (address_t i=0;i<len;++i) {
  705. printf("%04x %016lx\n", i, check[i].share());
  706. }
  707. }
  708. }
  709. auto checkread = A.reconstruct(Aa);
  710. auto checkreade = A.reconstruct(Ae);
  711. auto checkreadx = A.reconstruct(Ax);
  712. auto checkreado = A.reconstruct(Ox);
  713. if (tio.player() == 0) {
  714. printf("Read AS value = %016lx\n", checkread.share());
  715. printf("Read AX value = %016lx\n", checkreadx.share());
  716. printf("Read Ex value = %016lx\n", checkreade.share());
  717. printf("Read OI value = %016lx\n", checkreado.share());
  718. }
  719. for (auto &v : Av) {
  720. auto checkv = A.reconstruct(v);
  721. if (tio.player() == 0) {
  722. printf("Read Av value = %016lx\n", checkv.share());
  723. }
  724. }
  725. });
  726. }
  727. // This measures the same things as the Duoram paper: dependent and
  728. // independent reads, updates, writes, and interleaves
  729. // T is RegAS or RegXS for additive or XOR shared database respectively
  730. template <typename T>
  731. static void duoram(MPCIO &mpcio,
  732. const PRACOptions &opts, char **args)
  733. {
  734. nbits_t depth = 6;
  735. int items = 4;
  736. if (*args) {
  737. depth = atoi(*args);
  738. ++args;
  739. }
  740. if (*args) {
  741. items = atoi(*args);
  742. ++args;
  743. }
  744. MPCTIO tio(mpcio, 0, opts.num_threads);
  745. run_coroutines(tio, [&mpcio, &tio, depth, items] (yield_t &yield) {
  746. size_t size = size_t(1)<<depth;
  747. address_t mask = (depth < ADDRESS_MAX_BITS ?
  748. ((address_t(1)<<depth) - 1) : ~0);
  749. Duoram<T> oram(tio.player(), size);
  750. auto A = oram.flat(tio, yield);
  751. std::cout << "===== DEPENDENT UPDATES =====\n";
  752. mpcio.reset_stats();
  753. tio.reset_lamport();
  754. // Make a linked list of length items
  755. std::vector<T> list_indices;
  756. T prev_index, next_index;
  757. prev_index.randomize(depth);
  758. for (int i=0;i<items;++i) {
  759. next_index.randomize(depth);
  760. A[next_index] += prev_index;
  761. list_indices.push_back(next_index);
  762. prev_index = next_index;
  763. }
  764. tio.sync_lamport();
  765. mpcio.dump_stats(std::cout);
  766. std::cout << "\n===== DEPENDENT READS =====\n";
  767. mpcio.reset_stats();
  768. tio.reset_lamport();
  769. // Read the linked list starting with prev_index
  770. T cur_index = prev_index;
  771. for (int i=0;i<items;++i) {
  772. cur_index = A[cur_index];
  773. }
  774. tio.sync_lamport();
  775. mpcio.dump_stats(std::cout);
  776. std::cout << "\n===== INDEPENDENT READS =====\n";
  777. mpcio.reset_stats();
  778. tio.reset_lamport();
  779. // Read all the entries in the list at once
  780. std::vector<T> read_outputs = A[list_indices];
  781. tio.sync_lamport();
  782. mpcio.dump_stats(std::cout);
  783. std::cout << "\n===== INDEPENDENT UPDATES =====\n";
  784. mpcio.reset_stats();
  785. tio.reset_lamport();
  786. // Make a vector of indices 1 larger than those in list_indices,
  787. // and a vector of values 1 larger than those in outputs
  788. std::vector<T> indep_indices, indep_values;
  789. T one;
  790. one.set(tio.player()); // Sets the shared value to 1
  791. for (int i=0;i<items;++i) {
  792. indep_indices.push_back(list_indices[i]+one);
  793. indep_values.push_back(read_outputs[i]+one);
  794. }
  795. // Update all the indices at once
  796. A[indep_indices] += indep_values;
  797. tio.sync_lamport();
  798. mpcio.dump_stats(std::cout);
  799. std::cout << "\n===== DEPENDENT WRITES =====\n";
  800. mpcio.reset_stats();
  801. tio.reset_lamport();
  802. T two;
  803. two.set(2*tio.player()); // Sets the shared value to 2
  804. // For each address addr that's number i from the end of the
  805. // linked list, write i+1 into location addr+2
  806. for (int i=0;i<items;++i) {
  807. T val;
  808. val.set((i+1)*tio.player());
  809. A[list_indices[i]+two] = val;
  810. }
  811. tio.sync_lamport();
  812. mpcio.dump_stats(std::cout);
  813. std::cout << "\n===== DEPENDENT INTERLEAVED =====\n";
  814. mpcio.reset_stats();
  815. tio.reset_lamport();
  816. T three;
  817. three.set(3*tio.player()); // Sets the shared value to 3
  818. // Follow the linked list and whenever A[addr]=val, set
  819. // A[addr+3]=val+3
  820. cur_index = prev_index;
  821. for (int i=0;i<items;++i) {
  822. T next_index = A[cur_index];
  823. A[cur_index+three] = next_index+three;
  824. cur_index = next_index;
  825. }
  826. tio.sync_lamport();
  827. mpcio.dump_stats(std::cout);
  828. std::cout << "\n";
  829. mpcio.reset_stats();
  830. tio.reset_lamport();
  831. if (depth <= 30) {
  832. auto check = A.reconstruct();
  833. auto head = A.reconstruct(prev_index);
  834. if (tio.player() == 0) {
  835. int width = (depth+3)/4;
  836. printf("Head of linked list: %0*lx\n\n", width,
  837. head.share() & mask);
  838. std::cout << "Non-zero reconstructed database entries:\n";
  839. for (address_t i=0;i<size;++i) {
  840. value_t share = check[i].share() & mask;
  841. if (share) printf("%0*x: %0*lx\n", width, i, width, share);
  842. }
  843. }
  844. }
  845. });
  846. }
  847. // This measures just sequential (dependent) reads
  848. // T is RegAS or RegXS for additive or XOR shared database respectively
  849. template <typename T>
  850. static void read_test(MPCIO &mpcio,
  851. const PRACOptions &opts, char **args)
  852. {
  853. nbits_t depth = 6;
  854. int items = 4;
  855. if (*args) {
  856. depth = atoi(*args);
  857. ++args;
  858. }
  859. if (*args) {
  860. items = atoi(*args);
  861. ++args;
  862. }
  863. MPCTIO tio(mpcio, 0, opts.num_threads);
  864. run_coroutines(tio, [&mpcio, &tio, depth, items] (yield_t &yield) {
  865. size_t size = size_t(1)<<depth;
  866. Duoram<T> oram(tio.player(), size);
  867. auto A = oram.flat(tio, yield);
  868. std::cout << "\n===== SEQUENTIAL READS =====\n";
  869. T totval;
  870. for (int i=0;i<items;++i) {
  871. RegXS idx;
  872. idx.randomize(depth);
  873. T val = A[idx];
  874. totval += val;
  875. }
  876. printf("Total value read: %016lx\n", totval.share());
  877. });
  878. }
  879. static void cdpf_test(MPCIO &mpcio,
  880. const PRACOptions &opts, char **args)
  881. {
  882. value_t query, target;
  883. int iters = 1;
  884. arc4random_buf(&query, sizeof(query));
  885. arc4random_buf(&target, sizeof(target));
  886. if (*args) {
  887. query = strtoull(*args, NULL, 16);
  888. ++args;
  889. }
  890. if (*args) {
  891. target = strtoull(*args, NULL, 16);
  892. ++args;
  893. }
  894. if (*args) {
  895. iters = atoi(*args);
  896. ++args;
  897. }
  898. int num_threads = opts.num_threads;
  899. boost::asio::thread_pool pool(num_threads);
  900. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  901. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  902. MPCTIO tio(mpcio, thread_num);
  903. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  904. size_t &aes_ops = tio.aes_ops();
  905. for (int i=0;i<iters;++i) {
  906. if (tio.player() == 2) {
  907. tio.cdpf(yield);
  908. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  909. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  910. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  911. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  912. dump_node(leaf0 ^ leaf1);
  913. } else {
  914. CDPF dpf = tio.cdpf(yield);
  915. printf("ashare = %016lX\nxshare = %016lX\n",
  916. dpf.as_target.ashare, dpf.xs_target.xshare);
  917. DPFnode leaf = dpf.leaf(query, aes_ops);
  918. printf("DPF(%016lx) = ", query);
  919. dump_node(leaf);
  920. if (tio.player() == 1) {
  921. tio.iostream_peer() << leaf;
  922. } else {
  923. DPFnode peerleaf;
  924. tio.iostream_peer() >> peerleaf;
  925. printf("XOR = ");
  926. dump_node(leaf ^ peerleaf);
  927. }
  928. }
  929. }
  930. });
  931. });
  932. }
  933. pool.join();
  934. }
  935. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  936. value_t target, value_t x)
  937. {
  938. int player = tio.player();
  939. size_t &aes_ops = tio.aes_ops();
  940. int res = 1;
  941. if (player == 2) {
  942. // Create a CDPF pair with the given target
  943. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  944. // Send it and a share of x to the computational parties
  945. RegAS x0, x1;
  946. x0.randomize();
  947. x1.set(x-x0.share());
  948. tio.iostream_p0() << dpf0 << x0;
  949. tio.iostream_p1() << dpf1 << x1;
  950. } else {
  951. CDPF dpf;
  952. RegAS xsh;
  953. tio.iostream_server() >> dpf >> xsh;
  954. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  955. RegBS eeq = dpf.is_zero(tio, yield, xsh, aes_ops);
  956. printf("%016lx %016lx %d %d %d %d ", target, x, lt.bshare,
  957. eq.bshare, gt.bshare, eeq.bshare);
  958. // Check the answer
  959. if (player == 1) {
  960. tio.iostream_peer() << xsh << lt << eq << gt << eeq;
  961. } else {
  962. RegAS peer_xsh;
  963. RegBS peer_lt, peer_eq, peer_gt, peer_eeq;
  964. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >>
  965. peer_gt >> peer_eeq;
  966. lt ^= peer_lt;
  967. eq ^= peer_eq;
  968. gt ^= peer_gt;
  969. eeq ^= peer_eeq;
  970. xsh += peer_xsh;
  971. int lti = int(lt.bshare);
  972. int eqi = int(eq.bshare);
  973. int gti = int(gt.bshare);
  974. int eeqi = int(eeq.bshare);
  975. x = xsh.share();
  976. printf(": %d %d %d %d ", lti, eqi, gti, eeqi);
  977. bool signbit = (x >> 63);
  978. if (lti + eqi + gti != 1 || eqi != eeqi) {
  979. printf("INCONSISTENT");
  980. res = 0;
  981. } else if (x == 0 && eqi) {
  982. printf("=");
  983. } else if (!signbit && gti) {
  984. printf(">");
  985. } else if (signbit && lti) {
  986. printf("<");
  987. } else {
  988. printf("INCORRECT");
  989. res = 0;
  990. }
  991. }
  992. printf("\n");
  993. }
  994. return res;
  995. }
  996. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  997. value_t target, value_t x)
  998. {
  999. int res = 1;
  1000. res &= compare_test_one(tio, yield, target, x);
  1001. res &= compare_test_one(tio, yield, target, 0);
  1002. res &= compare_test_one(tio, yield, target, 1);
  1003. res &= compare_test_one(tio, yield, target, 15);
  1004. res &= compare_test_one(tio, yield, target, 16);
  1005. res &= compare_test_one(tio, yield, target, 17);
  1006. res &= compare_test_one(tio, yield, target, -1);
  1007. res &= compare_test_one(tio, yield, target, -15);
  1008. res &= compare_test_one(tio, yield, target, -16);
  1009. res &= compare_test_one(tio, yield, target, -17);
  1010. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  1011. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  1012. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  1013. return res;
  1014. }
  1015. static void compare_test(MPCIO &mpcio,
  1016. const PRACOptions &opts, char **args)
  1017. {
  1018. value_t target, x;
  1019. arc4random_buf(&target, sizeof(target));
  1020. arc4random_buf(&x, sizeof(x));
  1021. if (*args) {
  1022. target = strtoull(*args, NULL, 16);
  1023. ++args;
  1024. }
  1025. if (*args) {
  1026. x = strtoull(*args, NULL, 16);
  1027. ++args;
  1028. }
  1029. int num_threads = opts.num_threads;
  1030. boost::asio::thread_pool pool(num_threads);
  1031. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  1032. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  1033. MPCTIO tio(mpcio, thread_num);
  1034. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  1035. int res = 1;
  1036. res &= compare_test_target(tio, yield, target, x);
  1037. res &= compare_test_target(tio, yield, 0, x);
  1038. res &= compare_test_target(tio, yield, 1, x);
  1039. res &= compare_test_target(tio, yield, 15, x);
  1040. res &= compare_test_target(tio, yield, 16, x);
  1041. res &= compare_test_target(tio, yield, 17, x);
  1042. res &= compare_test_target(tio, yield, -1, x);
  1043. res &= compare_test_target(tio, yield, -15, x);
  1044. res &= compare_test_target(tio, yield, -16, x);
  1045. res &= compare_test_target(tio, yield, -17, x);
  1046. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  1047. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  1048. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  1049. if (tio.player() == 0) {
  1050. if (res == 1) {
  1051. printf("All tests passed!\n");
  1052. } else {
  1053. printf("TEST FAILURES\n");
  1054. }
  1055. }
  1056. });
  1057. });
  1058. }
  1059. pool.join();
  1060. }
  1061. static void sort_test(MPCIO &mpcio,
  1062. const PRACOptions &opts, char **args)
  1063. {
  1064. nbits_t depth=6;
  1065. if (*args) {
  1066. depth = atoi(*args);
  1067. ++args;
  1068. }
  1069. address_t len = (1<<depth);
  1070. if (*args) {
  1071. len = atoi(*args);
  1072. ++args;
  1073. }
  1074. MPCTIO tio(mpcio, 0, opts.num_threads);
  1075. run_coroutines(tio, [&tio, depth, len] (yield_t &yield) {
  1076. address_t size = address_t(1)<<depth;
  1077. // size_t &aes_ops = tio.aes_ops();
  1078. Duoram<RegAS> oram(tio.player(), size);
  1079. auto A = oram.flat(tio, yield);
  1080. A.explicitonly(true);
  1081. // Initialize the memory to random values in parallel
  1082. std::vector<coro_t> coroutines;
  1083. for (address_t i=0; i<size; ++i) {
  1084. coroutines.emplace_back(
  1085. [&A, i](yield_t &yield) {
  1086. auto Acoro = A.context(yield);
  1087. RegAS v;
  1088. v.randomize(62);
  1089. Acoro[i] += v;
  1090. });
  1091. }
  1092. run_coroutines(yield, coroutines);
  1093. A.bitonic_sort(0, len);
  1094. if (depth <= 10) {
  1095. oram.dump();
  1096. }
  1097. auto check = A.reconstruct();
  1098. bool fail = false;
  1099. if (tio.player() == 0) {
  1100. for (address_t i=0;i<size;++i) {
  1101. if (depth <= 10) {
  1102. printf("%04x %016lx\n", i, check[i].share());
  1103. }
  1104. if (i>0 && i<len &&
  1105. check[i].share() < check[i-1].share()) {
  1106. fail = true;
  1107. }
  1108. }
  1109. if (fail) {
  1110. printf("FAIL\n");
  1111. } else {
  1112. printf("PASS\n");
  1113. }
  1114. }
  1115. });
  1116. }
  1117. static void pad_test(MPCIO &mpcio,
  1118. const PRACOptions &opts, char **args)
  1119. {
  1120. nbits_t depth=6;
  1121. if (*args) {
  1122. depth = atoi(*args);
  1123. ++args;
  1124. }
  1125. address_t len = (1<<depth);
  1126. if (*args) {
  1127. len = atoi(*args);
  1128. ++args;
  1129. }
  1130. MPCTIO tio(mpcio, 0, opts.num_threads);
  1131. run_coroutines(tio, [&mpcio, &tio, depth, len] (yield_t &yield) {
  1132. int player = tio.player();
  1133. Duoram<RegAS> oram(player, len);
  1134. auto A = oram.flat(tio, yield);
  1135. // Initialize the ORAM in explicit mode
  1136. A.explicitonly(true);
  1137. for (address_t i=0; i<len; ++i) {
  1138. RegAS v;
  1139. v.set((player*0xffff+1)*i);
  1140. A[i] = v;
  1141. }
  1142. A.explicitonly(false);
  1143. // Obliviously add 0 to A[0], which reblinds the whole database
  1144. RegAS z;
  1145. A[z] += z;
  1146. auto check = A.reconstruct();
  1147. if (player == 0) {
  1148. for (address_t i=0;i<len;++i) {
  1149. if (depth <= 10) {
  1150. printf("%04x %016lx\n", i, check[i].share());
  1151. }
  1152. }
  1153. printf("\n");
  1154. }
  1155. address_t maxsize = address_t(1)<<depth;
  1156. Duoram<RegAS>::Pad P(A, tio, yield, maxsize);
  1157. for (address_t i=0; i<maxsize; ++i) {
  1158. RegAS v = P[i];
  1159. if (depth <= 10) {
  1160. value_t vval = mpc_reconstruct(tio, yield, v);
  1161. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1162. }
  1163. }
  1164. printf("\n");
  1165. for (address_t i=0; i<maxsize; ++i) {
  1166. value_t offset = 0xdeadbeef;
  1167. if (player) {
  1168. offset = -offset;
  1169. }
  1170. RegAS ind;
  1171. ind.set(player*i+offset);
  1172. RegAS v = P[ind];
  1173. if (depth <= 10) {
  1174. value_t vval = mpc_reconstruct(tio, yield, v);
  1175. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1176. }
  1177. }
  1178. printf("\n");
  1179. });
  1180. }
  1181. static void bsearch_test(MPCIO &mpcio,
  1182. const PRACOptions &opts, char **args, bool basic)
  1183. {
  1184. value_t target;
  1185. arc4random_buf(&target, sizeof(target));
  1186. target >>= 1;
  1187. nbits_t depth=6;
  1188. bool is_presorted = true;
  1189. // Use a random array (which we explicitly sort) instead of a
  1190. // presorted array
  1191. if (*args && !strcmp(args[0], "-r")) {
  1192. is_presorted = false;
  1193. ++args;
  1194. }
  1195. if (*args) {
  1196. depth = atoi(*args);
  1197. ++args;
  1198. }
  1199. address_t len = (1<<depth) - 1;
  1200. int iters = 1;
  1201. if (*args) {
  1202. iters = atoi(*args);
  1203. ++args;
  1204. }
  1205. if (is_presorted) {
  1206. target %= (value_t(len) << 16);
  1207. }
  1208. if (*args) {
  1209. target = strtoull(*args, NULL, 16);
  1210. ++args;
  1211. }
  1212. MPCTIO tio(mpcio, 0, opts.num_threads);
  1213. run_coroutines(tio, [&tio, &mpcio, depth, len, iters, target, basic, is_presorted] (yield_t &yield) {
  1214. RegAS tshare;
  1215. std::cout << "\n===== SETUP =====\n";
  1216. if (tio.player() == 2) {
  1217. // Send shares of the target to the computational
  1218. // players
  1219. RegAS tshare0, tshare1;
  1220. tshare0.randomize();
  1221. tshare1.set(target-tshare0.share());
  1222. tio.iostream_p0() << tshare0;
  1223. tio.iostream_p1() << tshare1;
  1224. printf("Using target = %016lx\n", target);
  1225. yield();
  1226. } else {
  1227. // Get the share of the target
  1228. tio.iostream_server() >> tshare;
  1229. }
  1230. tio.sync_lamport();
  1231. mpcio.dump_stats(std::cout);
  1232. std::cout << "\n===== " << (is_presorted ? "CREATE" : "SORT RANDOM")
  1233. << " DATABASE =====\n";
  1234. mpcio.reset_stats();
  1235. tio.reset_lamport();
  1236. // If is_presorted is true, create a database of presorted
  1237. // values. If is_presorted is false, create a database of
  1238. // random values and explicitly sort it.
  1239. Duoram<RegAS> oram(tio.player(), len);
  1240. auto A = oram.flat(tio, yield);
  1241. A.explicitonly(true);
  1242. // Initialize the memory to sorted or random values, depending
  1243. // on the is_presorted flag
  1244. for (address_t i=0; i<len; ++i) {
  1245. RegAS v;
  1246. if (!is_presorted) {
  1247. v.randomize(62);
  1248. } else {
  1249. v.ashare = (tio.player() * value_t(i)) << 16;
  1250. }
  1251. A[i] = v;
  1252. }
  1253. A.explicitonly(false);
  1254. if (!is_presorted) {
  1255. A.bitonic_sort(0, len);
  1256. }
  1257. tio.sync_lamport();
  1258. mpcio.dump_stats(std::cout);
  1259. std::cout << "\n===== BINARY SEARCH =====\n";
  1260. mpcio.reset_stats();
  1261. tio.reset_lamport();
  1262. // Binary search for the target
  1263. value_t checkindex = 0;
  1264. for (int i=0; i<iters; ++i) {
  1265. if (basic) {
  1266. RegAS tindex = A.basic_binary_search(tshare);
  1267. checkindex = mpc_reconstruct(tio, yield, tindex);
  1268. } else {
  1269. RegXS tindex = A.binary_search(tshare);
  1270. checkindex = mpc_reconstruct(tio, yield, tindex);
  1271. }
  1272. }
  1273. tio.sync_lamport();
  1274. mpcio.dump_stats(std::cout);
  1275. std::cout << "\n===== CHECK ANSWER =====\n";
  1276. mpcio.reset_stats();
  1277. tio.reset_lamport();
  1278. // Check the answer
  1279. size_t size = size_t(1) << depth;
  1280. value_t checktarget = mpc_reconstruct(tio, yield, tshare);
  1281. auto check = A.reconstruct();
  1282. bool fail = false;
  1283. if (tio.player() == 0) {
  1284. for (address_t i=0;i<len;++i) {
  1285. if (depth <= 10) {
  1286. printf("%c%04x %016lx\n",
  1287. (i == checkindex ? '*' : ' '),
  1288. i, check[i].share());
  1289. }
  1290. if (i>0 && i<len &&
  1291. check[i].share() < check[i-1].share()) {
  1292. fail = true;
  1293. }
  1294. if (i == checkindex) {
  1295. // check[i] should be >= target, and check[i-1]
  1296. // should be < target
  1297. if ((i < len && check[i].share() < checktarget) ||
  1298. (i > 0 && check[i-1].share() >= checktarget)) {
  1299. fail = true;
  1300. }
  1301. }
  1302. }
  1303. if (checkindex == len && check[len-1].share() >= checktarget) {
  1304. fail = true;
  1305. }
  1306. printf("Target = %016lx\n", checktarget);
  1307. printf("Found index = %02lx\n", checkindex);
  1308. if (checkindex > size) {
  1309. fail = true;
  1310. }
  1311. if (fail) {
  1312. printf("FAIL\n");
  1313. } else {
  1314. printf("PASS\n");
  1315. }
  1316. }
  1317. });
  1318. }
  1319. template <typename T>
  1320. static void related(MPCIO &mpcio,
  1321. const PRACOptions &opts, char **args)
  1322. {
  1323. nbits_t depth = 5;
  1324. // The depth of the (complete) binary tree
  1325. if (*args) {
  1326. depth = atoi(*args);
  1327. ++args;
  1328. }
  1329. // The layer at which to choose a random parent node (and its two
  1330. // children along with it)
  1331. nbits_t layer = depth-1;
  1332. if (*args) {
  1333. layer = atoi(*args);
  1334. ++args;
  1335. }
  1336. assert(layer < depth);
  1337. MPCTIO tio(mpcio, 0, opts.num_threads);
  1338. run_coroutines(tio, [&mpcio, &tio, depth, layer] (yield_t &yield) {
  1339. size_t size = size_t(1)<<(depth+1);
  1340. Duoram<T> oram(tio.player(), size);
  1341. auto A = oram.flat(tio, yield);
  1342. // Initialize A with words with sequential top and bottom halves
  1343. // (just so we can more easily eyeball the right answers)
  1344. A.init([] (size_t i) { return i * 0x100000001; } );
  1345. // We use this layout for the tree:
  1346. // A[0] is unused
  1347. // A[1] is the root (layer 0)
  1348. // A[2..3] is layer 1
  1349. // A[4..7] is layer 2
  1350. // ...
  1351. // A[(1<<j)..((2<<j)-1)] is layer j
  1352. //
  1353. // So the parent of x is at location (x/2) and the children of x
  1354. // are at locations 2*x and 2*x+1
  1355. // Pick a random index _within_ the given layer (i.e., the
  1356. // offset from the beginning of the layer, not the absolute
  1357. // location in A)
  1358. RegXS idx;
  1359. idx.randomize(layer);
  1360. // Create the OblivIndex. RegXS is the type of the common index
  1361. // (idx), 3 is the maximum number of related updates to support
  1362. // (which equals the width of the underlying RDPF, currently
  1363. // maximum 5), layer is the depth of the underlying RDPF (the
  1364. // bit length of idx).
  1365. typename Duoram<T>::template OblivIndex<RegXS,3> oidx(tio, yield, idx, layer);
  1366. // This is the (known) layer containing the (unknown) parent
  1367. // node
  1368. typename Duoram<T>::Flat P(A, tio, yield, 1<<layer, 1<<layer);
  1369. // This is the layer below that one, containing all possible
  1370. // children
  1371. typename Duoram<T>::Flat C(A, tio, yield, 2<<layer, 2<<layer);
  1372. // These are the subsets of C containing the left children and
  1373. // the right children respectively
  1374. typename Duoram<T>::Stride L(C, tio, yield, 0, 2);
  1375. typename Duoram<T>::Stride R(C, tio, yield, 1, 2);
  1376. T parent, left, right;
  1377. // Do three related reads. In this version, only one DPF will
  1378. // be used, but it will still be _evaluated_ three times.
  1379. parent = P[oidx];
  1380. left = L[oidx];
  1381. right = R[oidx];
  1382. // The operation is just a simple rotation: the value in the
  1383. // parent moves to the left child, the left child moves to the
  1384. // right child, and the right child becomes the parent
  1385. // Do three related updates. As above, only one (wide) DPF will
  1386. // be used (the same one as for the reads in fact), but it will
  1387. // still be _evaluated_ three more times.
  1388. P[oidx] += right-parent;
  1389. L[oidx] += parent-left;
  1390. R[oidx] += left-right;
  1391. // Check the answer
  1392. auto check = A.reconstruct();
  1393. if (depth <= 10) {
  1394. oram.dump();
  1395. if (tio.player() == 0) {
  1396. for (address_t i=0;i<size;++i) {
  1397. printf("%04x %016lx\n", i, check[i].share());
  1398. }
  1399. }
  1400. }
  1401. value_t pval = mpc_reconstruct(tio, yield, parent);
  1402. value_t lval = mpc_reconstruct(tio, yield, left);
  1403. value_t rval = mpc_reconstruct(tio, yield, right);
  1404. printf("parent = %016lx\nleft = %016lx\nright = %016lx\n",
  1405. pval, lval, rval);
  1406. });
  1407. }
  1408. template <typename T>
  1409. static void path(MPCIO &mpcio,
  1410. const PRACOptions &opts, char **args)
  1411. {
  1412. nbits_t depth = 5;
  1413. // The depth of the (complete) binary tree
  1414. if (*args) {
  1415. depth = atoi(*args);
  1416. ++args;
  1417. }
  1418. // The target node
  1419. size_t target_node = 3 << (depth-1);
  1420. if (*args) {
  1421. target_node = atoi(*args);
  1422. ++args;
  1423. }
  1424. MPCTIO tio(mpcio, 0, opts.num_threads);
  1425. run_coroutines(tio, [&mpcio, &tio, depth, target_node] (yield_t &yield) {
  1426. size_t size = size_t(1)<<(depth+1);
  1427. Duoram<T> oram(tio.player(), size);
  1428. auto A = oram.flat(tio, yield);
  1429. // Initialize A with words with sequential top and bottom halves
  1430. // (just so we can more easily eyeball the right answers)
  1431. A.init([] (size_t i) { return i * 0x100000001; } );
  1432. // We use this layout for the tree:
  1433. // A[0] is unused
  1434. // A[1] is the root (layer 0)
  1435. // A[2..3] is layer 1
  1436. // A[4..7] is layer 2
  1437. // ...
  1438. // A[(1<<j)..((2<<j)-1)] is layer j
  1439. //
  1440. // So the parent of x is at location (x/2) and the children of x
  1441. // are at locations 2*x and 2*x+1
  1442. // Create a Path from the root to the target node
  1443. typename Duoram<T>::Path P(A, tio, yield, target_node);
  1444. // Re-initialize that path to something recognizable
  1445. P.init([] (size_t i) { return 0xff + i * 0x1000000010000; } );
  1446. // ORAM update along that path
  1447. RegXS idx;
  1448. idx.set(tio.player() * arc4random_uniform(P.size()));
  1449. T val;
  1450. val.set(tio.player() * 0xaaaa00000000);
  1451. P[idx] += val;
  1452. // Binary search along that path
  1453. T lookup;
  1454. lookup.set(tio.player() * 0x3000000000000);
  1455. RegXS foundidx = P.binary_search(lookup);
  1456. // Check the answer
  1457. auto check = A.reconstruct();
  1458. if (depth <= 10) {
  1459. oram.dump();
  1460. if (tio.player() == 0) {
  1461. for (address_t i=0;i<size;++i) {
  1462. printf("%04x %016lx\n", i, check[i].share());
  1463. }
  1464. }
  1465. }
  1466. value_t found = mpc_reconstruct(tio, yield, foundidx);
  1467. printf("foundidx = %lu\n", found);
  1468. });
  1469. }
  1470. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  1471. {
  1472. if (!*args) {
  1473. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  1474. return;
  1475. } else if (!strcmp(*args, "test")) {
  1476. ++args;
  1477. online_test(mpcio, opts, args);
  1478. } else if (!strcmp(*args, "lamporttest")) {
  1479. ++args;
  1480. lamport_test(mpcio, opts, args);
  1481. } else if (!strcmp(*args, "rdpftest")) {
  1482. ++args;
  1483. rdpf_test<1>(mpcio, opts, args, false);
  1484. } else if (!strcmp(*args, "rdpftest2")) {
  1485. ++args;
  1486. rdpf_test<2>(mpcio, opts, args, false);
  1487. } else if (!strcmp(*args, "rdpftest3")) {
  1488. ++args;
  1489. rdpf_test<3>(mpcio, opts, args, false);
  1490. } else if (!strcmp(*args, "rdpftest4")) {
  1491. ++args;
  1492. rdpf_test<4>(mpcio, opts, args, false);
  1493. } else if (!strcmp(*args, "rdpftest5")) {
  1494. ++args;
  1495. rdpf_test<5>(mpcio, opts, args, false);
  1496. } else if (!strcmp(*args, "irdpftest")) {
  1497. ++args;
  1498. rdpf_test<1>(mpcio, opts, args, true);
  1499. } else if (!strcmp(*args, "irdpftest2")) {
  1500. ++args;
  1501. rdpf_test<2>(mpcio, opts, args, true);
  1502. } else if (!strcmp(*args, "irdpftest3")) {
  1503. ++args;
  1504. rdpf_test<3>(mpcio, opts, args, true);
  1505. } else if (!strcmp(*args, "irdpftest4")) {
  1506. ++args;
  1507. rdpf_test<4>(mpcio, opts, args, true);
  1508. } else if (!strcmp(*args, "irdpftest5")) {
  1509. ++args;
  1510. rdpf_test<5>(mpcio, opts, args, true);
  1511. } else if (!strcmp(*args, "rdpftime")) {
  1512. ++args;
  1513. rdpf_timing(mpcio, opts, args);
  1514. } else if (!strcmp(*args, "evaltime")) {
  1515. ++args;
  1516. rdpfeval_timing(mpcio, opts, args);
  1517. } else if (!strcmp(*args, "parevaltime")) {
  1518. ++args;
  1519. par_rdpfeval_timing(mpcio, opts, args);
  1520. } else if (!strcmp(*args, "tupletime")) {
  1521. ++args;
  1522. tupleeval_timing(mpcio, opts, args);
  1523. } else if (!strcmp(*args, "partupletime")) {
  1524. ++args;
  1525. par_tupleeval_timing(mpcio, opts, args);
  1526. } else if (!strcmp(*args, "duotest")) {
  1527. ++args;
  1528. if (opts.use_xor_db) {
  1529. duoram_test<RegXS>(mpcio, opts, args);
  1530. } else {
  1531. duoram_test<RegAS>(mpcio, opts, args);
  1532. }
  1533. } else if (!strcmp(*args, "read")) {
  1534. ++args;
  1535. if (opts.use_xor_db) {
  1536. read_test<RegXS>(mpcio, opts, args);
  1537. } else {
  1538. read_test<RegAS>(mpcio, opts, args);
  1539. }
  1540. } else if (!strcmp(*args, "cdpftest")) {
  1541. ++args;
  1542. cdpf_test(mpcio, opts, args);
  1543. } else if (!strcmp(*args, "cmptest")) {
  1544. ++args;
  1545. compare_test(mpcio, opts, args);
  1546. } else if (!strcmp(*args, "sorttest")) {
  1547. ++args;
  1548. sort_test(mpcio, opts, args);
  1549. } else if (!strcmp(*args, "padtest")) {
  1550. ++args;
  1551. pad_test(mpcio, opts, args);
  1552. } else if (!strcmp(*args, "bbsearch")) {
  1553. ++args;
  1554. bsearch_test(mpcio, opts, args, true);
  1555. } else if (!strcmp(*args, "bsearch")) {
  1556. ++args;
  1557. bsearch_test(mpcio, opts, args, false);
  1558. } else if (!strcmp(*args, "duoram")) {
  1559. ++args;
  1560. if (opts.use_xor_db) {
  1561. duoram<RegXS>(mpcio, opts, args);
  1562. } else {
  1563. duoram<RegAS>(mpcio, opts, args);
  1564. }
  1565. } else if (!strcmp(*args, "related")) {
  1566. ++args;
  1567. if (opts.use_xor_db) {
  1568. related<RegXS>(mpcio, opts, args);
  1569. } else {
  1570. related<RegAS>(mpcio, opts, args);
  1571. }
  1572. } else if (!strcmp(*args, "path")) {
  1573. ++args;
  1574. path<RegAS>(mpcio, opts, args);
  1575. } else if (!strcmp(*args, "cell")) {
  1576. ++args;
  1577. cell(mpcio, opts, args);
  1578. } else if (!strcmp(*args, "bst")) {
  1579. ++args;
  1580. bst(mpcio, opts, args);
  1581. } else if (!strcmp(*args, "avl")) {
  1582. ++args;
  1583. avl(mpcio, opts, args);
  1584. } else if (!strcmp(*args, "avl_tests")) {
  1585. ++args;
  1586. avl_tests(mpcio, opts, args);
  1587. } else if (!strcmp(*args, "heap")) {
  1588. ++args;
  1589. Heap(mpcio, opts, args);
  1590. } else {
  1591. std::cerr << "Unknown mode " << *args << "\n";
  1592. }
  1593. }