online.cpp 58 KB

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