online.cpp 36 KB

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