heap.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. #include <functional>
  2. #include "types.hpp"
  3. #include "duoram.hpp"
  4. #include "cell.hpp"
  5. #include "rdpf.hpp"
  6. #include "shapes.hpp"
  7. #include "heap.hpp"
  8. // The protocol begins by adding an empty node at the end of the heap array.
  9. // The key observation is that after \heapinsert is complete, the only entries
  10. // that might change are the ones on the path from the root to this new node.
  11. // Further, since the number of entries in the heap is public, \emph{which} entries in $\database$ form this path is also public.
  12. // We form the accessible set $\pdatabase$ of the nodes from the root to the newly added (empty) node.
  13. // The next observation is that this path (from root to leaf) starts off sorted, and will end up with the new element $\insertval$ inserted into the correct position so as to keep the path sorted.
  14. // The path is of length $\lg \heapsize$, so we use our binary search to find the appropriate insertion position with a single IDPF of height $\lg \lg \heapsize$.
  15. // The advice bits of that IDPF will then be bit shares of a vector $\flag = [0,0,1,0,\dots,0]$ with the $1$ indicating the position at which the new value must be inserted.
  16. // The shares of $\flag$ are (locally) converted to shares of $\u = [0,0,1,1,\dots,1]$ by taking running XORs.
  17. // The bits of $\flag$ and $\u$ are used in $2 \lg \heapsize$ parallel Flag-Word multiplications to shift the elements greater than $\insertval$ down one position, and to write $\insertval$ into the resulting hole, with a single message of communication.
  18. // This Protocol 4 from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  19. int MinHeap::insert_optimized(MPCTIO tio, yield_t & yield, RegAS val) {
  20. auto HeapArray = oram.flat(tio, yield);
  21. num_items++;
  22. typename Duoram<RegAS>::Path old_P(HeapArray, tio, yield, num_items);
  23. const RegXS foundidx = old_P.binary_search(val);
  24. size_t childindex = num_items;
  25. uint64_t height = std::ceil(std::log2(num_items + 1)) + 1;
  26. RegAS zero;
  27. zero.ashare = 0;
  28. HeapArray[childindex] = zero;
  29. typename Duoram<RegAS>::Path P(HeapArray, tio, yield, num_items);
  30. #ifdef VERBOSE
  31. uint64_t val_reconstruction = mpc_reconstruct(tio, yield, val, 64);
  32. std::cout << "val_reconstruction = " << val_reconstruction << std::endl;
  33. #endif
  34. uint64_t logheight = std::ceil(double(std::log2(height))) + 1;
  35. std::vector<RegBS> flag(height+1);
  36. std::vector<RegBS> u(height+1);
  37. typename Duoram<RegAS>::template OblivIndex<RegXS,1> oidx(tio, yield, foundidx, logheight);
  38. u = oidx.unit_vector(tio, yield, 1 << logheight, foundidx);
  39. #ifdef VERBOSE
  40. uint64_t foundidx_reconstruction = mpc_reconstruct(tio, yield, foundidx);
  41. std::cout << "foundidx_reconstruction = " << foundidx_reconstruction << std::endl;
  42. std::cout << std::endl << " =============== " << std::endl;
  43. for (size_t j = 0; j < height; ++j) {
  44. uint64_t reconstruction = mpc_reconstruct(tio, yield, u[j]);
  45. std::cout << " --->> u[" << j << "] = " << reconstruction << std::endl;
  46. }
  47. #endif
  48. for (size_t j = 0; j < height; ++j) {
  49. if(tio.player() !=2) {
  50. flag[j] = u[j];
  51. if(j > 0) u[j] = u[j] ^ u[j-1];
  52. }
  53. }
  54. #ifdef VERBOSE
  55. for (size_t j = 0; j < height; ++j) {
  56. uint64_t reconstruction = mpc_reconstruct(tio, yield, u[j]);
  57. std::cout << " --->> [0000111111]][" << j << "] = " << reconstruction << std::endl;
  58. }
  59. #endif
  60. RegAS * path = new RegAS[height+1];
  61. RegAS * w = new RegAS[height+1];
  62. RegAS * v = new RegAS[height+1];
  63. for (size_t j = 0; j < height+1; ++j) path[j] = P[j];
  64. std::vector<coro_t> coroutines;
  65. for (size_t j = 1; j < height+1; ++j) {
  66. coroutines.emplace_back(
  67. [&tio, w, u, path, j](yield_t &yield) {
  68. mpc_flagmult(tio, yield, w[j], u[j-1], (path[j-1]-path[j]));
  69. }
  70. );
  71. coroutines.emplace_back(
  72. [&tio, v, flag, val, path, j](yield_t &yield) {
  73. mpc_flagmult(tio, yield, v[j-1], flag[j-1], (val - path[j-1]));
  74. }
  75. );
  76. }
  77. run_coroutines(tio, coroutines);
  78. for (size_t j = 0; j < height; ++j) P[j] += (w[j] + v[j]);
  79. #ifdef VERBOSE
  80. std::cout << "\n\n=================Before===========\n\n";
  81. for (size_t j = 0; j < height-1; ++j) {
  82. auto path_rec = mpc_reconstruct(tio, yield, P[j]);
  83. std::cout << j << " --->: " << path_rec << std::endl;
  84. }
  85. std::cout << "\n\n============================\n\n";
  86. std::cout << "\n\n=================Aftter===========\n\n";
  87. for (size_t j = 0; j < height-1; ++j) {
  88. auto path_rec = mpc_reconstruct(tio, yield, P[j]);
  89. std::cout << j << " --->: " << path_rec << std::endl;
  90. }
  91. std::cout << "\n\n============================\n\n";
  92. #endif
  93. delete[] path;
  94. delete[] w;
  95. delete[] v;
  96. return 1;
  97. }
  98. // The insert protocol works as follows:
  99. // It adds a new element in the last entry of the array
  100. // From the leaf (the element added), compare with its parent (1 oblivious compare)
  101. // This Protocol 3 from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  102. int MinHeap::insert(MPCTIO tio, yield_t & yield, RegAS val) {
  103. auto HeapArray = oram.flat(tio, yield);
  104. num_items++;
  105. size_t childindex = num_items;
  106. size_t parentindex = childindex / 2;
  107. #ifdef VERBOSE
  108. std::cout << "childindex = " << childindex << std::endl;
  109. std::cout << "parentindex = " << parentindex << std::endl;
  110. #endif
  111. HeapArray[num_items] = val;
  112. typename Duoram<RegAS>::Path P(HeapArray, tio, yield, childindex);
  113. while (parentindex > 0) {
  114. RegAS sharechild = HeapArray[childindex];
  115. RegAS shareparent = HeapArray[parentindex];
  116. CDPF cdpf = tio.cdpf(yield);
  117. RegAS diff = sharechild - shareparent;
  118. auto[lt, eq, gt] = cdpf.compare(tio, yield, diff, tio.aes_ops());
  119. auto lteq = lt ^ eq;
  120. mpc_oswap(tio, yield, sharechild, shareparent, lteq, 64);
  121. HeapArray[childindex] = sharechild;
  122. HeapArray[parentindex] = shareparent;
  123. childindex = parentindex;
  124. parentindex = parentindex / 2;
  125. }
  126. return 1;
  127. }
  128. // This is only for debugging purposes
  129. // This function just verifies that the heap property is satisfied
  130. int MinHeap::verify_heap_property(MPCTIO tio, yield_t & yield) {
  131. #ifdef VERBOSE
  132. std::cout << std::endl << std::endl << "verify_heap_property is being called " << std::endl;
  133. #endif
  134. auto HeapArray = oram.flat(tio, yield);
  135. uint64_t heapreconstruction[num_items];
  136. for (size_t j = 0; j <= num_items; ++j) {
  137. heapreconstruction[j] = mpc_reconstruct(tio, yield, HeapArray[j]);
  138. }
  139. for (size_t j = 1; j < num_items / 2; ++j) {
  140. if (heapreconstruction[j] > heapreconstruction[2 * j]) {
  141. std::cout << "heap property failure\n\n";
  142. std::cout << "j = " << j << std::endl;
  143. std::cout << heapreconstruction[j] << std::endl;
  144. std::cout << "2*j = " << 2 * j << std::endl;
  145. std::cout << heapreconstruction[2 * j] << std::endl;
  146. }
  147. if (heapreconstruction[j] > heapreconstruction[2 * j + 1]) {
  148. std::cout << "heap property failure\n\n";
  149. std::cout << "j = " << j << std::endl;
  150. std::cout << heapreconstruction[j] << std::endl;
  151. std::cout << "2*j + 1 = " << 2 * j + 1<< std::endl;
  152. std::cout << heapreconstruction[2 * j + 1] << std::endl;
  153. }
  154. assert(heapreconstruction[j] <= heapreconstruction[2 * j]);
  155. assert(heapreconstruction[j] <= heapreconstruction[2 * j + 1]);
  156. }
  157. return 1;
  158. }
  159. //This is only for debugging purposes
  160. void verify_parent_children_heaps(MPCTIO tio, yield_t & yield, RegAS parent, RegAS leftchild, RegAS rightchild) {
  161. uint64_t parent_reconstruction = mpc_reconstruct(tio, yield, parent);
  162. uint64_t leftchild_reconstruction = mpc_reconstruct(tio, yield, leftchild);
  163. uint64_t rightchild_reconstruction = mpc_reconstruct(tio, yield, rightchild);
  164. #ifdef VERBOSE
  165. std::cout << "parent_reconstruction = " << parent_reconstruction << std::endl;
  166. std::cout << "leftchild_reconstruction = " << leftchild_reconstruction << std::endl;
  167. std::cout << "rightchild_reconstruction = " << rightchild_reconstruction << std::endl << std::endl << std::endl;
  168. #endif
  169. assert(parent_reconstruction <= leftchild_reconstruction);
  170. assert(parent_reconstruction <= rightchild_reconstruction);
  171. }
  172. // This Protocol 6 from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  173. RegXS MinHeap::restore_heap_property(MPCIO & mpcio, MPCTIO tio, yield_t & yield, RegXS index) {
  174. RegAS smallest;
  175. auto HeapArray = oram.flat(tio, yield);
  176. mpcio.reset_stats();
  177. tio.reset_lamport();
  178. RegXS leftchildindex = index;
  179. leftchildindex = index << 1;
  180. RegXS rightchildindex;
  181. rightchildindex.xshare = leftchildindex.xshare ^ (tio.player());
  182. RegAS parent;
  183. RegAS leftchild;
  184. RegAS rightchild;
  185. #ifdef VERBOSE
  186. auto index_reconstruction = mpc_reconstruct(tio, yield, index);
  187. auto leftchildindex_reconstruction = mpc_reconstruct(tio, yield, leftchildindex);
  188. auto rightchildindex_reconstruction = mpc_reconstruct(tio, yield, rightchildindex);
  189. std::cout << "index_reconstruction = " << index_reconstruction << std::endl;
  190. std::cout << "leftchildindex_reconstruction = " << leftchildindex_reconstruction << std::endl;
  191. std::cout << "rightchildindex_reconstruction = " << rightchildindex_reconstruction << std::endl;
  192. #endif
  193. std::vector<coro_t> coroutines_read;
  194. coroutines_read.emplace_back(
  195. [&tio, &parent, &HeapArray, index](yield_t &yield) {
  196. auto Acoro = HeapArray.context(yield);
  197. parent = Acoro[index];
  198. }
  199. );
  200. coroutines_read.emplace_back(
  201. [&tio, &HeapArray, &leftchild, leftchildindex](yield_t &yield) {
  202. auto Acoro = HeapArray.context(yield);
  203. leftchild = Acoro[leftchildindex];
  204. }
  205. );
  206. coroutines_read.emplace_back(
  207. [&tio, &rightchild, &HeapArray, rightchildindex](yield_t &yield) {
  208. auto Acoro = HeapArray.context(yield);
  209. rightchild = Acoro[rightchildindex];
  210. }
  211. );
  212. run_coroutines(tio, coroutines_read);
  213. CDPF cdpf = tio.cdpf(yield);
  214. auto[lt_c, eq_c, gt_c] = cdpf.compare(tio, yield, leftchild - rightchild, tio.aes_ops());
  215. auto lteq = lt_c ^ eq_c;
  216. RegXS smallerindex;
  217. RegAS smallerchild;
  218. run_coroutines(tio, [&tio, &smallerindex, lteq, rightchildindex, leftchildindex](yield_t &yield) {
  219. mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, 64);
  220. }, [&tio, &smallerchild, lteq, rightchild, leftchild](yield_t &yield) {
  221. mpc_select(tio, yield, smallerchild, lteq, rightchild, leftchild, 64);
  222. }
  223. );
  224. CDPF cdpf0 = tio.cdpf(yield);
  225. auto[lt_p, eq_p, gt_p] = cdpf0.compare(tio, yield, smallerchild - parent, tio.aes_ops());
  226. auto lt_p_eq_p = lt_p ^ eq_p;
  227. RegBS ltlt1;
  228. mpc_and(tio, yield, ltlt1, lteq, lt_p_eq_p);
  229. RegAS update_index_by, update_leftindex_by;
  230. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent, leftchild](yield_t &yield) {
  231. mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, (parent - leftchild), 64);
  232. }, [&tio, &update_index_by, lt_p, parent, smallerchild](yield_t &yield) {
  233. mpc_flagmult(tio, yield, update_index_by, lt_p, smallerchild - parent, 64);
  234. }
  235. );
  236. std::vector<coro_t> coroutines;
  237. coroutines.emplace_back(
  238. [&tio, &HeapArray, index, update_index_by](yield_t &yield) {
  239. auto Acoro = HeapArray.context(yield);
  240. Acoro[index] += update_index_by;
  241. }
  242. );
  243. coroutines.emplace_back(
  244. [&tio, &HeapArray, leftchildindex, update_leftindex_by](yield_t &yield) {
  245. auto Acoro = HeapArray.context(yield);
  246. Acoro[leftchildindex] += update_leftindex_by;
  247. }
  248. );
  249. coroutines.emplace_back(
  250. [&tio, &HeapArray, rightchildindex, update_index_by, update_leftindex_by](yield_t &yield) {
  251. auto Acoro = HeapArray.context(yield);
  252. Acoro[rightchildindex] += -(update_index_by + update_leftindex_by);
  253. }
  254. );
  255. run_coroutines(tio, coroutines);
  256. #ifdef DEBUG
  257. verify_parent_children_heaps(tio, yield, HeapArray[index], HeapArray[leftchildindex] , HeapArray[rightchildindex]);
  258. #endif
  259. return smallerindex;
  260. }
  261. // This Protocol 7 from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  262. auto MinHeap::restore_heap_property_optimized(MPCTIO tio, yield_t & yield, RegXS index, size_t layer, size_t depth, typename Duoram < RegAS > ::template OblivIndex < RegXS, 3 > (oidx)) {
  263. auto HeapArray = oram.flat(tio, yield);
  264. RegXS leftchildindex = index;
  265. leftchildindex = index << 1;
  266. RegXS rightchildindex;
  267. rightchildindex.xshare = leftchildindex.xshare ^ (tio.player());
  268. typename Duoram < RegAS > ::Flat P(HeapArray, tio, yield, 1 << layer, 1 << layer);
  269. typename Duoram < RegAS > ::Flat C(HeapArray, tio, yield, 2 << layer, 2 << layer);
  270. typename Duoram < RegAS > ::Stride L(C, tio, yield, 0, 2);
  271. typename Duoram < RegAS > ::Stride R(C, tio, yield, 1, 2);
  272. RegAS parent_tmp, leftchild_tmp, rightchild_tmp;
  273. std::vector<coro_t> coroutines_read;
  274. coroutines_read.emplace_back(
  275. [&tio, &parent_tmp, &P, &oidx](yield_t &yield) {
  276. auto Acoro = P.context(yield);
  277. parent_tmp = Acoro[oidx]; //inserted_val;
  278. });
  279. coroutines_read.emplace_back(
  280. [&tio, &L, &leftchild_tmp, &oidx](yield_t &yield) {
  281. auto Acoro = L.context(yield);
  282. leftchild_tmp = Acoro[oidx]; //inserted_val;
  283. });
  284. coroutines_read.emplace_back(
  285. [&tio, &R, &rightchild_tmp, &oidx](yield_t &yield) {
  286. auto Acoro = R.context(yield);
  287. rightchild_tmp = Acoro[oidx];
  288. });
  289. run_coroutines(tio, coroutines_read);
  290. CDPF cdpf = tio.cdpf(yield);
  291. auto[lt, eq, gt] = cdpf.compare(tio, yield, leftchild_tmp - rightchild_tmp, tio.aes_ops());
  292. auto lteq = lt ^ eq;
  293. RegXS smallerindex;
  294. RegAS smallerchild;
  295. run_coroutines(tio, [&tio, &smallerindex, lteq, rightchildindex, leftchildindex](yield_t &yield)
  296. { mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, 64);;},
  297. [&tio, &smallerchild, lt, rightchild_tmp, leftchild_tmp](yield_t &yield)
  298. { mpc_select(tio, yield, smallerchild, lt, rightchild_tmp, leftchild_tmp, 64);;});
  299. CDPF cdpf0 = tio.cdpf(yield);
  300. auto[lt1, eq1, gt1] = cdpf0.compare(tio, yield, smallerchild - parent_tmp, tio.aes_ops());
  301. auto lt1eq1 = lt1 ^ eq1;
  302. RegBS ltlt1;
  303. mpc_and(tio, yield, ltlt1, lteq, lt1eq1);
  304. RegAS update_index_by, update_leftindex_by;
  305. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent_tmp, leftchild_tmp](yield_t &yield)
  306. { mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, (parent_tmp - leftchild_tmp), 64);},
  307. [&tio, &update_index_by, lt1eq1, parent_tmp, smallerchild](yield_t &yield)
  308. {mpc_flagmult(tio, yield, update_index_by, lt1eq1, smallerchild - parent_tmp, 64);}
  309. );
  310. std::vector<coro_t> coroutines;
  311. coroutines.emplace_back(
  312. [&tio, &P, &oidx, update_index_by](yield_t &yield) {
  313. auto Acoro = P.context(yield);
  314. Acoro[oidx] += update_index_by; //inserted_val;
  315. });
  316. coroutines.emplace_back(
  317. [&tio, &L, &oidx, update_leftindex_by](yield_t &yield) {
  318. auto Acoro = L.context(yield);
  319. Acoro[oidx] += update_leftindex_by; //inserted_val;
  320. });
  321. coroutines.emplace_back(
  322. [&tio, &R, &oidx, update_leftindex_by, update_index_by](yield_t &yield) {
  323. auto Acoro = R.context(yield);
  324. Acoro[oidx] += -(update_leftindex_by + update_index_by);
  325. });
  326. run_coroutines(tio, coroutines);
  327. return std::make_pair(smallerindex, gt);
  328. }
  329. void MinHeap::initialize(MPCTIO tio, yield_t & yield) {
  330. auto HeapArray = oram.flat(tio, yield);
  331. HeapArray.init(0x7fffffffffffff);
  332. }
  333. // This function simply initializes a heap with values 1,2,...,n
  334. // We use this function only to setup our heap
  335. // to do timing experiments on insert and extractmins
  336. void MinHeap::initialize_heap(MPCTIO tio, yield_t & yield) {
  337. auto HeapArray = oram.flat(tio, yield);
  338. std::vector<coro_t> coroutines;
  339. for (size_t j = 1; j <= num_items; ++j) {
  340. coroutines.emplace_back(
  341. [&tio, &HeapArray, j](yield_t &yield) {
  342. auto Acoro = HeapArray.context(yield);
  343. RegAS v;
  344. v.ashare = j * tio.player();
  345. Acoro[j] = v;
  346. }
  347. );
  348. }
  349. run_coroutines(tio, coroutines);
  350. }
  351. void MinHeap::print_heap(MPCTIO tio, yield_t & yield) {
  352. auto HeapArray = oram.flat(tio, yield);
  353. uint64_t * Pjreconstruction = new uint64_t[num_items + 1];
  354. for (size_t j = 0; j <= num_items; ++j) Pjreconstruction[j] = mpc_reconstruct(tio, yield, HeapArray[j]);
  355. for (size_t j = 0; j <= num_items; ++j) {
  356. if(2 * j < num_items) {
  357. std::cout << j << "-->> HeapArray[" << j << "] = " << std::dec << Pjreconstruction[j] << ", children are: " << Pjreconstruction[2 * j] << " and " << Pjreconstruction[2 * j + 1] << std::endl;
  358. } else {
  359. std::cout << j << "-->> HeapArray[" << j << "] = " << std::dec << Pjreconstruction[j] << " is a LEAF " << std::endl;
  360. }
  361. }
  362. delete[] Pjreconstruction;
  363. }
  364. auto MinHeap::restore_heap_property_at_root(MPCTIO tio, yield_t & yield, size_t index = 1) {
  365. auto HeapArray = oram.flat(tio, yield);
  366. RegAS parent = HeapArray[index];
  367. RegAS leftchild = HeapArray[2 * index];
  368. RegAS rightchild = HeapArray[2 * index + 1];
  369. CDPF cdpf = tio.cdpf(yield);
  370. auto[lt, eq, gt] = cdpf.compare(tio, yield, leftchild - rightchild, tio.aes_ops());
  371. auto lteq = lt ^ eq;
  372. RegAS smallerchild;
  373. mpc_select(tio, yield, smallerchild, lteq, rightchild, leftchild);
  374. RegXS smallerindex(lt);
  375. uint64_t leftchildindex = (2 * index);
  376. uint64_t rightchildindex = (2 * index) + 1;
  377. smallerindex = (RegXS(lteq) & leftchildindex) ^ (RegXS(gt) & rightchildindex);
  378. CDPF cdpf0 = tio.cdpf(yield);
  379. auto[lt1, eq1, gt1] = cdpf0.compare(tio, yield, smallerchild - parent, tio.aes_ops());
  380. auto lt1eq1 = lt1 ^ eq1;
  381. RegBS ltlt1;
  382. mpc_and(tio, yield, ltlt1, lteq, lt1eq1);
  383. RegAS update_index_by, update_leftindex_by;
  384. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent, leftchild](yield_t &yield) {
  385. mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, (parent - leftchild), 64);
  386. }, [&tio, &update_index_by, lt1eq1, parent, smallerchild](yield_t &yield) {
  387. mpc_flagmult(tio, yield, update_index_by, lt1eq1, smallerchild - parent, 64);
  388. }
  389. );
  390. std::vector<coro_t> coroutines;
  391. coroutines.emplace_back(
  392. [&tio, &HeapArray, index, update_index_by](yield_t &yield) {
  393. auto Acoro = HeapArray.context(yield);
  394. Acoro[index] += update_index_by;
  395. }
  396. );
  397. coroutines.emplace_back(
  398. [&tio, &HeapArray, leftchildindex, update_leftindex_by](yield_t &yield) {
  399. auto Acoro = HeapArray.context(yield);
  400. Acoro[leftchildindex] += update_leftindex_by;
  401. }
  402. );
  403. coroutines.emplace_back(
  404. [&tio, &HeapArray, rightchildindex, update_index_by, update_leftindex_by](yield_t &yield) {
  405. auto Acoro = HeapArray.context(yield);
  406. Acoro[rightchildindex] += -(update_index_by + update_leftindex_by);
  407. }
  408. );
  409. run_coroutines(tio, coroutines);
  410. #ifdef VERBOSE
  411. RegAS new_parent = HeapArray[index];
  412. RegAS new_left = HeapArray[leftchildindex];
  413. RegAS new_right = HeapArray[rightchildindex];
  414. uint64_t parent_R = mpc_reconstruct(tio, yield, new_parent);
  415. uint64_t left_R = mpc_reconstruct(tio, yield, new_left);
  416. uint64_t right_R = mpc_reconstruct(tio, yield, new_right);
  417. std::cout << "parent_R = " << parent_R << std::endl;
  418. std::cout << "left_R = " << left_R << std::endl;
  419. std::cout << "right_R = " << right_R << std::endl;
  420. #endif
  421. #ifdef DEBUG
  422. verify_parent_children_heaps(tio, yield, HeapArray[index], HeapArray[leftchildindex] , HeapArray[rightchildindex]);
  423. #endif
  424. return std::make_pair(smallerindex, gt);
  425. }
  426. // This Protocol 5 from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  427. // Like in the paper, there is only version of extract_min
  428. // the optimized version calls the optimized restore_heap_property
  429. RegAS MinHeap::extract_min(MPCIO & mpcio, MPCTIO tio, yield_t & yield, int is_optimized) {
  430. size_t height = std::log2(num_items);
  431. RegAS minval;
  432. auto HeapArray = oram.flat(tio, yield);
  433. minval = HeapArray[1];
  434. HeapArray[1] = RegAS(HeapArray[num_items]);
  435. num_items--;
  436. auto outroot = restore_heap_property_at_root(tio, yield);
  437. RegXS smaller = outroot.first;
  438. if(is_optimized > 0) {
  439. typename Duoram < RegAS > ::template OblivIndex < RegXS, 3 > oidx(tio, yield, height);
  440. oidx.incr(outroot.second);
  441. for (size_t i = 0; i < height-1; ++i) {
  442. auto out = restore_heap_property_optimized(tio, yield, smaller, i + 1, height, typename Duoram < RegAS > ::template OblivIndex < RegXS, 3 > (oidx));
  443. smaller = out.first;
  444. oidx.incr(out.second);
  445. }
  446. }
  447. if(is_optimized == 0) {
  448. for (size_t i = 0; i < height - 1; ++i) {
  449. smaller = restore_heap_property(mpcio, tio, yield, smaller);
  450. }
  451. }
  452. return minval;
  453. }
  454. // This function is not used in the evaluation in PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  455. // This function is called by heapify which takes in a random array and turns it into a heap
  456. void MinHeap::heapify_at_level(MPCIO & mpcio, MPCTIO tio, yield_t & yield, size_t index = 1) {
  457. auto outroot = restore_heap_property_at_root(tio, yield, index);
  458. RegXS smaller = outroot.first;
  459. #ifdef VERBOSE
  460. uint64_t smaller_rec = mpc_reconstruct(tio, yield, smaller, 64);
  461. std::cout << "smaller_rec = " << smaller_rec << std::endl;
  462. std::cout << "num_items = " << num_items << std::endl;
  463. std::cout << "index = " << index << std::endl;
  464. #endif
  465. size_t height = std::log2(num_items) - std::floor(log2(index)) ;
  466. #ifdef VERBOSE
  467. std::cout << "height = " << height << std::endl << "===================" << std::endl;
  468. #endif
  469. for (size_t i = 0; i < height - 1; ++i) {
  470. #ifdef VERBOSE
  471. std::cout << "index = " << index << ", i = " << i << std::endl;
  472. uint64_t smaller_rec = mpc_reconstruct(tio, yield, smaller, 64);
  473. std::cout << "[inside loop] smaller_rec = " << smaller_rec << std::endl;
  474. #endif
  475. smaller = restore_heap_property(mpcio, tio, yield, smaller);
  476. }
  477. }
  478. // This function is not used in the evaluation in PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  479. // This function takes in a random array turns into a heap
  480. void MinHeap::heapify(MPCIO & mpcio, MPCTIO tio, yield_t & yield) {
  481. size_t startIdx = ((num_items + 1) / 2) - 1;
  482. for (size_t i = startIdx; i >= 1; i--) {
  483. heapify_at_level(mpcio, tio, yield, i);
  484. }
  485. }
  486. void Heap(MPCIO & mpcio,
  487. const PRACOptions & opts, char ** args) {
  488. // nbits_t depth = atoi(args[0]);
  489. // nbits_t depth2 = atoi(args[1]);
  490. // size_t n_inserts = atoi(args[2]);
  491. // size_t n_extracts = atoi(args[3]);
  492. // int is_optimized = atoi(args[4]);
  493. // int run_sanity = atoi(args[5]);
  494. int argc = 12;
  495. int depth = 0;
  496. int depth2 = 0;
  497. size_t n_inserts = 0;
  498. size_t n_extracts = 0;
  499. int is_optimized = 0;
  500. int run_sanity = 0;
  501. // Process command line arguments
  502. for (int i = 0; i < argc; i += 2) {
  503. std::string option = args[i];
  504. if (option == "-m" && i + 1 < argc) {
  505. depth = std::atoi(args[i + 1]);
  506. } else if (option == "-d" && i + 1 < argc) {
  507. depth2 = std::atoi(args[i + 1]);
  508. } else if (option == "-i" && i + 1 < argc) {
  509. n_inserts = std::atoi(args[i + 1]);
  510. } else if (option == "-e" && i + 1 < argc) {
  511. n_extracts = std::atoi(args[i + 1]);
  512. } else if (option == "-opt" && i + 1 < argc) {
  513. is_optimized = std::atoi(args[i + 1]);
  514. } else if (option == "-s" && i + 1 < argc) {
  515. run_sanity = std::atoi(args[i + 1]);
  516. }
  517. }
  518. // Use the values
  519. std::cout << "depth: " << depth << std::endl;
  520. std::cout << "depth2: " << depth2 << std::endl;
  521. std::cout << "n_inserts: " << n_inserts << std::endl;
  522. std::cout << "n_extracts: " << n_extracts << std::endl;
  523. std::cout << "is_optimized: " << is_optimized << std::endl;
  524. std::cout << "run_sanity: " << run_sanity << std::endl;
  525. // if ( * args) {
  526. // depth = atoi( * args);
  527. // ++args;
  528. // }
  529. size_t items = (size_t(1) << depth) - 1;
  530. // if ( * args) {
  531. // items = atoi( * args);
  532. // ++args;
  533. // }
  534. MPCTIO tio(mpcio, 0, opts.num_threads);
  535. run_coroutines(tio, [ & tio, depth, depth2, items, n_inserts, n_extracts, is_optimized, run_sanity, &mpcio](yield_t & yield) {
  536. size_t size = size_t(1) << depth;
  537. MinHeap tree(tio.player(), size);
  538. tree.initialize(tio, yield);
  539. tree.num_items = (size_t(1) << depth2) - 1;
  540. tree.initialize_heap(tio, yield);
  541. std::cout << "\n===== Init Stats =====\n";
  542. tio.sync_lamport();
  543. mpcio.dump_stats(std::cout);
  544. mpcio.reset_stats();
  545. tio.reset_lamport();
  546. for (size_t j = 0; j < n_inserts; ++j) {
  547. RegAS inserted_val;
  548. inserted_val.randomize(10);
  549. #ifdef VERBOSE
  550. inserted_val.ashare = inserted_val.ashare;
  551. uint64_t inserted_val_rec = mpc_reconstruct(tio, yield, inserted_val, 64);
  552. std::cout << "inserted_val_rec = " << inserted_val_rec << std::endl << std::endl;
  553. #endif
  554. if(is_optimized > 0) tree.insert_optimized(tio, yield, inserted_val);
  555. if(is_optimized == 0) tree.insert(tio, yield, inserted_val);
  556. }
  557. std::cout << "\n===== Insert Stats =====\n";
  558. tio.sync_lamport();
  559. mpcio.dump_stats(std::cout);
  560. if(run_sanity == 1) tree.verify_heap_property(tio, yield);
  561. mpcio.reset_stats();
  562. tio.reset_lamport();
  563. #ifdef VERBOSE
  564. tree.print_heap(tio, yield);
  565. #endif
  566. for (size_t j = 0; j < n_extracts; ++j) {
  567. tree.extract_min(mpcio, tio, yield, is_optimized);
  568. #ifdef VERBOSE
  569. RegAS minval = tree.extract_min(mpcio, tio, yield, is_optimized);
  570. uint64_t minval_reconstruction = mpc_reconstruct(tio, yield, minval, 64);
  571. std::cout << "minval_reconstruction = " << minval_reconstruction << std::endl;
  572. #endif
  573. #ifdef DEBUG
  574. tree.verify_heap_property(tio, yield);
  575. #endif
  576. #ifdef VERBOSE
  577. tree.print_heap(tio, yield);
  578. #endif
  579. }
  580. std::cout << "\n===== Extract Min Stats =====\n";
  581. tio.sync_lamport();
  582. mpcio.dump_stats(std::cout);
  583. #ifdef VERBOSE
  584. tree.print_heap(tio, yield);
  585. #endif
  586. if(run_sanity == 1) tree.verify_heap_property(tio, yield);
  587. }
  588. );
  589. }