heap.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 optimized insertion protocol works as follows
  9. // Do a binary search from the root of the rightmost child
  10. // Binary search returns the index in the path where the new element should go to
  11. // We next do a trickle down.
  12. // Consdier a path P = [a1, a2, a3, a4, ..., a_n]
  13. // Consider a standard-basis vector [0, ... 0, 1 , 0, ... , 0] (at i)
  14. // we get a new path P = [a1, ... , ai,ai ..., a_n]
  15. // P[i] = newelement
  16. int MinHeap::insert_optimized(MPCTIO tio, yield_t & yield, RegAS val) {
  17. auto HeapArray = oram.flat(tio, yield);
  18. num_items++;
  19. //std::cout << "num_items = " << num_items << std::endl;
  20. uint64_t height = std::ceil(std::log2(num_items)) + 1;
  21. //std::cout << "height = " << height << std::endl;
  22. size_t childindex = num_items;
  23. RegAS zero;
  24. zero.ashare = 0;
  25. HeapArray[childindex] = zero;
  26. typename Duoram<RegAS>::Path P(HeapArray, tio, yield, childindex);
  27. const RegXS foundidx = P.binary_search(val);
  28. uint64_t logheight = std::ceil(double(std::log2(height))) + 1;
  29. // std::cout << "logheight = " << logheight << std::endl;
  30. // RDPF<1> dpf2(tio, yield, foundidx, logheight, false, false);
  31. // RegBS * flags_array = new RegBS[height];
  32. std::vector<RegBS> standard_basis_vector(height+1);
  33. typename Duoram<RegAS>::template OblivIndex<RegXS,1> oidx(tio, yield, foundidx, logheight);
  34. auto flags_array = oidx.unit_vector(tio, yield, 1 << logheight, foundidx);
  35. // for(size_t j = 0; j < height; ++j)
  36. // {
  37. // uint64_t reconstruction = mpc_reconstruct(tio, yield, new_stand[j], 64);
  38. // if(reconstruction != 0) std::cout << j << " --->> reconstruction from OblivIndex [new_stand] = " << reconstruction << std::endl;
  39. // }
  40. for(size_t j = 0; j < height; ++j)
  41. {
  42. if(tio.player() !=2)
  43. {
  44. // RDPF<1>::LeafNode leafval = dpf2.leaf(j, tio.aes_ops());
  45. // flags_array[j] = dpf2.unit_bs(leafval);
  46. standard_basis_vector[j] = flags_array[j];
  47. if(j > 0) flags_array[j] = flags_array[j] ^ flags_array[j-1];
  48. }
  49. }
  50. // // #ifdef VERBOSE
  51. // for(size_t j = 0; j < height; ++j)
  52. // {
  53. // uint64_t reconstruction = mpc_reconstruct(tio, yield, standard_basis_vector[j], 64);
  54. // if(reconstruction != 0) std::cout << j << " --->> reconstruction Explicitly Calling the DPF [standard_basis_vector] = " << reconstruction << std::endl;
  55. // }
  56. // // #endif
  57. RegAS * z_array2 = new RegAS[height];
  58. RegAS * z2_tmp = new RegAS[height];
  59. RegAS * standard_basis_vector_time_value = new RegAS[height];
  60. for(size_t j = 0; j < height; ++j) z_array2[j] = P[j];
  61. //print_heap(tio, yield);
  62. std::vector<coro_t> coroutines;
  63. for(size_t j = 1; j < height; ++j)
  64. {
  65. coroutines.emplace_back(
  66. [&tio, z2_tmp, flags_array, z_array2, j](yield_t &yield) {
  67. mpc_flagmult(tio, yield, z2_tmp[j], flags_array[j-1], (z_array2[j-1]-z_array2[j]), 64);
  68. });
  69. coroutines.emplace_back(
  70. [&tio, standard_basis_vector_time_value, standard_basis_vector, val, z_array2, j](yield_t &yield) {
  71. mpc_flagmult(tio, yield, standard_basis_vector_time_value[j-1], standard_basis_vector[j-1], (val - z_array2[j-1]) , 64);
  72. });
  73. }
  74. run_coroutines(tio, coroutines);
  75. // //#ifdef VERBOSE
  76. // for(size_t j = 0; j < height; ++j)
  77. // {
  78. // int64_t reconstruction = mpc_reconstruct(tio, yield, z2_tmp[j], 64);
  79. // std::cout << j << " --->> reconstruction [z2_tmp] = " << reconstruction << std::endl;
  80. // }
  81. // std::cout << std::endl << " =============== " << std::endl;
  82. // for(size_t j = 0; j < height; ++j)
  83. // {
  84. // int64_t reconstruction = mpc_reconstruct(tio, yield, flags_array[j], 64);
  85. // std::cout << j << " --->> reconstruction [flags_array] = " << reconstruction << std::endl;
  86. // }
  87. // std::cout << std::endl << " =============== " << std::endl;
  88. // for(size_t j = 0; j < height; ++j)
  89. // {
  90. // int64_t reconstruction = mpc_reconstruct(tio, yield, standard_basis_vector[j], 64);
  91. // std::cout << j << " --->> reconstruction [standard_basis_vector] = " << reconstruction << std::endl;
  92. // }
  93. // std::cout << std::endl << " =============== " << std::endl;
  94. // for(size_t j = 0; j < height; ++j)
  95. // {
  96. // int64_t reconstruction = mpc_reconstruct(tio, yield, z_array2[j], 64);
  97. // std::cout << j << " --->> reconstruction [z_array2] = " << reconstruction << std::endl;
  98. // }
  99. // //#endif
  100. // for(size_t j = 0; j < height; ++j) P[j] += (z2_tmp[j] + standard_basis_vector_time_value[j]);
  101. // //#ifdef VERBOSE
  102. // std::cout << std::endl << " =============== " << std::endl;
  103. // for(size_t j = 0; j < height; ++j)
  104. // {
  105. // int64_t reconstruction = mpc_reconstruct(tio, yield, P[j], 64);
  106. // std::cout << j << " --->> reconstruction [P] = " << reconstruction << std::endl;
  107. // }
  108. // print_heap(tio, yield);
  109. // //#endif
  110. // for(size_t j = 1; j < height; ++j) P[j] += z2_tmp[j];
  111. // typename Duoram<RegAS>::template OblivIndex<RegXS,1> oidx(tio, yield, foundidx, height);
  112. //P[oidx] = val;
  113. return 1;
  114. }
  115. // The insert protocol works as follows:
  116. // It adds a new element in the last entry of the array
  117. // From the leaf (the element added), compare with its parent (1 oblivious compare)
  118. // If the child is larger, then we do an OSWAP.
  119. int MinHeap::insert(MPCTIO tio, yield_t & yield, RegAS val) {
  120. auto HeapArray = oram.flat(tio, yield);
  121. num_items++;
  122. //std::cout << "num_items = " << num_items << std::endl;
  123. // uint64_t val_reconstruct = mpc_reconstruct(tio, yield, val);
  124. // std::cout << "val_reconstruct = " << val_reconstruct << std::endl;
  125. size_t childindex = num_items;
  126. size_t parentindex = childindex / 2;
  127. #ifdef VERBOSE
  128. std::cout << "childindex = " << childindex << std::endl;
  129. std::cout << "parentindex = " << parentindex << std::endl;
  130. #endif
  131. HeapArray[num_items] = val;
  132. typename Duoram<RegAS>::Path P(HeapArray, tio, yield, childindex);
  133. //RegXS foundidx = P.binary_search(val);
  134. while (parentindex > 0) {
  135. RegAS sharechild = HeapArray[childindex];
  136. RegAS shareparent = HeapArray[parentindex];
  137. CDPF cdpf = tio.cdpf(yield);
  138. RegAS diff = sharechild - shareparent;
  139. auto[lt, eq, gt] = cdpf.compare(tio, yield, diff, tio.aes_ops());
  140. auto lteq = lt ^ eq;
  141. mpc_oswap(tio, yield, sharechild, shareparent, lteq, 64);
  142. HeapArray[childindex] = sharechild;
  143. HeapArray[parentindex] = shareparent;
  144. childindex = parentindex;
  145. parentindex = parentindex / 2;
  146. }
  147. return 1;
  148. }
  149. int MinHeap::verify_heap_property(MPCTIO tio, yield_t & yield) {
  150. std::cout << std::endl << std::endl << "verify_heap_property is being called " << std::endl;
  151. auto HeapArray = oram.flat(tio, yield);
  152. uint64_t heapreconstruction[num_items];
  153. for (size_t j = 0; j <= num_items; ++j) {
  154. heapreconstruction[j] = mpc_reconstruct(tio, yield, HeapArray[j]);
  155. }
  156. for (size_t j = 1; j < num_items / 2; ++j) {
  157. if (heapreconstruction[j] > heapreconstruction[2 * j]) {
  158. std::cout << "heap property failure\n\n";
  159. std::cout << "j = " << j << std::endl;
  160. std::cout << heapreconstruction[j] << std::endl;
  161. std::cout << "2*j = " << 2 * j << std::endl;
  162. std::cout << heapreconstruction[2 * j] << std::endl;
  163. }
  164. if (heapreconstruction[j] > heapreconstruction[2 * j + 1]) {
  165. std::cout << "heap property failure\n\n";
  166. std::cout << "j = " << j << std::endl;
  167. std::cout << heapreconstruction[j] << std::endl;
  168. std::cout << "2*j + 1 = " << 2 * j + 1<< std::endl;
  169. std::cout << heapreconstruction[2 * j + 1] << std::endl;
  170. }
  171. //assert(heapreconstruction[j] <= heapreconstruction[2 * j]);
  172. //assert(heapreconstruction[j] <= heapreconstruction[2 * j + 1]);
  173. }
  174. return 1;
  175. }
  176. void verify_parent_children_heaps(MPCTIO tio, yield_t & yield, RegAS parent, RegAS leftchild, RegAS rightchild) {
  177. std::cout << "calling this ... \n";
  178. uint64_t parent_reconstruction = mpc_reconstruct(tio, yield, parent);
  179. uint64_t leftchild_reconstruction = mpc_reconstruct(tio, yield, leftchild);
  180. uint64_t rightchild_reconstruction = mpc_reconstruct(tio, yield, rightchild);
  181. assert(parent_reconstruction <= leftchild_reconstruction);
  182. assert(parent_reconstruction <= rightchild_reconstruction);
  183. }
  184. RegXS MinHeap::restore_heap_property(MPCIO & mpcio, MPCTIO tio, yield_t & yield, RegXS index) {
  185. RegAS smallest;
  186. auto HeapArray = oram.flat(tio, yield);
  187. mpcio.reset_stats();
  188. tio.reset_lamport();
  189. RegXS leftchildindex = index;
  190. leftchildindex = index << 1;
  191. RegXS rightchildindex;
  192. rightchildindex.xshare = leftchildindex.xshare ^ (tio.player());
  193. RegAS parent; // = HeapArray[index];
  194. RegAS leftchild; // = HeapArray[leftchildindex];
  195. RegAS rightchild; // = HeapArray[rightchildindex];
  196. std::vector<coro_t> coroutines_read;
  197. coroutines_read.emplace_back(
  198. [&tio, &parent, &HeapArray, index](yield_t &yield) {
  199. auto Acoro = HeapArray.context(yield);
  200. parent = Acoro[index]; //inserted_val;
  201. });
  202. coroutines_read.emplace_back(
  203. [&tio, &HeapArray, &leftchild, leftchildindex](yield_t &yield) {
  204. auto Acoro = HeapArray.context(yield);
  205. leftchild = Acoro[leftchildindex]; //inserted_val;
  206. });
  207. coroutines_read.emplace_back(
  208. [&tio, &rightchild, &HeapArray, rightchildindex](yield_t &yield) {
  209. auto Acoro = HeapArray.context(yield);
  210. rightchild = Acoro[rightchildindex];
  211. });
  212. run_coroutines(tio, coroutines_read);
  213. std::cout << "=========== READS DONE =========== \n";
  214. tio.sync_lamport();
  215. mpcio.dump_stats(std::cout);
  216. //RegAS sum = parent + leftchild + rightchild;
  217. CDPF cdpf = tio.cdpf(yield);
  218. auto[lt_c, eq_c, gt_c] = cdpf.compare(tio, yield, leftchild - rightchild, tio.aes_ops());
  219. auto lteq = lt_c ^ eq_c;
  220. RegXS smallerindex;
  221. RegAS smallerchild;
  222. #ifdef VERBOSE
  223. uint64_t LC_rec = mpc_reconstruct(tio, yield, leftchildindex);
  224. std::cout << "LC_rec = " << LC_rec << std::endl;
  225. #endif
  226. std::cout << "=========== Compare DONE =========== \n";
  227. tio.sync_lamport();
  228. mpcio.dump_stats(std::cout);
  229. // mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, 64);
  230. // mpc_select(tio, yield, smallerchild, lt_c, rightchild, leftchild, 64);
  231. run_coroutines(tio, [&tio, &smallerindex, lteq, rightchildindex, leftchildindex](yield_t &yield)
  232. { mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, 64);},
  233. [&tio, &smallerchild, lteq, rightchild, leftchild](yield_t &yield)
  234. { mpc_select(tio, yield, smallerchild, lteq, rightchild, leftchild, 64);});
  235. #ifdef VERBOSE
  236. uint64_t smallerindex_rec = mpc_reconstruct(tio, yield, smallerindex);
  237. std::cout << "smallerindex_rec = " << smallerindex_rec << std::endl;
  238. #endif
  239. std::cout << "=========== mpc_select DONE =========== \n";
  240. tio.sync_lamport();
  241. mpcio.dump_stats(std::cout);
  242. CDPF cdpf0 = tio.cdpf(yield);
  243. auto[lt_p, eq_p, gt_p] = cdpf0.compare(tio, yield, smallerchild - parent, tio.aes_ops());
  244. std::cout << "=========== Compare DONE =========== \n";
  245. tio.sync_lamport();
  246. mpcio.dump_stats(std::cout);
  247. auto lt_p_eq_p = lt_p ^ eq_p;
  248. RegBS ltlt1;
  249. mpc_and(tio, yield, ltlt1, lteq, lt_p_eq_p);
  250. std::cout << "=========== mpc_and DONE =========== \n";
  251. tio.sync_lamport();
  252. mpcio.dump_stats(std::cout);
  253. RegAS update_index_by, update_leftindex_by;
  254. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent, leftchild](yield_t &yield)
  255. { mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, (parent - leftchild), 64);},
  256. [&tio, &update_index_by, lt_p, parent, smallerchild](yield_t &yield)
  257. {mpc_flagmult(tio, yield, update_index_by, lt_p, smallerchild - parent, 64);}
  258. );
  259. std::cout << "=========== flag mults =========== \n";
  260. tio.sync_lamport();
  261. mpcio.dump_stats(std::cout);
  262. std::vector<coro_t> coroutines;
  263. // HeapArray[index] += update_index_by;
  264. // HeapArray[leftchildindex] += update_leftindex_by;
  265. // HeapArray[rightchildindex] += -(update_index_by + update_leftindex_by);
  266. coroutines.emplace_back(
  267. [&tio, &HeapArray, index, update_index_by](yield_t &yield) {
  268. auto Acoro = HeapArray.context(yield);
  269. Acoro[index] += update_index_by; //inserted_val;
  270. });
  271. coroutines.emplace_back(
  272. [&tio, &HeapArray, leftchildindex, update_leftindex_by](yield_t &yield) {
  273. auto Acoro = HeapArray.context(yield);
  274. Acoro[leftchildindex] += update_leftindex_by; //inserted_val;
  275. });
  276. coroutines.emplace_back(
  277. [&tio, &HeapArray, rightchildindex, update_index_by, update_leftindex_by](yield_t &yield) {
  278. auto Acoro = HeapArray.context(yield);
  279. Acoro[rightchildindex] += -(update_index_by + update_leftindex_by);
  280. });
  281. run_coroutines(tio, coroutines);
  282. std::cout << "=========== updates done =========== \n";
  283. tio.sync_lamport();
  284. mpcio.dump_stats(std::cout);
  285. // verify_parent_children_heaps(tio, yield, HeapArray[index], HeapArray[leftchildindex] , HeapArray[rightchildindex]);
  286. return smallerindex;
  287. }
  288. 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)) {
  289. auto HeapArray = oram.flat(tio, yield);
  290. RegXS leftchildindex = index;
  291. leftchildindex = index << 1;
  292. RegXS rightchildindex;
  293. rightchildindex.xshare = leftchildindex.xshare ^ (tio.player());
  294. typename Duoram < RegAS > ::Flat P(HeapArray, tio, yield, 1 << layer, 1 << layer);
  295. typename Duoram < RegAS > ::Flat C(HeapArray, tio, yield, 2 << layer, 2 << layer);
  296. typename Duoram < RegAS > ::Stride L(C, tio, yield, 0, 2);
  297. typename Duoram < RegAS > ::Stride R(C, tio, yield, 1, 2);
  298. // RegAS parent_tmp = P[oidx];
  299. // RegAS leftchild_tmp = L[oidx];
  300. // RegAS rightchild_tmp = R[oidx];
  301. RegAS parent_tmp; // = HeapArray[index];
  302. RegAS leftchild_tmp; // = HeapArray[leftchildindex];
  303. RegAS rightchild_tmp; // = HeapArray[rightchildindex];
  304. std::vector<coro_t> coroutines_read;
  305. coroutines_read.emplace_back(
  306. [&tio, &parent_tmp, &P, &oidx](yield_t &yield) {
  307. // auto Acoro = P.context(yield);
  308. parent_tmp = P[oidx]; //inserted_val;
  309. });
  310. coroutines_read.emplace_back(
  311. [&tio, &L, &leftchild_tmp, &oidx](yield_t &yield) {
  312. //auto Acoro = L.context(yield);
  313. leftchild_tmp = L[oidx]; //inserted_val;
  314. });
  315. coroutines_read.emplace_back(
  316. [&tio, &R, &rightchild_tmp, &oidx](yield_t &yield) {
  317. // auto Acoro = R.context(yield);
  318. rightchild_tmp = R[oidx];
  319. });
  320. run_coroutines(tio, coroutines_read);
  321. //RegAS sum = parent_tmp + leftchild_tmp + rightchild_tmp;
  322. CDPF cdpf = tio.cdpf(yield);
  323. auto[lt, eq, gt] = cdpf.compare(tio, yield, leftchild_tmp - rightchild_tmp, tio.aes_ops());
  324. auto lteq = lt ^ eq;
  325. RegXS smallerindex;
  326. RegAS smallerchild;
  327. // mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, 64);
  328. // mpc_select(tio, yield, smallerchild, lt, rightchild_tmp, leftchild_tmp, 64);
  329. run_coroutines(tio, [&tio, &smallerindex, lteq, rightchildindex, leftchildindex](yield_t &yield)
  330. { mpc_select(tio, yield, smallerindex, lteq, rightchildindex, leftchildindex, 64);;},
  331. [&tio, &smallerchild, lt, rightchild_tmp, leftchild_tmp](yield_t &yield)
  332. { mpc_select(tio, yield, smallerchild, lt, rightchild_tmp, leftchild_tmp, 64);;});
  333. CDPF cdpf0 = tio.cdpf(yield);
  334. auto[lt1, eq1, gt1] = cdpf0.compare(tio, yield, smallerchild - parent_tmp, tio.aes_ops());
  335. // RegAS z;
  336. // mpc_flagmult(tio, yield, z, lt1, smallerchild - parent_tmp, 64);
  337. auto lt1eq1 = lt1 ^ eq1;
  338. RegBS ltlt1;
  339. // RegAS zz;
  340. mpc_and(tio, yield, ltlt1, lteq, lt1eq1);
  341. // // mpc_flagmult(tio, yield, zz, ltlt1, (parent_tmp - leftchild_tmp), 64);
  342. // run_coroutines(tio, [&tio, &ltlt1, lteq, lt1eq1](yield_t &yield)
  343. // { mpc_and(tio, yield, ltlt1, lteq, lt1eq1);},
  344. // [&tio, &zz, ltlt1, parent_tmp, leftchild_tmp](yield_t &yield)
  345. // { mpc_flagmult(tio, yield, zz, ltlt1, (parent_tmp - leftchild_tmp), 64);});
  346. RegAS update_index_by, update_leftindex_by;
  347. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent_tmp, leftchild_tmp](yield_t &yield)
  348. { mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, (parent_tmp - leftchild_tmp), 64);},
  349. [&tio, &update_index_by, lt1eq1, parent_tmp, smallerchild](yield_t &yield)
  350. {mpc_flagmult(tio, yield, update_index_by, lt1eq1, smallerchild - parent_tmp, 64);}
  351. );
  352. // RegAS leftchildplusparent = RegAS(HeapArray[index]) + RegAS(HeapArray[leftchildindex]);
  353. // RegAS tmp = (sum - leftchildplusparent);
  354. std::vector<coro_t> coroutines;
  355. coroutines.emplace_back(
  356. [&tio, &P, &oidx, update_index_by](yield_t &yield) {
  357. auto Acoro = P.context(yield);
  358. Acoro[oidx] += update_index_by; //inserted_val;
  359. });
  360. coroutines.emplace_back(
  361. [&tio, &L, &oidx, update_leftindex_by](yield_t &yield) {
  362. auto Acoro = L.context(yield);
  363. Acoro[oidx] += update_leftindex_by; //inserted_val;
  364. });
  365. coroutines.emplace_back(
  366. [&tio, &R, &oidx, update_leftindex_by, update_index_by](yield_t &yield) {
  367. auto Acoro = R.context(yield);
  368. Acoro[oidx] += -(update_leftindex_by + update_index_by);
  369. });
  370. run_coroutines(tio, coroutines);
  371. // P[oidx] += z;
  372. // L[oidx] += zz;
  373. // R[oidx] += zzz;
  374. return std::make_pair(smallerindex, gt);
  375. }
  376. void MinHeap::initialize(MPCTIO tio, yield_t & yield) {
  377. auto HeapArray = oram.flat(tio, yield);
  378. HeapArray.init(0x7fffffffffffff);
  379. }
  380. void MinHeap::initialize_random(MPCTIO tio, yield_t & yield) {
  381. auto HeapArray = oram.flat(tio, yield);
  382. std::cout << "initialize_random " << num_items << std::endl;
  383. std::vector<coro_t> coroutines;
  384. // RegAS v[num_items+1];
  385. // for(size_t j = 1; j < num_items; ++j) v[j].ashare = j * tio.player();
  386. for(size_t j = 1; j <= num_items; ++j)
  387. {
  388. coroutines.emplace_back(
  389. [&tio, &HeapArray, j](yield_t &yield) {
  390. auto Acoro = HeapArray.context(yield);
  391. RegAS v;
  392. v.ashare = j * tio.player();
  393. Acoro[j] = v; //inserted_val;
  394. });
  395. }
  396. run_coroutines(tio, coroutines);
  397. // for(size_t j = 1; j <= num_items; ++j)
  398. // {
  399. // RegAS v;
  400. // RegAS inserted_val;
  401. // inserted_val.randomize(6);
  402. // v.ashare = j * tio.player();
  403. // HeapArray[j] = v; //inserted_val;
  404. // // HeapArray.init([v] (size_t j) { return v; });
  405. // }
  406. // HeapArray.init(0x7fffffffffffff);
  407. }
  408. void MinHeap::print_heap(MPCTIO tio, yield_t & yield) {
  409. auto HeapArray = oram.flat(tio, yield);
  410. uint64_t * Pjreconstruction = new uint64_t[num_items + 1];
  411. for(size_t j = 0; j <= num_items; ++j) Pjreconstruction[j] = mpc_reconstruct(tio, yield, HeapArray[j]);
  412. for(size_t j = 0; j <= num_items; ++j)
  413. {
  414. if(2 * j < num_items) {
  415. std::cout << j << "-->> HeapArray[" << j << "] = " << std::dec << Pjreconstruction[j] << ", children are: " << Pjreconstruction[2 * j] << " and " << Pjreconstruction[2 * j + 1] << std::endl;
  416. }
  417. else
  418. {
  419. std::cout << j << "-->> HeapArray[" << j << "] = " << std::dec << Pjreconstruction[j] << " is a LEAF " << std::endl;
  420. }
  421. }
  422. }
  423. auto MinHeap::restore_heap_property_at_root(MPCTIO tio, yield_t & yield, size_t index = 1) {
  424. //size_t index = 1;
  425. //std::cout << "index = " << index << std::endl;
  426. auto HeapArray = oram.flat(tio, yield);
  427. RegAS parent = HeapArray[index];
  428. RegAS leftchild = HeapArray[2 * index];
  429. RegAS rightchild = HeapArray[2 * index + 1];
  430. RegAS sum = parent + leftchild + rightchild;
  431. CDPF cdpf = tio.cdpf(yield);
  432. auto[lt, eq, gt] = cdpf.compare(tio, yield, leftchild - rightchild, tio.aes_ops()); // c_1 in the paper
  433. RegAS smallerchild;
  434. mpc_select(tio, yield, smallerchild, lt, rightchild, leftchild, 64); // smallerchild holds smaller of left and right child
  435. auto lteq = lt ^ eq;
  436. RegXS smallerindex(lt);
  437. uint64_t leftchildindex = (2 * index);
  438. uint64_t rightchildindex = (2 * index) + 1;
  439. smallerindex = (RegXS(lteq) & leftchildindex) ^ (RegXS(gt) & rightchildindex);
  440. CDPF cdpf0 = tio.cdpf(yield);
  441. auto[lt1, eq1, gt1] = cdpf0.compare(tio, yield, smallerchild - parent, tio.aes_ops());
  442. auto lt_p_eq_p = lt1 ^ eq1;
  443. RegBS ltlt1;
  444. mpc_and(tio, yield, ltlt1, lteq, lt_p_eq_p);
  445. RegAS update_index_by, update_leftindex_by;
  446. run_coroutines(tio, [&tio, &update_leftindex_by, ltlt1, parent, leftchild](yield_t &yield)
  447. { mpc_flagmult(tio, yield, update_leftindex_by, ltlt1, (parent - leftchild), 64);},
  448. [&tio, &update_index_by, lt1, parent, smallerchild](yield_t &yield)
  449. {mpc_flagmult(tio, yield, update_index_by, lt1, smallerchild - parent, 64);}
  450. );
  451. // HeapArray[index] += update_index_by;
  452. // HeapArray[leftchildindex] += update_leftindex_by;
  453. std::vector<coro_t> coroutines;
  454. coroutines.emplace_back(
  455. [&tio, &HeapArray, index, update_index_by](yield_t &yield) {
  456. auto Acoro = HeapArray.context(yield);
  457. Acoro[index] += update_index_by; //inserted_val;
  458. });
  459. coroutines.emplace_back(
  460. [&tio, &HeapArray, leftchildindex, update_leftindex_by](yield_t &yield) {
  461. auto Acoro = HeapArray.context(yield);
  462. Acoro[leftchildindex] += update_leftindex_by; //inserted_val;
  463. });
  464. coroutines.emplace_back(
  465. [&tio, &HeapArray, rightchildindex, update_index_by, update_leftindex_by](yield_t &yield) {
  466. auto Acoro = HeapArray.context(yield);
  467. Acoro[rightchildindex] += -(update_index_by + update_leftindex_by);
  468. });
  469. run_coroutines(tio, coroutines);
  470. RegAS leftchildplusparent = RegAS(HeapArray[index]) + RegAS(HeapArray[leftchildindex]);
  471. RegAS tmp = (sum - leftchildplusparent);
  472. HeapArray[rightchildindex] += tmp - rightchild;
  473. #ifdef VERBOSE
  474. RegAS new_parent = HeapArray[index];
  475. RegAS new_left = HeapArray[leftchildindex];
  476. RegAS new_right = HeapArray[rightchildindex];
  477. uint64_t parent_R = mpc_reconstruct(tio, yield, new_parent);
  478. uint64_t left_R = mpc_reconstruct(tio, yield, new_left);
  479. uint64_t right_R = mpc_reconstruct(tio, yield, new_right);
  480. std::cout << "parent_R = " << parent_R << std::endl;
  481. std::cout << "left_R = " << left_R << std::endl;
  482. std::cout << "right_R = " << right_R << std::endl;
  483. #endif
  484. //verify_parent_children_heaps(tio, yield, HeapArray[index], HeapArray[leftchildindex] , HeapArray[rightchildindex]);
  485. return std::make_pair(smallerindex, gt);
  486. }
  487. RegAS MinHeap::extract_min(MPCIO & mpcio, MPCTIO tio, yield_t & yield, int is_optimized) {
  488. RegAS minval;
  489. auto HeapArray = oram.flat(tio, yield);
  490. minval = HeapArray[1];
  491. HeapArray[1] = RegAS(HeapArray[num_items]);
  492. num_items--;
  493. auto outroot = restore_heap_property_at_root(tio, yield);
  494. RegXS smaller = outroot.first;
  495. size_t height = std::log2(num_items);
  496. if(is_optimized > 0)
  497. {
  498. typename Duoram < RegAS > ::template OblivIndex < RegXS, 3 > oidx(tio, yield, height);
  499. oidx.incr(outroot.second);
  500. for (size_t i = 0; i < height; ++i) {
  501. auto out = restore_heap_property_optimized(tio, yield, smaller, i + 1, height, typename Duoram < RegAS > ::template OblivIndex < RegXS, 3 > (oidx));;
  502. smaller = out.first;
  503. oidx.incr(out.second);
  504. }
  505. }
  506. if(is_optimized == 0)
  507. {
  508. for (size_t i = 0; i < height; ++i) {
  509. smaller = restore_heap_property(mpcio, tio, yield, smaller);
  510. std::cout << "one iter done ... \n \n \n";
  511. }
  512. }
  513. return minval;
  514. }
  515. void MinHeap::heapify2(MPCTIO tio, yield_t & yield, size_t index = 1) {
  516. // auto outroot = restore_heap_property_at_root(tio, yield, index);
  517. // RegXS smaller = outroot.first;
  518. // #ifdef VERBOSE
  519. // uint64_t smaller_rec = mpc_reconstruct(tio, yield, smaller, 64);
  520. // std::cout << "smaller_rec = " << smaller_rec << std::endl;
  521. // std::cout << "num_items = " << num_items << std::endl;
  522. // std::cout << "index = " << index << std::endl;
  523. // #endif
  524. // size_t height = std::log2(num_items) - std::floor(log2(index)) ;
  525. // #ifdef VERBOSE
  526. // std::cout << "height = " << height << std::endl << "===================" << std::endl;
  527. // #endif
  528. // for (size_t i = 0; i < height - 1; ++i) {
  529. // #ifdef VERBOSE
  530. // std::cout << "index = " << index << ", i = " << i << std::endl;
  531. // uint64_t smaller_rec = mpc_reconstruct(tio, yield, smaller, 64);
  532. // std::cout << "[inside loop] smaller_rec = " << smaller_rec << std::endl;
  533. // #endif
  534. // smaller = restore_heap_property(tio, yield, smaller);
  535. // }
  536. }
  537. void MinHeap::heapify(MPCTIO tio, yield_t & yield) {
  538. size_t startIdx = ((num_items + 1) / 2) - 1;
  539. //std::cout << "startIdx " << startIdx << std::endl;
  540. for (size_t i = startIdx; i >= 1; i--) {
  541. heapify2(tio, yield, i);
  542. //print_heap(tio, yield);
  543. }
  544. }
  545. void Heap(MPCIO & mpcio,
  546. const PRACOptions & opts, char ** args) {
  547. nbits_t depth = atoi(args[0]);
  548. nbits_t depth2 = atoi(args[1]);
  549. size_t n_inserts = atoi(args[2]);
  550. size_t n_extracts = atoi(args[3]);
  551. int is_optimized = atoi(args[4]);
  552. std::cout << "print arguements " << std::endl;
  553. std::cout << args[0] << std::endl;
  554. if ( * args) {
  555. depth = atoi( * args);
  556. ++args;
  557. }
  558. size_t items = (size_t(1) << depth) - 1;
  559. if ( * args) {
  560. items = atoi( * args);
  561. ++args;
  562. }
  563. //
  564. std::cout << "items = " << items << std::endl;
  565. MPCTIO tio(mpcio, 0, opts.num_threads);
  566. run_coroutines(tio, [ & tio, depth, depth2, items, n_inserts, n_extracts, is_optimized, &mpcio](yield_t & yield) {
  567. size_t size = size_t(1) << depth;
  568. // std::cout << "size = " << size << std::endl;
  569. MinHeap tree(tio.player(), size);
  570. tree.initialize(tio, yield);
  571. tree.num_items = (size_t(1) << depth2) - 1;
  572. // std::cout << "num_items " << tree.num_items << std::endl;
  573. tree.initialize_random(tio, yield);
  574. std::cout << "\n===== Init Stats =====\n";
  575. tio.sync_lamport();
  576. mpcio.dump_stats(std::cout);
  577. mpcio.reset_stats();
  578. tio.reset_lamport();
  579. // tree.heapify(tio, yield);
  580. // tree.print_heap(tio, yield);
  581. for (size_t j = 0; j < n_inserts; ++j) {
  582. RegAS inserted_val;
  583. inserted_val.randomize(6);
  584. #ifdef VERBOSE
  585. inserted_val.ashare = inserted_val.ashare;
  586. uint64_t inserted_val_rec = mpc_reconstruct(tio, yield, inserted_val, 64);
  587. std::cout << "inserted_val_rec = " << inserted_val_rec << std::endl << std::endl;
  588. #endif
  589. if(is_optimized > 0) tree.insert_optimized(tio, yield, inserted_val);
  590. if(is_optimized == 0) tree.insert(tio, yield, inserted_val);
  591. //tree.print_heap(tio, yield);
  592. }
  593. std::cout << "\n===== Insert Stats =====\n";
  594. tio.sync_lamport();
  595. mpcio.dump_stats(std::cout);
  596. mpcio.reset_stats();
  597. tio.reset_lamport();
  598. // tree.verify_heap_property(tio, yield);
  599. // tree.print_heap(tio, yield);
  600. for (size_t j = 0; j < n_extracts; ++j) {
  601. tree.extract_min(mpcio, tio, yield, is_optimized);
  602. //RegAS minval = tree.extract_min(tio, yield, is_optimized);
  603. // uint64_t minval_reconstruction = mpc_reconstruct(tio, yield, minval, 64);
  604. // std::cout << "minval_reconstruction = " << minval_reconstruction << std::endl;
  605. // tree.verify_heap_property(tio, yield);
  606. // tree.print_heap(tio, yield);
  607. }
  608. std::cout << "\n===== Extract Min Stats =====\n";
  609. tio.sync_lamport();
  610. mpcio.dump_stats(std::cout);
  611. //tree.print_heap(tio, yield);
  612. //tree.verify_heap_property(tio, yield);
  613. });
  614. }