heap.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. /*
  9. The heap datastructure is stored in an array with the starting index as 1 (and not 0)
  10. For nodes stored in index i of the array, the parent is stored at i/2 and
  11. The left and right children are stored at 2i and 2i + 1
  12. All the unused array indicies have MAX_INT stored in them
  13. TODO: Draw a diagram to show the layout
  14. _Protocol 4_ from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  15. Consider the following insertion path with: x0 < x1 < x2 < NewElement < x3 < x4
  16. x0 x0 x0
  17. \ \ \
  18. x1 x1 x1
  19. \ \ \
  20. x2 x2 x2
  21. \ \ \
  22. x3 ( ) NewElement
  23. \ \ \
  24. x4 x3 x3
  25. \ \ \
  26. ( ) x4 x4
  27. (Path with new element) (binary search to determine (After insertion)
  28. the point where New Element
  29. should be and shift the elements
  30. from that point down the path
  31. from the point)
  32. The insert protocol begins by adding an empty node at the end of the heap array
  33. The key observation is that after the insert operation, the only entries that might change are the ones on the path from the root to the new node
  34. The path from the root to the new node is determined based on the number of entries in the heap, which is publicly known
  35. The observation is that this path starts off sorted and will end up with the new element (NewElement) inserted into the correct position, preserving the sorted property of the path
  36. The length of the path is logarithmic with respect to the heap size (path length = log(heap size))
  37. To find the appropriate insertion position, we use binary search with a single IDPF of height logarithmic with respect to the logarithm of the heap size (IDPF height = log(log(heap size)))
  38. The advice bits of the IDPF correspond to the bit shares of a vector 'flag' with a single '1' indicating the position where the new value (insertval) must be inserted.
  39. The shares of 'flag' are locally converted to shares of a vector 'u = [000011111]' using running XORs.
  40. The bits of 'flag' and 'u' are then used in parallel Flag-Word multiplications, totaling 2 times the logarithm of the heap size, to shift the elements greater than 'insertval' down one position
  41. And write 'insertval' into the resulting empty location in the path
  42. This process requires a single message of communication
  43. Overall, the insert protocol achieves efficient insertion of a new element into the heap, with a complexity of log(heap size) oblivious comparisons and log(heap size) oblivious swaps
  44. */
  45. void MinHeap::insert_optimized(MPCTIO tio, yield_t & yield, RegAS val) {
  46. auto HeapArray = oram.flat(tio, yield);
  47. num_items++;
  48. typename Duoram<RegAS>::Path old_P(HeapArray, tio, yield, num_items);
  49. const RegXS foundidx = old_P.binary_search(val);
  50. size_t childindex = num_items;
  51. uint64_t height = old_P.size();//std::ceil(std::log2(num_items + 1)) + 1;
  52. RegAS zero;
  53. HeapArray[childindex] = zero;
  54. typename Duoram<RegAS>::Path P(HeapArray, tio, yield, num_items);
  55. #ifdef HEAP_VERBOSE
  56. uint64_t val_reconstruction = mpc_reconstruct(tio, yield, val, VALUE_BITS);
  57. std::cout << "val_reconstruction = " << val_reconstruction << std::endl;
  58. #endif
  59. uint64_t logheight = std::floor(double(std::log2(height))) + 1;
  60. std::vector<RegBS> flag;
  61. std::vector<RegBS> u(height);
  62. typename Duoram<RegAS>::template OblivIndex<RegXS,1> oidx(tio, yield, foundidx, logheight);
  63. flag = oidx.unit_vector(tio, yield, height, foundidx); // changed the third param to height from 1 << logheight
  64. #ifdef HEAP_VERBOSE
  65. uint64_t foundidx_reconstruction = mpc_reconstruct(tio, yield, foundidx);
  66. std::cout << "foundidx_reconstruction = " << foundidx_reconstruction << std::endl;
  67. std::cout << std::endl << " =============== " << std::endl;
  68. for (size_t j = 0; j < height; ++j) {
  69. uint64_t reconstruction = mpc_reconstruct(tio, yield, flag[j]);
  70. std::cout << " --->> flag[" << j << "] = " << reconstruction << std::endl;
  71. }
  72. #endif
  73. for (size_t j = 0; j < height; ++j) {
  74. if(tio.player() !=2) {
  75. //flag[j] = u[j];
  76. if(j > 0) u[j] = flag[j] ^ u[j-1];
  77. }
  78. }
  79. #ifdef HEAP_VERBOSE
  80. for (size_t j = 0; j < height; ++j) {
  81. uint64_t reconstruction = mpc_reconstruct(tio, yield, u[j]);
  82. std::cout << " --->> [0000111111]][" << j << "] = " << reconstruction << std::endl;
  83. }
  84. #endif
  85. RegAS * path = new RegAS[height];
  86. RegAS * w = new RegAS[height];
  87. RegAS * v = new RegAS[height];
  88. for (size_t j = 0; j < height; ++j) path[j] = P[j];
  89. std::vector<coro_t> coroutines;
  90. for (size_t j = 1; j < height; ++j) {
  91. coroutines.emplace_back(
  92. [&tio, w, u, path, j](yield_t &yield) {
  93. mpc_flagmult(tio, yield, w[j], u[j-1], path[j-1]-path[j]);
  94. }
  95. );
  96. coroutines.emplace_back(
  97. [&tio, v, flag, val, path, j](yield_t &yield) {
  98. mpc_flagmult(tio, yield, v[j-1], flag[j-1], val - path[j-1]);
  99. }
  100. );
  101. }
  102. run_coroutines(tio, coroutines);
  103. #ifdef HEAP_VERBOSE
  104. std::cout << "\n\n=================Before===========\n\n";
  105. for (size_t j = 0; j < height-1; ++j) {
  106. auto path_rec = mpc_reconstruct(tio, yield, P[j]);
  107. std::cout << j << " --->: " << path_rec << std::endl;
  108. }
  109. std::cout << "\n\n============================\n\n";
  110. #endif
  111. for (size_t j = 0; j < height; ++j) P[j] += (w[j] + v[j]);
  112. #ifdef HEAP_VERBOSE
  113. std::cout << "\n\n=================After===========\n\n";
  114. for (size_t j = 0; j < height-1; ++j) {
  115. auto path_rec = mpc_reconstruct(tio, yield, P[j]);
  116. std::cout << j << " --->: " << path_rec << std::endl;
  117. }
  118. std::cout << "\n\n============================\n\n";
  119. #endif
  120. delete[] path;
  121. delete[] w;
  122. delete[] v;
  123. }
  124. // The insert protocol works as follows:
  125. // Step 1: Add a new element to the last entry of the array.
  126. // This new element becomes a leaf in the heap.
  127. // Step 2: Starting from the leaf (the newly added element), compare it with its parent.
  128. // Perform 1 oblivious comparison to determine if the parent is greater than the child.
  129. // Step 3: If the parent is greater than the child, swap them obliviously to maintain the heap property.
  130. // This swap ensures that the parent is always greater than both its children.
  131. // Step 4: Continue moving up the tree by repeating steps 2 and 3 until we reach the root.
  132. // This process ensures that the newly inserted element is correctly positioned in the heap.
  133. // The total cost of the insert protocol is log(num_items) oblivious comparisons and log(num_items) oblivious swaps.
  134. // This protocol follows the approach described as Protocol 3 in the paper "PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures."
  135. int MinHeap::insert(MPCTIO tio, yield_t & yield, RegAS val) {
  136. auto HeapArray = oram.flat(tio, yield);
  137. num_items++;
  138. size_t childindex = num_items;
  139. size_t parentindex = childindex / 2;
  140. #ifdef HEAP_VERBOSE
  141. std::cout << "childindex = " << childindex << std::endl;
  142. std::cout << "parentindex = " << parentindex << std::endl;
  143. #endif
  144. HeapArray[num_items] = val;
  145. while (parentindex > 0) {
  146. RegAS sharechild = HeapArray[childindex];
  147. RegAS shareparent = HeapArray[parentindex];
  148. CDPF cdpf = tio.cdpf(yield);
  149. RegAS diff = sharechild - shareparent;
  150. auto[lt, eq, gt] = cdpf.compare(tio, yield, diff, tio.aes_ops());
  151. //auto lteq = lt ^ eq;
  152. mpc_oswap(tio, yield, sharechild, shareparent, lt, VALUE_BITS);
  153. HeapArray[childindex] = sharechild;
  154. HeapArray[parentindex] = shareparent;
  155. childindex = parentindex;
  156. parentindex = parentindex / 2;
  157. }
  158. return 1;
  159. }
  160. // Note: This function is intended for debugging purposes only.
  161. // The purpose of this function is to verify that the heap property is satisfied.
  162. // The function checks if the heap property holds for the given heap structure. It ensures that for each node in the heap, the value of the parent node is less than or equal to the values of its children.
  163. // By calling this function during debugging, you can validate the integrity of the heap structure and ensure that the heap property is maintained correctly.
  164. // It is important to note that this function is not meant for production use and should be used solely for debugging and testing purposes.
  165. void MinHeap::verify_heap_property(MPCTIO tio, yield_t & yield) {
  166. #ifdef HEAP_VERBOSE
  167. std::cout << std::endl << std::endl << "verify_heap_property is being called " << std::endl;
  168. #endif
  169. auto HeapArray = oram.flat(tio, yield);
  170. uint64_t * heapreconstruction = new uint64_t[num_items + 1];
  171. for (size_t j = 1; j < num_items + 1; ++j) {
  172. heapreconstruction[j] = mpc_reconstruct(tio, yield, HeapArray[j]);
  173. #ifdef HEAP_VERBOSE
  174. if(tio.player() < 2) std::cout << j << " -----> heapreconstruction[" << j << "] = " << heapreconstruction[j] << std::endl;
  175. #endif
  176. }
  177. for (size_t j = 2; j <= num_items; ++j) {
  178. if (heapreconstruction[j/2] > heapreconstruction[j]) {
  179. std::cout << "heap property failure\n\n";
  180. std::cout << "j = " << j << std::endl;
  181. std::cout << heapreconstruction[j] << std::endl;
  182. std::cout << "j/2 = " << j/2 << std::endl;
  183. std::cout << heapreconstruction[j/2] << std::endl;
  184. }
  185. assert(heapreconstruction[j/2] <= heapreconstruction[j]);
  186. }
  187. delete [] heapreconstruction;
  188. }
  189. // Note: This function is intended for debugging purposes only.
  190. // The purpose of this function is to assert the fact that the reconstruction values of both the left child and right child are greater than or equal to the reconstruction value of the parent.
  191. // The function performs an assertion check to validate this condition. If the condition is not satisfied, an assertion error will be triggered.
  192. // This function is useful for verifying the correctness of reconstruction values during debugging and ensuring the integrity of the heap structure.
  193. // It is important to note that this function is not meant for production use and should be used solely for debugging and testing purposes.
  194. void verify_parent_children_heaps(MPCTIO tio, yield_t & yield, RegAS parent, RegAS leftchild, RegAS rightchild) {
  195. uint64_t parent_reconstruction = mpc_reconstruct(tio, yield, parent);
  196. uint64_t leftchild_reconstruction = mpc_reconstruct(tio, yield, leftchild);
  197. uint64_t rightchild_reconstruction = mpc_reconstruct(tio, yield, rightchild);
  198. #ifdef HEAP_VERBOSE
  199. std::cout << "parent_reconstruction = " << parent_reconstruction << std::endl;
  200. std::cout << "leftchild_reconstruction = " << leftchild_reconstruction << std::endl;
  201. std::cout << "rightchild_reconstruction = " << rightchild_reconstruction << std::endl << std::endl << std::endl;
  202. #endif
  203. assert(parent_reconstruction <= leftchild_reconstruction);
  204. assert(parent_reconstruction <= rightchild_reconstruction);
  205. }
  206. /*
  207. Protocol 6 from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  208. Basic restore heap property has the following functionality:
  209. Before restoring heap property: z
  210. / \
  211. y x
  212. After restoring heap property: if(y < x AND z < y) if(y < x AND z > y) if(y > x AND z < x) if(y > x AND z > x)
  213. z y z x
  214. / \ / \ / \ / \
  215. y x z x y x y z
  216. The protocol works as follows:
  217. Step 1: Compare the left and right children.
  218. Step 2: Compare the smaller child with the parent.
  219. If the smaller child is smaller than the parent, swap the smaller child with the root.
  220. The protocol requires three DORAM (Distributed Oblivious RAM) reads performed in parallel:
  221. - Read the parent, left child, and right child.
  222. Two comparisons are performed:
  223. a) Comparison between the left and right child.
  224. b) Comparison between the smaller child and the parent.
  225. Two MPC-selects are performed in parallel:
  226. - Computing the smaller child and the smaller index using MPC-select operations.
  227. Next, the offsets by which the parent and children need to be updated are computed.
  228. Offset computation involves:
  229. - One flag-flag multiplication.
  230. - Two flag-word multiplications performed in parallel.
  231. Three DORAM update operations are performed in parallel:
  232. - Update the parent, left child, and right child.
  233. The function returns the XOR-share of the smaller child's index.
  234. The total cost of the protocol includes:
  235. - 3 DORAM reads (performed in parallel).
  236. - 2 comparisons.
  237. - 2 MPC-selects (performed in parallel).
  238. - 1 flag-flag multiplication.
  239. - 2 flag-word multiplications (performed in parallel).
  240. - 3 DORAM updates (performed in parallel).
  241. */
  242. RegXS MinHeap::restore_heap_property(MPCIO & mpcio, MPCTIO tio, yield_t & yield, RegXS index) {
  243. RegAS smallest;
  244. auto HeapArray = oram.flat(tio, yield);
  245. RegXS leftchildindex = index;
  246. leftchildindex = index << 1;
  247. RegXS rightchildindex;
  248. rightchildindex.xshare = leftchildindex.xshare ^ (tio.player());
  249. RegAS parent, leftchild, rightchild;
  250. #ifdef HEAP_VERBOSE
  251. auto index_reconstruction = mpc_reconstruct(tio, yield, index);
  252. auto leftchildindex_reconstruction = mpc_reconstruct(tio, yield, leftchildindex);
  253. auto rightchildindex_reconstruction = mpc_reconstruct(tio, yield, rightchildindex);
  254. std::cout << "index_reconstruction = " << index_reconstruction << std::endl;
  255. std::cout << "leftchildindex_reconstruction = " << leftchildindex_reconstruction << std::endl;
  256. std::cout << "rightchildindex_reconstruction = " << rightchildindex_reconstruction << std::endl;
  257. #endif
  258. run_coroutines(tio, [&tio, &parent, &HeapArray, index](yield_t &yield) {
  259. auto Acoro = HeapArray.context(yield);
  260. parent = Acoro[index];},
  261. [&tio, &HeapArray, &leftchild, leftchildindex](yield_t &yield) {
  262. auto Acoro = HeapArray.context(yield);
  263. leftchild = Acoro[leftchildindex];},
  264. [&tio, &rightchild, &HeapArray, rightchildindex](yield_t &yield) {
  265. auto Acoro = HeapArray.context(yield);
  266. rightchild = Acoro[rightchildindex];});
  267. CDPF cdpf = tio.cdpf(yield);
  268. auto[lt_c, eq_c, gt_c] = cdpf.compare(tio, yield, leftchild - rightchild, tio.aes_ops());
  269. auto lteq = lt_c ^ eq_c;
  270. RegXS smallerindex;
  271. RegAS smallerchild;
  272. run_coroutines(tio, [&tio, &smallerindex, lteq, rightchildindex, leftchildindex](yield_t &yield) {
  273. mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, VALUE_BITS);
  274. }, [&tio, &smallerchild, lteq, rightchild, leftchild](yield_t &yield) {
  275. mpc_select(tio, yield, smallerchild, lteq, rightchild, leftchild, VALUE_BITS);
  276. }
  277. );
  278. CDPF cdpf0 = tio.cdpf(yield);
  279. auto[lt_p, eq_p, gt_p] = cdpf0.compare(tio, yield, smallerchild - parent, tio.aes_ops());
  280. auto lt_p_eq_p = lt_p ^ eq_p;
  281. RegBS ltlt1;
  282. mpc_and(tio, yield, ltlt1, lteq, lt_p_eq_p);
  283. RegAS update_index_by, update_leftindex_by;
  284. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent, leftchild](yield_t &yield) {
  285. mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, parent - leftchild, VALUE_BITS);
  286. }, [&tio, &update_index_by, lt_p, parent, smallerchild](yield_t &yield) {
  287. mpc_flagmult(tio, yield, update_index_by, lt_p, smallerchild - parent, VALUE_BITS);
  288. }
  289. );
  290. run_coroutines(tio, [&tio, &HeapArray, index, update_index_by](yield_t &yield) {
  291. auto Acoro = HeapArray.context(yield);
  292. Acoro[index] += update_index_by;},
  293. [&tio, &HeapArray, leftchildindex, update_leftindex_by](yield_t &yield) {
  294. auto Acoro = HeapArray.context(yield);
  295. Acoro[leftchildindex] += update_leftindex_by;},
  296. [&tio, &HeapArray, rightchildindex, update_index_by, update_leftindex_by](yield_t &yield) {
  297. auto Acoro = HeapArray.context(yield);
  298. Acoro[rightchildindex] += -(update_index_by + update_leftindex_by);});
  299. #ifdef HEAP_DEBUG
  300. verify_parent_children_heaps(tio, yield, HeapArray[index], HeapArray[leftchildindex] , HeapArray[rightchildindex]);
  301. #endif
  302. return smallerindex;
  303. }
  304. // This Protocol 7 is derived from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  305. // This protocol represents an optimized version of restoring the heap property
  306. // The key difference between the optimized and basic versions is that the optimized version utilizes a wide DPF (Distributed Point Function) for reads and writes
  307. // In addition to restoring the heap property, the function also returns the result of the comparison (leftchild > rightchild)
  308. // The (leftchild > rightchild) comparison is utilized in the extract_min operation to increment the oblivindx by a certain value
  309. // The optimized version achieves improved efficiency by leveraging wide DPF operations for read and write operations
  310. std::pair<RegXS, RegBS> 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) {
  311. auto HeapArray = oram.flat(tio, yield);
  312. RegXS leftchildindex = index;
  313. leftchildindex = index << 1;
  314. RegXS rightchildindex;
  315. rightchildindex.xshare = leftchildindex.xshare ^ tio.player();
  316. typename Duoram < RegAS > ::Flat P(HeapArray, tio, yield, 1 << layer, 1 << layer);
  317. typename Duoram < RegAS > ::Flat C(HeapArray, tio, yield, 2 << layer, 2 << layer);
  318. typename Duoram < RegAS > ::Stride L(C, tio, yield, 0, 2);
  319. typename Duoram < RegAS > ::Stride R(C, tio, yield, 1, 2);
  320. RegAS parent, leftchild, rightchild;
  321. run_coroutines(tio, [&tio, &parent, &P, &oidx](yield_t &yield) {
  322. auto Pcoro = P.context(yield);
  323. parent = Pcoro[oidx]; },
  324. [&tio, &L, &leftchild, &oidx](yield_t &yield) {
  325. auto Lcoro = L.context(yield);
  326. leftchild = Lcoro[oidx];},
  327. [&tio, &R, &rightchild, &oidx](yield_t &yield) {
  328. auto Rcoro = R.context(yield);
  329. rightchild = Rcoro[oidx];
  330. });
  331. CDPF cdpf = tio.cdpf(yield);
  332. auto[lt, eq, gt] = cdpf.compare(tio, yield, leftchild - rightchild, tio.aes_ops());
  333. auto lteq = lt ^ eq;
  334. RegXS smallerindex;
  335. RegAS smallerchild;
  336. run_coroutines(tio, [&tio, &smallerindex, lteq, rightchildindex, leftchildindex](yield_t &yield)
  337. { mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, VALUE_BITS);},
  338. [&tio, &smallerchild, lt, rightchild, leftchild](yield_t &yield)
  339. { mpc_select(tio, yield, smallerchild, lt, rightchild, leftchild, VALUE_BITS);});
  340. CDPF cdpf0 = tio.cdpf(yield);
  341. auto[lt1, eq1, gt1] = cdpf0.compare(tio, yield, smallerchild - parent, tio.aes_ops());
  342. auto lt1eq1 = lt1 ^ eq1;
  343. RegBS ltlt1;
  344. mpc_and(tio, yield, ltlt1, lteq, lt1eq1);
  345. RegAS update_index_by, update_leftindex_by;
  346. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent, leftchild](yield_t &yield)
  347. { mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, parent - leftchild, VALUE_BITS);},
  348. [&tio, &update_index_by, lt1eq1, parent, smallerchild](yield_t &yield)
  349. {mpc_flagmult(tio, yield, update_index_by, lt1eq1, smallerchild - parent, VALUE_BITS);}
  350. );
  351. run_coroutines(tio, [&tio, &P, &oidx, update_index_by](yield_t &yield) {
  352. auto Pcoro = P.context(yield);
  353. Pcoro[oidx] += update_index_by;},
  354. [&tio, &L, &oidx, update_leftindex_by](yield_t &yield) {
  355. auto Lcoro = L.context(yield);
  356. Lcoro[oidx] += update_leftindex_by;},
  357. [&tio, &R, &oidx, update_leftindex_by, update_index_by](yield_t &yield) {
  358. auto Rcoro = R.context(yield);
  359. Rcoro[oidx] += -(update_leftindex_by + update_index_by);
  360. });
  361. return {smallerindex, gt};
  362. }
  363. // Intializes the heap array with 0x7fffffffffffff
  364. void MinHeap::init(MPCTIO tio, yield_t & yield) {
  365. auto HeapArray = oram.flat(tio, yield);
  366. HeapArray.init(0x7fffffffffffff);
  367. }
  368. // This function simply inits a heap with values 1,2,...,n
  369. // We use this function only to setup our heap
  370. // to do timing experiments on insert and extractmins
  371. void MinHeap::init(MPCTIO tio, yield_t & yield, size_t which_init) {
  372. auto HeapArray = oram.flat(tio, yield);
  373. HeapArray.explicitonly(true);
  374. for (size_t j = 1; j <= num_items; ++j) {
  375. RegAS v;
  376. v.ashare = j * tio.player();
  377. HeapArray[j] = v;
  378. }
  379. HeapArray.explicitonly(false);
  380. }
  381. // Note: This function is intended for debugging purposes only.
  382. // The purpose of this function is to reconstruct the heap and print its contents.
  383. // The function performs the necessary operations to reconstruct the heap, ensuring that the heap property is satisfied. It then prints the contents of the reconstructed heap.
  384. // This function is useful for debugging and inspecting the state of the heap at a particular point in the program execution.
  385. // It is important to note that this function is not meant for production use and should be used solely for debugging and testing purposes.
  386. void MinHeap::print_heap(MPCTIO tio, yield_t & yield) {
  387. auto HeapArray = oram.flat(tio, yield);
  388. uint64_t * Pjreconstruction = new uint64_t[num_items + 1];
  389. for (size_t j = 0; j <= num_items; ++j) Pjreconstruction[j] = mpc_reconstruct(tio, yield, HeapArray[j]);
  390. for (size_t j = 0; j <= num_items; ++j) {
  391. if(2 * j < num_items) {
  392. std::cout << j << "-->> HeapArray[" << j << "] = " << std::dec << Pjreconstruction[j] << ", children are: " << Pjreconstruction[2 * j] << " and " << Pjreconstruction[2 * j + 1] << std::endl;
  393. } else {
  394. std::cout << j << "-->> HeapArray[" << j << "] = " << std::dec << Pjreconstruction[j] << " is a LEAF " << std::endl;
  395. }
  396. }
  397. delete[] Pjreconstruction;
  398. }
  399. /*
  400. Restore the head property at the root.
  401. the only reason this function exists is because at the root level
  402. the indices to read (the root and its two children) are explicit and not shared
  403. root
  404. / \
  405. leftchild rightchild
  406. After restoring heap property:
  407. if(leftchild < rightchild AND root < leftchild) if(leftchild < rightchild AND root > leftchild) if(leftchild > rightchild AND root < rightchild) if(leftchild > rightchild AND root > rightchild)
  408. root leftchild root rightchild
  409. / \ / \ / \ / \
  410. leftchild rightchild root rightchild leftchild rightchild leftchild root
  411. The restore_heap_property_at_explicit_index protocol works as follows:
  412. Step 1: Compare the left and right children.
  413. Step 2: Compare the smaller child with the root.
  414. If the smaller child is smaller than the root, swap the smaller child with the root.
  415. Unlike the restore_heap_property protocol, restore_heap_property_at_explicit_index begins with three regular (non-DORAM) read operations:
  416. - Read the parent, left child, and right child.
  417. Two comparisons are performed:
  418. a) Comparison between the left and right child.
  419. b) Comparison between the smaller child and the parent.
  420. The above comparisons have to be sequential because we need to find the smallerindex and smallerchild
  421. Which is dependent on the first comparison
  422. Next, the offsets by which the parent and children need to be updated are computed.
  423. Offset computation involves:
  424. - One flag-flag multiplication.
  425. - Two flag-word multiplications.
  426. Three DORAM update operations are required (performed in parallel) to update the parent, left child, and right child.
  427. In total, this protocol requires:
  428. - 2 comparisons.
  429. - 1 flag-flag multiplication.
  430. - 2 flag-word multiplications.
  431. - 3 DORAM updates.
  432. The function returns a pair of a) XOR-share of the index of the smaller child and b) the comparison between left and right children
  433. */
  434. std::pair<RegXS, RegBS> MinHeap::restore_heap_property_at_explicit_index(MPCTIO tio, yield_t & yield, size_t index = 1) {
  435. auto HeapArray = oram.flat(tio, yield);
  436. RegAS parent = HeapArray[index];
  437. RegAS leftchild = HeapArray[2 * index];
  438. RegAS rightchild = HeapArray[2 * index + 1];
  439. CDPF cdpf = tio.cdpf(yield);
  440. auto[lt, eq, gt] = cdpf.compare(tio, yield, leftchild - rightchild, tio.aes_ops());
  441. auto lteq = lt ^ eq;
  442. RegAS smallerchild;
  443. mpc_select(tio, yield, smallerchild, lteq, rightchild, leftchild);
  444. uint64_t leftchildindex = (2 * index);
  445. uint64_t rightchildindex = (2 * index) + 1;
  446. RegXS smallerindex = (RegXS(lteq) & leftchildindex) ^ (RegXS(gt) & rightchildindex);
  447. CDPF cdpf0 = tio.cdpf(yield);
  448. auto[lt1, eq1, gt1] = cdpf0.compare(tio, yield, smallerchild - parent, tio.aes_ops());
  449. auto lt1eq1 = lt1 ^ eq1;
  450. RegBS ltlt1;
  451. mpc_and(tio, yield, ltlt1, lteq, lt1eq1);
  452. RegAS update_index_by, update_leftindex_by;
  453. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent, leftchild](yield_t &yield) {
  454. mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, parent - leftchild, VALUE_BITS);
  455. }, [&tio, &update_index_by, lt1eq1, parent, smallerchild](yield_t &yield) {
  456. mpc_flagmult(tio, yield, update_index_by, lt1eq1, smallerchild - parent, VALUE_BITS);
  457. }
  458. );
  459. HeapArray[index] += update_index_by;
  460. HeapArray[leftchildindex] += update_leftindex_by;
  461. HeapArray[rightchildindex] += -(update_index_by + update_leftindex_by);
  462. #ifdef HEAP_VERBOSE
  463. RegAS new_parent = HeapArray[index];
  464. RegAS new_left = HeapArray[leftchildindex];
  465. RegAS new_right = HeapArray[rightchildindex];
  466. uint64_t parent_R = mpc_reconstruct(tio, yield, new_parent);
  467. uint64_t left_R = mpc_reconstruct(tio, yield, new_left);
  468. uint64_t right_R = mpc_reconstruct(tio, yield, new_right);
  469. std::cout << "parent_R = " << parent_R << std::endl;
  470. std::cout << "left_R = " << left_R << std::endl;
  471. std::cout << "right_R = " << right_R << std::endl;
  472. #endif
  473. #ifdef HEAP_DEBUG
  474. verify_parent_children_heaps(tio, yield, HeapArray[index], HeapArray[leftchildindex] , HeapArray[rightchildindex]);
  475. #endif
  476. return {smallerindex, gt};
  477. }
  478. // This Protocol 5 from PRAC: Round-Efficient 3-Party MPC for Dynamic Data Structures
  479. // The function extract_min cannot be called on an empty heap
  480. // Like in the paper, there is only one version of extract_min
  481. // the optimized version calls the optimized restore_heap_property
  482. // The extractmin algorithm removes the root and replaces it with last leaf node
  483. // After extracting the minimum element from the heap, the heap property is temporarily violated.
  484. // To restore the heap property, we begin at the root layer.
  485. // Step 1: Swap the root with the smaller child if the smaller child is less than the root.
  486. // This step is performed by the function restore_heap_property_at_explicit_index.
  487. // Step 2: Proceed down the tree along the path of the smaller child.
  488. // Repeat the process of swapping the parent with the smaller child if the parent is greater than the smaller child.
  489. // After the swap, make the smaller child the new parent.
  490. // The choice of whether to use restore_heap_property or restore_heap_property_optimized
  491. // depends on whether it is a basic or optimized extraction of the minimum element.
  492. // These functions ensure that the heap property is maintained throughout the tree.
  493. RegAS MinHeap::extract_min(MPCIO & mpcio, MPCTIO tio, yield_t & yield, int is_optimized) {
  494. size_t height = std::log2(num_items);
  495. RegAS minval;
  496. auto HeapArray = oram.flat(tio, yield);
  497. minval = HeapArray[1];
  498. HeapArray[1] = RegAS(HeapArray[num_items]);
  499. RegAS v;
  500. v.ashare = 0x7fffffffffffff * !tio.player();
  501. HeapArray[num_items] = v;
  502. num_items--;
  503. auto outroot = restore_heap_property_at_explicit_index(tio, yield);
  504. RegXS smaller = outroot.first;
  505. if(is_optimized > 0) {
  506. typename Duoram < RegAS > ::template OblivIndex < RegXS, 3 > oidx(tio, yield, height);
  507. oidx.incr(outroot.second);
  508. for (size_t i = 0; i < height-1; ++i) {
  509. auto out = restore_heap_property_optimized(tio, yield, smaller, i + 1, height, oidx);
  510. smaller = out.first;
  511. oidx.incr(out.second);
  512. }
  513. }
  514. if(is_optimized == 0) {
  515. for (size_t i = 0; i < height - 1; ++i) {
  516. smaller = restore_heap_property(mpcio, tio, yield, smaller);
  517. }
  518. }
  519. return minval;
  520. }
  521. void Heap(MPCIO & mpcio, const PRACOptions & opts, char ** args, int argc) {
  522. std::cout << "argc = " << argc << std::endl;
  523. MPCTIO tio(mpcio, 0, opts.num_threads);
  524. int nargs = argc;
  525. if(tio.player() == 2) nargs -= 6;
  526. if(tio.player() == 1) nargs -= 5;
  527. if(tio.player() == 0) nargs -= 4;
  528. int maxdepth = 0;
  529. int heapdepth = 0;
  530. size_t n_inserts = 0;
  531. size_t n_extracts = 0;
  532. int is_optimized = 0;
  533. int run_sanity = 0;
  534. // Process command line arguments
  535. for (int i = 0; i < nargs; i += 2) {
  536. std::string option = args[i];
  537. if (option == "-m" && i + 1 < nargs) {
  538. maxdepth = std::atoi(args[i + 1]);
  539. } else if (option == "-d" && i + 1 < nargs) {
  540. heapdepth = std::atoi(args[i + 1]);
  541. } else if (option == "-i" && i + 1 < nargs) {
  542. n_inserts = std::atoi(args[i + 1]);
  543. } else if (option == "-e" && i + 1 < nargs) {
  544. n_extracts = std::atoi(args[i + 1]);
  545. } else if (option == "-opt" && i + 1 < nargs) {
  546. is_optimized = std::atoi(args[i + 1]);
  547. } else if (option == "-s" && i + 1 < nargs) {
  548. run_sanity = std::atoi(args[i + 1]);
  549. }
  550. }
  551. run_coroutines(tio, [ & tio, maxdepth, heapdepth, n_inserts, n_extracts, is_optimized, run_sanity, &mpcio](yield_t & yield) {
  552. size_t size = size_t(1) << maxdepth;
  553. MinHeap tree(tio.player(), size);
  554. tree.init(tio, yield);
  555. tree.num_items = (size_t(1) << heapdepth) - 1;
  556. tree.init(tio, yield, 1);
  557. std::cout << "\n===== Init Stats =====\n";
  558. tio.sync_lamport();
  559. mpcio.dump_stats(std::cout);
  560. mpcio.reset_stats();
  561. tio.reset_lamport();
  562. for (size_t j = 0; j < n_inserts; ++j) {
  563. RegAS inserted_val;
  564. inserted_val.randomize(10);
  565. #ifdef HEAP_VERBOSE
  566. inserted_val.ashare = inserted_val.ashare;
  567. uint64_t inserted_val_rec = mpc_reconstruct(tio, yield, inserted_val, VALUE_BITS);
  568. std::cout << "inserted_val_rec = " << inserted_val_rec << std::endl << std::endl;
  569. #endif
  570. if(is_optimized > 0) tree.insert_optimized(tio, yield, inserted_val);
  571. if(is_optimized == 0) tree.insert(tio, yield, inserted_val);
  572. }
  573. std::cout << "\n===== Insert Stats =====\n";
  574. tio.sync_lamport();
  575. mpcio.dump_stats(std::cout);
  576. if(run_sanity == 1 && n_inserts != 0) tree.verify_heap_property(tio, yield);
  577. mpcio.reset_stats();
  578. tio.reset_lamport();
  579. #ifdef HEAP_VERBOSE
  580. tree.print_heap(tio, yield);
  581. #endif
  582. for (size_t j = 0; j < n_extracts; ++j) {
  583. tree.extract_min(mpcio, tio, yield, is_optimized);
  584. #ifdef HEAP_VERBOSE
  585. RegAS minval = tree.extract_min(mpcio, tio, yield, is_optimized);
  586. uint64_t minval_reconstruction = mpc_reconstruct(tio, yield, minval, VALUE_BITS);
  587. std::cout << "minval_reconstruction = " << minval_reconstruction << std::endl;
  588. #endif
  589. #ifdef HEAP_DEBUG
  590. tree.verify_heap_property(tio, yield);
  591. #endif
  592. #ifdef HEAP_VERBOSE
  593. tree.print_heap(tio, yield);
  594. #endif
  595. }
  596. std::cout << "\n===== Extract Min Stats =====\n";
  597. tio.sync_lamport();
  598. mpcio.dump_stats(std::cout);
  599. #ifdef HEAP_VERBOSE
  600. tree.print_heap(tio, yield);
  601. #endif
  602. if(run_sanity == 1 && n_extracts != 0) tree.verify_heap_property(tio, yield);
  603. }
  604. );
  605. }