online.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. static void online_test(MPCIO &mpcio,
  8. const PRACOptions &opts, char **args)
  9. {
  10. nbits_t nbits = VALUE_BITS;
  11. if (*args) {
  12. nbits = atoi(*args);
  13. }
  14. size_t memsize = 9;
  15. MPCTIO tio(mpcio, 0);
  16. bool is_server = (mpcio.player == 2);
  17. RegAS *A = new RegAS[memsize];
  18. value_t V;
  19. RegBS F0, F1;
  20. RegXS X;
  21. if (!is_server) {
  22. A[0].randomize();
  23. A[1].randomize();
  24. F0.randomize();
  25. A[4].randomize();
  26. F1.randomize();
  27. A[6].randomize();
  28. A[7].randomize();
  29. X.randomize();
  30. arc4random_buf(&V, sizeof(V));
  31. printf("A:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  32. printf("V : %016lX\n", V);
  33. printf("F0 : %01X\n", F0.bshare);
  34. printf("F1 : %01X\n", F1.bshare);
  35. printf("X : %016lX\n", X.xshare);
  36. }
  37. std::vector<coro_t> coroutines;
  38. coroutines.emplace_back(
  39. [&tio, &A, nbits](yield_t &yield) {
  40. mpc_mul(tio, yield, A[2], A[0], A[1], nbits);
  41. });
  42. coroutines.emplace_back(
  43. [&tio, &A, V, nbits](yield_t &yield) {
  44. mpc_valuemul(tio, yield, A[3], V, nbits);
  45. });
  46. coroutines.emplace_back(
  47. [&tio, &A, &F0, nbits](yield_t &yield) {
  48. mpc_flagmult(tio, yield, A[5], F0, A[4], nbits);
  49. });
  50. coroutines.emplace_back(
  51. [&tio, &A, &F1, nbits](yield_t &yield) {
  52. mpc_oswap(tio, yield, A[6], A[7], F1, nbits);
  53. });
  54. coroutines.emplace_back(
  55. [&tio, &A, &X, nbits](yield_t &yield) {
  56. mpc_xs_to_as(tio, yield, A[8], X, nbits);
  57. });
  58. run_coroutines(tio, coroutines);
  59. if (!is_server) {
  60. printf("\n");
  61. printf("A:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  62. }
  63. // Check the answers
  64. if (mpcio.player == 1) {
  65. tio.queue_peer(A, memsize*sizeof(RegAS));
  66. tio.queue_peer(&V, sizeof(V));
  67. tio.queue_peer(&F0, sizeof(RegBS));
  68. tio.queue_peer(&F1, sizeof(RegBS));
  69. tio.queue_peer(&X, sizeof(RegXS));
  70. tio.send();
  71. } else if (mpcio.player == 0) {
  72. RegAS *B = new RegAS[memsize];
  73. RegBS BF0, BF1;
  74. RegXS BX;
  75. value_t BV;
  76. value_t *S = new value_t[memsize];
  77. bit_t SF0, SF1;
  78. value_t SX;
  79. tio.recv_peer(B, memsize*sizeof(RegAS));
  80. tio.recv_peer(&BV, sizeof(BV));
  81. tio.recv_peer(&BF0, sizeof(RegBS));
  82. tio.recv_peer(&BF1, sizeof(RegBS));
  83. tio.recv_peer(&BX, sizeof(RegXS));
  84. for(size_t i=0; i<memsize; ++i) S[i] = A[i].ashare+B[i].ashare;
  85. SF0 = F0.bshare ^ BF0.bshare;
  86. SF1 = F1.bshare ^ BF1.bshare;
  87. SX = X.xshare ^ BX.xshare;
  88. printf("S:\n"); for (size_t i=0; i<memsize; ++i) printf("%3lu: %016lX\n", i, S[i]);
  89. printf("SF0: %01X\n", SF0);
  90. printf("SF1: %01X\n", SF1);
  91. printf("SX : %016lX\n", SX);
  92. printf("\n%016lx\n", S[0]*S[1]-S[2]);
  93. printf("%016lx\n", (V*BV)-S[3]);
  94. printf("%016lx\n", (SF0*S[4])-S[5]);
  95. printf("%016lx\n", S[8]-SX);
  96. delete[] B;
  97. delete[] S;
  98. }
  99. delete[] A;
  100. }
  101. static void lamport_test(MPCIO &mpcio,
  102. const PRACOptions &opts, char **args)
  103. {
  104. // Create a bunch of threads and send a bunch of data to the other
  105. // peer, and receive their data. If an arg is specified, repeat
  106. // that many times. The Lamport clock at the end should be just the
  107. // number of repetitions. Subsequent args are the chunk size and
  108. // the number of chunks per message
  109. size_t niters = 1;
  110. size_t chunksize = 1<<20;
  111. size_t numchunks = 1;
  112. if (*args) {
  113. niters = atoi(*args);
  114. ++args;
  115. }
  116. if (*args) {
  117. chunksize = atoi(*args);
  118. ++args;
  119. }
  120. if (*args) {
  121. numchunks = atoi(*args);
  122. ++args;
  123. }
  124. int num_threads = opts.num_threads;
  125. boost::asio::thread_pool pool(num_threads);
  126. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  127. boost::asio::post(pool, [&mpcio, thread_num, niters, chunksize, numchunks] {
  128. MPCTIO tio(mpcio, thread_num);
  129. char *sendbuf = new char[chunksize];
  130. char *recvbuf = new char[chunksize*numchunks];
  131. for (size_t i=0; i<niters; ++i) {
  132. for (size_t chunk=0; chunk<numchunks; ++chunk) {
  133. arc4random_buf(sendbuf, chunksize);
  134. tio.queue_peer(sendbuf, chunksize);
  135. }
  136. tio.send();
  137. tio.recv_peer(recvbuf, chunksize*numchunks);
  138. }
  139. delete[] recvbuf;
  140. delete[] sendbuf;
  141. });
  142. }
  143. pool.join();
  144. }
  145. static void rdpf_test(MPCIO &mpcio,
  146. const PRACOptions &opts, char **args)
  147. {
  148. nbits_t depth=6;
  149. size_t num_iters = 1;
  150. if (*args) {
  151. depth = atoi(*args);
  152. ++args;
  153. }
  154. if (*args) {
  155. num_iters = atoi(*args);
  156. ++args;
  157. }
  158. int num_threads = opts.num_threads;
  159. boost::asio::thread_pool pool(num_threads);
  160. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  161. boost::asio::post(pool, [&mpcio, thread_num, depth, num_iters] {
  162. MPCTIO tio(mpcio, thread_num);
  163. run_coroutines(tio, [&tio, depth, num_iters] (yield_t &yield) {
  164. size_t &aes_ops = tio.aes_ops();
  165. for (size_t iter=0; iter < num_iters; ++iter) {
  166. if (tio.player() == 2) {
  167. RDPFPair dp = tio.rdpfpair(yield, depth);
  168. for (int i=0;i<2;++i) {
  169. const RDPF &dpf = dp.dpf[i];
  170. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  171. DPFnode leaf = dpf.leaf(x, aes_ops);
  172. RegBS ub = dpf.unit_bs(leaf);
  173. RegAS ua = dpf.unit_as(leaf);
  174. RegXS sx = dpf.scaled_xs(leaf);
  175. RegAS sa = dpf.scaled_as(leaf);
  176. printf("%04x %x %016lx %016lx %016lx\n", x,
  177. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  178. }
  179. printf("\n");
  180. }
  181. } else {
  182. RDPFTriple dt = tio.rdpftriple(yield, depth);
  183. for (int i=0;i<3;++i) {
  184. const RDPF &dpf = dt.dpf[i];
  185. RegXS peer_scaled_xor;
  186. RegAS peer_scaled_sum;
  187. if (tio.player() == 1) {
  188. tio.iostream_peer() << dpf.scaled_xor << dpf.scaled_sum;
  189. } else {
  190. tio.iostream_peer() >> peer_scaled_xor >> peer_scaled_sum;
  191. peer_scaled_sum += dpf.scaled_sum;
  192. peer_scaled_xor ^= dpf.scaled_xor;
  193. }
  194. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  195. DPFnode leaf = dpf.leaf(x, aes_ops);
  196. RegBS ub = dpf.unit_bs(leaf);
  197. RegAS ua = dpf.unit_as(leaf);
  198. RegXS sx = dpf.scaled_xs(leaf);
  199. RegAS sa = dpf.scaled_as(leaf);
  200. printf("%04x %x %016lx %016lx %016lx\n", x,
  201. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  202. if (tio.player() == 1) {
  203. tio.iostream_peer() << ub << ua << sx << sa;
  204. } else {
  205. RegBS peer_ub;
  206. RegAS peer_ua;
  207. RegXS peer_sx;
  208. RegAS peer_sa;
  209. tio.iostream_peer() >> peer_ub >> peer_ua >>
  210. peer_sx >> peer_sa;
  211. ub ^= peer_ub;
  212. ua += peer_ua;
  213. sx ^= peer_sx;
  214. sa += peer_sa;
  215. if (ub.bshare || ua.ashare || sx.xshare || sa.ashare) {
  216. printf("**** %x %016lx %016lx %016lx\n",
  217. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  218. printf("SCALE %016lx %016lx\n",
  219. peer_scaled_xor.xshare, peer_scaled_sum.ashare);
  220. }
  221. }
  222. }
  223. printf("\n");
  224. }
  225. }
  226. }
  227. });
  228. });
  229. }
  230. pool.join();
  231. }
  232. static void rdpf_timing(MPCIO &mpcio,
  233. const PRACOptions &opts, char **args)
  234. {
  235. nbits_t depth=6;
  236. if (*args) {
  237. depth = atoi(*args);
  238. ++args;
  239. }
  240. int num_threads = opts.num_threads;
  241. boost::asio::thread_pool pool(num_threads);
  242. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  243. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  244. MPCTIO tio(mpcio, thread_num);
  245. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  246. size_t &aes_ops = tio.aes_ops();
  247. if (tio.player() == 2) {
  248. RDPFPair dp = tio.rdpfpair(yield, depth);
  249. for (int i=0;i<2;++i) {
  250. RDPF &dpf = dp.dpf[i];
  251. dpf.expand(aes_ops);
  252. RegXS scaled_xor;
  253. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  254. DPFnode leaf = dpf.leaf(x, aes_ops);
  255. RegXS sx = dpf.scaled_xs(leaf);
  256. scaled_xor ^= sx;
  257. }
  258. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  259. dpf.scaled_xor.xshare);
  260. printf("\n");
  261. }
  262. } else {
  263. RDPFTriple dt = tio.rdpftriple(yield, depth);
  264. for (int i=0;i<3;++i) {
  265. RDPF &dpf = dt.dpf[i];
  266. dpf.expand(aes_ops);
  267. RegXS scaled_xor;
  268. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  269. DPFnode leaf = dpf.leaf(x, aes_ops);
  270. RegXS sx = dpf.scaled_xs(leaf);
  271. scaled_xor ^= sx;
  272. }
  273. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  274. dpf.scaled_xor.xshare);
  275. printf("\n");
  276. }
  277. }
  278. });
  279. });
  280. }
  281. pool.join();
  282. }
  283. static value_t parallel_streameval_rdpf(MPCIO &mpcio, const RDPF &dpf,
  284. address_t start, int num_threads)
  285. {
  286. RegXS scaled_xor[num_threads];
  287. boost::asio::thread_pool pool(num_threads);
  288. address_t totsize = (address_t(1)<<dpf.depth());
  289. address_t threadstart = start;
  290. address_t threadchunk = totsize / num_threads;
  291. address_t threadextra = totsize % num_threads;
  292. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  293. address_t threadsize = threadchunk + (address_t(thread_num) < threadextra);
  294. boost::asio::post(pool,
  295. [&mpcio, &dpf, &scaled_xor, thread_num, threadstart, threadsize] {
  296. MPCTIO tio(mpcio, thread_num);
  297. //printf("Thread %d from %X for %X\n", thread_num, threadstart, threadsize);
  298. RegXS local_xor;
  299. size_t local_aes_ops = 0;
  300. auto ev = StreamEval(dpf, threadstart, 0, local_aes_ops);
  301. for (address_t x=0;x<threadsize;++x) {
  302. //if (x%0x10000 == 0) printf("%d", thread_num);
  303. DPFnode leaf = ev.next();
  304. local_xor ^= dpf.scaled_xs(leaf);
  305. }
  306. scaled_xor[thread_num] = local_xor;
  307. tio.aes_ops() += local_aes_ops;
  308. //printf("Thread %d complete\n", thread_num);
  309. });
  310. threadstart = (threadstart + threadsize) % totsize;
  311. }
  312. pool.join();
  313. RegXS res;
  314. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  315. res ^= scaled_xor[thread_num];
  316. }
  317. return res.xshare;
  318. }
  319. static void rdpfeval_timing(MPCIO &mpcio,
  320. const PRACOptions &opts, char **args)
  321. {
  322. nbits_t depth=6;
  323. address_t start=0;
  324. if (*args) {
  325. depth = atoi(*args);
  326. ++args;
  327. }
  328. if (*args) {
  329. start = strtoull(*args, NULL, 16);
  330. ++args;
  331. }
  332. int num_threads = opts.num_threads;
  333. MPCTIO tio(mpcio, 0, num_threads);
  334. run_coroutines(tio, [&mpcio, &tio, depth, start, num_threads] (yield_t &yield) {
  335. if (tio.player() == 2) {
  336. RDPFPair dp = tio.rdpfpair(yield, depth);
  337. for (int i=0;i<2;++i) {
  338. RDPF &dpf = dp.dpf[i];
  339. value_t scaled_xor =
  340. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  341. printf("%016lx\n%016lx\n", scaled_xor,
  342. dpf.scaled_xor.xshare);
  343. printf("\n");
  344. }
  345. } else {
  346. RDPFTriple dt = tio.rdpftriple(yield, depth);
  347. for (int i=0;i<3;++i) {
  348. RDPF &dpf = dt.dpf[i];
  349. value_t scaled_xor =
  350. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  351. printf("%016lx\n%016lx\n", scaled_xor,
  352. dpf.scaled_xor.xshare);
  353. printf("\n");
  354. }
  355. }
  356. });
  357. }
  358. static void tupleeval_timing(MPCIO &mpcio,
  359. const PRACOptions &opts, char **args)
  360. {
  361. nbits_t depth=6;
  362. address_t start=0;
  363. if (*args) {
  364. depth = atoi(*args);
  365. ++args;
  366. }
  367. if (*args) {
  368. start = atoi(*args);
  369. ++args;
  370. }
  371. int num_threads = opts.num_threads;
  372. MPCTIO tio(mpcio, 0, num_threads);
  373. run_coroutines(tio, [&tio, depth, start] (yield_t &yield) {
  374. size_t &aes_ops = tio.aes_ops();
  375. if (tio.player() == 2) {
  376. RDPFPair dp = tio.rdpfpair(yield, depth);
  377. RegXS scaled_xor0, scaled_xor1;
  378. auto ev = StreamEval(dp, start, 0, aes_ops, false);
  379. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  380. auto [L0, L1] = ev.next();
  381. RegXS sx0 = dp.dpf[0].scaled_xs(L0);
  382. RegXS sx1 = dp.dpf[1].scaled_xs(L1);
  383. scaled_xor0 ^= sx0;
  384. scaled_xor1 ^= sx1;
  385. }
  386. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  387. dp.dpf[0].scaled_xor.xshare);
  388. printf("\n");
  389. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  390. dp.dpf[1].scaled_xor.xshare);
  391. printf("\n");
  392. } else {
  393. RDPFTriple dt = tio.rdpftriple(yield, depth);
  394. RegXS scaled_xor0, scaled_xor1, scaled_xor2;
  395. auto ev = StreamEval(dt, start, 0, aes_ops, false);
  396. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  397. auto [L0, L1, L2] = ev.next();
  398. RegXS sx0 = dt.dpf[0].scaled_xs(L0);
  399. RegXS sx1 = dt.dpf[1].scaled_xs(L1);
  400. RegXS sx2 = dt.dpf[2].scaled_xs(L2);
  401. scaled_xor0 ^= sx0;
  402. scaled_xor1 ^= sx1;
  403. scaled_xor2 ^= sx2;
  404. }
  405. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  406. dt.dpf[0].scaled_xor.xshare);
  407. printf("\n");
  408. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  409. dt.dpf[1].scaled_xor.xshare);
  410. printf("\n");
  411. printf("%016lx\n%016lx\n", scaled_xor2.xshare,
  412. dt.dpf[2].scaled_xor.xshare);
  413. printf("\n");
  414. }
  415. });
  416. }
  417. // T is RegAS or RegXS for additive or XOR shared database respectively
  418. template <typename T>
  419. static void duoram_test(MPCIO &mpcio,
  420. const PRACOptions &opts, char **args)
  421. {
  422. nbits_t depth=6;
  423. address_t share=arc4random();
  424. if (*args) {
  425. depth = atoi(*args);
  426. ++args;
  427. }
  428. if (*args) {
  429. share = atoi(*args);
  430. ++args;
  431. }
  432. share &= ((address_t(1)<<depth)-1);
  433. int num_threads = opts.num_threads;
  434. boost::asio::thread_pool pool(num_threads);
  435. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  436. boost::asio::post(pool, [&mpcio, thread_num, depth, share] {
  437. MPCTIO tio(mpcio, thread_num);
  438. run_coroutines(tio, [&tio, depth, share] (yield_t &yield) {
  439. size_t size = size_t(1)<<depth;
  440. // size_t &aes_ops = tio.aes_ops();
  441. Duoram<T> oram(tio.player(), size);
  442. auto A = oram.flat(tio, yield);
  443. RegAS aidx;
  444. aidx.ashare = share;
  445. T M;
  446. if (tio.player() == 0) {
  447. M.set(0xbabb0000);
  448. } else {
  449. M.set(0x0000a66e);
  450. }
  451. RegXS xidx;
  452. xidx.xshare = share;
  453. T N;
  454. if (tio.player() == 0) {
  455. N.set(0xdead0000);
  456. } else {
  457. N.set(0x0000beef);
  458. }
  459. // Writing and reading with additively shared indices
  460. printf("Updating\n");
  461. A[aidx] += M;
  462. printf("Reading\n");
  463. T Aa = A[aidx];
  464. // Writing and reading with XOR shared indices
  465. printf("Updating\n");
  466. A[xidx] += N;
  467. printf("Reading\n");
  468. T Ax = A[xidx];
  469. T Ae;
  470. // Writing and reading with explicit indices
  471. if (depth > 2) {
  472. A[5] += Aa;
  473. Ae = A[6];
  474. }
  475. if (depth <= 10) {
  476. oram.dump();
  477. auto check = A.reconstruct();
  478. if (tio.player() == 0) {
  479. for (address_t i=0;i<size;++i) {
  480. printf("%04x %016lx\n", i, check[i].share());
  481. }
  482. }
  483. }
  484. auto checkread = A.reconstruct(Aa);
  485. auto checkreade = A.reconstruct(Ae);
  486. auto checkreadx = A.reconstruct(Ax);
  487. if (tio.player() == 0) {
  488. printf("Read AS value = %016lx\n", checkread.share());
  489. printf("Read AX value = %016lx\n", checkreadx.share());
  490. printf("Read Ex value = %016lx\n", checkreade.share());
  491. }
  492. });
  493. });
  494. }
  495. pool.join();
  496. }
  497. static void cdpf_test(MPCIO &mpcio,
  498. const PRACOptions &opts, char **args)
  499. {
  500. value_t query, target;
  501. int iters = 1;
  502. arc4random_buf(&query, sizeof(query));
  503. arc4random_buf(&target, sizeof(target));
  504. if (*args) {
  505. query = strtoull(*args, NULL, 16);
  506. ++args;
  507. }
  508. if (*args) {
  509. target = strtoull(*args, NULL, 16);
  510. ++args;
  511. }
  512. if (*args) {
  513. iters = atoi(*args);
  514. ++args;
  515. }
  516. int num_threads = opts.num_threads;
  517. boost::asio::thread_pool pool(num_threads);
  518. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  519. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  520. MPCTIO tio(mpcio, thread_num);
  521. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  522. size_t &aes_ops = tio.aes_ops();
  523. for (int i=0;i<iters;++i) {
  524. if (tio.player() == 2) {
  525. tio.cdpf(yield);
  526. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  527. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  528. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  529. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  530. dump_node(leaf0 ^ leaf1);
  531. } else {
  532. CDPF dpf = tio.cdpf(yield);
  533. printf("ashare = %016lX\nxshare = %016lX\n",
  534. dpf.as_target.ashare, dpf.xs_target.xshare);
  535. DPFnode leaf = dpf.leaf(query, aes_ops);
  536. printf("DPF(%016lx) = ", query);
  537. dump_node(leaf);
  538. if (tio.player() == 1) {
  539. tio.iostream_peer() << leaf;
  540. } else {
  541. DPFnode peerleaf;
  542. tio.iostream_peer() >> peerleaf;
  543. printf("XOR = ");
  544. dump_node(leaf ^ peerleaf);
  545. }
  546. }
  547. }
  548. });
  549. });
  550. }
  551. pool.join();
  552. }
  553. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  554. value_t target, value_t x)
  555. {
  556. int player = tio.player();
  557. size_t &aes_ops = tio.aes_ops();
  558. int res = 1;
  559. if (player == 2) {
  560. // Create a CDPF pair with the given target
  561. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  562. // Send it and a share of x to the computational parties
  563. RegAS x0, x1;
  564. x0.randomize();
  565. x1.set(x-x0.share());
  566. tio.iostream_p0() << dpf0 << x0;
  567. tio.iostream_p1() << dpf1 << x1;
  568. } else {
  569. CDPF dpf;
  570. RegAS xsh;
  571. tio.iostream_server() >> dpf >> xsh;
  572. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  573. printf("%016lx %016lx %d %d %d ", target, x, lt.bshare,
  574. eq.bshare, gt.bshare);
  575. // Check the answer
  576. if (player == 1) {
  577. tio.iostream_peer() << xsh << lt << eq << gt;
  578. } else {
  579. RegAS peer_xsh;
  580. RegBS peer_lt, peer_eq, peer_gt;
  581. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >> peer_gt;
  582. lt ^= peer_lt;
  583. eq ^= peer_eq;
  584. gt ^= peer_gt;
  585. xsh += peer_xsh;
  586. int lti = int(lt.bshare);
  587. int eqi = int(eq.bshare);
  588. int gti = int(gt.bshare);
  589. x = xsh.share();
  590. printf(": %d %d %d ", lti, eqi, gti);
  591. bool signbit = (x >> 63);
  592. if (lti + eqi + gti != 1) {
  593. printf("INCONSISTENT");
  594. res = 0;
  595. } else if (x == 0 && eqi) {
  596. printf("=");
  597. } else if (!signbit && gti) {
  598. printf(">");
  599. } else if (signbit && lti) {
  600. printf("<");
  601. } else {
  602. printf("INCORRECT");
  603. res = 0;
  604. }
  605. }
  606. printf("\n");
  607. }
  608. return res;
  609. }
  610. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  611. value_t target, value_t x)
  612. {
  613. int res = 1;
  614. res &= compare_test_one(tio, yield, target, x);
  615. res &= compare_test_one(tio, yield, target, 0);
  616. res &= compare_test_one(tio, yield, target, 1);
  617. res &= compare_test_one(tio, yield, target, 15);
  618. res &= compare_test_one(tio, yield, target, 16);
  619. res &= compare_test_one(tio, yield, target, 17);
  620. res &= compare_test_one(tio, yield, target, -1);
  621. res &= compare_test_one(tio, yield, target, -15);
  622. res &= compare_test_one(tio, yield, target, -16);
  623. res &= compare_test_one(tio, yield, target, -17);
  624. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  625. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  626. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  627. return res;
  628. }
  629. static void compare_test(MPCIO &mpcio,
  630. const PRACOptions &opts, char **args)
  631. {
  632. value_t target, x;
  633. arc4random_buf(&target, sizeof(target));
  634. arc4random_buf(&x, sizeof(x));
  635. if (*args) {
  636. target = strtoull(*args, NULL, 16);
  637. ++args;
  638. }
  639. if (*args) {
  640. x = strtoull(*args, NULL, 16);
  641. ++args;
  642. }
  643. int num_threads = opts.num_threads;
  644. boost::asio::thread_pool pool(num_threads);
  645. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  646. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  647. MPCTIO tio(mpcio, thread_num);
  648. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  649. int res = 1;
  650. res &= compare_test_target(tio, yield, target, x);
  651. res &= compare_test_target(tio, yield, 0, x);
  652. res &= compare_test_target(tio, yield, 1, x);
  653. res &= compare_test_target(tio, yield, 15, x);
  654. res &= compare_test_target(tio, yield, 16, x);
  655. res &= compare_test_target(tio, yield, 17, x);
  656. res &= compare_test_target(tio, yield, -1, x);
  657. res &= compare_test_target(tio, yield, -15, x);
  658. res &= compare_test_target(tio, yield, -16, x);
  659. res &= compare_test_target(tio, yield, -17, x);
  660. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  661. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  662. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  663. if (tio.player() == 0) {
  664. if (res == 1) {
  665. printf("All tests passed!\n");
  666. } else {
  667. printf("TEST FAILURES\n");
  668. }
  669. }
  670. });
  671. });
  672. }
  673. pool.join();
  674. }
  675. static void sort_test(MPCIO &mpcio,
  676. const PRACOptions &opts, char **args)
  677. {
  678. nbits_t depth=6;
  679. if (*args) {
  680. depth = atoi(*args);
  681. ++args;
  682. }
  683. int num_threads = opts.num_threads;
  684. boost::asio::thread_pool pool(num_threads);
  685. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  686. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  687. MPCTIO tio(mpcio, thread_num);
  688. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  689. address_t size = address_t(1)<<depth;
  690. // size_t &aes_ops = tio.aes_ops();
  691. Duoram<RegAS> oram(tio.player(), size);
  692. auto A = oram.flat(tio, yield);
  693. A.explicitonly(true);
  694. // Initialize the memory to random values in parallel
  695. std::vector<coro_t> coroutines;
  696. for (address_t i=0; i<size; ++i) {
  697. coroutines.emplace_back(
  698. [&A, i](yield_t &yield) {
  699. auto Acoro = A.context(yield);
  700. RegAS v;
  701. v.randomize(62);
  702. Acoro[i] += v;
  703. });
  704. }
  705. run_coroutines(yield, coroutines);
  706. A.bitonic_sort(0, depth);
  707. if (depth <= 10) {
  708. oram.dump();
  709. auto check = A.reconstruct();
  710. if (tio.player() == 0) {
  711. for (address_t i=0;i<size;++i) {
  712. printf("%04x %016lx\n", i, check[i].share());
  713. }
  714. }
  715. }
  716. });
  717. });
  718. }
  719. pool.join();
  720. }
  721. static void bsearch_test(MPCIO &mpcio,
  722. const PRACOptions &opts, char **args)
  723. {
  724. value_t target;
  725. arc4random_buf(&target, sizeof(target));
  726. target >>= 1;
  727. nbits_t depth=6;
  728. if (*args) {
  729. depth = atoi(*args);
  730. ++args;
  731. }
  732. if (*args) {
  733. target = strtoull(*args, NULL, 16);
  734. ++args;
  735. }
  736. int num_threads = opts.num_threads;
  737. boost::asio::thread_pool pool(num_threads);
  738. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  739. boost::asio::post(pool, [&mpcio, thread_num, depth, target] {
  740. MPCTIO tio(mpcio, thread_num);
  741. run_coroutines(tio, [&tio, depth, target] (yield_t &yield) {
  742. address_t size = address_t(1)<<depth;
  743. RegAS tshare;
  744. if (tio.player() == 2) {
  745. // Send shares of the target to the computational
  746. // players
  747. RegAS tshare0, tshare1;
  748. tshare0.randomize();
  749. tshare1.set(target-tshare0.share());
  750. tio.iostream_p0() << tshare0;
  751. tio.iostream_p1() << tshare1;
  752. printf("Using target = %016lx\n", target);
  753. yield();
  754. } else {
  755. // Get the share of the target
  756. tio.iostream_server() >> tshare;
  757. }
  758. // Create a random database and sort it
  759. // size_t &aes_ops = tio.aes_ops();
  760. Duoram<RegAS> oram(tio.player(), size);
  761. auto A = oram.flat(tio, yield);
  762. A.explicitonly(true);
  763. // Initialize the memory to random values in parallel
  764. std::vector<coro_t> coroutines;
  765. for (address_t i=0; i<size; ++i) {
  766. coroutines.emplace_back(
  767. [&A, i](yield_t &yield) {
  768. auto Acoro = A.context(yield);
  769. RegAS v;
  770. v.randomize(62);
  771. Acoro[i] += v;
  772. });
  773. }
  774. run_coroutines(yield, coroutines);
  775. A.bitonic_sort(0, depth);
  776. // Binary search for the target
  777. RegAS tindex = A.obliv_binary_search(tshare);
  778. // Check the answer
  779. if (tio.player() == 1) {
  780. tio.iostream_peer() << tindex;
  781. } else if (tio.player() == 0) {
  782. RegAS peer_tindex;
  783. tio.iostream_peer() >> peer_tindex;
  784. tindex += peer_tindex;
  785. }
  786. if (depth <= 10) {
  787. auto check = A.reconstruct();
  788. if (tio.player() == 0) {
  789. for (address_t i=0;i<size;++i) {
  790. printf("%04x %016lx\n", i, check[i].share());
  791. }
  792. }
  793. }
  794. if (tio.player() == 0) {
  795. printf("Found index = %lx\n", tindex.share());
  796. }
  797. });
  798. });
  799. }
  800. pool.join();
  801. }
  802. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  803. {
  804. MPCTIO tio(mpcio, 0);
  805. if (!*args) {
  806. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  807. return;
  808. } else if (!strcmp(*args, "test")) {
  809. ++args;
  810. online_test(mpcio, opts, args);
  811. } else if (!strcmp(*args, "lamporttest")) {
  812. ++args;
  813. lamport_test(mpcio, opts, args);
  814. } else if (!strcmp(*args, "rdpftest")) {
  815. ++args;
  816. rdpf_test(mpcio, opts, args);
  817. } else if (!strcmp(*args, "rdpftime")) {
  818. ++args;
  819. rdpf_timing(mpcio, opts, args);
  820. } else if (!strcmp(*args, "evaltime")) {
  821. ++args;
  822. rdpfeval_timing(mpcio, opts, args);
  823. } else if (!strcmp(*args, "tupletime")) {
  824. ++args;
  825. tupleeval_timing(mpcio, opts, args);
  826. } else if (!strcmp(*args, "duotest")) {
  827. ++args;
  828. if (opts.use_xor_db) {
  829. duoram_test<RegXS>(mpcio, opts, args);
  830. } else {
  831. duoram_test<RegAS>(mpcio, opts, args);
  832. }
  833. } else if (!strcmp(*args, "cdpftest")) {
  834. ++args;
  835. cdpf_test(mpcio, opts, args);
  836. } else if (!strcmp(*args, "cmptest")) {
  837. ++args;
  838. compare_test(mpcio, opts, args);
  839. } else if (!strcmp(*args, "sorttest")) {
  840. ++args;
  841. sort_test(mpcio, opts, args);
  842. } else if (!strcmp(*args, "bsearch")) {
  843. ++args;
  844. bsearch_test(mpcio, opts, args);
  845. } else {
  846. std::cerr << "Unknown mode " << *args << "\n";
  847. }
  848. }