online.cpp 49 KB

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