online.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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, &yield, 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(yield, 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(yield, 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, &yield, 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(yield, 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(yield, 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, &yield, 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(yield, 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(yield, 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, &yield, 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(yield, 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(yield, 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. int iters = 1;
  477. arc4random_buf(&query, sizeof(query));
  478. arc4random_buf(&target, sizeof(target));
  479. if (*args) {
  480. query = strtoull(*args, NULL, 16);
  481. ++args;
  482. }
  483. if (*args) {
  484. target = strtoull(*args, NULL, 16);
  485. ++args;
  486. }
  487. if (*args) {
  488. iters = atoi(*args);
  489. ++args;
  490. }
  491. int num_threads = opts.num_threads;
  492. boost::asio::thread_pool pool(num_threads);
  493. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  494. boost::asio::post(pool, [&mpcio, thread_num, &query, &target, &iters] {
  495. MPCTIO tio(mpcio, thread_num);
  496. size_t &aes_ops = tio.aes_ops();
  497. for (int i=0;i<iters;++i) {
  498. if (mpcio.player == 2) {
  499. tio.cdpf();
  500. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  501. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  502. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  503. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  504. dump_node(leaf0 ^ leaf1);
  505. } else {
  506. CDPF dpf = tio.cdpf();
  507. printf("ashare = %016lX\nxshare = %016lX\n",
  508. dpf.as_target.ashare, dpf.xs_target.xshare);
  509. DPFnode leaf = dpf.leaf(query, aes_ops);
  510. printf("DPF(%016lx) = ", query);
  511. dump_node(leaf);
  512. if (mpcio.player == 1) {
  513. tio.iostream_peer() << leaf;
  514. } else {
  515. DPFnode peerleaf;
  516. tio.iostream_peer() >> peerleaf;
  517. printf("XOR = ");
  518. dump_node(leaf ^ peerleaf);
  519. }
  520. }
  521. }
  522. tio.send();
  523. });
  524. }
  525. pool.join();
  526. }
  527. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  528. value_t target, value_t x)
  529. {
  530. int player = tio.player();
  531. size_t &aes_ops = tio.aes_ops();
  532. int res = 1;
  533. if (player == 2) {
  534. // Create a CDPF pair with the given target
  535. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  536. // Send it and a share of x to the computational parties
  537. RegAS x0, x1;
  538. x0.randomize();
  539. x1.set(x-x0.share());
  540. tio.iostream_p0() << dpf0 << x0;
  541. tio.iostream_p1() << dpf1 << x1;
  542. } else {
  543. CDPF dpf;
  544. RegAS xsh;
  545. tio.iostream_server() >> dpf >> xsh;
  546. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  547. printf("%016lx %016lx %d %d %d ", target, x, lt.bshare,
  548. eq.bshare, gt.bshare);
  549. // Check the answer
  550. if (player == 1) {
  551. tio.iostream_peer() << xsh << lt << eq << gt;
  552. } else {
  553. RegAS peer_xsh;
  554. RegBS peer_lt, peer_eq, peer_gt;
  555. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >> peer_gt;
  556. lt ^= peer_lt;
  557. eq ^= peer_eq;
  558. gt ^= peer_gt;
  559. xsh += peer_xsh;
  560. int lti = int(lt.bshare);
  561. int eqi = int(eq.bshare);
  562. int gti = int(gt.bshare);
  563. x = xsh.share();
  564. printf(": %d %d %d ", lti, eqi, gti);
  565. bool signbit = (x >> 63);
  566. if (lti + eqi + gti != 1) {
  567. printf("INCONSISTENT");
  568. res = 0;
  569. } else if (x == 0 && eqi) {
  570. printf("=");
  571. } else if (!signbit && gti) {
  572. printf(">");
  573. } else if (signbit && lti) {
  574. printf("<");
  575. } else {
  576. printf("INCORRECT");
  577. res = 0;
  578. }
  579. }
  580. printf("\n");
  581. }
  582. return res;
  583. }
  584. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  585. value_t target, value_t x)
  586. {
  587. int res = 1;
  588. res &= compare_test_one(tio, yield, target, x);
  589. res &= compare_test_one(tio, yield, target, 0);
  590. res &= compare_test_one(tio, yield, target, 1);
  591. res &= compare_test_one(tio, yield, target, 15);
  592. res &= compare_test_one(tio, yield, target, 16);
  593. res &= compare_test_one(tio, yield, target, 17);
  594. res &= compare_test_one(tio, yield, target, -1);
  595. res &= compare_test_one(tio, yield, target, -15);
  596. res &= compare_test_one(tio, yield, target, -16);
  597. res &= compare_test_one(tio, yield, target, -17);
  598. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  599. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  600. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  601. return res;
  602. }
  603. static void compare_test(MPCIO &mpcio, yield_t &yield,
  604. const PRACOptions &opts, char **args)
  605. {
  606. value_t target, x;
  607. arc4random_buf(&target, sizeof(target));
  608. arc4random_buf(&x, sizeof(x));
  609. if (*args) {
  610. target = strtoull(*args, NULL, 16);
  611. ++args;
  612. }
  613. if (*args) {
  614. x = strtoull(*args, NULL, 16);
  615. ++args;
  616. }
  617. int num_threads = opts.num_threads;
  618. boost::asio::thread_pool pool(num_threads);
  619. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  620. boost::asio::post(pool, [&mpcio, &yield, thread_num, &target, &x] {
  621. MPCTIO tio(mpcio, thread_num);
  622. int res = 1;
  623. res &= compare_test_target(tio, yield, target, x);
  624. res &= compare_test_target(tio, yield, 0, x);
  625. res &= compare_test_target(tio, yield, 1, x);
  626. res &= compare_test_target(tio, yield, 15, x);
  627. res &= compare_test_target(tio, yield, 16, x);
  628. res &= compare_test_target(tio, yield, 17, x);
  629. res &= compare_test_target(tio, yield, -1, x);
  630. res &= compare_test_target(tio, yield, -15, x);
  631. res &= compare_test_target(tio, yield, -16, x);
  632. res &= compare_test_target(tio, yield, -17, x);
  633. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  634. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  635. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  636. tio.send();
  637. if (tio.player() == 0) {
  638. if (res == 1) {
  639. printf("All tests passed!\n");
  640. } else {
  641. printf("TEST FAILURES\n");
  642. }
  643. }
  644. });
  645. }
  646. pool.join();
  647. }
  648. static void sort_test(MPCIO &mpcio, yield_t &yield,
  649. const PRACOptions &opts, char **args)
  650. {
  651. nbits_t depth=6;
  652. if (*args) {
  653. depth = atoi(*args);
  654. ++args;
  655. }
  656. int num_threads = opts.num_threads;
  657. boost::asio::thread_pool pool(num_threads);
  658. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  659. boost::asio::post(pool, [&mpcio, &yield, thread_num, depth] {
  660. address_t size = address_t(1)<<depth;
  661. MPCTIO tio(mpcio, thread_num);
  662. // size_t &aes_ops = tio.aes_ops();
  663. Duoram<RegAS> oram(mpcio.player, size);
  664. auto A = oram.flat(tio, yield);
  665. A.explicitonly(true);
  666. // Initialize the memory to random values in parallel
  667. std::vector<coro_t> coroutines;
  668. for (address_t i=0; i<size; ++i) {
  669. coroutines.emplace_back(
  670. [&A, i](yield_t &yield) {
  671. auto Acoro = A.context(yield);
  672. RegAS v;
  673. v.randomize(62);
  674. Acoro[i] += v;
  675. });
  676. }
  677. run_coroutines(yield, coroutines);
  678. A.bitonic_sort(0, depth);
  679. if (depth <= 10) {
  680. oram.dump();
  681. auto check = A.reconstruct();
  682. if (tio.player() == 0) {
  683. for (address_t i=0;i<size;++i) {
  684. printf("%04x %016lx\n", i, check[i].share());
  685. }
  686. }
  687. }
  688. tio.send();
  689. });
  690. }
  691. pool.join();
  692. }
  693. static void bsearch_test(MPCIO &mpcio, yield_t &yield,
  694. const PRACOptions &opts, char **args)
  695. {
  696. value_t target;
  697. arc4random_buf(&target, sizeof(target));
  698. target >>= 1;
  699. nbits_t depth=6;
  700. if (*args) {
  701. depth = atoi(*args);
  702. ++args;
  703. }
  704. if (*args) {
  705. target = strtoull(*args, NULL, 16);
  706. ++args;
  707. }
  708. int num_threads = opts.num_threads;
  709. boost::asio::thread_pool pool(num_threads);
  710. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  711. boost::asio::post(pool, [&mpcio, &yield, thread_num, depth, target] {
  712. address_t size = address_t(1)<<depth;
  713. MPCTIO tio(mpcio, thread_num);
  714. RegAS tshare;
  715. if (tio.player() == 2) {
  716. // Send shares of the target to the computational
  717. // players
  718. RegAS tshare0, tshare1;
  719. tshare0.randomize();
  720. tshare1.set(target-tshare0.share());
  721. tio.iostream_p0() << tshare0;
  722. tio.iostream_p1() << tshare1;
  723. printf("Using target = %016lx\n", target);
  724. yield();
  725. } else {
  726. // Get the share of the target
  727. tio.iostream_server() >> tshare;
  728. }
  729. // Create a random database and sort it
  730. // size_t &aes_ops = tio.aes_ops();
  731. Duoram<RegAS> oram(mpcio.player, size);
  732. auto A = oram.flat(tio, yield);
  733. A.explicitonly(true);
  734. // Initialize the memory to random values in parallel
  735. std::vector<coro_t> coroutines;
  736. for (address_t i=0; i<size; ++i) {
  737. coroutines.emplace_back(
  738. [&A, i](yield_t &yield) {
  739. auto Acoro = A.context(yield);
  740. RegAS v;
  741. v.randomize(62);
  742. Acoro[i] += v;
  743. });
  744. }
  745. run_coroutines(yield, coroutines);
  746. A.bitonic_sort(0, depth);
  747. // Binary search for the target
  748. RegAS tindex = A.obliv_binary_search(tshare);
  749. // Check the answer
  750. if (tio.player() == 1) {
  751. tio.iostream_peer() << tindex;
  752. } else if (tio.player() == 0) {
  753. RegAS peer_tindex;
  754. tio.iostream_peer() >> peer_tindex;
  755. tindex += peer_tindex;
  756. }
  757. if (depth <= 10) {
  758. auto check = A.reconstruct();
  759. if (tio.player() == 0) {
  760. for (address_t i=0;i<size;++i) {
  761. printf("%04x %016lx\n", i, check[i].share());
  762. }
  763. }
  764. }
  765. if (tio.player() == 0) {
  766. printf("Found index = %lx\n", tindex.share());
  767. }
  768. tio.send();
  769. });
  770. }
  771. pool.join();
  772. }
  773. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  774. {
  775. // Run everything inside a coroutine so that simple tests don't have
  776. // to start one themselves
  777. MPCTIO tio(mpcio, 0);
  778. run_coroutines(tio,
  779. [&](yield_t &yield) {
  780. if (!*args) {
  781. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  782. return;
  783. } else if (!strcmp(*args, "test")) {
  784. ++args;
  785. online_test(mpcio, yield, opts, args);
  786. } else if (!strcmp(*args, "lamporttest")) {
  787. ++args;
  788. lamport_test(mpcio, yield, opts, args);
  789. } else if (!strcmp(*args, "rdpftest")) {
  790. ++args;
  791. rdpf_test(mpcio, yield, opts, args);
  792. } else if (!strcmp(*args, "rdpftime")) {
  793. ++args;
  794. rdpf_timing(mpcio, yield, opts, args);
  795. } else if (!strcmp(*args, "evaltime")) {
  796. ++args;
  797. rdpfeval_timing(mpcio, yield, opts, args);
  798. } else if (!strcmp(*args, "tupletime")) {
  799. ++args;
  800. tupleeval_timing(mpcio, yield, opts, args);
  801. } else if (!strcmp(*args, "duotest")) {
  802. ++args;
  803. if (opts.use_xor_db) {
  804. duoram_test<RegXS>(mpcio, yield, opts, args);
  805. } else {
  806. duoram_test<RegAS>(mpcio, yield, opts, args);
  807. }
  808. } else if (!strcmp(*args, "cdpftest")) {
  809. ++args;
  810. cdpf_test(mpcio, yield, opts, args);
  811. } else if (!strcmp(*args, "cmptest")) {
  812. ++args;
  813. compare_test(mpcio, yield, opts, args);
  814. } else if (!strcmp(*args, "sorttest")) {
  815. ++args;
  816. sort_test(mpcio, yield, opts, args);
  817. } else if (!strcmp(*args, "bsearch")) {
  818. ++args;
  819. bsearch_test(mpcio, yield, opts, args);
  820. } else {
  821. std::cerr << "Unknown mode " << *args << "\n";
  822. }
  823. });
  824. }