online.cpp 58 KB

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