online.cpp 58 KB

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