online.cpp 54 KB

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