online.cpp 57 KB

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