online.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 par_rdpfeval_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 = strtoull(*args, NULL, 16);
  369. ++args;
  370. }
  371. int num_threads = opts.num_threads;
  372. MPCTIO tio(mpcio, 0, num_threads);
  373. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  374. if (tio.player() == 2) {
  375. RDPFPair dp = tio.rdpfpair(yield, depth);
  376. for (int i=0;i<2;++i) {
  377. RDPF &dpf = dp.dpf[i];
  378. nbits_t depth = dpf.depth();
  379. auto pe = ParallelEval(dpf, start, 0,
  380. address_t(1)<<depth, num_threads, tio.aes_ops());
  381. RegXS result, init;
  382. result = pe.reduce(init, [&dpf] (const ParallelEval<RDPF> &pe,
  383. int thread_num, address_t i, const RDPF::node &leaf) {
  384. return dpf.scaled_xs(leaf);
  385. },
  386. [] (const ParallelEval<RDPF> &pe, RegXS &accum,
  387. const RegXS &value) {
  388. accum ^= value;
  389. });
  390. printf("%016lx\n%016lx\n", result.xshare,
  391. dpf.scaled_xor.xshare);
  392. printf("\n");
  393. }
  394. } else {
  395. RDPFTriple dt = tio.rdpftriple(yield, depth);
  396. for (int i=0;i<3;++i) {
  397. RDPF &dpf = dt.dpf[i];
  398. nbits_t depth = dpf.depth();
  399. auto pe = ParallelEval(dpf, start, 0,
  400. address_t(1)<<depth, num_threads, tio.aes_ops());
  401. RegXS result, init;
  402. result = pe.reduce(init, [&dpf] (const ParallelEval<RDPF> &pe,
  403. int thread_num, address_t i, const RDPF::node &leaf) {
  404. return dpf.scaled_xs(leaf);
  405. },
  406. [] (const ParallelEval<RDPF> &pe, RegXS &accum,
  407. const RegXS &value) {
  408. accum ^= value;
  409. });
  410. printf("%016lx\n%016lx\n", result.xshare,
  411. dpf.scaled_xor.xshare);
  412. printf("\n");
  413. }
  414. }
  415. });
  416. }
  417. static void tupleeval_timing(MPCIO &mpcio,
  418. const PRACOptions &opts, char **args)
  419. {
  420. nbits_t depth=6;
  421. address_t start=0;
  422. if (*args) {
  423. depth = atoi(*args);
  424. ++args;
  425. }
  426. if (*args) {
  427. start = atoi(*args);
  428. ++args;
  429. }
  430. int num_threads = opts.num_threads;
  431. MPCTIO tio(mpcio, 0, num_threads);
  432. run_coroutines(tio, [&tio, depth, start] (yield_t &yield) {
  433. size_t &aes_ops = tio.aes_ops();
  434. if (tio.player() == 2) {
  435. RDPFPair dp = tio.rdpfpair(yield, depth);
  436. RegXS scaled_xor0, scaled_xor1;
  437. auto ev = StreamEval(dp, start, 0, aes_ops, false);
  438. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  439. auto [L0, L1] = ev.next();
  440. RegXS sx0 = dp.dpf[0].scaled_xs(L0);
  441. RegXS sx1 = dp.dpf[1].scaled_xs(L1);
  442. scaled_xor0 ^= sx0;
  443. scaled_xor1 ^= sx1;
  444. }
  445. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  446. dp.dpf[0].scaled_xor.xshare);
  447. printf("\n");
  448. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  449. dp.dpf[1].scaled_xor.xshare);
  450. printf("\n");
  451. } else {
  452. RDPFTriple dt = tio.rdpftriple(yield, depth);
  453. RegXS scaled_xor0, scaled_xor1, scaled_xor2;
  454. auto ev = StreamEval(dt, start, 0, aes_ops, false);
  455. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  456. auto [L0, L1, L2] = ev.next();
  457. RegXS sx0 = dt.dpf[0].scaled_xs(L0);
  458. RegXS sx1 = dt.dpf[1].scaled_xs(L1);
  459. RegXS sx2 = dt.dpf[2].scaled_xs(L2);
  460. scaled_xor0 ^= sx0;
  461. scaled_xor1 ^= sx1;
  462. scaled_xor2 ^= sx2;
  463. }
  464. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  465. dt.dpf[0].scaled_xor.xshare);
  466. printf("\n");
  467. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  468. dt.dpf[1].scaled_xor.xshare);
  469. printf("\n");
  470. printf("%016lx\n%016lx\n", scaled_xor2.xshare,
  471. dt.dpf[2].scaled_xor.xshare);
  472. printf("\n");
  473. }
  474. });
  475. }
  476. static void par_tupleeval_timing(MPCIO &mpcio,
  477. const PRACOptions &opts, char **args)
  478. {
  479. nbits_t depth=6;
  480. address_t start=0;
  481. if (*args) {
  482. depth = atoi(*args);
  483. ++args;
  484. }
  485. if (*args) {
  486. start = atoi(*args);
  487. ++args;
  488. }
  489. int num_threads = opts.num_threads;
  490. MPCTIO tio(mpcio, 0, num_threads);
  491. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  492. size_t &aes_ops = tio.aes_ops();
  493. if (tio.player() == 2) {
  494. RDPFPair dp = tio.rdpfpair(yield, depth);
  495. auto pe = ParallelEval(dp, start, 0, address_t(1)<<depth,
  496. num_threads, aes_ops);
  497. using V = std::tuple<RegXS,RegXS>;
  498. V result, init;
  499. result = pe.reduce(init, [&dp] (const ParallelEval<RDPFPair> &pe,
  500. int thread_num, address_t i, const RDPFPair::node &leaf) {
  501. return dp.scaled<RegXS>(leaf);
  502. },
  503. [] (const ParallelEval<RDPFPair> &pe, V &accum, const V &value) {
  504. accum += value;
  505. });
  506. printf("%016lx\n%016lx\n", std::get<0>(result).xshare,
  507. dp.dpf[0].scaled_xor.xshare);
  508. printf("\n");
  509. printf("%016lx\n%016lx\n", std::get<1>(result).xshare,
  510. dp.dpf[1].scaled_xor.xshare);
  511. printf("\n");
  512. } else {
  513. RDPFTriple dt = tio.rdpftriple(yield, depth);
  514. auto pe = ParallelEval(dt, start, 0, address_t(1)<<depth,
  515. num_threads, aes_ops);
  516. using V = std::tuple<RegXS,RegXS,RegXS>;
  517. V result, init;
  518. result = pe.reduce(init, [&dt] (const ParallelEval<RDPFTriple> &pe,
  519. int thread_num, address_t i, const RDPFTriple::node &leaf) {
  520. return dt.scaled<RegXS>(leaf);
  521. },
  522. [] (const ParallelEval<RDPFTriple> &pe, V &accum, const V &value) {
  523. accum += value;
  524. });
  525. printf("%016lx\n%016lx\n", std::get<0>(result).xshare,
  526. dt.dpf[0].scaled_xor.xshare);
  527. printf("\n");
  528. printf("%016lx\n%016lx\n", std::get<1>(result).xshare,
  529. dt.dpf[1].scaled_xor.xshare);
  530. printf("\n");
  531. printf("%016lx\n%016lx\n", std::get<2>(result).xshare,
  532. dt.dpf[2].scaled_xor.xshare);
  533. printf("\n");
  534. }
  535. });
  536. }
  537. // T is RegAS or RegXS for additive or XOR shared database respectively
  538. template <typename T>
  539. static void duoram_test(MPCIO &mpcio,
  540. const PRACOptions &opts, char **args)
  541. {
  542. nbits_t depth=6;
  543. address_t share=arc4random();
  544. if (*args) {
  545. depth = atoi(*args);
  546. ++args;
  547. }
  548. if (*args) {
  549. share = atoi(*args);
  550. ++args;
  551. }
  552. share &= ((address_t(1)<<depth)-1);
  553. int num_threads = opts.num_threads;
  554. boost::asio::thread_pool pool(num_threads);
  555. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  556. boost::asio::post(pool, [&mpcio, thread_num, depth, share] {
  557. MPCTIO tio(mpcio, thread_num);
  558. run_coroutines(tio, [&tio, depth, share] (yield_t &yield) {
  559. size_t size = size_t(1)<<depth;
  560. // size_t &aes_ops = tio.aes_ops();
  561. Duoram<T> oram(tio.player(), size);
  562. auto A = oram.flat(tio, yield);
  563. RegAS aidx;
  564. aidx.ashare = share;
  565. T M;
  566. if (tio.player() == 0) {
  567. M.set(0xbabb0000);
  568. } else {
  569. M.set(0x0000a66e);
  570. }
  571. RegXS xidx;
  572. xidx.xshare = share;
  573. T N;
  574. if (tio.player() == 0) {
  575. N.set(0xdead0000);
  576. } else {
  577. N.set(0x0000beef);
  578. }
  579. // Writing and reading with additively shared indices
  580. printf("Updating\n");
  581. A[aidx] += M;
  582. printf("Reading\n");
  583. T Aa = A[aidx];
  584. // Writing and reading with XOR shared indices
  585. printf("Updating\n");
  586. A[xidx] += N;
  587. printf("Reading\n");
  588. T Ax = A[xidx];
  589. T Ae;
  590. // Writing and reading with explicit indices
  591. if (depth > 2) {
  592. A[5] += Aa;
  593. Ae = A[6];
  594. }
  595. if (depth <= 10) {
  596. oram.dump();
  597. auto check = A.reconstruct();
  598. if (tio.player() == 0) {
  599. for (address_t i=0;i<size;++i) {
  600. printf("%04x %016lx\n", i, check[i].share());
  601. }
  602. }
  603. }
  604. auto checkread = A.reconstruct(Aa);
  605. auto checkreade = A.reconstruct(Ae);
  606. auto checkreadx = A.reconstruct(Ax);
  607. if (tio.player() == 0) {
  608. printf("Read AS value = %016lx\n", checkread.share());
  609. printf("Read AX value = %016lx\n", checkreadx.share());
  610. printf("Read Ex value = %016lx\n", checkreade.share());
  611. }
  612. });
  613. });
  614. }
  615. pool.join();
  616. }
  617. static void cdpf_test(MPCIO &mpcio,
  618. const PRACOptions &opts, char **args)
  619. {
  620. value_t query, target;
  621. int iters = 1;
  622. arc4random_buf(&query, sizeof(query));
  623. arc4random_buf(&target, sizeof(target));
  624. if (*args) {
  625. query = strtoull(*args, NULL, 16);
  626. ++args;
  627. }
  628. if (*args) {
  629. target = strtoull(*args, NULL, 16);
  630. ++args;
  631. }
  632. if (*args) {
  633. iters = atoi(*args);
  634. ++args;
  635. }
  636. int num_threads = opts.num_threads;
  637. boost::asio::thread_pool pool(num_threads);
  638. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  639. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  640. MPCTIO tio(mpcio, thread_num);
  641. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  642. size_t &aes_ops = tio.aes_ops();
  643. for (int i=0;i<iters;++i) {
  644. if (tio.player() == 2) {
  645. tio.cdpf(yield);
  646. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  647. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  648. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  649. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  650. dump_node(leaf0 ^ leaf1);
  651. } else {
  652. CDPF dpf = tio.cdpf(yield);
  653. printf("ashare = %016lX\nxshare = %016lX\n",
  654. dpf.as_target.ashare, dpf.xs_target.xshare);
  655. DPFnode leaf = dpf.leaf(query, aes_ops);
  656. printf("DPF(%016lx) = ", query);
  657. dump_node(leaf);
  658. if (tio.player() == 1) {
  659. tio.iostream_peer() << leaf;
  660. } else {
  661. DPFnode peerleaf;
  662. tio.iostream_peer() >> peerleaf;
  663. printf("XOR = ");
  664. dump_node(leaf ^ peerleaf);
  665. }
  666. }
  667. }
  668. });
  669. });
  670. }
  671. pool.join();
  672. }
  673. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  674. value_t target, value_t x)
  675. {
  676. int player = tio.player();
  677. size_t &aes_ops = tio.aes_ops();
  678. int res = 1;
  679. if (player == 2) {
  680. // Create a CDPF pair with the given target
  681. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  682. // Send it and a share of x to the computational parties
  683. RegAS x0, x1;
  684. x0.randomize();
  685. x1.set(x-x0.share());
  686. tio.iostream_p0() << dpf0 << x0;
  687. tio.iostream_p1() << dpf1 << x1;
  688. } else {
  689. CDPF dpf;
  690. RegAS xsh;
  691. tio.iostream_server() >> dpf >> xsh;
  692. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  693. printf("%016lx %016lx %d %d %d ", target, x, lt.bshare,
  694. eq.bshare, gt.bshare);
  695. // Check the answer
  696. if (player == 1) {
  697. tio.iostream_peer() << xsh << lt << eq << gt;
  698. } else {
  699. RegAS peer_xsh;
  700. RegBS peer_lt, peer_eq, peer_gt;
  701. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >> peer_gt;
  702. lt ^= peer_lt;
  703. eq ^= peer_eq;
  704. gt ^= peer_gt;
  705. xsh += peer_xsh;
  706. int lti = int(lt.bshare);
  707. int eqi = int(eq.bshare);
  708. int gti = int(gt.bshare);
  709. x = xsh.share();
  710. printf(": %d %d %d ", lti, eqi, gti);
  711. bool signbit = (x >> 63);
  712. if (lti + eqi + gti != 1) {
  713. printf("INCONSISTENT");
  714. res = 0;
  715. } else if (x == 0 && eqi) {
  716. printf("=");
  717. } else if (!signbit && gti) {
  718. printf(">");
  719. } else if (signbit && lti) {
  720. printf("<");
  721. } else {
  722. printf("INCORRECT");
  723. res = 0;
  724. }
  725. }
  726. printf("\n");
  727. }
  728. return res;
  729. }
  730. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  731. value_t target, value_t x)
  732. {
  733. int res = 1;
  734. res &= compare_test_one(tio, yield, target, x);
  735. res &= compare_test_one(tio, yield, target, 0);
  736. res &= compare_test_one(tio, yield, target, 1);
  737. res &= compare_test_one(tio, yield, target, 15);
  738. res &= compare_test_one(tio, yield, target, 16);
  739. res &= compare_test_one(tio, yield, target, 17);
  740. res &= compare_test_one(tio, yield, target, -1);
  741. res &= compare_test_one(tio, yield, target, -15);
  742. res &= compare_test_one(tio, yield, target, -16);
  743. res &= compare_test_one(tio, yield, target, -17);
  744. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  745. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  746. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  747. return res;
  748. }
  749. static void compare_test(MPCIO &mpcio,
  750. const PRACOptions &opts, char **args)
  751. {
  752. value_t target, x;
  753. arc4random_buf(&target, sizeof(target));
  754. arc4random_buf(&x, sizeof(x));
  755. if (*args) {
  756. target = strtoull(*args, NULL, 16);
  757. ++args;
  758. }
  759. if (*args) {
  760. x = strtoull(*args, NULL, 16);
  761. ++args;
  762. }
  763. int num_threads = opts.num_threads;
  764. boost::asio::thread_pool pool(num_threads);
  765. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  766. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  767. MPCTIO tio(mpcio, thread_num);
  768. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  769. int res = 1;
  770. res &= compare_test_target(tio, yield, target, x);
  771. res &= compare_test_target(tio, yield, 0, x);
  772. res &= compare_test_target(tio, yield, 1, x);
  773. res &= compare_test_target(tio, yield, 15, x);
  774. res &= compare_test_target(tio, yield, 16, x);
  775. res &= compare_test_target(tio, yield, 17, x);
  776. res &= compare_test_target(tio, yield, -1, x);
  777. res &= compare_test_target(tio, yield, -15, x);
  778. res &= compare_test_target(tio, yield, -16, x);
  779. res &= compare_test_target(tio, yield, -17, x);
  780. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  781. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  782. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  783. if (tio.player() == 0) {
  784. if (res == 1) {
  785. printf("All tests passed!\n");
  786. } else {
  787. printf("TEST FAILURES\n");
  788. }
  789. }
  790. });
  791. });
  792. }
  793. pool.join();
  794. }
  795. static void sort_test(MPCIO &mpcio,
  796. const PRACOptions &opts, char **args)
  797. {
  798. nbits_t depth=6;
  799. if (*args) {
  800. depth = atoi(*args);
  801. ++args;
  802. }
  803. int num_threads = opts.num_threads;
  804. boost::asio::thread_pool pool(num_threads);
  805. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  806. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  807. MPCTIO tio(mpcio, thread_num);
  808. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  809. address_t size = address_t(1)<<depth;
  810. // size_t &aes_ops = tio.aes_ops();
  811. Duoram<RegAS> oram(tio.player(), size);
  812. auto A = oram.flat(tio, yield);
  813. A.explicitonly(true);
  814. // Initialize the memory to random values in parallel
  815. std::vector<coro_t> coroutines;
  816. for (address_t i=0; i<size; ++i) {
  817. coroutines.emplace_back(
  818. [&A, i](yield_t &yield) {
  819. auto Acoro = A.context(yield);
  820. RegAS v;
  821. v.randomize(62);
  822. Acoro[i] += v;
  823. });
  824. }
  825. run_coroutines(yield, coroutines);
  826. A.bitonic_sort(0, depth);
  827. if (depth <= 10) {
  828. oram.dump();
  829. auto check = A.reconstruct();
  830. if (tio.player() == 0) {
  831. for (address_t i=0;i<size;++i) {
  832. printf("%04x %016lx\n", i, check[i].share());
  833. }
  834. }
  835. }
  836. });
  837. });
  838. }
  839. pool.join();
  840. }
  841. static void bsearch_test(MPCIO &mpcio,
  842. const PRACOptions &opts, char **args)
  843. {
  844. value_t target;
  845. arc4random_buf(&target, sizeof(target));
  846. target >>= 1;
  847. nbits_t depth=6;
  848. if (*args) {
  849. depth = atoi(*args);
  850. ++args;
  851. }
  852. if (*args) {
  853. target = strtoull(*args, NULL, 16);
  854. ++args;
  855. }
  856. int num_threads = opts.num_threads;
  857. boost::asio::thread_pool pool(num_threads);
  858. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  859. boost::asio::post(pool, [&mpcio, thread_num, depth, target] {
  860. MPCTIO tio(mpcio, thread_num);
  861. run_coroutines(tio, [&tio, depth, target] (yield_t &yield) {
  862. address_t size = address_t(1)<<depth;
  863. RegAS tshare;
  864. if (tio.player() == 2) {
  865. // Send shares of the target to the computational
  866. // players
  867. RegAS tshare0, tshare1;
  868. tshare0.randomize();
  869. tshare1.set(target-tshare0.share());
  870. tio.iostream_p0() << tshare0;
  871. tio.iostream_p1() << tshare1;
  872. printf("Using target = %016lx\n", target);
  873. yield();
  874. } else {
  875. // Get the share of the target
  876. tio.iostream_server() >> tshare;
  877. }
  878. // Create a random database and sort it
  879. // size_t &aes_ops = tio.aes_ops();
  880. Duoram<RegAS> oram(tio.player(), size);
  881. auto A = oram.flat(tio, yield);
  882. A.explicitonly(true);
  883. // Initialize the memory to random values in parallel
  884. std::vector<coro_t> coroutines;
  885. for (address_t i=0; i<size; ++i) {
  886. coroutines.emplace_back(
  887. [&A, i](yield_t &yield) {
  888. auto Acoro = A.context(yield);
  889. RegAS v;
  890. v.randomize(62);
  891. Acoro[i] += v;
  892. });
  893. }
  894. run_coroutines(yield, coroutines);
  895. A.bitonic_sort(0, depth);
  896. // Binary search for the target
  897. RegAS tindex = A.obliv_binary_search(tshare);
  898. // Check the answer
  899. if (tio.player() == 1) {
  900. tio.iostream_peer() << tindex;
  901. } else if (tio.player() == 0) {
  902. RegAS peer_tindex;
  903. tio.iostream_peer() >> peer_tindex;
  904. tindex += peer_tindex;
  905. }
  906. if (depth <= 10) {
  907. auto check = A.reconstruct();
  908. if (tio.player() == 0) {
  909. for (address_t i=0;i<size;++i) {
  910. printf("%04x %016lx\n", i, check[i].share());
  911. }
  912. }
  913. }
  914. if (tio.player() == 0) {
  915. printf("Found index = %lx\n", tindex.share());
  916. }
  917. });
  918. });
  919. }
  920. pool.join();
  921. }
  922. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  923. {
  924. MPCTIO tio(mpcio, 0);
  925. if (!*args) {
  926. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  927. return;
  928. } else if (!strcmp(*args, "test")) {
  929. ++args;
  930. online_test(mpcio, opts, args);
  931. } else if (!strcmp(*args, "lamporttest")) {
  932. ++args;
  933. lamport_test(mpcio, opts, args);
  934. } else if (!strcmp(*args, "rdpftest")) {
  935. ++args;
  936. rdpf_test(mpcio, opts, args);
  937. } else if (!strcmp(*args, "rdpftime")) {
  938. ++args;
  939. rdpf_timing(mpcio, opts, args);
  940. } else if (!strcmp(*args, "evaltime")) {
  941. ++args;
  942. rdpfeval_timing(mpcio, opts, args);
  943. } else if (!strcmp(*args, "parevaltime")) {
  944. ++args;
  945. par_rdpfeval_timing(mpcio, opts, args);
  946. } else if (!strcmp(*args, "tupletime")) {
  947. ++args;
  948. tupleeval_timing(mpcio, opts, args);
  949. } else if (!strcmp(*args, "partupletime")) {
  950. ++args;
  951. par_tupleeval_timing(mpcio, opts, args);
  952. } else if (!strcmp(*args, "duotest")) {
  953. ++args;
  954. if (opts.use_xor_db) {
  955. duoram_test<RegXS>(mpcio, opts, args);
  956. } else {
  957. duoram_test<RegAS>(mpcio, opts, args);
  958. }
  959. } else if (!strcmp(*args, "cdpftest")) {
  960. ++args;
  961. cdpf_test(mpcio, opts, args);
  962. } else if (!strcmp(*args, "cmptest")) {
  963. ++args;
  964. compare_test(mpcio, opts, args);
  965. } else if (!strcmp(*args, "sorttest")) {
  966. ++args;
  967. sort_test(mpcio, opts, args);
  968. } else if (!strcmp(*args, "bsearch")) {
  969. ++args;
  970. bsearch_test(mpcio, opts, args);
  971. } else {
  972. std::cerr << "Unknown mode " << *args << "\n";
  973. }
  974. }