online.cpp 59 KB

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