online.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. RegXS oxidx;
  650. oxidx.xshare = share+3*tio.player();
  651. T O;
  652. if (tio.player() == 0) {
  653. O.set(0x31410000);
  654. } else {
  655. O.set(0x00005926);
  656. }
  657. // Writing and reading with additively shared indices
  658. printf("Additive Updating\n");
  659. A[aidx] += M;
  660. printf("Additive Reading\n");
  661. T Aa = A[aidx];
  662. // Writing and reading with XOR shared indices
  663. printf("XOR Updating\n");
  664. A[xidx] += N;
  665. printf("XOR Reading\n");
  666. T Ax = A[xidx];
  667. // Writing and reading with OblivIndex indices
  668. auto oidx = A.oblivindex(oxidx);
  669. printf("OblivIndex Updating\n");
  670. A[oidx] += O;
  671. printf("OblivIndex Reading\n");
  672. T Ox = A[oidx];
  673. // Writing and reading with explicit indices
  674. T Ae;
  675. if (depth > 2) {
  676. printf("Explicit Updating\n");
  677. A[5] += Aa;
  678. printf("Explicit Reading\n");
  679. Ae = A[6];
  680. }
  681. // Simultaneous independent reads
  682. printf("3 independent reading\n");
  683. std::vector<T> Av = A[std::array {
  684. aidx, aidx2, aidx3
  685. }];
  686. // Simultaneous independent updates
  687. T Aw1, Aw2, Aw3;
  688. Aw1.set(0x101010101010101 * tio.player());
  689. Aw2.set(0x202020202020202 * tio.player());
  690. Aw3.set(0x303030303030303 * tio.player());
  691. printf("3 independent updating\n");
  692. A[std::array { aidx, aidx2, aidx3 }] -=
  693. std::array { Aw1, Aw2, Aw3 };
  694. if (depth <= 10) {
  695. oram.dump();
  696. auto check = A.reconstruct();
  697. if (tio.player() == 0) {
  698. for (address_t i=0;i<len;++i) {
  699. printf("%04x %016lx\n", i, check[i].share());
  700. }
  701. }
  702. }
  703. auto checkread = A.reconstruct(Aa);
  704. auto checkreade = A.reconstruct(Ae);
  705. auto checkreadx = A.reconstruct(Ax);
  706. auto checkreado = A.reconstruct(Ox);
  707. if (tio.player() == 0) {
  708. printf("Read AS value = %016lx\n", checkread.share());
  709. printf("Read AX value = %016lx\n", checkreadx.share());
  710. printf("Read Ex value = %016lx\n", checkreade.share());
  711. printf("Read OI value = %016lx\n", checkreado.share());
  712. }
  713. for (auto &v : Av) {
  714. auto checkv = A.reconstruct(v);
  715. if (tio.player() == 0) {
  716. printf("Read Av value = %016lx\n", checkv.share());
  717. }
  718. }
  719. });
  720. }
  721. // This measures the same things as the Duoram paper: dependent and
  722. // independent reads, updates, writes, and interleaves
  723. // T is RegAS or RegXS for additive or XOR shared database respectively
  724. template <typename T>
  725. static void duoram(MPCIO &mpcio,
  726. const PRACOptions &opts, char **args)
  727. {
  728. nbits_t depth = 6;
  729. int items = 4;
  730. if (*args) {
  731. depth = atoi(*args);
  732. ++args;
  733. }
  734. if (*args) {
  735. items = atoi(*args);
  736. ++args;
  737. }
  738. MPCTIO tio(mpcio, 0, opts.num_threads);
  739. run_coroutines(tio, [&mpcio, &tio, depth, items] (yield_t &yield) {
  740. size_t size = size_t(1)<<depth;
  741. address_t mask = (depth < ADDRESS_MAX_BITS ?
  742. ((address_t(1)<<depth) - 1) : ~0);
  743. Duoram<T> oram(tio.player(), size);
  744. auto A = oram.flat(tio, yield);
  745. std::cout << "===== DEPENDENT UPDATES =====\n";
  746. mpcio.reset_stats();
  747. tio.reset_lamport();
  748. // Make a linked list of length items
  749. std::vector<T> list_indices;
  750. T prev_index, next_index;
  751. prev_index.randomize(depth);
  752. for (int i=0;i<items;++i) {
  753. next_index.randomize(depth);
  754. A[next_index] += prev_index;
  755. list_indices.push_back(next_index);
  756. prev_index = next_index;
  757. }
  758. tio.sync_lamport();
  759. mpcio.dump_stats(std::cout);
  760. std::cout << "\n===== DEPENDENT READS =====\n";
  761. mpcio.reset_stats();
  762. tio.reset_lamport();
  763. // Read the linked list starting with prev_index
  764. T cur_index = prev_index;
  765. for (int i=0;i<items;++i) {
  766. cur_index = A[cur_index];
  767. }
  768. tio.sync_lamport();
  769. mpcio.dump_stats(std::cout);
  770. std::cout << "\n===== INDEPENDENT READS =====\n";
  771. mpcio.reset_stats();
  772. tio.reset_lamport();
  773. // Read all the entries in the list at once
  774. std::vector<T> read_outputs = A[list_indices];
  775. tio.sync_lamport();
  776. mpcio.dump_stats(std::cout);
  777. std::cout << "\n===== INDEPENDENT UPDATES =====\n";
  778. mpcio.reset_stats();
  779. tio.reset_lamport();
  780. // Make a vector of indices 1 larger than those in list_indices,
  781. // and a vector of values 1 larger than those in outputs
  782. std::vector<T> indep_indices, indep_values;
  783. T one;
  784. one.set(tio.player()); // Sets the shared value to 1
  785. for (int i=0;i<items;++i) {
  786. indep_indices.push_back(list_indices[i]+one);
  787. indep_values.push_back(read_outputs[i]+one);
  788. }
  789. // Update all the indices at once
  790. A[indep_indices] += indep_values;
  791. tio.sync_lamport();
  792. mpcio.dump_stats(std::cout);
  793. std::cout << "\n===== DEPENDENT WRITES =====\n";
  794. mpcio.reset_stats();
  795. tio.reset_lamport();
  796. T two;
  797. two.set(2*tio.player()); // Sets the shared value to 2
  798. // For each address addr that's number i from the end of the
  799. // linked list, write i+1 into location addr+2
  800. for (int i=0;i<items;++i) {
  801. T val;
  802. val.set((i+1)*tio.player());
  803. A[list_indices[i]+two] = val;
  804. }
  805. tio.sync_lamport();
  806. mpcio.dump_stats(std::cout);
  807. std::cout << "\n===== DEPENDENT INTERLEAVED =====\n";
  808. mpcio.reset_stats();
  809. tio.reset_lamport();
  810. T three;
  811. three.set(3*tio.player()); // Sets the shared value to 3
  812. // Follow the linked list and whenever A[addr]=val, set
  813. // A[addr+3]=val+3
  814. cur_index = prev_index;
  815. for (int i=0;i<items;++i) {
  816. T next_index = A[cur_index];
  817. A[cur_index+three] = next_index+three;
  818. cur_index = next_index;
  819. }
  820. tio.sync_lamport();
  821. mpcio.dump_stats(std::cout);
  822. std::cout << "\n";
  823. mpcio.reset_stats();
  824. tio.reset_lamport();
  825. if (depth <= 30) {
  826. auto check = A.reconstruct();
  827. auto head = A.reconstruct(prev_index);
  828. if (tio.player() == 0) {
  829. int width = (depth+3)/4;
  830. printf("Head of linked list: %0*lx\n\n", width,
  831. head.share() & mask);
  832. std::cout << "Non-zero reconstructed database entries:\n";
  833. for (address_t i=0;i<size;++i) {
  834. value_t share = check[i].share() & mask;
  835. if (share) printf("%0*x: %0*lx\n", width, i, width, share);
  836. }
  837. }
  838. }
  839. });
  840. }
  841. static void cdpf_test(MPCIO &mpcio,
  842. const PRACOptions &opts, char **args)
  843. {
  844. value_t query, target;
  845. int iters = 1;
  846. arc4random_buf(&query, sizeof(query));
  847. arc4random_buf(&target, sizeof(target));
  848. if (*args) {
  849. query = strtoull(*args, NULL, 16);
  850. ++args;
  851. }
  852. if (*args) {
  853. target = strtoull(*args, NULL, 16);
  854. ++args;
  855. }
  856. if (*args) {
  857. iters = atoi(*args);
  858. ++args;
  859. }
  860. int num_threads = opts.num_threads;
  861. boost::asio::thread_pool pool(num_threads);
  862. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  863. boost::asio::post(pool, [&mpcio, thread_num, query, target, iters] {
  864. MPCTIO tio(mpcio, thread_num);
  865. run_coroutines(tio, [&tio, query, target, iters] (yield_t &yield) {
  866. size_t &aes_ops = tio.aes_ops();
  867. for (int i=0;i<iters;++i) {
  868. if (tio.player() == 2) {
  869. tio.cdpf(yield);
  870. auto [ dpf0, dpf1 ] = CDPF::generate(target, aes_ops);
  871. DPFnode leaf0 = dpf0.leaf(query, aes_ops);
  872. DPFnode leaf1 = dpf1.leaf(query, aes_ops);
  873. printf("DPFXOR_{%016lx}(%016lx} = ", target, query);
  874. dump_node(leaf0 ^ leaf1);
  875. } else {
  876. CDPF dpf = tio.cdpf(yield);
  877. printf("ashare = %016lX\nxshare = %016lX\n",
  878. dpf.as_target.ashare, dpf.xs_target.xshare);
  879. DPFnode leaf = dpf.leaf(query, aes_ops);
  880. printf("DPF(%016lx) = ", query);
  881. dump_node(leaf);
  882. if (tio.player() == 1) {
  883. tio.iostream_peer() << leaf;
  884. } else {
  885. DPFnode peerleaf;
  886. tio.iostream_peer() >> peerleaf;
  887. printf("XOR = ");
  888. dump_node(leaf ^ peerleaf);
  889. }
  890. }
  891. }
  892. });
  893. });
  894. }
  895. pool.join();
  896. }
  897. static int compare_test_one(MPCTIO &tio, yield_t &yield,
  898. value_t target, value_t x)
  899. {
  900. int player = tio.player();
  901. size_t &aes_ops = tio.aes_ops();
  902. int res = 1;
  903. if (player == 2) {
  904. // Create a CDPF pair with the given target
  905. auto [dpf0, dpf1] = CDPF::generate(target, aes_ops);
  906. // Send it and a share of x to the computational parties
  907. RegAS x0, x1;
  908. x0.randomize();
  909. x1.set(x-x0.share());
  910. tio.iostream_p0() << dpf0 << x0;
  911. tio.iostream_p1() << dpf1 << x1;
  912. } else {
  913. CDPF dpf;
  914. RegAS xsh;
  915. tio.iostream_server() >> dpf >> xsh;
  916. auto [lt, eq, gt] = dpf.compare(tio, yield, xsh, aes_ops);
  917. RegBS eeq = dpf.is_zero(tio, yield, xsh, aes_ops);
  918. printf("%016lx %016lx %d %d %d %d ", target, x, lt.bshare,
  919. eq.bshare, gt.bshare, eeq.bshare);
  920. // Check the answer
  921. if (player == 1) {
  922. tio.iostream_peer() << xsh << lt << eq << gt << eeq;
  923. } else {
  924. RegAS peer_xsh;
  925. RegBS peer_lt, peer_eq, peer_gt, peer_eeq;
  926. tio.iostream_peer() >> peer_xsh >> peer_lt >> peer_eq >>
  927. peer_gt >> peer_eeq;
  928. lt ^= peer_lt;
  929. eq ^= peer_eq;
  930. gt ^= peer_gt;
  931. eeq ^= peer_eeq;
  932. xsh += peer_xsh;
  933. int lti = int(lt.bshare);
  934. int eqi = int(eq.bshare);
  935. int gti = int(gt.bshare);
  936. int eeqi = int(eeq.bshare);
  937. x = xsh.share();
  938. printf(": %d %d %d %d ", lti, eqi, gti, eeqi);
  939. bool signbit = (x >> 63);
  940. if (lti + eqi + gti != 1 || eqi != eeqi) {
  941. printf("INCONSISTENT");
  942. res = 0;
  943. } else if (x == 0 && eqi) {
  944. printf("=");
  945. } else if (!signbit && gti) {
  946. printf(">");
  947. } else if (signbit && lti) {
  948. printf("<");
  949. } else {
  950. printf("INCORRECT");
  951. res = 0;
  952. }
  953. }
  954. printf("\n");
  955. }
  956. return res;
  957. }
  958. static int compare_test_target(MPCTIO &tio, yield_t &yield,
  959. value_t target, value_t x)
  960. {
  961. int res = 1;
  962. res &= compare_test_one(tio, yield, target, x);
  963. res &= compare_test_one(tio, yield, target, 0);
  964. res &= compare_test_one(tio, yield, target, 1);
  965. res &= compare_test_one(tio, yield, target, 15);
  966. res &= compare_test_one(tio, yield, target, 16);
  967. res &= compare_test_one(tio, yield, target, 17);
  968. res &= compare_test_one(tio, yield, target, -1);
  969. res &= compare_test_one(tio, yield, target, -15);
  970. res &= compare_test_one(tio, yield, target, -16);
  971. res &= compare_test_one(tio, yield, target, -17);
  972. res &= compare_test_one(tio, yield, target, (value_t(1)<<63));
  973. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)+1);
  974. res &= compare_test_one(tio, yield, target, (value_t(1)<<63)-1);
  975. return res;
  976. }
  977. static void compare_test(MPCIO &mpcio,
  978. const PRACOptions &opts, char **args)
  979. {
  980. value_t target, x;
  981. arc4random_buf(&target, sizeof(target));
  982. arc4random_buf(&x, sizeof(x));
  983. if (*args) {
  984. target = strtoull(*args, NULL, 16);
  985. ++args;
  986. }
  987. if (*args) {
  988. x = strtoull(*args, NULL, 16);
  989. ++args;
  990. }
  991. int num_threads = opts.num_threads;
  992. boost::asio::thread_pool pool(num_threads);
  993. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  994. boost::asio::post(pool, [&mpcio, thread_num, target, x] {
  995. MPCTIO tio(mpcio, thread_num);
  996. run_coroutines(tio, [&tio, target, x] (yield_t &yield) {
  997. int res = 1;
  998. res &= compare_test_target(tio, yield, target, x);
  999. res &= compare_test_target(tio, yield, 0, x);
  1000. res &= compare_test_target(tio, yield, 1, x);
  1001. res &= compare_test_target(tio, yield, 15, x);
  1002. res &= compare_test_target(tio, yield, 16, x);
  1003. res &= compare_test_target(tio, yield, 17, x);
  1004. res &= compare_test_target(tio, yield, -1, x);
  1005. res &= compare_test_target(tio, yield, -15, x);
  1006. res &= compare_test_target(tio, yield, -16, x);
  1007. res &= compare_test_target(tio, yield, -17, x);
  1008. res &= compare_test_target(tio, yield, (value_t(1)<<63), x);
  1009. res &= compare_test_target(tio, yield, (value_t(1)<<63)+1, x);
  1010. res &= compare_test_target(tio, yield, (value_t(1)<<63)-1, x);
  1011. if (tio.player() == 0) {
  1012. if (res == 1) {
  1013. printf("All tests passed!\n");
  1014. } else {
  1015. printf("TEST FAILURES\n");
  1016. }
  1017. }
  1018. });
  1019. });
  1020. }
  1021. pool.join();
  1022. }
  1023. static void sort_test(MPCIO &mpcio,
  1024. const PRACOptions &opts, char **args)
  1025. {
  1026. nbits_t depth=6;
  1027. if (*args) {
  1028. depth = atoi(*args);
  1029. ++args;
  1030. }
  1031. address_t len = (1<<depth);
  1032. if (*args) {
  1033. len = atoi(*args);
  1034. ++args;
  1035. }
  1036. MPCTIO tio(mpcio, 0, opts.num_threads);
  1037. run_coroutines(tio, [&tio, depth, len] (yield_t &yield) {
  1038. address_t size = address_t(1)<<depth;
  1039. // size_t &aes_ops = tio.aes_ops();
  1040. Duoram<RegAS> oram(tio.player(), size);
  1041. auto A = oram.flat(tio, yield);
  1042. A.explicitonly(true);
  1043. // Initialize the memory to random values in parallel
  1044. std::vector<coro_t> coroutines;
  1045. for (address_t i=0; i<size; ++i) {
  1046. coroutines.emplace_back(
  1047. [&A, i](yield_t &yield) {
  1048. auto Acoro = A.context(yield);
  1049. RegAS v;
  1050. v.randomize(62);
  1051. Acoro[i] += v;
  1052. });
  1053. }
  1054. run_coroutines(yield, coroutines);
  1055. A.bitonic_sort(0, len);
  1056. if (depth <= 10) {
  1057. oram.dump();
  1058. }
  1059. auto check = A.reconstruct();
  1060. bool fail = false;
  1061. if (tio.player() == 0) {
  1062. for (address_t i=0;i<size;++i) {
  1063. if (depth <= 10) {
  1064. printf("%04x %016lx\n", i, check[i].share());
  1065. }
  1066. if (i>0 && i<len &&
  1067. check[i].share() < check[i-1].share()) {
  1068. fail = true;
  1069. }
  1070. }
  1071. if (fail) {
  1072. printf("FAIL\n");
  1073. } else {
  1074. printf("PASS\n");
  1075. }
  1076. }
  1077. });
  1078. }
  1079. static void pad_test(MPCIO &mpcio,
  1080. const PRACOptions &opts, char **args)
  1081. {
  1082. nbits_t depth=6;
  1083. if (*args) {
  1084. depth = atoi(*args);
  1085. ++args;
  1086. }
  1087. address_t len = (1<<depth);
  1088. if (*args) {
  1089. len = atoi(*args);
  1090. ++args;
  1091. }
  1092. MPCTIO tio(mpcio, 0, opts.num_threads);
  1093. run_coroutines(tio, [&mpcio, &tio, depth, len] (yield_t &yield) {
  1094. int player = tio.player();
  1095. Duoram<RegAS> oram(player, len);
  1096. auto A = oram.flat(tio, yield);
  1097. // Initialize the ORAM in explicit mode
  1098. A.explicitonly(true);
  1099. for (address_t i=0; i<len; ++i) {
  1100. RegAS v;
  1101. v.set((player*0xffff+1)*i);
  1102. A[i] = v;
  1103. }
  1104. A.explicitonly(false);
  1105. // Obliviously add 0 to A[0], which reblinds the whole database
  1106. RegAS z;
  1107. A[z] += z;
  1108. auto check = A.reconstruct();
  1109. if (player == 0) {
  1110. for (address_t i=0;i<len;++i) {
  1111. if (depth <= 10) {
  1112. printf("%04x %016lx\n", i, check[i].share());
  1113. }
  1114. }
  1115. printf("\n");
  1116. }
  1117. address_t maxsize = address_t(1)<<depth;
  1118. Duoram<RegAS>::Pad P(A, tio, yield, maxsize);
  1119. for (address_t i=0; i<maxsize; ++i) {
  1120. RegAS v = P[i];
  1121. if (depth <= 10) {
  1122. value_t vval = mpc_reconstruct(tio, yield, v);
  1123. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1124. }
  1125. }
  1126. printf("\n");
  1127. for (address_t i=0; i<maxsize; ++i) {
  1128. value_t offset = 0xdeadbeef;
  1129. if (player) {
  1130. offset = -offset;
  1131. }
  1132. RegAS ind;
  1133. ind.set(player*i+offset);
  1134. RegAS v = P[ind];
  1135. if (depth <= 10) {
  1136. value_t vval = mpc_reconstruct(tio, yield, v);
  1137. printf("%04x %016lx %016lx\n", i, v.share(), vval);
  1138. }
  1139. }
  1140. printf("\n");
  1141. });
  1142. }
  1143. static void bsearch_test(MPCIO &mpcio,
  1144. const PRACOptions &opts, char **args, bool basic)
  1145. {
  1146. value_t target;
  1147. arc4random_buf(&target, sizeof(target));
  1148. target >>= 1;
  1149. nbits_t depth=6;
  1150. if (*args) {
  1151. depth = atoi(*args);
  1152. ++args;
  1153. }
  1154. address_t len = (1<<depth);
  1155. if (*args) {
  1156. len = atoi(*args);
  1157. ++args;
  1158. }
  1159. if (*args) {
  1160. target = strtoull(*args, NULL, 16);
  1161. ++args;
  1162. }
  1163. MPCTIO tio(mpcio, 0, opts.num_threads);
  1164. run_coroutines(tio, [&tio, depth, len, target, basic] (yield_t &yield) {
  1165. RegAS tshare;
  1166. if (tio.player() == 2) {
  1167. // Send shares of the target to the computational
  1168. // players
  1169. RegAS tshare0, tshare1;
  1170. tshare0.randomize();
  1171. tshare1.set(target-tshare0.share());
  1172. tio.iostream_p0() << tshare0;
  1173. tio.iostream_p1() << tshare1;
  1174. printf("Using target = %016lx\n", target);
  1175. yield();
  1176. } else {
  1177. // Get the share of the target
  1178. tio.iostream_server() >> tshare;
  1179. }
  1180. // Create a random database and sort it
  1181. // size_t &aes_ops = tio.aes_ops();
  1182. Duoram<RegAS> oram(tio.player(), len);
  1183. auto A = oram.flat(tio, yield);
  1184. A.explicitonly(true);
  1185. // Initialize the memory to random values in parallel
  1186. std::vector<coro_t> coroutines;
  1187. for (address_t i=0; i<len; ++i) {
  1188. coroutines.emplace_back(
  1189. [&A, i](yield_t &yield) {
  1190. auto Acoro = A.context(yield);
  1191. RegAS v;
  1192. v.randomize(62);
  1193. Acoro[i] += v;
  1194. });
  1195. }
  1196. run_coroutines(yield, coroutines);
  1197. A.bitonic_sort(0, len);
  1198. A.explicitonly(false);
  1199. // Binary search for the target
  1200. value_t checkindex;
  1201. if (basic) {
  1202. RegAS tindex = A.basic_binary_search(tshare);
  1203. checkindex = mpc_reconstruct(tio, yield, tindex);
  1204. } else {
  1205. RegXS tindex = A.binary_search(tshare);
  1206. checkindex = mpc_reconstruct(tio, yield, tindex);
  1207. }
  1208. // Check the answer
  1209. size_t size = size_t(1) << depth;
  1210. value_t checktarget = mpc_reconstruct(tio, yield, tshare);
  1211. auto check = A.reconstruct();
  1212. bool fail = false;
  1213. if (tio.player() == 0) {
  1214. for (address_t i=0;i<len;++i) {
  1215. if (depth <= 10) {
  1216. printf("%c%04x %016lx\n",
  1217. (i == checkindex ? '*' : ' '),
  1218. i, check[i].share());
  1219. }
  1220. if (i>0 && i<len &&
  1221. check[i].share() < check[i-1].share()) {
  1222. fail = true;
  1223. }
  1224. if (i == checkindex) {
  1225. // check[i] should be >= target, and check[i-1]
  1226. // should be < target
  1227. if ((i < len && check[i].share() < checktarget) ||
  1228. (i > 0 && check[i-1].share() >= checktarget)) {
  1229. fail = true;
  1230. }
  1231. }
  1232. }
  1233. printf("Target = %016lx\n", checktarget);
  1234. printf("Found index = %02lx\n", checkindex);
  1235. if (checkindex > size) {
  1236. fail = true;
  1237. }
  1238. if (fail) {
  1239. printf("FAIL\n");
  1240. } else {
  1241. printf("PASS\n");
  1242. }
  1243. }
  1244. });
  1245. }
  1246. void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
  1247. {
  1248. MPCTIO tio(mpcio, 0);
  1249. if (!*args) {
  1250. std::cerr << "Mode is required as the first argument when not preprocessing.\n";
  1251. return;
  1252. } else if (!strcmp(*args, "test")) {
  1253. ++args;
  1254. online_test(mpcio, opts, args);
  1255. } else if (!strcmp(*args, "lamporttest")) {
  1256. ++args;
  1257. lamport_test(mpcio, opts, args);
  1258. } else if (!strcmp(*args, "rdpftest")) {
  1259. ++args;
  1260. rdpf_test<1>(mpcio, opts, args, false);
  1261. } else if (!strcmp(*args, "rdpftest2")) {
  1262. ++args;
  1263. rdpf_test<2>(mpcio, opts, args, false);
  1264. } else if (!strcmp(*args, "rdpftest3")) {
  1265. ++args;
  1266. rdpf_test<3>(mpcio, opts, args, false);
  1267. } else if (!strcmp(*args, "rdpftest4")) {
  1268. ++args;
  1269. rdpf_test<4>(mpcio, opts, args, false);
  1270. } else if (!strcmp(*args, "rdpftest5")) {
  1271. ++args;
  1272. rdpf_test<5>(mpcio, opts, args, false);
  1273. } else if (!strcmp(*args, "irdpftest")) {
  1274. ++args;
  1275. rdpf_test<1>(mpcio, opts, args, true);
  1276. } else if (!strcmp(*args, "irdpftest2")) {
  1277. ++args;
  1278. rdpf_test<2>(mpcio, opts, args, true);
  1279. } else if (!strcmp(*args, "irdpftest3")) {
  1280. ++args;
  1281. rdpf_test<3>(mpcio, opts, args, true);
  1282. } else if (!strcmp(*args, "irdpftest4")) {
  1283. ++args;
  1284. rdpf_test<4>(mpcio, opts, args, true);
  1285. } else if (!strcmp(*args, "irdpftest5")) {
  1286. ++args;
  1287. rdpf_test<5>(mpcio, opts, args, true);
  1288. } else if (!strcmp(*args, "rdpftime")) {
  1289. ++args;
  1290. rdpf_timing(mpcio, opts, args);
  1291. } else if (!strcmp(*args, "evaltime")) {
  1292. ++args;
  1293. rdpfeval_timing(mpcio, opts, args);
  1294. } else if (!strcmp(*args, "parevaltime")) {
  1295. ++args;
  1296. par_rdpfeval_timing(mpcio, opts, args);
  1297. } else if (!strcmp(*args, "tupletime")) {
  1298. ++args;
  1299. tupleeval_timing(mpcio, opts, args);
  1300. } else if (!strcmp(*args, "partupletime")) {
  1301. ++args;
  1302. par_tupleeval_timing(mpcio, opts, args);
  1303. } else if (!strcmp(*args, "duotest")) {
  1304. ++args;
  1305. if (opts.use_xor_db) {
  1306. duoram_test<RegXS>(mpcio, opts, args);
  1307. } else {
  1308. duoram_test<RegAS>(mpcio, opts, args);
  1309. }
  1310. } else if (!strcmp(*args, "cdpftest")) {
  1311. ++args;
  1312. cdpf_test(mpcio, opts, args);
  1313. } else if (!strcmp(*args, "cmptest")) {
  1314. ++args;
  1315. compare_test(mpcio, opts, args);
  1316. } else if (!strcmp(*args, "sorttest")) {
  1317. ++args;
  1318. sort_test(mpcio, opts, args);
  1319. } else if (!strcmp(*args, "padtest")) {
  1320. ++args;
  1321. pad_test(mpcio, opts, args);
  1322. } else if (!strcmp(*args, "bbsearch")) {
  1323. ++args;
  1324. bsearch_test(mpcio, opts, args, true);
  1325. } else if (!strcmp(*args, "bsearch")) {
  1326. ++args;
  1327. bsearch_test(mpcio, opts, args, false);
  1328. } else if (!strcmp(*args, "duoram")) {
  1329. ++args;
  1330. if (opts.use_xor_db) {
  1331. duoram<RegXS>(mpcio, opts, args);
  1332. } else {
  1333. duoram<RegAS>(mpcio, opts, args);
  1334. }
  1335. } else if (!strcmp(*args, "cell")) {
  1336. ++args;
  1337. cell(mpcio, opts, args);
  1338. } else {
  1339. std::cerr << "Unknown mode " << *args << "\n";
  1340. }
  1341. }