online.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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\n", level);
  249. dpf.depth(level);
  250. }
  251. typename RDPF<WIDTH>::RegXSW peer_scaled_xor;
  252. typename RDPF<WIDTH>::RegASW peer_scaled_sum;
  253. if (tio.player() == 1) {
  254. tio.iostream_peer() <<
  255. dpf.li[depth-level].scaled_xor <<
  256. dpf.li[depth-level].scaled_sum;
  257. } else {
  258. tio.iostream_peer() >> peer_scaled_xor >> peer_scaled_sum;
  259. peer_scaled_sum += dpf.li[depth-level].scaled_sum;
  260. peer_scaled_xor ^= dpf.li[depth-level].scaled_xor;
  261. }
  262. for (address_t x=0;x<(address_t(1)<<level);++x) {
  263. typename RDPF<WIDTH>::LeafNode leaf = dpf.leaf(x, aes_ops);
  264. RegBS ub = dpf.unit_bs(leaf);
  265. RegAS ua = dpf.unit_as(leaf);
  266. typename RDPF<WIDTH>::RegXSW sx = dpf.scaled_xs(leaf);
  267. typename RDPF<WIDTH>::RegASW sa = dpf.scaled_as(leaf);
  268. printf("%04x %x %016lx", x, ub.bshare, ua.ashare);
  269. for (nbits_t j=0;j<WIDTH;++j) {
  270. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  271. }
  272. printf("\n");
  273. if (tio.player() == 1) {
  274. tio.iostream_peer() << ub << ua << sx << sa;
  275. } else {
  276. RegBS peer_ub;
  277. RegAS peer_ua;
  278. typename RDPF<WIDTH>::RegXSW peer_sx;
  279. typename RDPF<WIDTH>::RegASW peer_sa;
  280. tio.iostream_peer() >> peer_ub >> peer_ua >>
  281. peer_sx >> peer_sa;
  282. ub ^= peer_ub;
  283. ua += peer_ua;
  284. sx ^= peer_sx;
  285. sa += peer_sa;
  286. bool is_nonzero = ub.bshare || ua.ashare;
  287. for (nbits_t j=0;j<WIDTH;++j) {
  288. is_nonzero |= (sx[j].xshare || sa[j].ashare);
  289. }
  290. if (is_nonzero) {
  291. printf("**** %x %016lx", ub.bshare, ua.ashare);
  292. for (nbits_t j=0;j<WIDTH;++j) {
  293. printf(" %016lx %016lx", sx[j].xshare, sa[j].ashare);
  294. }
  295. printf("\nSCALE ");
  296. for (nbits_t j=0;j<WIDTH;++j) {
  297. printf(" %016lx %016lx",
  298. peer_scaled_xor[j].xshare,
  299. peer_scaled_sum[j].ashare);
  300. }
  301. printf("\n");
  302. }
  303. }
  304. }
  305. printf("\n");
  306. }
  307. }
  308. }
  309. }
  310. });
  311. }
  312. static void rdpf_timing(MPCIO &mpcio,
  313. const PRACOptions &opts, char **args)
  314. {
  315. nbits_t depth=6;
  316. if (*args) {
  317. depth = atoi(*args);
  318. ++args;
  319. }
  320. int num_threads = opts.num_threads;
  321. boost::asio::thread_pool pool(num_threads);
  322. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  323. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  324. MPCTIO tio(mpcio, thread_num);
  325. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  326. size_t &aes_ops = tio.aes_ops();
  327. if (tio.player() == 2) {
  328. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  329. for (int i=0;i<2;++i) {
  330. RDPF<1> &dpf = dp.dpf[i];
  331. dpf.expand(aes_ops);
  332. RDPF<1>::RegXSW scaled_xor;
  333. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  334. RDPF<1>::LeafNode leaf = dpf.leaf(x, aes_ops);
  335. RDPF<1>::RegXSW sx = dpf.scaled_xs(leaf);
  336. scaled_xor ^= sx;
  337. }
  338. printf("%016lx\n%016lx\n", scaled_xor[0].xshare,
  339. dpf.li[0].scaled_xor[0].xshare);
  340. printf("\n");
  341. }
  342. } else {
  343. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  344. for (int i=0;i<3;++i) {
  345. RDPF<1> &dpf = dt.dpf[i];
  346. dpf.expand(aes_ops);
  347. RDPF<1>::RegXSW scaled_xor;
  348. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  349. RDPF<1>::LeafNode leaf = dpf.leaf(x, aes_ops);
  350. RDPF<1>::RegXSW sx = dpf.scaled_xs(leaf);
  351. scaled_xor ^= sx;
  352. }
  353. printf("%016lx\n%016lx\n", scaled_xor[0].xshare,
  354. dpf.li[0].scaled_xor[0].xshare);
  355. printf("\n");
  356. }
  357. }
  358. });
  359. });
  360. }
  361. pool.join();
  362. }
  363. static value_t parallel_streameval_rdpf(MPCIO &mpcio, const RDPF<1> &dpf,
  364. address_t start, int num_threads)
  365. {
  366. RDPF<1>::RegXSW scaled_xor[num_threads];
  367. boost::asio::thread_pool pool(num_threads);
  368. address_t totsize = (address_t(1)<<dpf.depth());
  369. address_t threadstart = start;
  370. address_t threadchunk = totsize / num_threads;
  371. address_t threadextra = totsize % num_threads;
  372. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  373. address_t threadsize = threadchunk + (address_t(thread_num) < threadextra);
  374. boost::asio::post(pool,
  375. [&mpcio, &dpf, &scaled_xor, thread_num, threadstart, threadsize] {
  376. MPCTIO tio(mpcio, thread_num);
  377. //printf("Thread %d from %X for %X\n", thread_num, threadstart, threadsize);
  378. RDPF<1>::RegXSW local_xor;
  379. size_t local_aes_ops = 0;
  380. auto ev = StreamEval(dpf, threadstart, 0, local_aes_ops);
  381. for (address_t x=0;x<threadsize;++x) {
  382. //if (x%0x10000 == 0) printf("%d", thread_num);
  383. RDPF<1>::LeafNode leaf = ev.next();
  384. local_xor ^= dpf.scaled_xs(leaf);
  385. }
  386. scaled_xor[thread_num] = local_xor;
  387. tio.aes_ops() += local_aes_ops;
  388. //printf("Thread %d complete\n", thread_num);
  389. });
  390. threadstart = (threadstart + threadsize) % totsize;
  391. }
  392. pool.join();
  393. RDPF<1>::RegXSW res;
  394. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  395. res ^= scaled_xor[thread_num];
  396. }
  397. return res[0].xshare;
  398. }
  399. static void rdpfeval_timing(MPCIO &mpcio,
  400. const PRACOptions &opts, char **args)
  401. {
  402. nbits_t depth=6;
  403. address_t start=0;
  404. if (*args) {
  405. depth = atoi(*args);
  406. ++args;
  407. }
  408. if (*args) {
  409. start = strtoull(*args, NULL, 16);
  410. ++args;
  411. }
  412. int num_threads = opts.num_threads;
  413. MPCTIO tio(mpcio, 0, num_threads);
  414. run_coroutines(tio, [&mpcio, &tio, depth, start, num_threads] (yield_t &yield) {
  415. if (tio.player() == 2) {
  416. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  417. for (int i=0;i<2;++i) {
  418. RDPF<1> &dpf = dp.dpf[i];
  419. value_t scaled_xor =
  420. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  421. printf("%016lx\n%016lx\n", scaled_xor,
  422. dpf.li[0].scaled_xor[0].xshare);
  423. printf("\n");
  424. }
  425. } else {
  426. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  427. for (int i=0;i<3;++i) {
  428. RDPF<1> &dpf = dt.dpf[i];
  429. value_t scaled_xor =
  430. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  431. printf("%016lx\n%016lx\n", scaled_xor,
  432. dpf.li[0].scaled_xor[0].xshare);
  433. printf("\n");
  434. }
  435. }
  436. });
  437. }
  438. static void par_rdpfeval_timing(MPCIO &mpcio,
  439. const PRACOptions &opts, char **args)
  440. {
  441. nbits_t depth=6;
  442. address_t start=0;
  443. if (*args) {
  444. depth = atoi(*args);
  445. ++args;
  446. }
  447. if (*args) {
  448. start = strtoull(*args, NULL, 16);
  449. ++args;
  450. }
  451. int num_threads = opts.num_threads;
  452. MPCTIO tio(mpcio, 0, num_threads);
  453. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  454. if (tio.player() == 2) {
  455. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  456. for (int i=0;i<2;++i) {
  457. RDPF<1> &dpf = dp.dpf[i];
  458. nbits_t depth = dpf.depth();
  459. auto pe = ParallelEval(dpf, start, 0,
  460. address_t(1)<<depth, num_threads, tio.aes_ops());
  461. RDPF<1>::RegXSW result, init;
  462. result = pe.reduce(init, [&dpf] (int thread_num,
  463. address_t i, const RDPF<1>::LeafNode &leaf) {
  464. return dpf.scaled_xs(leaf);
  465. });
  466. printf("%016lx\n%016lx\n", result[0].xshare,
  467. dpf.li[0].scaled_xor[0].xshare);
  468. printf("\n");
  469. }
  470. } else {
  471. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  472. for (int i=0;i<3;++i) {
  473. RDPF<1> &dpf = dt.dpf[i];
  474. nbits_t depth = dpf.depth();
  475. auto pe = ParallelEval(dpf, start, 0,
  476. address_t(1)<<depth, num_threads, tio.aes_ops());
  477. RDPF<1>::RegXSW result, init;
  478. result = pe.reduce(init, [&dpf] (int thread_num,
  479. address_t i, const RDPF<1>::LeafNode &leaf) {
  480. return dpf.scaled_xs(leaf);
  481. });
  482. printf("%016lx\n%016lx\n", result[0].xshare,
  483. dpf.li[0].scaled_xor[0].xshare);
  484. printf("\n");
  485. }
  486. }
  487. });
  488. }
  489. static void tupleeval_timing(MPCIO &mpcio,
  490. const PRACOptions &opts, char **args)
  491. {
  492. nbits_t depth=6;
  493. address_t start=0;
  494. if (*args) {
  495. depth = atoi(*args);
  496. ++args;
  497. }
  498. if (*args) {
  499. start = atoi(*args);
  500. ++args;
  501. }
  502. int num_threads = opts.num_threads;
  503. MPCTIO tio(mpcio, 0, num_threads);
  504. run_coroutines(tio, [&tio, depth, start] (yield_t &yield) {
  505. size_t &aes_ops = tio.aes_ops();
  506. if (tio.player() == 2) {
  507. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  508. RDPF<1>::RegXSW scaled_xor0, scaled_xor1;
  509. auto ev = StreamEval(dp, start, 0, aes_ops, false);
  510. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  511. auto [L0, L1] = ev.next();
  512. RDPF<1>::RegXSW sx0 = dp.dpf[0].scaled_xs(L0);
  513. RDPF<1>::RegXSW sx1 = dp.dpf[1].scaled_xs(L1);
  514. scaled_xor0 ^= sx0;
  515. scaled_xor1 ^= sx1;
  516. }
  517. printf("%016lx\n%016lx\n", scaled_xor0[0].xshare,
  518. dp.dpf[0].li[0].scaled_xor[0].xshare);
  519. printf("\n");
  520. printf("%016lx\n%016lx\n", scaled_xor1[0].xshare,
  521. dp.dpf[1].li[0].scaled_xor[0].xshare);
  522. printf("\n");
  523. } else {
  524. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  525. RDPF<1>::RegXSW scaled_xor0, scaled_xor1, scaled_xor2;
  526. auto ev = StreamEval(dt, start, 0, aes_ops, false);
  527. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  528. auto [L0, L1, L2] = ev.next();
  529. RDPF<1>::RegXSW sx0 = dt.dpf[0].scaled_xs(L0);
  530. RDPF<1>::RegXSW sx1 = dt.dpf[1].scaled_xs(L1);
  531. RDPF<1>::RegXSW sx2 = dt.dpf[2].scaled_xs(L2);
  532. scaled_xor0 ^= sx0;
  533. scaled_xor1 ^= sx1;
  534. scaled_xor2 ^= sx2;
  535. }
  536. printf("%016lx\n%016lx\n", scaled_xor0[0].xshare,
  537. dt.dpf[0].li[0].scaled_xor[0].xshare);
  538. printf("\n");
  539. printf("%016lx\n%016lx\n", scaled_xor1[0].xshare,
  540. dt.dpf[1].li[0].scaled_xor[0].xshare);
  541. printf("\n");
  542. printf("%016lx\n%016lx\n", scaled_xor2[0].xshare,
  543. dt.dpf[2].li[0].scaled_xor[0].xshare);
  544. printf("\n");
  545. }
  546. });
  547. }
  548. static void par_tupleeval_timing(MPCIO &mpcio,
  549. const PRACOptions &opts, char **args)
  550. {
  551. nbits_t depth=6;
  552. address_t start=0;
  553. if (*args) {
  554. depth = atoi(*args);
  555. ++args;
  556. }
  557. if (*args) {
  558. start = atoi(*args);
  559. ++args;
  560. }
  561. int num_threads = opts.num_threads;
  562. MPCTIO tio(mpcio, 0, num_threads);
  563. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  564. size_t &aes_ops = tio.aes_ops();
  565. if (tio.player() == 2) {
  566. RDPFPair<1> dp = tio.rdpfpair(yield, depth);
  567. auto pe = ParallelEval(dp, start, 0, address_t(1)<<depth,
  568. num_threads, aes_ops);
  569. RDPFPair<1>::RegXSWP result, init;
  570. result = pe.reduce(init, [&dp] (int thread_num, address_t i,
  571. const RDPFPair<1>::LeafNode &leaf) {
  572. RDPFPair<1>::RegXSWP scaled;
  573. dp.scaled(scaled, leaf);
  574. return scaled;
  575. });
  576. printf("%016lx\n%016lx\n", std::get<0>(result)[0].xshare,
  577. dp.dpf[0].li[0].scaled_xor[0].xshare);
  578. printf("\n");
  579. printf("%016lx\n%016lx\n", std::get<1>(result)[0].xshare,
  580. dp.dpf[1].li[0].scaled_xor[0].xshare);
  581. printf("\n");
  582. } else {
  583. RDPFTriple<1> dt = tio.rdpftriple(yield, depth);
  584. auto pe = ParallelEval(dt, start, 0, address_t(1)<<depth,
  585. num_threads, aes_ops);
  586. RDPFTriple<1>::RegXSWT result, init;
  587. result = pe.reduce(init, [&dt] (int thread_num, address_t i,
  588. const RDPFTriple<1>::LeafNode &leaf) {
  589. RDPFTriple<1>::RegXSWT scaled;
  590. dt.scaled(scaled, leaf);
  591. return scaled;
  592. });
  593. printf("%016lx\n%016lx\n", std::get<0>(result)[0].xshare,
  594. dt.dpf[0].li[0].scaled_xor[0].xshare);
  595. printf("\n");
  596. printf("%016lx\n%016lx\n", std::get<1>(result)[0].xshare,
  597. dt.dpf[1].li[0].scaled_xor[0].xshare);
  598. printf("\n");
  599. printf("%016lx\n%016lx\n", std::get<2>(result)[0].xshare,
  600. dt.dpf[2].li[0].scaled_xor[0].xshare);
  601. printf("\n");
  602. }
  603. });
  604. }
  605. // T is RegAS or RegXS for additive or XOR shared database respectively
  606. template <typename T>
  607. static void duoram_test(MPCIO &mpcio,
  608. const PRACOptions &opts, char **args)
  609. {
  610. nbits_t depth=6;
  611. address_t share=arc4random();
  612. if (*args) {
  613. depth = atoi(*args);
  614. ++args;
  615. }
  616. if (*args) {
  617. share = atoi(*args);
  618. ++args;
  619. }
  620. share &= ((address_t(1)<<depth)-1);
  621. address_t len = (1<<depth);
  622. if (*args) {
  623. len = atoi(*args);
  624. ++args;
  625. }
  626. MPCTIO tio(mpcio, 0, opts.num_threads);
  627. run_coroutines(tio, [&tio, depth, share, len] (yield_t &yield) {
  628. // size_t &aes_ops = tio.aes_ops();
  629. Duoram<T> oram(tio.player(), len);
  630. auto A = oram.flat(tio, yield);
  631. RegAS aidx, aidx2, aidx3;
  632. aidx.ashare = share;
  633. aidx2.ashare = share + tio.player();
  634. aidx3.ashare = share + 1;
  635. T M;
  636. if (tio.player() == 0) {
  637. M.set(0xbabb0000);
  638. } else {
  639. M.set(0x0000a66e);
  640. }
  641. RegXS xidx;
  642. xidx.xshare = share;
  643. T N;
  644. if (tio.player() == 0) {
  645. N.set(0xdead0000);
  646. } else {
  647. N.set(0x0000beef);
  648. }
  649. // Writing and reading with additively shared indices
  650. printf("Additive Updating\n");
  651. A[aidx] += M;
  652. printf("Additive Reading\n");
  653. T Aa = A[aidx];
  654. // Writing and reading with XOR shared indices
  655. printf("XOR Updating\n");
  656. A[xidx] += N;
  657. printf("XOR Reading\n");
  658. T Ax = A[xidx];
  659. T Ae;
  660. // Writing and reading with explicit indices
  661. if (depth > 2) {
  662. printf("Explicit Updating\n");
  663. A[5] += Aa;
  664. printf("Explicit Reading\n");
  665. Ae = A[6];
  666. }
  667. // Simultaneous independent reads
  668. printf("3 independent reading\n");
  669. std::vector<T> Av = A[std::array {
  670. aidx, aidx2, aidx3
  671. }];
  672. // Simultaneous independent updates
  673. T Aw1, Aw2, Aw3;
  674. Aw1.set(0x101010101010101 * tio.player());
  675. Aw2.set(0x202020202020202 * tio.player());
  676. Aw3.set(0x303030303030303 * tio.player());
  677. printf("3 independent updating\n");
  678. A[std::array { aidx, aidx2, aidx3 }] -=
  679. std::array { Aw1, Aw2, Aw3 };
  680. if (depth <= 10) {
  681. oram.dump();
  682. auto check = A.reconstruct();
  683. if (tio.player() == 0) {
  684. for (address_t i=0;i<len;++i) {
  685. printf("%04x %016lx\n", i, check[i].share());
  686. }
  687. }
  688. }
  689. auto checkread = A.reconstruct(Aa);
  690. auto checkreade = A.reconstruct(Ae);
  691. auto checkreadx = A.reconstruct(Ax);
  692. if (tio.player() == 0) {
  693. printf("Read AS value = %016lx\n", checkread.share());
  694. printf("Read AX value = %016lx\n", checkreadx.share());
  695. printf("Read Ex value = %016lx\n", checkreade.share());
  696. }
  697. for (auto &v : Av) {
  698. auto checkv = A.reconstruct(v);
  699. if (tio.player() == 0) {
  700. printf("Read Av value = %016lx\n", checkv.share());
  701. }
  702. }
  703. });
  704. }
  705. // This measures the same things as the Duoram paper: dependent and
  706. // independent reads, updates, writes, and interleaves
  707. // T is RegAS or RegXS for additive or XOR shared database respectively
  708. template <typename T>
  709. static void duoram(MPCIO &mpcio,
  710. const PRACOptions &opts, char **args)
  711. {
  712. nbits_t depth = 6;
  713. int items = 4;
  714. if (*args) {
  715. depth = atoi(*args);
  716. ++args;
  717. }
  718. if (*args) {
  719. items = atoi(*args);
  720. ++args;
  721. }
  722. MPCTIO tio(mpcio, 0, opts.num_threads);
  723. run_coroutines(tio, [&mpcio, &tio, depth, items] (yield_t &yield) {
  724. size_t size = size_t(1)<<depth;
  725. address_t mask = (depth < ADDRESS_MAX_BITS ?
  726. ((address_t(1)<<depth) - 1) : ~0);
  727. Duoram<T> oram(tio.player(), size);
  728. auto A = oram.flat(tio, yield);
  729. std::cout << "===== DEPENDENT UPDATES =====\n";
  730. mpcio.reset_stats();
  731. tio.reset_lamport();
  732. // Make a linked list of length items
  733. std::vector<T> list_indices;
  734. T prev_index, next_index;
  735. prev_index.randomize(depth);
  736. for (int i=0;i<items;++i) {
  737. next_index.randomize(depth);
  738. A[next_index] += prev_index;
  739. list_indices.push_back(next_index);
  740. prev_index = next_index;
  741. }
  742. tio.sync_lamport();
  743. mpcio.dump_stats(std::cout);
  744. std::cout << "\n===== DEPENDENT READS =====\n";
  745. mpcio.reset_stats();
  746. tio.reset_lamport();
  747. // Read the linked list starting with prev_index
  748. T cur_index = prev_index;
  749. for (int i=0;i<items;++i) {
  750. cur_index = A[cur_index];
  751. }
  752. tio.sync_lamport();
  753. mpcio.dump_stats(std::cout);
  754. std::cout << "\n===== INDEPENDENT READS =====\n";
  755. mpcio.reset_stats();
  756. tio.reset_lamport();
  757. // Read all the entries in the list at once
  758. std::vector<T> read_outputs = A[list_indices];
  759. tio.sync_lamport();
  760. mpcio.dump_stats(std::cout);
  761. std::cout << "\n===== INDEPENDENT UPDATES =====\n";
  762. mpcio.reset_stats();
  763. tio.reset_lamport();
  764. // Make a vector of indices 1 larger than those in list_indices,
  765. // and a vector of values 1 larger than those in outputs
  766. std::vector<T> indep_indices, indep_values;
  767. T one;
  768. one.set(tio.player()); // Sets the shared value to 1
  769. for (int i=0;i<items;++i) {
  770. indep_indices.push_back(list_indices[i]+one);
  771. indep_values.push_back(read_outputs[i]+one);
  772. }
  773. // Update all the indices at once
  774. A[indep_indices] += indep_values;
  775. tio.sync_lamport();
  776. mpcio.dump_stats(std::cout);
  777. std::cout << "\n===== DEPENDENT WRITES =====\n";
  778. mpcio.reset_stats();
  779. tio.reset_lamport();
  780. T two;
  781. two.set(2*tio.player()); // Sets the shared value to 2
  782. // For each address addr that's number i from the end of the
  783. // linked list, write i+1 into location addr+2
  784. for (int i=0;i<items;++i) {
  785. T val;
  786. val.set((i+1)*tio.player());
  787. A[list_indices[i]+two] = val;
  788. }
  789. tio.sync_lamport();
  790. mpcio.dump_stats(std::cout);
  791. std::cout << "\n===== DEPENDENT INTERLEAVED =====\n";
  792. mpcio.reset_stats();
  793. tio.reset_lamport();
  794. T three;
  795. three.set(3*tio.player()); // Sets the shared value to 3
  796. // Follow the linked list and whenever A[addr]=val, set
  797. // A[addr+3]=val+3
  798. cur_index = prev_index;
  799. for (int i=0;i<items;++i) {
  800. T next_index = A[cur_index];
  801. A[cur_index+three] = next_index+three;
  802. cur_index = next_index;
  803. }
  804. tio.sync_lamport();
  805. mpcio.dump_stats(std::cout);
  806. std::cout << "\n";
  807. mpcio.reset_stats();
  808. tio.reset_lamport();
  809. if (depth <= 30) {
  810. auto check = A.reconstruct();
  811. auto head = A.reconstruct(prev_index);
  812. if (tio.player() == 0) {
  813. int width = (depth+3)/4;
  814. printf("Head of linked list: %0*lx\n\n", width,
  815. head.share() & mask);
  816. std::cout << "Non-zero reconstructed database entries:\n";
  817. for (address_t i=0;i<size;++i) {
  818. value_t share = check[i].share() & mask;
  819. if (share) printf("%0*x: %0*lx\n", width, i, width, share);
  820. }
  821. }
  822. }
  823. });
  824. }
  825. static void cdpf_test(MPCIO &mpcio,
  826. const PRACOptions &opts, char **args)
  827. {
  828. value_t query, target;
  829. int iters = 1;
  830. arc4random_buf(&query, sizeof(query));
  831. arc4random_buf(&target, sizeof(target));
  832. if (*args) {
  833. query = strtoull(*args, NULL, 16);
  834. ++args;
  835. }
  836. if (*args) {
  837. target = strtoull(*args, NULL, 16);
  838. ++args;
  839. }
  840. if (*args) {
  841. iters = atoi(*args);
  842. ++args;
  843. }
  844. int num_threads = opts.num_threads;
  845. boost::asio::thread_pool pool(num_threads);
  846. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  847. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  848. MPCTIO tio(mpcio, thread_num);
  849. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  850. size_t &aes_ops = tio.aes_ops();
  851. for (int i=0;i<iters;++i) {
  852. if (tio.player() == 2) {
  853. tio.cdpf(yield);
  854. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  855. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  856. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  857. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  858. dump_node(leaf0 ^ leaf1);
  859. } else {
  860. CDPF dpf = tio.cdpf(yield);
  861. printf("ashare = %016lX\nxshare = %016lX\n",
  862. dpf.as_target.ashare, dpf.xs_target.xshare);
  863. DPFnode leaf = dpf.leaf(query, aes_ops);
  864. printf("DPF(%016lx) = ", query);
  865. dump_node(leaf);
  866. if (tio.player() == 1) {
  867. tio.iostream_peer() << leaf;
  868. } else {
  869. DPFnode peerleaf;
  870. tio.iostream_peer() >> peerleaf;
  871. printf("XOR = ");
  872. dump_node(leaf ^ peerleaf);
  873. }
  874. }
  875. }
  876. });
  877. });
  878. }
  879. pool.join();
  880. }
  881. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  882. value_t target, value_t x)
  883. {
  884. int player = tio.player();
  885. size_t &aes_ops = tio.aes_ops();
  886. int res = 1;
  887. if (player == 2) {
  888. // Create a CDPF pair with the given target
  889. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  890. // Send it and a share of x to the computational parties
  891. RegAS x0, x1;
  892. x0.randomize();
  893. x1.set(x-x0.share());
  894. tio.iostream_p0() << dpf0 << x0;
  895. tio.iostream_p1() << dpf1 << x1;
  896. } else {
  897. CDPF dpf;
  898. RegAS xsh;
  899. tio.iostream_server() >> dpf >> xsh;
  900. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  901. RegBS eeq = dpf.is_zero(tio, yield, xsh, aes_ops);
  902. printf("%016lx %016lx %d %d %d %d ", target, x, lt.bshare,
  903. eq.bshare, gt.bshare, eeq.bshare);
  904. // Check the answer
  905. if (player == 1) {
  906. tio.iostream_peer() << xsh << lt << eq << gt << eeq;
  907. } else {
  908. RegAS peer_xsh;
  909. RegBS peer_lt, peer_eq, peer_gt, peer_eeq;
  910. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >>
  911. peer_gt >> peer_eeq;
  912. lt ^= peer_lt;
  913. eq ^= peer_eq;
  914. gt ^= peer_gt;
  915. eeq ^= peer_eeq;
  916. xsh += peer_xsh;
  917. int lti = int(lt.bshare);
  918. int eqi = int(eq.bshare);
  919. int gti = int(gt.bshare);
  920. int eeqi = int(eeq.bshare);
  921. x = xsh.share();
  922. printf(": %d %d %d %d ", lti, eqi, gti, eeqi);
  923. bool signbit = (x >> 63);
  924. if (lti + eqi + gti != 1 || eqi != eeqi) {
  925. printf("INCONSISTENT");
  926. res = 0;
  927. } else if (x == 0 && eqi) {
  928. printf("=");
  929. } else if (!signbit && gti) {
  930. printf(">");
  931. } else if (signbit && lti) {
  932. printf("<");
  933. } else {
  934. printf("INCORRECT");
  935. res = 0;
  936. }
  937. }
  938. printf("\n");
  939. }
  940. return res;
  941. }
  942. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  943. value_t target, value_t x)
  944. {
  945. int res = 1;
  946. res &= compare_test_one(tio, yield, target, x);
  947. res &= compare_test_one(tio, yield, target, 0);
  948. res &= compare_test_one(tio, yield, target, 1);
  949. res &= compare_test_one(tio, yield, target, 15);
  950. res &= compare_test_one(tio, yield, target, 16);
  951. res &= compare_test_one(tio, yield, target, 17);
  952. res &= compare_test_one(tio, yield, target, -1);
  953. res &= compare_test_one(tio, yield, target, -15);
  954. res &= compare_test_one(tio, yield, target, -16);
  955. res &= compare_test_one(tio, yield, target, -17);
  956. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  957. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  958. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  959. return res;
  960. }
  961. static void compare_test(MPCIO &mpcio,
  962. const PRACOptions &opts, char **args)
  963. {
  964. value_t target, x;
  965. arc4random_buf(&target, sizeof(target));
  966. arc4random_buf(&x, sizeof(x));
  967. if (*args) {
  968. target = strtoull(*args, NULL, 16);
  969. ++args;
  970. }
  971. if (*args) {
  972. x = strtoull(*args, NULL, 16);
  973. ++args;
  974. }
  975. int num_threads = opts.num_threads;
  976. boost::asio::thread_pool pool(num_threads);
  977. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  978. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  979. MPCTIO tio(mpcio, thread_num);
  980. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  981. int res = 1;
  982. res &= compare_test_target(tio, yield, target, x);
  983. res &= compare_test_target(tio, yield, 0, x);
  984. res &= compare_test_target(tio, yield, 1, x);
  985. res &= compare_test_target(tio, yield, 15, x);
  986. res &= compare_test_target(tio, yield, 16, x);
  987. res &= compare_test_target(tio, yield, 17, x);
  988. res &= compare_test_target(tio, yield, -1, x);
  989. res &= compare_test_target(tio, yield, -15, x);
  990. res &= compare_test_target(tio, yield, -16, x);
  991. res &= compare_test_target(tio, yield, -17, x);
  992. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  993. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  994. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  995. if (tio.player() == 0) {
  996. if (res == 1) {
  997. printf("All tests passed!\n");
  998. } else {
  999. printf("TEST FAILURES\n");
  1000. }
  1001. }
  1002. });
  1003. });
  1004. }
  1005. pool.join();
  1006. }
  1007. static void sort_test(MPCIO &mpcio,
  1008. const PRACOptions &opts, char **args)
  1009. {
  1010. nbits_t depth=6;
  1011. if (*args) {
  1012. depth = atoi(*args);
  1013. ++args;
  1014. }
  1015. address_t len = (1<<depth);
  1016. if (*args) {
  1017. len = atoi(*args);
  1018. ++args;
  1019. }
  1020. MPCTIO tio(mpcio, 0, opts.num_threads);
  1021. run_coroutines(tio, [&tio, depth, len] (yield_t &yield) {
  1022. address_t size = address_t(1)<<depth;
  1023. // size_t &aes_ops = tio.aes_ops();
  1024. Duoram<RegAS> oram(tio.player(), size);
  1025. auto A = oram.flat(tio, yield);
  1026. A.explicitonly(true);
  1027. // Initialize the memory to random values in parallel
  1028. std::vector<coro_t> coroutines;
  1029. for (address_t i=0; i<size; ++i) {
  1030. coroutines.emplace_back(
  1031. [&A, i](yield_t &yield) {
  1032. auto Acoro = A.context(yield);
  1033. RegAS v;
  1034. v.randomize(62);
  1035. Acoro[i] += v;
  1036. });
  1037. }
  1038. run_coroutines(yield, coroutines);
  1039. A.bitonic_sort(0, len);
  1040. if (depth <= 10) {
  1041. oram.dump();
  1042. }
  1043. auto check = A.reconstruct();
  1044. bool fail = false;
  1045. if (tio.player() == 0) {
  1046. for (address_t i=0;i<size;++i) {
  1047. if (depth <= 10) {
  1048. printf("%04x %016lx\n", i, check[i].share());
  1049. }
  1050. if (i>0 && i<len &&
  1051. check[i].share() < check[i-1].share()) {
  1052. fail = true;
  1053. }
  1054. }
  1055. if (fail) {
  1056. printf("FAIL\n");
  1057. } else {
  1058. printf("PASS\n");
  1059. }
  1060. }
  1061. });
  1062. }
  1063. static void pad_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_threads);
  1077. run_coroutines(tio, [&mpcio, &tio, depth, len] (yield_t &yield) {
  1078. int player = tio.player();
  1079. Duoram<RegAS> oram(player, len);
  1080. auto A = oram.flat(tio, yield);
  1081. // Initialize the ORAM in explicit mode
  1082. A.explicitonly(true);
  1083. for (address_t i=0; i<len; ++i) {
  1084. RegAS v;
  1085. v.set((player*0xffff+1)*i);
  1086. A[i] = v;
  1087. }
  1088. A.explicitonly(false);
  1089. // Obliviously add 0 to A[0], which reblinds the whole database
  1090. RegAS z;
  1091. A[z] += z;
  1092. auto check = A.reconstruct();
  1093. if (player == 0) {
  1094. for (address_t i=0;i<len;++i) {
  1095. if (depth <= 10) {
  1096. printf("%04x %016lx\n", i, check[i].share());
  1097. }
  1098. }
  1099. printf("\n");
  1100. }
  1101. address_t maxsize = address_t(1)<<depth;
  1102. Duoram<RegAS>::Pad P(A, tio, yield, maxsize);
  1103. for (address_t i=0; i<maxsize; ++i) {
  1104. RegAS v = P[i];
  1105. if (depth <= 10) {
  1106. value_t vval = mpc_reconstruct(tio, yield, v);
  1107. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1108. }
  1109. }
  1110. printf("\n");
  1111. for (address_t i=0; i<maxsize; ++i) {
  1112. value_t offset = 0xdeadbeef;
  1113. if (player) {
  1114. offset = -offset;
  1115. }
  1116. RegAS ind;
  1117. ind.set(player*i+offset);
  1118. RegAS v = P[ind];
  1119. if (depth <= 10) {
  1120. value_t vval = mpc_reconstruct(tio, yield, v);
  1121. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1122. }
  1123. }
  1124. printf("\n");
  1125. });
  1126. }
  1127. static void bsearch_test(MPCIO &mpcio,
  1128. const PRACOptions &opts, char **args)
  1129. {
  1130. value_t target;
  1131. arc4random_buf(&target, sizeof(target));
  1132. target >>= 1;
  1133. nbits_t depth=6;
  1134. if (*args) {
  1135. depth = atoi(*args);
  1136. ++args;
  1137. }
  1138. address_t len = (1<<depth);
  1139. if (*args) {
  1140. len = atoi(*args);
  1141. ++args;
  1142. }
  1143. if (*args) {
  1144. target = strtoull(*args, NULL, 16);
  1145. ++args;
  1146. }
  1147. MPCTIO tio(mpcio, 0, opts.num_threads);
  1148. run_coroutines(tio, [&tio, depth, len, target] (yield_t &yield) {
  1149. RegAS tshare;
  1150. if (tio.player() == 2) {
  1151. // Send shares of the target to the computational
  1152. // players
  1153. RegAS tshare0, tshare1;
  1154. tshare0.randomize();
  1155. tshare1.set(target-tshare0.share());
  1156. tio.iostream_p0() << tshare0;
  1157. tio.iostream_p1() << tshare1;
  1158. printf("Using target = %016lx\n", target);
  1159. yield();
  1160. } else {
  1161. // Get the share of the target
  1162. tio.iostream_server() >> tshare;
  1163. }
  1164. // Create a random database and sort it
  1165. // size_t &aes_ops = tio.aes_ops();
  1166. Duoram<RegAS> oram(tio.player(), len);
  1167. auto A = oram.flat(tio, yield);
  1168. A.explicitonly(true);
  1169. // Initialize the memory to random values in parallel
  1170. std::vector<coro_t> coroutines;
  1171. for (address_t i=0; i<len; ++i) {
  1172. coroutines.emplace_back(
  1173. [&A, i](yield_t &yield) {
  1174. auto Acoro = A.context(yield);
  1175. RegAS v;
  1176. v.randomize(62);
  1177. Acoro[i] += v;
  1178. });
  1179. }
  1180. run_coroutines(yield, coroutines);
  1181. A.bitonic_sort(0, len);
  1182. A.explicitonly(false);
  1183. // Binary search for the target
  1184. RegAS tindex = A.obliv_binary_search(tshare);
  1185. // Check the answer
  1186. size_t size = size_t(1) << depth;
  1187. value_t checkindex = mpc_reconstruct(tio, yield, tindex);
  1188. value_t checktarget = mpc_reconstruct(tio, yield, tshare);
  1189. auto check = A.reconstruct();
  1190. bool fail = false;
  1191. if (tio.player() == 0) {
  1192. for (address_t i=0;i<len;++i) {
  1193. if (depth <= 10) {
  1194. printf("%c%04x %016lx\n",
  1195. (i == checkindex ? '*' : ' '),
  1196. i, check[i].share());
  1197. }
  1198. if (i>0 && i<len &&
  1199. check[i].share() < check[i-1].share()) {
  1200. fail = true;
  1201. }
  1202. if (i == checkindex) {
  1203. // check[i] should be >= target, and check[i-1]
  1204. // should be < target
  1205. if ((i < len && check[i].share() < checktarget) ||
  1206. (i > 0 && check[i-1].share() >= checktarget)) {
  1207. fail = true;
  1208. }
  1209. }
  1210. }
  1211. printf("Target = %016lx\n", checktarget);
  1212. printf("Found index = %02lx\n", checkindex);
  1213. if (checkindex > size) {
  1214. fail = true;
  1215. }
  1216. if (fail) {
  1217. printf("FAIL\n");
  1218. } else {
  1219. printf("PASS\n");
  1220. }
  1221. }
  1222. });
  1223. }
  1224. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  1225. {
  1226. MPCTIO tio(mpcio, 0);
  1227. if (!*args) {
  1228. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  1229. return;
  1230. } else if (!strcmp(*args, "test")) {
  1231. ++args;
  1232. online_test(mpcio, opts, args);
  1233. } else if (!strcmp(*args, "lamporttest")) {
  1234. ++args;
  1235. lamport_test(mpcio, opts, args);
  1236. } else if (!strcmp(*args, "rdpftest")) {
  1237. ++args;
  1238. rdpf_test<1>(mpcio, opts, args, false);
  1239. } else if (!strcmp(*args, "rdpftest2")) {
  1240. ++args;
  1241. rdpf_test<2>(mpcio, opts, args, false);
  1242. } else if (!strcmp(*args, "rdpftest3")) {
  1243. ++args;
  1244. rdpf_test<3>(mpcio, opts, args, false);
  1245. } else if (!strcmp(*args, "rdpftest4")) {
  1246. ++args;
  1247. rdpf_test<4>(mpcio, opts, args, false);
  1248. } else if (!strcmp(*args, "rdpftest5")) {
  1249. ++args;
  1250. rdpf_test<5>(mpcio, opts, args, false);
  1251. } else if (!strcmp(*args, "irdpftest")) {
  1252. ++args;
  1253. rdpf_test<1>(mpcio, opts, args, true);
  1254. } else if (!strcmp(*args, "irdpftest2")) {
  1255. ++args;
  1256. rdpf_test<2>(mpcio, opts, args, true);
  1257. } else if (!strcmp(*args, "irdpftest3")) {
  1258. ++args;
  1259. rdpf_test<3>(mpcio, opts, args, true);
  1260. } else if (!strcmp(*args, "irdpftest4")) {
  1261. ++args;
  1262. rdpf_test<4>(mpcio, opts, args, true);
  1263. } else if (!strcmp(*args, "irdpftest5")) {
  1264. ++args;
  1265. rdpf_test<5>(mpcio, opts, args, true);
  1266. } else if (!strcmp(*args, "rdpftime")) {
  1267. ++args;
  1268. rdpf_timing(mpcio, opts, args);
  1269. } else if (!strcmp(*args, "evaltime")) {
  1270. ++args;
  1271. rdpfeval_timing(mpcio, opts, args);
  1272. } else if (!strcmp(*args, "parevaltime")) {
  1273. ++args;
  1274. par_rdpfeval_timing(mpcio, opts, args);
  1275. } else if (!strcmp(*args, "tupletime")) {
  1276. ++args;
  1277. tupleeval_timing(mpcio, opts, args);
  1278. } else if (!strcmp(*args, "partupletime")) {
  1279. ++args;
  1280. par_tupleeval_timing(mpcio, opts, args);
  1281. } else if (!strcmp(*args, "duotest")) {
  1282. ++args;
  1283. if (opts.use_xor_db) {
  1284. duoram_test<RegXS>(mpcio, opts, args);
  1285. } else {
  1286. duoram_test<RegAS>(mpcio, opts, args);
  1287. }
  1288. } else if (!strcmp(*args, "cdpftest")) {
  1289. ++args;
  1290. cdpf_test(mpcio, opts, args);
  1291. } else if (!strcmp(*args, "cmptest")) {
  1292. ++args;
  1293. compare_test(mpcio, opts, args);
  1294. } else if (!strcmp(*args, "sorttest")) {
  1295. ++args;
  1296. sort_test(mpcio, opts, args);
  1297. } else if (!strcmp(*args, "padtest")) {
  1298. ++args;
  1299. pad_test(mpcio, opts, args);
  1300. } else if (!strcmp(*args, "bsearch")) {
  1301. ++args;
  1302. bsearch_test(mpcio, opts, args);
  1303. } else if (!strcmp(*args, "duoram")) {
  1304. ++args;
  1305. if (opts.use_xor_db) {
  1306. duoram<RegXS>(mpcio, opts, args);
  1307. } else {
  1308. duoram<RegAS>(mpcio, opts, args);
  1309. }
  1310. } else if (!strcmp(*args, "cell")) {
  1311. ++args;
  1312. cell(mpcio, opts, args);
  1313. } else {
  1314. std::cerr << "Unknown mode " << *args << "\n";
  1315. }
  1316. }