online.cpp 30 KB

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