online.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. #include <bsd/stdlib.h> // arc4random_buf
  2. #include "online.hpp"
  3. #include "mpcops.hpp"
  4. #include "rdpf.hpp"
  5. #include "duoram.hpp"
  6. #include "cdpf.hpp"
  7. #include "cell.hpp"
  8. static void online_test(MPCIO &mpcio,
  9. const PRACOptions &opts, char **args)
  10. {
  11. nbits_t nbits = VALUE_BITS;
  12. if (*args) {
  13. nbits = atoi(*args);
  14. }
  15. size_t as_memsize = 9;
  16. size_t xs_memsize = 3;
  17. MPCTIO tio(mpcio, 0);
  18. bool is_server = (mpcio.player == 2);
  19. RegAS *A = new RegAS[as_memsize];
  20. RegXS *AX = new RegXS[xs_memsize];
  21. value_t V;
  22. RegBS F0, F1;
  23. RegBS FA, FO;
  24. RegXS X;
  25. if (!is_server) {
  26. A[0].randomize();
  27. A[1].randomize();
  28. F0.randomize();
  29. A[4].randomize();
  30. F1.randomize();
  31. A[6].randomize();
  32. A[7].randomize();
  33. X.randomize();
  34. AX[0].randomize();
  35. AX[1].randomize();
  36. arc4random_buf(&V, sizeof(V));
  37. printf("A:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  38. printf("AX:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, AX[i].xshare);
  39. printf("V : %016lX\n", V);
  40. printf("F0 : %01X\n", F0.bshare);
  41. printf("F1 : %01X\n", F1.bshare);
  42. printf("X : %016lX\n", X.xshare);
  43. }
  44. std::vector<coro_t> coroutines;
  45. coroutines.emplace_back(
  46. [&tio, &A, nbits](yield_t &yield) {
  47. mpc_mul(tio, yield, A[2], A[0], A[1], nbits);
  48. });
  49. coroutines.emplace_back(
  50. [&tio, &A, V, nbits](yield_t &yield) {
  51. mpc_valuemul(tio, yield, A[3], V, nbits);
  52. });
  53. coroutines.emplace_back(
  54. [&tio, &A, &F0, nbits](yield_t &yield) {
  55. mpc_flagmult(tio, yield, A[5], F0, A[4], nbits);
  56. });
  57. coroutines.emplace_back(
  58. [&tio, &A, &F1, nbits](yield_t &yield) {
  59. mpc_oswap(tio, yield, A[6], A[7], F1, nbits);
  60. });
  61. coroutines.emplace_back(
  62. [&tio, &A, &X, nbits](yield_t &yield) {
  63. mpc_xs_to_as(tio, yield, A[8], X, nbits);
  64. });
  65. coroutines.emplace_back(
  66. [&tio, &AX, &F0, nbits](yield_t &yield) {
  67. mpc_select(tio, yield, AX[2], F0, AX[0], AX[1], nbits);
  68. });
  69. coroutines.emplace_back(
  70. [&tio, &FA, &F0, &F1, nbits](yield_t &yield) {
  71. mpc_and(tio, yield, FA, F0, F1);
  72. });
  73. coroutines.emplace_back(
  74. [&tio, &FO, &F0, &F1, nbits](yield_t &yield) {
  75. mpc_or(tio, yield, FO, F0, F1);
  76. });
  77. run_coroutines(tio, coroutines);
  78. if (!is_server) {
  79. printf("\n");
  80. printf("A:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, A[i].ashare);
  81. printf("AX:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, AX[i].xshare);
  82. }
  83. // Check the answers
  84. if (mpcio.player == 1) {
  85. tio.queue_peer(A, as_memsize*sizeof(RegAS));
  86. tio.queue_peer(AX, xs_memsize*sizeof(RegXS));
  87. tio.queue_peer(&V, sizeof(V));
  88. tio.queue_peer(&F0, sizeof(RegBS));
  89. tio.queue_peer(&F1, sizeof(RegBS));
  90. tio.queue_peer(&FA, sizeof(RegBS));
  91. tio.queue_peer(&FO, sizeof(RegBS));
  92. tio.queue_peer(&X, sizeof(RegXS));
  93. tio.send();
  94. } else if (mpcio.player == 0) {
  95. RegAS *B = new RegAS[as_memsize];
  96. RegXS *BAX = new RegXS[xs_memsize];
  97. RegBS BF0, BF1;
  98. RegBS BFA, BFO;
  99. RegXS BX;
  100. value_t BV;
  101. value_t *S = new value_t[as_memsize];
  102. value_t *Y = new value_t[xs_memsize];
  103. bit_t SF0, SF1;
  104. bit_t SFA, SFO;
  105. value_t SX;
  106. tio.recv_peer(B, as_memsize*sizeof(RegAS));
  107. tio.recv_peer(BAX, xs_memsize*sizeof(RegXS));
  108. tio.recv_peer(&BV, sizeof(BV));
  109. tio.recv_peer(&BF0, sizeof(RegBS));
  110. tio.recv_peer(&BF1, sizeof(RegBS));
  111. tio.recv_peer(&BFA, sizeof(RegBS));
  112. tio.recv_peer(&BFO, sizeof(RegBS));
  113. tio.recv_peer(&BX, sizeof(RegXS));
  114. for(size_t i=0; i<as_memsize; ++i) S[i] = A[i].ashare+B[i].ashare;
  115. for(size_t i=0; i<xs_memsize; ++i) Y[i] = AX[i].xshare^BAX[i].xshare;
  116. SF0 = F0.bshare ^ BF0.bshare;
  117. SF1 = F1.bshare ^ BF1.bshare;
  118. SFA = FA.bshare ^ BFA.bshare;
  119. SFO = FO.bshare ^ BFO.bshare;
  120. SX = X.xshare ^ BX.xshare;
  121. printf("S:\n"); for (size_t i=0; i<as_memsize; ++i) printf("%3lu: %016lX\n", i, S[i]);
  122. printf("Y:\n"); for (size_t i=0; i<xs_memsize; ++i) printf("%3lu: %016lX\n", i, Y[i]);
  123. printf("SF0: %01X\n", SF0);
  124. printf("SF1: %01X\n", SF1);
  125. printf("SFA: %01X\n", SFA);
  126. printf("SFO: %01X\n", SFO);
  127. printf("SX : %016lX\n", SX);
  128. printf("\n%016lx\n", S[0]*S[1]-S[2]);
  129. printf("%016lx\n", (V*BV)-S[3]);
  130. printf("%016lx\n", (SF0*S[4])-S[5]);
  131. printf("%016lx\n", S[8]-SX);
  132. delete[] B;
  133. delete[] S;
  134. }
  135. delete[] A;
  136. delete[] AX;
  137. }
  138. static void lamport_test(MPCIO &mpcio,
  139. const PRACOptions &opts, char **args)
  140. {
  141. // Create a bunch of threads and send a bunch of data to the other
  142. // peer, and receive their data. If an arg is specified, repeat
  143. // that many times. The Lamport clock at the end should be just the
  144. // number of repetitions. Subsequent args are the chunk size and
  145. // the number of chunks per message
  146. size_t niters = 1;
  147. size_t chunksize = 1<<20;
  148. size_t numchunks = 1;
  149. if (*args) {
  150. niters = atoi(*args);
  151. ++args;
  152. }
  153. if (*args) {
  154. chunksize = atoi(*args);
  155. ++args;
  156. }
  157. if (*args) {
  158. numchunks = atoi(*args);
  159. ++args;
  160. }
  161. int num_threads = opts.num_threads;
  162. boost::asio::thread_pool pool(num_threads);
  163. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  164. boost::asio::post(pool, [&mpcio, thread_num, niters, chunksize, numchunks] {
  165. MPCTIO tio(mpcio, thread_num);
  166. char *sendbuf = new char[chunksize];
  167. char *recvbuf = new char[chunksize*numchunks];
  168. for (size_t i=0; i<niters; ++i) {
  169. for (size_t chunk=0; chunk<numchunks; ++chunk) {
  170. arc4random_buf(sendbuf, chunksize);
  171. tio.queue_peer(sendbuf, chunksize);
  172. }
  173. tio.send();
  174. tio.recv_peer(recvbuf, chunksize*numchunks);
  175. }
  176. delete[] recvbuf;
  177. delete[] sendbuf;
  178. });
  179. }
  180. pool.join();
  181. }
  182. static void rdpf_test(MPCIO &mpcio,
  183. const PRACOptions &opts, char **args)
  184. {
  185. nbits_t depth=6;
  186. size_t num_iters = 1;
  187. if (*args) {
  188. depth = atoi(*args);
  189. ++args;
  190. }
  191. if (*args) {
  192. num_iters = atoi(*args);
  193. ++args;
  194. }
  195. int num_threads = opts.num_threads;
  196. boost::asio::thread_pool pool(num_threads);
  197. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  198. boost::asio::post(pool, [&mpcio, thread_num, depth, num_iters] {
  199. MPCTIO tio(mpcio, thread_num);
  200. run_coroutines(tio, [&tio, depth, num_iters] (yield_t &yield) {
  201. size_t &aes_ops = tio.aes_ops();
  202. for (size_t iter=0; iter < num_iters; ++iter) {
  203. if (tio.player() == 2) {
  204. RDPFPair dp = tio.rdpfpair(yield, depth);
  205. for (int i=0;i<2;++i) {
  206. const RDPF &dpf = dp.dpf[i];
  207. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  208. DPFnode leaf = dpf.leaf(x, aes_ops);
  209. RegBS ub = dpf.unit_bs(leaf);
  210. RegAS ua = dpf.unit_as(leaf);
  211. RegXS sx = dpf.scaled_xs(leaf);
  212. RegAS sa = dpf.scaled_as(leaf);
  213. printf("%04x %x %016lx %016lx %016lx\n", x,
  214. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  215. }
  216. printf("\n");
  217. }
  218. } else {
  219. RDPFTriple dt = tio.rdpftriple(yield, depth);
  220. for (int i=0;i<3;++i) {
  221. const RDPF &dpf = dt.dpf[i];
  222. RegXS peer_scaled_xor;
  223. RegAS peer_scaled_sum;
  224. if (tio.player() == 1) {
  225. tio.iostream_peer() << dpf.scaled_xor << dpf.scaled_sum;
  226. } else {
  227. tio.iostream_peer() >> peer_scaled_xor >> peer_scaled_sum;
  228. peer_scaled_sum += dpf.scaled_sum;
  229. peer_scaled_xor ^= dpf.scaled_xor;
  230. }
  231. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  232. DPFnode leaf = dpf.leaf(x, aes_ops);
  233. RegBS ub = dpf.unit_bs(leaf);
  234. RegAS ua = dpf.unit_as(leaf);
  235. RegXS sx = dpf.scaled_xs(leaf);
  236. RegAS sa = dpf.scaled_as(leaf);
  237. printf("%04x %x %016lx %016lx %016lx\n", x,
  238. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  239. if (tio.player() == 1) {
  240. tio.iostream_peer() << ub << ua << sx << sa;
  241. } else {
  242. RegBS peer_ub;
  243. RegAS peer_ua;
  244. RegXS peer_sx;
  245. RegAS peer_sa;
  246. tio.iostream_peer() >> peer_ub >> peer_ua >>
  247. peer_sx >> peer_sa;
  248. ub ^= peer_ub;
  249. ua += peer_ua;
  250. sx ^= peer_sx;
  251. sa += peer_sa;
  252. if (ub.bshare || ua.ashare || sx.xshare || sa.ashare) {
  253. printf("**** %x %016lx %016lx %016lx\n",
  254. ub.bshare, ua.ashare, sx.xshare, sa.ashare);
  255. printf("SCALE %016lx %016lx\n",
  256. peer_scaled_xor.xshare, peer_scaled_sum.ashare);
  257. }
  258. }
  259. }
  260. printf("\n");
  261. }
  262. }
  263. }
  264. });
  265. });
  266. }
  267. pool.join();
  268. }
  269. static void rdpf_timing(MPCIO &mpcio,
  270. const PRACOptions &opts, char **args)
  271. {
  272. nbits_t depth=6;
  273. if (*args) {
  274. depth = atoi(*args);
  275. ++args;
  276. }
  277. int num_threads = opts.num_threads;
  278. boost::asio::thread_pool pool(num_threads);
  279. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  280. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  281. MPCTIO tio(mpcio, thread_num);
  282. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  283. size_t &aes_ops = tio.aes_ops();
  284. if (tio.player() == 2) {
  285. RDPFPair dp = tio.rdpfpair(yield, depth);
  286. for (int i=0;i<2;++i) {
  287. RDPF &dpf = dp.dpf[i];
  288. dpf.expand(aes_ops);
  289. RegXS scaled_xor;
  290. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  291. DPFnode leaf = dpf.leaf(x, aes_ops);
  292. RegXS sx = dpf.scaled_xs(leaf);
  293. scaled_xor ^= sx;
  294. }
  295. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  296. dpf.scaled_xor.xshare);
  297. printf("\n");
  298. }
  299. } else {
  300. RDPFTriple dt = tio.rdpftriple(yield, depth);
  301. for (int i=0;i<3;++i) {
  302. RDPF &dpf = dt.dpf[i];
  303. dpf.expand(aes_ops);
  304. RegXS scaled_xor;
  305. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  306. DPFnode leaf = dpf.leaf(x, aes_ops);
  307. RegXS sx = dpf.scaled_xs(leaf);
  308. scaled_xor ^= sx;
  309. }
  310. printf("%016lx\n%016lx\n", scaled_xor.xshare,
  311. dpf.scaled_xor.xshare);
  312. printf("\n");
  313. }
  314. }
  315. });
  316. });
  317. }
  318. pool.join();
  319. }
  320. static value_t parallel_streameval_rdpf(MPCIO &mpcio, const RDPF &dpf,
  321. address_t start, int num_threads)
  322. {
  323. RegXS scaled_xor[num_threads];
  324. boost::asio::thread_pool pool(num_threads);
  325. address_t totsize = (address_t(1)<<dpf.depth());
  326. address_t threadstart = start;
  327. address_t threadchunk = totsize / num_threads;
  328. address_t threadextra = totsize % num_threads;
  329. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  330. address_t threadsize = threadchunk + (address_t(thread_num) < threadextra);
  331. boost::asio::post(pool,
  332. [&mpcio, &dpf, &scaled_xor, thread_num, threadstart, threadsize] {
  333. MPCTIO tio(mpcio, thread_num);
  334. //printf("Thread %d from %X for %X\n", thread_num, threadstart, threadsize);
  335. RegXS local_xor;
  336. size_t local_aes_ops = 0;
  337. auto ev = StreamEval(dpf, threadstart, 0, local_aes_ops);
  338. for (address_t x=0;x<threadsize;++x) {
  339. //if (x%0x10000 == 0) printf("%d", thread_num);
  340. DPFnode leaf = ev.next();
  341. local_xor ^= dpf.scaled_xs(leaf);
  342. }
  343. scaled_xor[thread_num] = local_xor;
  344. tio.aes_ops() += local_aes_ops;
  345. //printf("Thread %d complete\n", thread_num);
  346. });
  347. threadstart = (threadstart + threadsize) % totsize;
  348. }
  349. pool.join();
  350. RegXS res;
  351. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  352. res ^= scaled_xor[thread_num];
  353. }
  354. return res.xshare;
  355. }
  356. static void rdpfeval_timing(MPCIO &mpcio,
  357. const PRACOptions &opts, char **args)
  358. {
  359. nbits_t depth=6;
  360. address_t start=0;
  361. if (*args) {
  362. depth = atoi(*args);
  363. ++args;
  364. }
  365. if (*args) {
  366. start = strtoull(*args, NULL, 16);
  367. ++args;
  368. }
  369. int num_threads = opts.num_threads;
  370. MPCTIO tio(mpcio, 0, num_threads);
  371. run_coroutines(tio, [&mpcio, &tio, depth, start, num_threads] (yield_t &yield) {
  372. if (tio.player() == 2) {
  373. RDPFPair dp = tio.rdpfpair(yield, depth);
  374. for (int i=0;i<2;++i) {
  375. RDPF &dpf = dp.dpf[i];
  376. value_t scaled_xor =
  377. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  378. printf("%016lx\n%016lx\n", scaled_xor,
  379. dpf.scaled_xor.xshare);
  380. printf("\n");
  381. }
  382. } else {
  383. RDPFTriple dt = tio.rdpftriple(yield, depth);
  384. for (int i=0;i<3;++i) {
  385. RDPF &dpf = dt.dpf[i];
  386. value_t scaled_xor =
  387. parallel_streameval_rdpf(mpcio, dpf, start, num_threads);
  388. printf("%016lx\n%016lx\n", scaled_xor,
  389. dpf.scaled_xor.xshare);
  390. printf("\n");
  391. }
  392. }
  393. });
  394. }
  395. static void par_rdpfeval_timing(MPCIO &mpcio,
  396. const PRACOptions &opts, char **args)
  397. {
  398. nbits_t depth=6;
  399. address_t start=0;
  400. if (*args) {
  401. depth = atoi(*args);
  402. ++args;
  403. }
  404. if (*args) {
  405. start = strtoull(*args, NULL, 16);
  406. ++args;
  407. }
  408. int num_threads = opts.num_threads;
  409. MPCTIO tio(mpcio, 0, num_threads);
  410. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  411. if (tio.player() == 2) {
  412. RDPFPair dp = tio.rdpfpair(yield, depth);
  413. for (int i=0;i<2;++i) {
  414. RDPF &dpf = dp.dpf[i];
  415. nbits_t depth = dpf.depth();
  416. auto pe = ParallelEval(dpf, start, 0,
  417. address_t(1)<<depth, num_threads, tio.aes_ops());
  418. RegXS result, init;
  419. result = pe.reduce(init, [&dpf] (int thread_num,
  420. address_t i, const RDPF::node &leaf) {
  421. return dpf.scaled_xs(leaf);
  422. });
  423. printf("%016lx\n%016lx\n", result.xshare,
  424. dpf.scaled_xor.xshare);
  425. printf("\n");
  426. }
  427. } else {
  428. RDPFTriple dt = tio.rdpftriple(yield, depth);
  429. for (int i=0;i<3;++i) {
  430. RDPF &dpf = dt.dpf[i];
  431. nbits_t depth = dpf.depth();
  432. auto pe = ParallelEval(dpf, start, 0,
  433. address_t(1)<<depth, num_threads, tio.aes_ops());
  434. RegXS result, init;
  435. result = pe.reduce(init, [&dpf] (int thread_num,
  436. address_t i, const RDPF::node &leaf) {
  437. return dpf.scaled_xs(leaf);
  438. });
  439. printf("%016lx\n%016lx\n", result.xshare,
  440. dpf.scaled_xor.xshare);
  441. printf("\n");
  442. }
  443. }
  444. });
  445. }
  446. static void tupleeval_timing(MPCIO &mpcio,
  447. const PRACOptions &opts, char **args)
  448. {
  449. nbits_t depth=6;
  450. address_t start=0;
  451. if (*args) {
  452. depth = atoi(*args);
  453. ++args;
  454. }
  455. if (*args) {
  456. start = atoi(*args);
  457. ++args;
  458. }
  459. int num_threads = opts.num_threads;
  460. MPCTIO tio(mpcio, 0, num_threads);
  461. run_coroutines(tio, [&tio, depth, start] (yield_t &yield) {
  462. size_t &aes_ops = tio.aes_ops();
  463. if (tio.player() == 2) {
  464. RDPFPair dp = tio.rdpfpair(yield, depth);
  465. RegXS scaled_xor0, scaled_xor1;
  466. auto ev = StreamEval(dp, start, 0, aes_ops, false);
  467. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  468. auto [L0, L1] = ev.next();
  469. RegXS sx0 = dp.dpf[0].scaled_xs(L0);
  470. RegXS sx1 = dp.dpf[1].scaled_xs(L1);
  471. scaled_xor0 ^= sx0;
  472. scaled_xor1 ^= sx1;
  473. }
  474. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  475. dp.dpf[0].scaled_xor.xshare);
  476. printf("\n");
  477. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  478. dp.dpf[1].scaled_xor.xshare);
  479. printf("\n");
  480. } else {
  481. RDPFTriple dt = tio.rdpftriple(yield, depth);
  482. RegXS scaled_xor0, scaled_xor1, scaled_xor2;
  483. auto ev = StreamEval(dt, start, 0, aes_ops, false);
  484. for (address_t x=0;x<(address_t(1)<<depth);++x) {
  485. auto [L0, L1, L2] = ev.next();
  486. RegXS sx0 = dt.dpf[0].scaled_xs(L0);
  487. RegXS sx1 = dt.dpf[1].scaled_xs(L1);
  488. RegXS sx2 = dt.dpf[2].scaled_xs(L2);
  489. scaled_xor0 ^= sx0;
  490. scaled_xor1 ^= sx1;
  491. scaled_xor2 ^= sx2;
  492. }
  493. printf("%016lx\n%016lx\n", scaled_xor0.xshare,
  494. dt.dpf[0].scaled_xor.xshare);
  495. printf("\n");
  496. printf("%016lx\n%016lx\n", scaled_xor1.xshare,
  497. dt.dpf[1].scaled_xor.xshare);
  498. printf("\n");
  499. printf("%016lx\n%016lx\n", scaled_xor2.xshare,
  500. dt.dpf[2].scaled_xor.xshare);
  501. printf("\n");
  502. }
  503. });
  504. }
  505. static void par_tupleeval_timing(MPCIO &mpcio,
  506. const PRACOptions &opts, char **args)
  507. {
  508. nbits_t depth=6;
  509. address_t start=0;
  510. if (*args) {
  511. depth = atoi(*args);
  512. ++args;
  513. }
  514. if (*args) {
  515. start = atoi(*args);
  516. ++args;
  517. }
  518. int num_threads = opts.num_threads;
  519. MPCTIO tio(mpcio, 0, num_threads);
  520. run_coroutines(tio, [&tio, depth, start, num_threads] (yield_t &yield) {
  521. size_t &aes_ops = tio.aes_ops();
  522. if (tio.player() == 2) {
  523. RDPFPair dp = tio.rdpfpair(yield, depth);
  524. auto pe = ParallelEval(dp, start, 0, address_t(1)<<depth,
  525. num_threads, aes_ops);
  526. using V = std::tuple<RegXS,RegXS>;
  527. V result, init;
  528. result = pe.reduce(init, [&dp] (int thread_num, address_t i,
  529. const RDPFPair::node &leaf) {
  530. return dp.scaled<RegXS>(leaf);
  531. });
  532. printf("%016lx\n%016lx\n", std::get<0>(result).xshare,
  533. dp.dpf[0].scaled_xor.xshare);
  534. printf("\n");
  535. printf("%016lx\n%016lx\n", std::get<1>(result).xshare,
  536. dp.dpf[1].scaled_xor.xshare);
  537. printf("\n");
  538. } else {
  539. RDPFTriple dt = tio.rdpftriple(yield, depth);
  540. auto pe = ParallelEval(dt, start, 0, address_t(1)<<depth,
  541. num_threads, aes_ops);
  542. using V = std::tuple<RegXS,RegXS,RegXS>;
  543. V result, init;
  544. result = pe.reduce(init, [&dt] (int thread_num, address_t i,
  545. const RDPFTriple::node &leaf) {
  546. return dt.scaled<RegXS>(leaf);
  547. });
  548. printf("%016lx\n%016lx\n", std::get<0>(result).xshare,
  549. dt.dpf[0].scaled_xor.xshare);
  550. printf("\n");
  551. printf("%016lx\n%016lx\n", std::get<1>(result).xshare,
  552. dt.dpf[1].scaled_xor.xshare);
  553. printf("\n");
  554. printf("%016lx\n%016lx\n", std::get<2>(result).xshare,
  555. dt.dpf[2].scaled_xor.xshare);
  556. printf("\n");
  557. }
  558. });
  559. }
  560. // T is RegAS or RegXS for additive or XOR shared database respectively
  561. template <typename T>
  562. static void duoram_test(MPCIO &mpcio,
  563. const PRACOptions &opts, char **args)
  564. {
  565. nbits_t depth=6;
  566. address_t share=arc4random();
  567. if (*args) {
  568. depth = atoi(*args);
  569. ++args;
  570. }
  571. if (*args) {
  572. share = atoi(*args);
  573. ++args;
  574. }
  575. share &= ((address_t(1)<<depth)-1);
  576. MPCTIO tio(mpcio, 0, opts.num_threads);
  577. run_coroutines(tio, [&tio, depth, share] (yield_t &yield) {
  578. size_t size = size_t(1)<<depth;
  579. // size_t &aes_ops = tio.aes_ops();
  580. Duoram<T> oram(tio.player(), size);
  581. auto A = oram.flat(tio, yield);
  582. RegAS aidx, aidx2, aidx3;
  583. aidx.ashare = share;
  584. aidx2.ashare = share + tio.player();
  585. aidx3.ashare = share + 1;
  586. T M;
  587. if (tio.player() == 0) {
  588. M.set(0xbabb0000);
  589. } else {
  590. M.set(0x0000a66e);
  591. }
  592. RegXS xidx;
  593. xidx.xshare = share;
  594. T N;
  595. if (tio.player() == 0) {
  596. N.set(0xdead0000);
  597. } else {
  598. N.set(0x0000beef);
  599. }
  600. // Writing and reading with additively shared indices
  601. printf("Additive Updating\n");
  602. A[aidx] += M;
  603. printf("Additive Reading\n");
  604. T Aa = A[aidx];
  605. // Writing and reading with XOR shared indices
  606. printf("XOR Updating\n");
  607. A[xidx] += N;
  608. printf("XOR Reading\n");
  609. T Ax = A[xidx];
  610. T Ae;
  611. // Writing and reading with explicit indices
  612. if (depth > 2) {
  613. printf("Explicit Updating\n");
  614. A[5] += Aa;
  615. printf("Explicit Reading\n");
  616. Ae = A[6];
  617. }
  618. // Simultaneous independent reads
  619. printf("3 independent reading\n");
  620. std::vector<T> Av = A[std::array {
  621. aidx, aidx2, aidx3
  622. }];
  623. // Simultaneous independent updates
  624. T Aw1, Aw2, Aw3;
  625. Aw1.set(0x101010101010101 * tio.player());
  626. Aw2.set(0x202020202020202 * tio.player());
  627. Aw3.set(0x303030303030303 * tio.player());
  628. printf("3 independent updating\n");
  629. A[std::array { aidx, aidx2, aidx3 }] -=
  630. std::array { Aw1, Aw2, Aw3 };
  631. if (depth <= 10) {
  632. oram.dump();
  633. auto check = A.reconstruct();
  634. if (tio.player() == 0) {
  635. for (address_t i=0;i<size;++i) {
  636. printf("%04x %016lx\n", i, check[i].share());
  637. }
  638. }
  639. }
  640. auto checkread = A.reconstruct(Aa);
  641. auto checkreade = A.reconstruct(Ae);
  642. auto checkreadx = A.reconstruct(Ax);
  643. if (tio.player() == 0) {
  644. printf("Read AS value = %016lx\n", checkread.share());
  645. printf("Read AX value = %016lx\n", checkreadx.share());
  646. printf("Read Ex value = %016lx\n", checkreade.share());
  647. }
  648. for (auto &v : Av) {
  649. auto checkv = A.reconstruct(v);
  650. if (tio.player() == 0) {
  651. printf("Read Av value = %016lx\n", checkv.share());
  652. }
  653. }
  654. });
  655. }
  656. // This measures the same things as the Duoram paper: dependent and
  657. // independent reads, updates, writes, and interleaves
  658. // T is RegAS or RegXS for additive or XOR shared database respectively
  659. template <typename T>
  660. static void duoram(MPCIO &mpcio,
  661. const PRACOptions &opts, char **args)
  662. {
  663. nbits_t depth = 6;
  664. int items = 4;
  665. if (*args) {
  666. depth = atoi(*args);
  667. ++args;
  668. }
  669. if (*args) {
  670. items = atoi(*args);
  671. ++args;
  672. }
  673. MPCTIO tio(mpcio, 0, opts.num_threads);
  674. run_coroutines(tio, [&mpcio, &tio, depth, items] (yield_t &yield) {
  675. size_t size = size_t(1)<<depth;
  676. address_t mask = (depth < ADDRESS_MAX_BITS ?
  677. ((address_t(1)<<depth) - 1) : ~0);
  678. Duoram<T> oram(tio.player(), size);
  679. auto A = oram.flat(tio, yield);
  680. std::cout << "===== DEPENDENT UPDATES =====\n";
  681. mpcio.reset_stats();
  682. tio.reset_lamport();
  683. // Make a linked list of length items
  684. std::vector<T> list_indices;
  685. T prev_index, next_index;
  686. prev_index.randomize(depth);
  687. for (int i=0;i<items;++i) {
  688. next_index.randomize(depth);
  689. A[next_index] += prev_index;
  690. list_indices.push_back(next_index);
  691. prev_index = next_index;
  692. }
  693. tio.sync_lamport();
  694. mpcio.dump_stats(std::cout);
  695. std::cout << "\n===== DEPENDENT READS =====\n";
  696. mpcio.reset_stats();
  697. tio.reset_lamport();
  698. // Read the linked list starting with prev_index
  699. T cur_index = prev_index;
  700. for (int i=0;i<items;++i) {
  701. cur_index = A[cur_index];
  702. }
  703. tio.sync_lamport();
  704. mpcio.dump_stats(std::cout);
  705. std::cout << "\n===== INDEPENDENT READS =====\n";
  706. mpcio.reset_stats();
  707. tio.reset_lamport();
  708. // Read all the entries in the list at once
  709. std::vector<T> read_outputs = A[list_indices];
  710. tio.sync_lamport();
  711. mpcio.dump_stats(std::cout);
  712. std::cout << "\n===== INDEPENDENT UPDATES =====\n";
  713. mpcio.reset_stats();
  714. tio.reset_lamport();
  715. // Make a vector of indices 1 larger than those in list_indices,
  716. // and a vector of values 1 larger than those in outputs
  717. std::vector<T> indep_indices, indep_values;
  718. T one;
  719. one.set(tio.player()); // Sets the shared value to 1
  720. for (int i=0;i<items;++i) {
  721. indep_indices.push_back(list_indices[i]+one);
  722. indep_values.push_back(read_outputs[i]+one);
  723. }
  724. // Update all the indices at once
  725. A[indep_indices] += indep_values;
  726. tio.sync_lamport();
  727. mpcio.dump_stats(std::cout);
  728. std::cout << "\n===== DEPENDENT WRITES =====\n";
  729. mpcio.reset_stats();
  730. tio.reset_lamport();
  731. T two;
  732. two.set(2*tio.player()); // Sets the shared value to 2
  733. // For each address addr that's number i from the end of the
  734. // linked list, write i+1 into location addr+2
  735. for (int i=0;i<items;++i) {
  736. T val;
  737. val.set((i+1)*tio.player());
  738. A[list_indices[i]+two] = val;
  739. }
  740. tio.sync_lamport();
  741. mpcio.dump_stats(std::cout);
  742. std::cout << "\n===== DEPENDENT INTERLEAVED =====\n";
  743. mpcio.reset_stats();
  744. tio.reset_lamport();
  745. T three;
  746. three.set(3*tio.player()); // Sets the shared value to 3
  747. // Follow the linked list and whenever A[addr]=val, set
  748. // A[addr+3]=val+3
  749. cur_index = prev_index;
  750. for (int i=0;i<items;++i) {
  751. T next_index = A[cur_index];
  752. A[cur_index+three] = next_index+three;
  753. cur_index = next_index;
  754. }
  755. tio.sync_lamport();
  756. mpcio.dump_stats(std::cout);
  757. std::cout << "\n";
  758. mpcio.reset_stats();
  759. tio.reset_lamport();
  760. if (depth <= 30) {
  761. auto check = A.reconstruct();
  762. auto head = A.reconstruct(prev_index);
  763. if (tio.player() == 0) {
  764. int width = (depth+3)/4;
  765. printf("Head of linked list: %0*lx\n\n", width,
  766. head.share() & mask);
  767. std::cout << "Non-zero reconstructed database entries:\n";
  768. for (address_t i=0;i<size;++i) {
  769. value_t share = check[i].share() & mask;
  770. if (share) printf("%0*x: %0*lx\n", width, i, width, share);
  771. }
  772. }
  773. }
  774. });
  775. }
  776. static void cdpf_test(MPCIO &mpcio,
  777. const PRACOptions &opts, char **args)
  778. {
  779. value_t query, target;
  780. int iters = 1;
  781. arc4random_buf(&query, sizeof(query));
  782. arc4random_buf(&target, sizeof(target));
  783. if (*args) {
  784. query = strtoull(*args, NULL, 16);
  785. ++args;
  786. }
  787. if (*args) {
  788. target = strtoull(*args, NULL, 16);
  789. ++args;
  790. }
  791. if (*args) {
  792. iters = atoi(*args);
  793. ++args;
  794. }
  795. int num_threads = opts.num_threads;
  796. boost::asio::thread_pool pool(num_threads);
  797. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  798. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  799. MPCTIO tio(mpcio, thread_num);
  800. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  801. size_t &aes_ops = tio.aes_ops();
  802. for (int i=0;i<iters;++i) {
  803. if (tio.player() == 2) {
  804. tio.cdpf(yield);
  805. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  806. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  807. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  808. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  809. dump_node(leaf0 ^ leaf1);
  810. } else {
  811. CDPF dpf = tio.cdpf(yield);
  812. printf("ashare = %016lX\nxshare = %016lX\n",
  813. dpf.as_target.ashare, dpf.xs_target.xshare);
  814. DPFnode leaf = dpf.leaf(query, aes_ops);
  815. printf("DPF(%016lx) = ", query);
  816. dump_node(leaf);
  817. if (tio.player() == 1) {
  818. tio.iostream_peer() << leaf;
  819. } else {
  820. DPFnode peerleaf;
  821. tio.iostream_peer() >> peerleaf;
  822. printf("XOR = ");
  823. dump_node(leaf ^ peerleaf);
  824. }
  825. }
  826. }
  827. });
  828. });
  829. }
  830. pool.join();
  831. }
  832. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  833. value_t target, value_t x)
  834. {
  835. int player = tio.player();
  836. size_t &aes_ops = tio.aes_ops();
  837. int res = 1;
  838. if (player == 2) {
  839. // Create a CDPF pair with the given target
  840. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  841. // Send it and a share of x to the computational parties
  842. RegAS x0, x1;
  843. x0.randomize();
  844. x1.set(x-x0.share());
  845. tio.iostream_p0() << dpf0 << x0;
  846. tio.iostream_p1() << dpf1 << x1;
  847. } else {
  848. CDPF dpf;
  849. RegAS xsh;
  850. tio.iostream_server() >> dpf >> xsh;
  851. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  852. RegBS eeq = dpf.is_zero(tio, yield, xsh, aes_ops);
  853. printf("%016lx %016lx %d %d %d %d ", target, x, lt.bshare,
  854. eq.bshare, gt.bshare, eeq.bshare);
  855. // Check the answer
  856. if (player == 1) {
  857. tio.iostream_peer() << xsh << lt << eq << gt << eeq;
  858. } else {
  859. RegAS peer_xsh;
  860. RegBS peer_lt, peer_eq, peer_gt, peer_eeq;
  861. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >>
  862. peer_gt >> peer_eeq;
  863. lt ^= peer_lt;
  864. eq ^= peer_eq;
  865. gt ^= peer_gt;
  866. eeq ^= peer_eeq;
  867. xsh += peer_xsh;
  868. int lti = int(lt.bshare);
  869. int eqi = int(eq.bshare);
  870. int gti = int(gt.bshare);
  871. int eeqi = int(eeq.bshare);
  872. x = xsh.share();
  873. printf(": %d %d %d %d ", lti, eqi, gti, eeqi);
  874. bool signbit = (x >> 63);
  875. if (lti + eqi + gti != 1 || eqi != eeqi) {
  876. printf("INCONSISTENT");
  877. res = 0;
  878. } else if (x == 0 && eqi) {
  879. printf("=");
  880. } else if (!signbit && gti) {
  881. printf(">");
  882. } else if (signbit && lti) {
  883. printf("<");
  884. } else {
  885. printf("INCORRECT");
  886. res = 0;
  887. }
  888. }
  889. printf("\n");
  890. }
  891. return res;
  892. }
  893. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  894. value_t target, value_t x)
  895. {
  896. int res = 1;
  897. res &= compare_test_one(tio, yield, target, x);
  898. res &= compare_test_one(tio, yield, target, 0);
  899. res &= compare_test_one(tio, yield, target, 1);
  900. res &= compare_test_one(tio, yield, target, 15);
  901. res &= compare_test_one(tio, yield, target, 16);
  902. res &= compare_test_one(tio, yield, target, 17);
  903. res &= compare_test_one(tio, yield, target, -1);
  904. res &= compare_test_one(tio, yield, target, -15);
  905. res &= compare_test_one(tio, yield, target, -16);
  906. res &= compare_test_one(tio, yield, target, -17);
  907. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  908. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  909. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  910. return res;
  911. }
  912. static void compare_test(MPCIO &mpcio,
  913. const PRACOptions &opts, char **args)
  914. {
  915. value_t target, x;
  916. arc4random_buf(&target, sizeof(target));
  917. arc4random_buf(&x, sizeof(x));
  918. if (*args) {
  919. target = strtoull(*args, NULL, 16);
  920. ++args;
  921. }
  922. if (*args) {
  923. x = strtoull(*args, NULL, 16);
  924. ++args;
  925. }
  926. int num_threads = opts.num_threads;
  927. boost::asio::thread_pool pool(num_threads);
  928. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  929. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  930. MPCTIO tio(mpcio, thread_num);
  931. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  932. int res = 1;
  933. res &= compare_test_target(tio, yield, target, x);
  934. res &= compare_test_target(tio, yield, 0, x);
  935. res &= compare_test_target(tio, yield, 1, x);
  936. res &= compare_test_target(tio, yield, 15, x);
  937. res &= compare_test_target(tio, yield, 16, x);
  938. res &= compare_test_target(tio, yield, 17, x);
  939. res &= compare_test_target(tio, yield, -1, x);
  940. res &= compare_test_target(tio, yield, -15, x);
  941. res &= compare_test_target(tio, yield, -16, x);
  942. res &= compare_test_target(tio, yield, -17, x);
  943. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  944. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  945. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  946. if (tio.player() == 0) {
  947. if (res == 1) {
  948. printf("All tests passed!\n");
  949. } else {
  950. printf("TEST FAILURES\n");
  951. }
  952. }
  953. });
  954. });
  955. }
  956. pool.join();
  957. }
  958. static void sort_test(MPCIO &mpcio,
  959. const PRACOptions &opts, char **args)
  960. {
  961. nbits_t depth=6;
  962. if (*args) {
  963. depth = atoi(*args);
  964. ++args;
  965. }
  966. int num_threads = opts.num_threads;
  967. boost::asio::thread_pool pool(num_threads);
  968. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  969. boost::asio::post(pool, [&mpcio, thread_num, depth] {
  970. MPCTIO tio(mpcio, thread_num);
  971. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  972. address_t size = address_t(1)<<depth;
  973. // size_t &aes_ops = tio.aes_ops();
  974. Duoram<RegAS> oram(tio.player(), size);
  975. auto A = oram.flat(tio, yield);
  976. A.explicitonly(true);
  977. // Initialize the memory to random values in parallel
  978. std::vector<coro_t> coroutines;
  979. for (address_t i=0; i<size; ++i) {
  980. coroutines.emplace_back(
  981. [&A, i](yield_t &yield) {
  982. auto Acoro = A.context(yield);
  983. RegAS v;
  984. v.randomize(62);
  985. Acoro[i] += v;
  986. });
  987. }
  988. run_coroutines(yield, coroutines);
  989. A.bitonic_sort(0, depth);
  990. if (depth <= 10) {
  991. oram.dump();
  992. auto check = A.reconstruct();
  993. if (tio.player() == 0) {
  994. for (address_t i=0;i<size;++i) {
  995. printf("%04x %016lx\n", i, check[i].share());
  996. }
  997. }
  998. }
  999. });
  1000. });
  1001. }
  1002. pool.join();
  1003. }
  1004. static void bsearch_test(MPCIO &mpcio,
  1005. const PRACOptions &opts, char **args)
  1006. {
  1007. value_t target;
  1008. arc4random_buf(&target, sizeof(target));
  1009. target >>= 1;
  1010. nbits_t depth=6;
  1011. if (*args) {
  1012. depth = atoi(*args);
  1013. ++args;
  1014. }
  1015. if (*args) {
  1016. target = strtoull(*args, NULL, 16);
  1017. ++args;
  1018. }
  1019. int num_threads = opts.num_threads;
  1020. boost::asio::thread_pool pool(num_threads);
  1021. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  1022. boost::asio::post(pool, [&mpcio, thread_num, depth, target] {
  1023. MPCTIO tio(mpcio, thread_num);
  1024. run_coroutines(tio, [&tio, depth, target] (yield_t &yield) {
  1025. address_t size = address_t(1)<<depth;
  1026. RegAS tshare;
  1027. if (tio.player() == 2) {
  1028. // Send shares of the target to the computational
  1029. // players
  1030. RegAS tshare0, tshare1;
  1031. tshare0.randomize();
  1032. tshare1.set(target-tshare0.share());
  1033. tio.iostream_p0() << tshare0;
  1034. tio.iostream_p1() << tshare1;
  1035. printf("Using target = %016lx\n", target);
  1036. yield();
  1037. } else {
  1038. // Get the share of the target
  1039. tio.iostream_server() >> tshare;
  1040. }
  1041. // Create a random database and sort it
  1042. // size_t &aes_ops = tio.aes_ops();
  1043. Duoram<RegAS> oram(tio.player(), size);
  1044. auto A = oram.flat(tio, yield);
  1045. A.explicitonly(true);
  1046. // Initialize the memory to random values in parallel
  1047. std::vector<coro_t> coroutines;
  1048. for (address_t i=0; i<size; ++i) {
  1049. coroutines.emplace_back(
  1050. [&A, i](yield_t &yield) {
  1051. auto Acoro = A.context(yield);
  1052. RegAS v;
  1053. v.randomize(62);
  1054. Acoro[i] += v;
  1055. });
  1056. }
  1057. run_coroutines(yield, coroutines);
  1058. A.bitonic_sort(0, depth);
  1059. // Binary search for the target
  1060. RegAS tindex = A.obliv_binary_search(tshare);
  1061. // Check the answer
  1062. if (tio.player() == 1) {
  1063. tio.iostream_peer() << tindex;
  1064. } else if (tio.player() == 0) {
  1065. RegAS peer_tindex;
  1066. tio.iostream_peer() >> peer_tindex;
  1067. tindex += peer_tindex;
  1068. }
  1069. if (depth <= 10) {
  1070. auto check = A.reconstruct();
  1071. if (tio.player() == 0) {
  1072. for (address_t i=0;i<size;++i) {
  1073. printf("%04x %016lx\n", i, check[i].share());
  1074. }
  1075. }
  1076. }
  1077. if (tio.player() == 0) {
  1078. printf("Found index = %lx\n", tindex.share());
  1079. }
  1080. });
  1081. });
  1082. }
  1083. pool.join();
  1084. }
  1085. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  1086. {
  1087. MPCTIO tio(mpcio, 0);
  1088. if (!*args) {
  1089. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  1090. return;
  1091. } else if (!strcmp(*args, "test")) {
  1092. ++args;
  1093. online_test(mpcio, opts, args);
  1094. } else if (!strcmp(*args, "lamporttest")) {
  1095. ++args;
  1096. lamport_test(mpcio, opts, args);
  1097. } else if (!strcmp(*args, "rdpftest")) {
  1098. ++args;
  1099. rdpf_test(mpcio, opts, args);
  1100. } else if (!strcmp(*args, "rdpftime")) {
  1101. ++args;
  1102. rdpf_timing(mpcio, opts, args);
  1103. } else if (!strcmp(*args, "evaltime")) {
  1104. ++args;
  1105. rdpfeval_timing(mpcio, opts, args);
  1106. } else if (!strcmp(*args, "parevaltime")) {
  1107. ++args;
  1108. par_rdpfeval_timing(mpcio, opts, args);
  1109. } else if (!strcmp(*args, "tupletime")) {
  1110. ++args;
  1111. tupleeval_timing(mpcio, opts, args);
  1112. } else if (!strcmp(*args, "partupletime")) {
  1113. ++args;
  1114. par_tupleeval_timing(mpcio, opts, args);
  1115. } else if (!strcmp(*args, "duotest")) {
  1116. ++args;
  1117. if (opts.use_xor_db) {
  1118. duoram_test<RegXS>(mpcio, opts, args);
  1119. } else {
  1120. duoram_test<RegAS>(mpcio, opts, args);
  1121. }
  1122. } else if (!strcmp(*args, "cdpftest")) {
  1123. ++args;
  1124. cdpf_test(mpcio, opts, args);
  1125. } else if (!strcmp(*args, "cmptest")) {
  1126. ++args;
  1127. compare_test(mpcio, opts, args);
  1128. } else if (!strcmp(*args, "sorttest")) {
  1129. ++args;
  1130. sort_test(mpcio, opts, args);
  1131. } else if (!strcmp(*args, "bsearch")) {
  1132. ++args;
  1133. bsearch_test(mpcio, opts, args);
  1134. } else if (!strcmp(*args, "duoram")) {
  1135. ++args;
  1136. if (opts.use_xor_db) {
  1137. duoram<RegXS>(mpcio, opts, args);
  1138. } else {
  1139. duoram<RegAS>(mpcio, opts, args);
  1140. }
  1141. } else if (!strcmp(*args, "cell")) {
  1142. ++args;
  1143. cell(mpcio, opts, args);
  1144. } else {
  1145. std::cerr << "Unknown mode " << *args << "\n";
  1146. }
  1147. }