online.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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. static void cdpf_test(MPCIO &mpcio,
  846. const PRACOptions &opts, char **args)
  847. {
  848. value_t query, target;
  849. int iters = 1;
  850. arc4random_buf(&query, sizeof(query));
  851. arc4random_buf(&target, sizeof(target));
  852. if (*args) {
  853. query = strtoull(*args, NULL, 16);
  854. ++args;
  855. }
  856. if (*args) {
  857. target = strtoull(*args, NULL, 16);
  858. ++args;
  859. }
  860. if (*args) {
  861. iters = atoi(*args);
  862. ++args;
  863. }
  864. int num_threads = opts.num_threads;
  865. boost::asio::thread_pool pool(num_threads);
  866. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  867. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  868. MPCTIO tio(mpcio, thread_num);
  869. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  870. size_t &aes_ops = tio.aes_ops();
  871. for (int i=0;i<iters;++i) {
  872. if (tio.player() == 2) {
  873. tio.cdpf(yield);
  874. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  875. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  876. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  877. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  878. dump_node(leaf0 ^ leaf1);
  879. } else {
  880. CDPF dpf = tio.cdpf(yield);
  881. printf("ashare = %016lX\nxshare = %016lX\n",
  882. dpf.as_target.ashare, dpf.xs_target.xshare);
  883. DPFnode leaf = dpf.leaf(query, aes_ops);
  884. printf("DPF(%016lx) = ", query);
  885. dump_node(leaf);
  886. if (tio.player() == 1) {
  887. tio.iostream_peer() << leaf;
  888. } else {
  889. DPFnode peerleaf;
  890. tio.iostream_peer() >> peerleaf;
  891. printf("XOR = ");
  892. dump_node(leaf ^ peerleaf);
  893. }
  894. }
  895. }
  896. });
  897. });
  898. }
  899. pool.join();
  900. }
  901. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  902. value_t target, value_t x)
  903. {
  904. int player = tio.player();
  905. size_t &aes_ops = tio.aes_ops();
  906. int res = 1;
  907. if (player == 2) {
  908. // Create a CDPF pair with the given target
  909. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  910. // Send it and a share of x to the computational parties
  911. RegAS x0, x1;
  912. x0.randomize();
  913. x1.set(x-x0.share());
  914. tio.iostream_p0() << dpf0 << x0;
  915. tio.iostream_p1() << dpf1 << x1;
  916. } else {
  917. CDPF dpf;
  918. RegAS xsh;
  919. tio.iostream_server() >> dpf >> xsh;
  920. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  921. RegBS eeq = dpf.is_zero(tio, yield, xsh, aes_ops);
  922. printf("%016lx %016lx %d %d %d %d ", target, x, lt.bshare,
  923. eq.bshare, gt.bshare, eeq.bshare);
  924. // Check the answer
  925. if (player == 1) {
  926. tio.iostream_peer() << xsh << lt << eq << gt << eeq;
  927. } else {
  928. RegAS peer_xsh;
  929. RegBS peer_lt, peer_eq, peer_gt, peer_eeq;
  930. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >>
  931. peer_gt >> peer_eeq;
  932. lt ^= peer_lt;
  933. eq ^= peer_eq;
  934. gt ^= peer_gt;
  935. eeq ^= peer_eeq;
  936. xsh += peer_xsh;
  937. int lti = int(lt.bshare);
  938. int eqi = int(eq.bshare);
  939. int gti = int(gt.bshare);
  940. int eeqi = int(eeq.bshare);
  941. x = xsh.share();
  942. printf(": %d %d %d %d ", lti, eqi, gti, eeqi);
  943. bool signbit = (x >> 63);
  944. if (lti + eqi + gti != 1 || eqi != eeqi) {
  945. printf("INCONSISTENT");
  946. res = 0;
  947. } else if (x == 0 && eqi) {
  948. printf("=");
  949. } else if (!signbit && gti) {
  950. printf(">");
  951. } else if (signbit && lti) {
  952. printf("<");
  953. } else {
  954. printf("INCORRECT");
  955. res = 0;
  956. }
  957. }
  958. printf("\n");
  959. }
  960. return res;
  961. }
  962. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  963. value_t target, value_t x)
  964. {
  965. int res = 1;
  966. res &= compare_test_one(tio, yield, target, x);
  967. res &= compare_test_one(tio, yield, target, 0);
  968. res &= compare_test_one(tio, yield, target, 1);
  969. res &= compare_test_one(tio, yield, target, 15);
  970. res &= compare_test_one(tio, yield, target, 16);
  971. res &= compare_test_one(tio, yield, target, 17);
  972. res &= compare_test_one(tio, yield, target, -1);
  973. res &= compare_test_one(tio, yield, target, -15);
  974. res &= compare_test_one(tio, yield, target, -16);
  975. res &= compare_test_one(tio, yield, target, -17);
  976. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  977. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  978. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  979. return res;
  980. }
  981. static void compare_test(MPCIO &mpcio,
  982. const PRACOptions &opts, char **args)
  983. {
  984. value_t target, x;
  985. arc4random_buf(&target, sizeof(target));
  986. arc4random_buf(&x, sizeof(x));
  987. if (*args) {
  988. target = strtoull(*args, NULL, 16);
  989. ++args;
  990. }
  991. if (*args) {
  992. x = strtoull(*args, NULL, 16);
  993. ++args;
  994. }
  995. int num_threads = opts.num_threads;
  996. boost::asio::thread_pool pool(num_threads);
  997. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  998. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  999. MPCTIO tio(mpcio, thread_num);
  1000. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  1001. int res = 1;
  1002. res &= compare_test_target(tio, yield, target, x);
  1003. res &= compare_test_target(tio, yield, 0, x);
  1004. res &= compare_test_target(tio, yield, 1, x);
  1005. res &= compare_test_target(tio, yield, 15, x);
  1006. res &= compare_test_target(tio, yield, 16, x);
  1007. res &= compare_test_target(tio, yield, 17, x);
  1008. res &= compare_test_target(tio, yield, -1, x);
  1009. res &= compare_test_target(tio, yield, -15, x);
  1010. res &= compare_test_target(tio, yield, -16, x);
  1011. res &= compare_test_target(tio, yield, -17, x);
  1012. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  1013. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  1014. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  1015. if (tio.player() == 0) {
  1016. if (res == 1) {
  1017. printf("All tests passed!\n");
  1018. } else {
  1019. printf("TEST FAILURES\n");
  1020. }
  1021. }
  1022. });
  1023. });
  1024. }
  1025. pool.join();
  1026. }
  1027. static void sort_test(MPCIO &mpcio,
  1028. const PRACOptions &opts, char **args)
  1029. {
  1030. nbits_t depth=6;
  1031. if (*args) {
  1032. depth = atoi(*args);
  1033. ++args;
  1034. }
  1035. address_t len = (1<<depth);
  1036. if (*args) {
  1037. len = atoi(*args);
  1038. ++args;
  1039. }
  1040. MPCTIO tio(mpcio, 0, opts.num_threads);
  1041. run_coroutines(tio, [&tio, depth, len] (yield_t &yield) {
  1042. address_t size = address_t(1)<<depth;
  1043. // size_t &aes_ops = tio.aes_ops();
  1044. Duoram<RegAS> oram(tio.player(), size);
  1045. auto A = oram.flat(tio, yield);
  1046. A.explicitonly(true);
  1047. // Initialize the memory to random values in parallel
  1048. std::vector<coro_t> coroutines;
  1049. for (address_t i=0; i<size; ++i) {
  1050. coroutines.emplace_back(
  1051. [&A, i](yield_t &yield) {
  1052. auto Acoro = A.context(yield);
  1053. RegAS v;
  1054. v.randomize(62);
  1055. Acoro[i] += v;
  1056. });
  1057. }
  1058. run_coroutines(yield, coroutines);
  1059. A.bitonic_sort(0, len);
  1060. if (depth <= 10) {
  1061. oram.dump();
  1062. }
  1063. auto check = A.reconstruct();
  1064. bool fail = false;
  1065. if (tio.player() == 0) {
  1066. for (address_t i=0;i<size;++i) {
  1067. if (depth <= 10) {
  1068. printf("%04x %016lx\n", i, check[i].share());
  1069. }
  1070. if (i>0 && i<len &&
  1071. check[i].share() < check[i-1].share()) {
  1072. fail = true;
  1073. }
  1074. }
  1075. if (fail) {
  1076. printf("FAIL\n");
  1077. } else {
  1078. printf("PASS\n");
  1079. }
  1080. }
  1081. });
  1082. }
  1083. static void pad_test(MPCIO &mpcio,
  1084. const PRACOptions &opts, char **args)
  1085. {
  1086. nbits_t depth=6;
  1087. if (*args) {
  1088. depth = atoi(*args);
  1089. ++args;
  1090. }
  1091. address_t len = (1<<depth);
  1092. if (*args) {
  1093. len = atoi(*args);
  1094. ++args;
  1095. }
  1096. MPCTIO tio(mpcio, 0, opts.num_threads);
  1097. run_coroutines(tio, [&mpcio, &tio, depth, len] (yield_t &yield) {
  1098. int player = tio.player();
  1099. Duoram<RegAS> oram(player, len);
  1100. auto A = oram.flat(tio, yield);
  1101. // Initialize the ORAM in explicit mode
  1102. A.explicitonly(true);
  1103. for (address_t i=0; i<len; ++i) {
  1104. RegAS v;
  1105. v.set((player*0xffff+1)*i);
  1106. A[i] = v;
  1107. }
  1108. A.explicitonly(false);
  1109. // Obliviously add 0 to A[0], which reblinds the whole database
  1110. RegAS z;
  1111. A[z] += z;
  1112. auto check = A.reconstruct();
  1113. if (player == 0) {
  1114. for (address_t i=0;i<len;++i) {
  1115. if (depth <= 10) {
  1116. printf("%04x %016lx\n", i, check[i].share());
  1117. }
  1118. }
  1119. printf("\n");
  1120. }
  1121. address_t maxsize = address_t(1)<<depth;
  1122. Duoram<RegAS>::Pad P(A, tio, yield, maxsize);
  1123. for (address_t i=0; i<maxsize; ++i) {
  1124. RegAS v = P[i];
  1125. if (depth <= 10) {
  1126. value_t vval = mpc_reconstruct(tio, yield, v);
  1127. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1128. }
  1129. }
  1130. printf("\n");
  1131. for (address_t i=0; i<maxsize; ++i) {
  1132. value_t offset = 0xdeadbeef;
  1133. if (player) {
  1134. offset = -offset;
  1135. }
  1136. RegAS ind;
  1137. ind.set(player*i+offset);
  1138. RegAS v = P[ind];
  1139. if (depth <= 10) {
  1140. value_t vval = mpc_reconstruct(tio, yield, v);
  1141. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1142. }
  1143. }
  1144. printf("\n");
  1145. });
  1146. }
  1147. static void bsearch_test(MPCIO &mpcio,
  1148. const PRACOptions &opts, char **args, bool basic)
  1149. {
  1150. value_t target;
  1151. arc4random_buf(&target, sizeof(target));
  1152. target >>= 1;
  1153. nbits_t depth=6;
  1154. if (*args) {
  1155. depth = atoi(*args);
  1156. ++args;
  1157. }
  1158. address_t len = (1<<depth);
  1159. if (*args) {
  1160. len = atoi(*args);
  1161. ++args;
  1162. }
  1163. if (*args) {
  1164. target = strtoull(*args, NULL, 16);
  1165. ++args;
  1166. }
  1167. MPCTIO tio(mpcio, 0, opts.num_threads);
  1168. run_coroutines(tio, [&tio, &mpcio, depth, len, target, basic] (yield_t &yield) {
  1169. RegAS tshare;
  1170. std::cout << "\n===== SETUP =====\n";
  1171. if (tio.player() == 2) {
  1172. // Send shares of the target to the computational
  1173. // players
  1174. RegAS tshare0, tshare1;
  1175. tshare0.randomize();
  1176. tshare1.set(target-tshare0.share());
  1177. tio.iostream_p0() << tshare0;
  1178. tio.iostream_p1() << tshare1;
  1179. printf("Using target = %016lx\n", target);
  1180. yield();
  1181. } else {
  1182. // Get the share of the target
  1183. tio.iostream_server() >> tshare;
  1184. }
  1185. tio.sync_lamport();
  1186. mpcio.dump_stats(std::cout);
  1187. std::cout << "\n===== SORT RANDOM DATABASE =====\n";
  1188. mpcio.reset_stats();
  1189. tio.reset_lamport();
  1190. // Create a random database and sort it
  1191. // size_t &aes_ops = tio.aes_ops();
  1192. Duoram<RegAS> oram(tio.player(), len);
  1193. auto A = oram.flat(tio, yield);
  1194. A.explicitonly(true);
  1195. // Initialize the memory to random values in parallel
  1196. std::vector<coro_t> coroutines;
  1197. for (address_t i=0; i<len; ++i) {
  1198. coroutines.emplace_back(
  1199. [&A, i](yield_t &yield) {
  1200. auto Acoro = A.context(yield);
  1201. RegAS v;
  1202. v.randomize(62);
  1203. Acoro[i] += v;
  1204. });
  1205. }
  1206. run_coroutines(yield, coroutines);
  1207. A.bitonic_sort(0, len);
  1208. A.explicitonly(false);
  1209. tio.sync_lamport();
  1210. mpcio.dump_stats(std::cout);
  1211. std::cout << "\n===== BINARY SEARCH =====\n";
  1212. mpcio.reset_stats();
  1213. tio.reset_lamport();
  1214. // Binary search for the target
  1215. value_t checkindex;
  1216. if (basic) {
  1217. RegAS tindex = A.basic_binary_search(tshare);
  1218. checkindex = mpc_reconstruct(tio, yield, tindex);
  1219. } else {
  1220. RegXS tindex = A.binary_search(tshare);
  1221. checkindex = mpc_reconstruct(tio, yield, tindex);
  1222. }
  1223. tio.sync_lamport();
  1224. mpcio.dump_stats(std::cout);
  1225. std::cout << "\n===== CHECK ANSWER =====\n";
  1226. mpcio.reset_stats();
  1227. tio.reset_lamport();
  1228. // Check the answer
  1229. size_t size = size_t(1) << depth;
  1230. value_t checktarget = mpc_reconstruct(tio, yield, tshare);
  1231. auto check = A.reconstruct();
  1232. bool fail = false;
  1233. if (tio.player() == 0) {
  1234. for (address_t i=0;i<len;++i) {
  1235. if (depth <= 10) {
  1236. printf("%c%04x %016lx\n",
  1237. (i == checkindex ? '*' : ' '),
  1238. i, check[i].share());
  1239. }
  1240. if (i>0 && i<len &&
  1241. check[i].share() < check[i-1].share()) {
  1242. fail = true;
  1243. }
  1244. if (i == checkindex) {
  1245. // check[i] should be >= target, and check[i-1]
  1246. // should be < target
  1247. if ((i < len && check[i].share() < checktarget) ||
  1248. (i > 0 && check[i-1].share() >= checktarget)) {
  1249. fail = true;
  1250. }
  1251. }
  1252. }
  1253. if (checkindex == len && check[len-1].share() >= checktarget) {
  1254. fail = true;
  1255. }
  1256. printf("Target = %016lx\n", checktarget);
  1257. printf("Found index = %02lx\n", checkindex);
  1258. if (checkindex > size) {
  1259. fail = true;
  1260. }
  1261. if (fail) {
  1262. printf("FAIL\n");
  1263. } else {
  1264. printf("PASS\n");
  1265. }
  1266. }
  1267. });
  1268. }
  1269. template <typename T>
  1270. static void related(MPCIO &mpcio,
  1271. const PRACOptions &opts, char **args)
  1272. {
  1273. nbits_t depth = 5;
  1274. // The depth of the (complete) binary tree
  1275. if (*args) {
  1276. depth = atoi(*args);
  1277. ++args;
  1278. }
  1279. // The layer at which to choose a random parent node (and its two
  1280. // children along with it)
  1281. nbits_t layer = depth-1;
  1282. if (*args) {
  1283. layer = atoi(*args);
  1284. ++args;
  1285. }
  1286. assert(layer < depth);
  1287. MPCTIO tio(mpcio, 0, opts.num_threads);
  1288. run_coroutines(tio, [&mpcio, &tio, depth, layer] (yield_t &yield) {
  1289. size_t size = size_t(1)<<(depth+1);
  1290. Duoram<T> oram(tio.player(), size);
  1291. auto A = oram.flat(tio, yield);
  1292. // Initialize A with words with sequential top and bottom halves
  1293. // (just so we can more easily eyeball the right answers)
  1294. A.init([] (size_t i) { return i * 0x100000001; } );
  1295. // We use this layout for the tree:
  1296. // A[0] is unused
  1297. // A[1] is the root (layer 0)
  1298. // A[2..3] is layer 1
  1299. // A[4..7] is layer 2
  1300. // ...
  1301. // A[(1<<j)..((2<<j)-1)] is layer j
  1302. //
  1303. // So the parent of x is at location (x/2) and the children of x
  1304. // are at locations 2*x and 2*x+1
  1305. // Pick a random index _within_ the given layer (i.e., the
  1306. // offset from the beginning of the layer, not the absolute
  1307. // location in A)
  1308. RegXS idx;
  1309. idx.randomize(layer);
  1310. // Create the OblivIndex. RegXS is the type of the common index
  1311. // (idx), 3 is the maximum number of related updates to support
  1312. // (which equals the width of the underlying RDPF, currently
  1313. // maximum 5), layer is the depth of the underlying RDPF (the
  1314. // bit length of idx).
  1315. typename Duoram<T>::template OblivIndex<RegXS,3> oidx(tio, yield, idx, layer);
  1316. // This is the (known) layer containing the (unknown) parent
  1317. // node
  1318. typename Duoram<T>::Flat P(A, tio, yield, 1<<layer, 1<<layer);
  1319. // This is the layer below that one, containing all possible
  1320. // children
  1321. typename Duoram<T>::Flat C(A, tio, yield, 2<<layer, 2<<layer);
  1322. // These are the subsets of C containing the left children and
  1323. // the right children respectively
  1324. typename Duoram<T>::Stride L(C, tio, yield, 0, 2);
  1325. typename Duoram<T>::Stride R(C, tio, yield, 1, 2);
  1326. T parent, left, right;
  1327. // Do three related reads. In this version, only one DPF will
  1328. // be used, but it will still be _evaluated_ three times.
  1329. parent = P[oidx];
  1330. left = L[oidx];
  1331. right = R[oidx];
  1332. // The operation is just a simple rotation: the value in the
  1333. // parent moves to the left child, the left child moves to the
  1334. // right child, and the right child becomes the parent
  1335. // Do three related updates. As above, only one (wide) DPF will
  1336. // be used (the same one as for the reads in fact), but it will
  1337. // still be _evaluated_ three more times.
  1338. P[oidx] += right-parent;
  1339. L[oidx] += parent-left;
  1340. R[oidx] += left-right;
  1341. // Check the answer
  1342. auto check = A.reconstruct();
  1343. if (depth <= 10) {
  1344. oram.dump();
  1345. if (tio.player() == 0) {
  1346. for (address_t i=0;i<size;++i) {
  1347. printf("%04x %016lx\n", i, check[i].share());
  1348. }
  1349. }
  1350. }
  1351. value_t pval = mpc_reconstruct(tio, yield, parent);
  1352. value_t lval = mpc_reconstruct(tio, yield, left);
  1353. value_t rval = mpc_reconstruct(tio, yield, right);
  1354. printf("parent = %016lx\nleft = %016lx\nright = %016lx\n",
  1355. pval, lval, rval);
  1356. });
  1357. }
  1358. template <typename T>
  1359. static void path(MPCIO &mpcio,
  1360. const PRACOptions &opts, char **args)
  1361. {
  1362. nbits_t depth = 5;
  1363. // The depth of the (complete) binary tree
  1364. if (*args) {
  1365. depth = atoi(*args);
  1366. ++args;
  1367. }
  1368. // The target node
  1369. size_t target_node = 3 << (depth-1);
  1370. if (*args) {
  1371. target_node = atoi(*args);
  1372. ++args;
  1373. }
  1374. MPCTIO tio(mpcio, 0, opts.num_threads);
  1375. run_coroutines(tio, [&mpcio, &tio, depth, target_node] (yield_t &yield) {
  1376. size_t size = size_t(1)<<(depth+1);
  1377. Duoram<T> oram(tio.player(), size);
  1378. auto A = oram.flat(tio, yield);
  1379. // Initialize A with words with sequential top and bottom halves
  1380. // (just so we can more easily eyeball the right answers)
  1381. A.init([] (size_t i) { return i * 0x100000001; } );
  1382. // We use this layout for the tree:
  1383. // A[0] is unused
  1384. // A[1] is the root (layer 0)
  1385. // A[2..3] is layer 1
  1386. // A[4..7] is layer 2
  1387. // ...
  1388. // A[(1<<j)..((2<<j)-1)] is layer j
  1389. //
  1390. // So the parent of x is at location (x/2) and the children of x
  1391. // are at locations 2*x and 2*x+1
  1392. // Create a Path from the root to the target node
  1393. typename Duoram<T>::Path P(A, tio, yield, target_node);
  1394. // Re-initialize that path to something recognizable
  1395. P.init([] (size_t i) { return 0xff + i * 0x1000000010000; } );
  1396. // ORAM update along that path
  1397. RegXS idx;
  1398. idx.set(tio.player() * arc4random_uniform(P.size()));
  1399. T val;
  1400. val.set(tio.player() * 0xaaaa00000000);
  1401. P[idx] += val;
  1402. // Binary search along that path
  1403. T lookup;
  1404. lookup.set(tio.player() * 0x3000000000000);
  1405. RegXS foundidx = P.binary_search(lookup);
  1406. // Check the answer
  1407. auto check = A.reconstruct();
  1408. if (depth <= 10) {
  1409. oram.dump();
  1410. if (tio.player() == 0) {
  1411. for (address_t i=0;i<size;++i) {
  1412. printf("%04x %016lx\n", i, check[i].share());
  1413. }
  1414. }
  1415. }
  1416. value_t found = mpc_reconstruct(tio, yield, foundidx);
  1417. printf("foundidx = %lu\n", found);
  1418. });
  1419. }
  1420. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  1421. {
  1422. MPCTIO tio(mpcio, 0);
  1423. if (!*args) {
  1424. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  1425. return;
  1426. } else if (!strcmp(*args, "test")) {
  1427. ++args;
  1428. online_test(mpcio, opts, args);
  1429. } else if (!strcmp(*args, "lamporttest")) {
  1430. ++args;
  1431. lamport_test(mpcio, opts, args);
  1432. } else if (!strcmp(*args, "rdpftest")) {
  1433. ++args;
  1434. rdpf_test<1>(mpcio, opts, args, false);
  1435. } else if (!strcmp(*args, "rdpftest2")) {
  1436. ++args;
  1437. rdpf_test<2>(mpcio, opts, args, false);
  1438. } else if (!strcmp(*args, "rdpftest3")) {
  1439. ++args;
  1440. rdpf_test<3>(mpcio, opts, args, false);
  1441. } else if (!strcmp(*args, "rdpftest4")) {
  1442. ++args;
  1443. rdpf_test<4>(mpcio, opts, args, false);
  1444. } else if (!strcmp(*args, "rdpftest5")) {
  1445. ++args;
  1446. rdpf_test<5>(mpcio, opts, args, false);
  1447. } else if (!strcmp(*args, "irdpftest")) {
  1448. ++args;
  1449. rdpf_test<1>(mpcio, opts, args, true);
  1450. } else if (!strcmp(*args, "irdpftest2")) {
  1451. ++args;
  1452. rdpf_test<2>(mpcio, opts, args, true);
  1453. } else if (!strcmp(*args, "irdpftest3")) {
  1454. ++args;
  1455. rdpf_test<3>(mpcio, opts, args, true);
  1456. } else if (!strcmp(*args, "irdpftest4")) {
  1457. ++args;
  1458. rdpf_test<4>(mpcio, opts, args, true);
  1459. } else if (!strcmp(*args, "irdpftest5")) {
  1460. ++args;
  1461. rdpf_test<5>(mpcio, opts, args, true);
  1462. } else if (!strcmp(*args, "rdpftime")) {
  1463. ++args;
  1464. rdpf_timing(mpcio, opts, args);
  1465. } else if (!strcmp(*args, "evaltime")) {
  1466. ++args;
  1467. rdpfeval_timing(mpcio, opts, args);
  1468. } else if (!strcmp(*args, "parevaltime")) {
  1469. ++args;
  1470. par_rdpfeval_timing(mpcio, opts, args);
  1471. } else if (!strcmp(*args, "tupletime")) {
  1472. ++args;
  1473. tupleeval_timing(mpcio, opts, args);
  1474. } else if (!strcmp(*args, "partupletime")) {
  1475. ++args;
  1476. par_tupleeval_timing(mpcio, opts, args);
  1477. } else if (!strcmp(*args, "duotest")) {
  1478. ++args;
  1479. if (opts.use_xor_db) {
  1480. duoram_test<RegXS>(mpcio, opts, args);
  1481. } else {
  1482. duoram_test<RegAS>(mpcio, opts, args);
  1483. }
  1484. } else if (!strcmp(*args, "cdpftest")) {
  1485. ++args;
  1486. cdpf_test(mpcio, opts, args);
  1487. } else if (!strcmp(*args, "cmptest")) {
  1488. ++args;
  1489. compare_test(mpcio, opts, args);
  1490. } else if (!strcmp(*args, "sorttest")) {
  1491. ++args;
  1492. sort_test(mpcio, opts, args);
  1493. } else if (!strcmp(*args, "padtest")) {
  1494. ++args;
  1495. pad_test(mpcio, opts, args);
  1496. } else if (!strcmp(*args, "bbsearch")) {
  1497. ++args;
  1498. bsearch_test(mpcio, opts, args, true);
  1499. } else if (!strcmp(*args, "bsearch")) {
  1500. ++args;
  1501. bsearch_test(mpcio, opts, args, false);
  1502. } else if (!strcmp(*args, "duoram")) {
  1503. ++args;
  1504. if (opts.use_xor_db) {
  1505. duoram<RegXS>(mpcio, opts, args);
  1506. } else {
  1507. duoram<RegAS>(mpcio, opts, args);
  1508. }
  1509. } else if (!strcmp(*args, "related")) {
  1510. ++args;
  1511. if (opts.use_xor_db) {
  1512. related<RegXS>(mpcio, opts, args);
  1513. } else {
  1514. related<RegAS>(mpcio, opts, args);
  1515. }
  1516. } else if (!strcmp(*args, "path")) {
  1517. ++args;
  1518. path<RegAS>(mpcio, opts, args);
  1519. } else if (!strcmp(*args, "cell")) {
  1520. ++args;
  1521. cell(mpcio, opts, args);
  1522. }
  1523. else if (!strcmp(*args, "heap")) {
  1524. ++args;
  1525. Heap(mpcio, opts, args);
  1526. } else {
  1527. std::cerr << "Unknown mode " << *args << "\n";
  1528. }
  1529. }