online.cpp 58 KB

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