online.cpp 56 KB

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