duoram.tcc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Templated method implementations for duoram.hpp
  2. #include <stdio.h>
  3. #include "cdpf.hpp"
  4. // Pass the player number and desired size
  5. template <typename T>
  6. Duoram<T>::Duoram(int player, size_t size) : player(player),
  7. oram_size(size), p0_blind(blind), p1_blind(peer_blinded_db) {
  8. if (player < 2) {
  9. database.resize(size);
  10. blind.resize(size);
  11. peer_blinded_db.resize(size);
  12. } else {
  13. p0_blind.resize(size);
  14. p1_blind.resize(size);
  15. }
  16. }
  17. // For debugging; print the contents of the Duoram to stdout
  18. template <typename T>
  19. void Duoram<T>::dump() const
  20. {
  21. for (size_t i=0; i<oram_size; ++i) {
  22. if (player < 2) {
  23. printf("%04lx %016lx %016lx %016lx\n",
  24. i, database[i].share(), blind[i].share(),
  25. peer_blinded_db[i].share());
  26. } else {
  27. printf("%04lx %016lx %016lx\n",
  28. i, p0_blind[i].share(), p1_blind[i].share());
  29. }
  30. }
  31. printf("\n");
  32. }
  33. // Enable or disable explicit-only mode. Only using [] with
  34. // explicit (address_t) indices are allowed in this mode. Using []
  35. // with RegAS or RegXS indices will automatically turn off this
  36. // mode, or you can turn it off explicitly. In explicit-only mode,
  37. // updates to the memory in the Shape will not induce communication
  38. // to the server or peer, but when it turns off, a message of the
  39. // size of the entire Shape will be sent to each of the server and
  40. // the peer. This is useful if you're going to be doing multiple
  41. // explicit writes to every element of the Shape before you do your
  42. // next oblivious read or write. Bitonic sort is a prime example.
  43. template <typename T>
  44. void Duoram<T>::Shape::explicitonly(bool enable)
  45. {
  46. if (enable == true) {
  47. explicitmode = true;
  48. } else if (explicitmode == true) {
  49. explicitmode = false;
  50. // Reblind the whole Shape
  51. int player = tio.player();
  52. if (player < 2) {
  53. for (size_t i=0; i<shape_size; ++i) {
  54. auto [ DB, BL, PBD ] = get_comp(i);
  55. BL.randomize();
  56. tio.iostream_server() << BL;
  57. tio.iostream_peer() << (DB + BL);
  58. }
  59. yield();
  60. for (size_t i=0; i<shape_size; ++i) {
  61. auto [ DB, BL, PBD ] = get_comp(i);
  62. tio.iostream_peer() >> PBD;
  63. }
  64. } else {
  65. yield();
  66. for (size_t i=0; i<shape_size; ++i) {
  67. auto [BL0, BL1] = get_server(i);
  68. tio.iostream_p0() >> BL0;
  69. tio.iostream_p1() >> BL1;
  70. }
  71. }
  72. }
  73. }
  74. // For debugging or checking your answers (using this in general is
  75. // of course insecure)
  76. // This one reconstructs the whole database
  77. template <typename T>
  78. std::vector<T> Duoram<T>::Shape::reconstruct() const
  79. {
  80. int player = tio.player();
  81. std::vector<T> res;
  82. res.resize(duoram.size());
  83. // Player 1 sends their share of the database to player 0
  84. if (player == 1) {
  85. tio.queue_peer(duoram.database.data(), duoram.size()*sizeof(T));
  86. } else if (player == 0) {
  87. tio.recv_peer(res.data(), duoram.size()*sizeof(T));
  88. for(size_t i=0;i<duoram.size();++i) {
  89. res[i] += duoram.database[i];
  90. }
  91. }
  92. // The server (player 2) does nothing
  93. // Players 1 and 2 will get an empty vector here
  94. return res;
  95. }
  96. // This one reconstructs a single database value
  97. template <typename T>
  98. T Duoram<T>::Shape::reconstruct(const T& share) const
  99. {
  100. int player = tio.player();
  101. T res;
  102. // Player 1 sends their share of the value to player 0
  103. if (player == 1) {
  104. tio.queue_peer(&share, sizeof(T));
  105. } else if (player == 0) {
  106. tio.recv_peer(&res, sizeof(T));
  107. res += share;
  108. }
  109. // The server (player 2) does nothing
  110. // Players 1 and 2 will get 0 here
  111. return res;
  112. }
  113. // Function to set the shape_size of a shape and compute the number of
  114. // bits you need to address a shape of that size (which is the number of
  115. // bits in sz-1). This is typically called by subclass constructors.
  116. template <typename T>
  117. void Duoram<T>::Shape::set_shape_size(size_t sz)
  118. {
  119. shape_size = sz;
  120. // Compute the number of bits in (sz-1)
  121. // But use 0 if sz=0 for some reason (though that should never
  122. // happen)
  123. if (sz > 1) {
  124. addr_size = 64-__builtin_clzll(sz-1);
  125. addr_mask = address_t((size_t(1)<<addr_size)-1);
  126. } else {
  127. addr_size = 0;
  128. addr_mask = 0;
  129. }
  130. }
  131. // Constructor for the Flat shape. len=0 means the maximum size (the
  132. // parent's size minus start).
  133. template <typename T>
  134. Duoram<T>::Flat::Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield,
  135. size_t start, size_t len) : Shape(*this, duoram, tio, yield)
  136. {
  137. size_t parentsize = duoram.size();
  138. if (start > parentsize) {
  139. start = parentsize;
  140. }
  141. this->start = start;
  142. size_t maxshapesize = parentsize - start;
  143. if (len > maxshapesize || len == 0) {
  144. len = maxshapesize;
  145. }
  146. this->len = len;
  147. this->set_shape_size(len);
  148. }
  149. // Bitonic sort the elements from start to start+(1<<depth)-1, in
  150. // increasing order if dir=0 or decreasing order if dir=1. Note that
  151. // the elements must be at most 63 bits long each for the notion of
  152. // ">" to make consistent sense.
  153. template <typename T>
  154. void Duoram<T>::Flat::bitonic_sort(address_t start, nbits_t depth, bool dir)
  155. {
  156. if (depth == 0) return;
  157. if (depth == 1) {
  158. osort(start, start+1, dir);
  159. return;
  160. }
  161. // Recurse on the first half (increasing order) and the second half
  162. // (decreasing order) in parallel
  163. run_coroutines(this->yield,
  164. [this, start, depth](yield_t &yield) {
  165. Flat Acoro = context(yield);
  166. Acoro.bitonic_sort(start, depth-1, 0);
  167. },
  168. [this, start, depth](yield_t &yield) {
  169. Flat Acoro = context(yield);
  170. Acoro.bitonic_sort(start+(1<<(depth-1)), depth-1, 1);
  171. });
  172. // Merge the two into the desired order
  173. butterfly(start, depth, dir);
  174. }
  175. // Internal function to aid bitonic_sort
  176. template <typename T>
  177. void Duoram<T>::Flat::butterfly(address_t start, nbits_t depth, bool dir)
  178. {
  179. if (depth == 0) return;
  180. if (depth == 1) {
  181. osort(start, start+1, dir);
  182. return;
  183. }
  184. // Sort pairs of elements half the width apart in parallel
  185. address_t halfwidth = address_t(1)<<(depth-1);
  186. std::vector<coro_t> coroutines;
  187. for (address_t i=0; i<halfwidth;++i) {
  188. coroutines.emplace_back(
  189. [this, start, halfwidth, dir, i](yield_t &yield) {
  190. Flat Acoro = context(yield);
  191. Acoro.osort(start+i, start+i+halfwidth, dir);
  192. });
  193. }
  194. run_coroutines(this->yield, coroutines);
  195. // Recurse on each half in parallel
  196. run_coroutines(this->yield,
  197. [this, start, depth, dir](yield_t &yield) {
  198. Flat Acoro = context(yield);
  199. Acoro.butterfly(start, depth-1, dir);
  200. },
  201. [this, start, halfwidth, depth, dir](yield_t &yield) {
  202. Flat Acoro = context(yield);
  203. Acoro.butterfly(start+halfwidth, depth-1, dir);
  204. });
  205. }
  206. // Assuming the memory is already sorted, do an oblivious binary
  207. // search for the largest index containing the value at most the
  208. // given one. (The answer will be 0 if all of the memory elements
  209. // are greate than the target.) This Flat must be a power of 2 size.
  210. // Only available for additive shared databases for now.
  211. template <>
  212. RegAS Duoram<RegAS>::Flat::obliv_binary_search(RegAS &target)
  213. {
  214. nbits_t depth = this->addr_size;
  215. // Start in the middle
  216. RegAS index;
  217. index.set(this->tio.player() ? 0 : 1<<(depth-1));
  218. // Invariant: index points to the first element of the right half of
  219. // the remaining possible range
  220. while (depth > 0) {
  221. // Obliviously read the value there
  222. RegAS val = operator[](index);
  223. // Compare it to the target
  224. CDPF cdpf = tio.cdpf(this->yield);
  225. auto [lt, eq, gt] = cdpf.compare(this->tio, this->yield,
  226. val-target, tio.aes_ops());
  227. if (depth > 1) {
  228. // If val > target, the answer is strictly to the left
  229. // and we should subtract 2^{depth-2} from index
  230. // If val <= target, the answer is here or to the right
  231. // and we should add 2^{depth-2} to index
  232. // So we unconditionally subtract 2^{depth-2} from index, and
  233. // add (lt+eq)*2^{depth-1}.
  234. RegAS uncond;
  235. uncond.set(tio.player() ? 0 : address_t(1)<<(depth-2));
  236. RegAS cond;
  237. cond.set(tio.player() ? 0 : address_t(1)<<(depth-1));
  238. RegAS condprod;
  239. RegBS le = lt ^ eq;
  240. mpc_flagmult(this->tio, this->yield, condprod, le, cond);
  241. index -= uncond;
  242. index += condprod;
  243. } else {
  244. // If val > target, the answer is strictly to the left
  245. // If val <= target, the answer is here or to the right
  246. // so subtract gt from index
  247. RegAS cond;
  248. cond.set(tio.player() ? 0 : 1);
  249. RegAS condprod;
  250. mpc_flagmult(this->tio, this->yield, condprod, gt, cond);
  251. index -= condprod;
  252. }
  253. --depth;
  254. }
  255. return index;
  256. }
  257. // Helper functions to specialize the read and update operations for
  258. // RegAS and RegXS shared indices
  259. template <typename U>
  260. inline address_t IfRegAS(address_t val);
  261. template <typename U>
  262. inline address_t IfRegXS(address_t val);
  263. template <>
  264. inline address_t IfRegAS<RegAS>(address_t val) { return val; }
  265. template <>
  266. inline address_t IfRegAS<RegXS>(address_t val) { return 0; }
  267. template <>
  268. inline address_t IfRegXS<RegAS>(address_t val) { return 0; }
  269. template <>
  270. inline address_t IfRegXS<RegXS>(address_t val) { return val; }
  271. // Oblivious read from an additively or XOR shared index of Duoram memory
  272. // T is the sharing type of the _values_ in the database; U is the
  273. // sharing type of the _indices_ in the database.
  274. template <typename T> template <typename U>
  275. Duoram<T>::Shape::MemRefS<U>::operator T()
  276. {
  277. T res;
  278. Shape &shape = this->shape;
  279. shape.explicitonly(false);
  280. int player = shape.tio.player();
  281. if (player < 2) {
  282. // Computational players do this
  283. RDPFTriple dt = shape.tio.rdpftriple(shape.yield, shape.addr_size);
  284. // Compute the index offset
  285. U indoffset = dt.target<U>();
  286. indoffset -= idx;
  287. // We only need two of the DPFs for reading
  288. RDPFPair dp(std::move(dt), 0, player == 0 ? 2 : 1);
  289. // The RDPFTriple dt is now broken, since we've moved things out
  290. // of it.
  291. // Send it to the peer and the server
  292. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  293. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  294. shape.yield();
  295. // Receive the above from the peer
  296. U peerindoffset;
  297. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  298. // Reconstruct the total offset
  299. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  300. // Evaluate the DPFs and compute the dotproducts
  301. ParallelEval pe(dp, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  302. shape.shape_size, shape.tio.cpu_nthreads(),
  303. shape.tio.aes_ops());
  304. T init;
  305. res = pe.reduce(init, [&dp, &shape] (int thread_num, address_t i,
  306. const RDPFPair::node &leaf) {
  307. // The values from the two DPFs
  308. auto [V0, V1] = dp.unit<T>(leaf);
  309. // References to the appropriate cells in our database, our
  310. // blind, and our copy of the peer's blinded database
  311. auto [DB, BL, PBD] = shape.get_comp(i);
  312. return (DB + PBD) * V0.share() - BL * (V1-V0).share();
  313. });
  314. shape.yield();
  315. // Receive the cancellation term from the server
  316. T gamma;
  317. shape.tio.iostream_server() >> gamma;
  318. res += gamma;
  319. } else {
  320. // The server does this
  321. RDPFPair dp = shape.tio.rdpfpair(shape.yield, shape.addr_size);
  322. U p0indoffset, p1indoffset;
  323. shape.yield();
  324. // Receive the index offset from the computational players and
  325. // combine them
  326. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  327. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  328. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  329. // Evaluate the DPFs to compute the cancellation terms
  330. std::tuple<T,T> init, gamma;
  331. ParallelEval pe(dp, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  332. shape.shape_size, shape.tio.cpu_nthreads(),
  333. shape.tio.aes_ops());
  334. gamma = pe.reduce(init, [&dp, &shape] (int thread_num, address_t i,
  335. const RDPFPair::node &leaf) {
  336. // The values from the two DPFs
  337. auto [V0, V1] = dp.unit<T>(leaf);
  338. // shape.get_server(i) returns a pair of references to the
  339. // appropriate cells in the two blinded databases
  340. auto [BL0, BL1] = shape.get_server(i);
  341. return std::make_tuple(-BL0 * V1.share(), -BL1 * V0.share());
  342. });
  343. // Choose a random blinding factor
  344. T rho;
  345. rho.randomize();
  346. std::get<0>(gamma) += rho;
  347. std::get<1>(gamma) -= rho;
  348. // Send the cancellation terms to the computational players
  349. shape.tio.iostream_p0() << std::get<0>(gamma);
  350. shape.tio.iostream_p1() << std::get<1>(gamma);
  351. shape.yield();
  352. }
  353. return res; // The server will always get 0
  354. }
  355. // Oblivious update to an additively shared index of Duoram memory
  356. template <typename T> template <typename U>
  357. typename Duoram<T>::Shape::MemRefS<U>
  358. &Duoram<T>::Shape::MemRefS<U>::operator+=(const T& M)
  359. {
  360. Shape &shape = this->shape;
  361. shape.explicitonly(false);
  362. int player = shape.tio.player();
  363. if (player < 2) {
  364. // Computational players do this
  365. RDPFTriple dt = shape.tio.rdpftriple(shape.yield, shape.addr_size);
  366. // Compute the index and message offsets
  367. U indoffset = dt.target<U>();
  368. indoffset -= idx;
  369. auto Moffset = std::make_tuple(M, M, M);
  370. Moffset -= dt.scaled_value<T>();
  371. // Send them to the peer, and everything except the first offset
  372. // to the server
  373. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  374. shape.tio.iostream_peer() << Moffset;
  375. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  376. shape.tio.iostream_server() << std::get<1>(Moffset) <<
  377. std::get<2>(Moffset);
  378. shape.yield();
  379. // Receive the above from the peer
  380. U peerindoffset;
  381. std::tuple<T,T,T> peerMoffset;
  382. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  383. shape.tio.iostream_peer() >> peerMoffset;
  384. // Reconstruct the total offsets
  385. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  386. auto Mshift = combine(Moffset, peerMoffset);
  387. // Evaluate the DPFs and add them to the database
  388. ParallelEval pe(dt, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  389. shape.shape_size, shape.tio.cpu_nthreads(),
  390. shape.tio.aes_ops());
  391. int init = 0;
  392. pe.reduce(init, [&dt, &shape, &Mshift, player] (int thread_num,
  393. address_t i, const RDPFTriple::node &leaf) {
  394. // The values from the three DPFs
  395. auto [V0, V1, V2] = dt.scaled<T>(leaf) + dt.unit<T>(leaf) * Mshift;
  396. // References to the appropriate cells in our database, our
  397. // blind, and our copy of the peer's blinded database
  398. auto [DB, BL, PBD] = shape.get_comp(i);
  399. DB += V0;
  400. if (player == 0) {
  401. BL -= V1;
  402. PBD += V2-V0;
  403. } else {
  404. BL -= V2;
  405. PBD += V1-V0;
  406. }
  407. return 0;
  408. });
  409. } else {
  410. // The server does this
  411. RDPFPair dp = shape.tio.rdpfpair(shape.yield, shape.addr_size);
  412. U p0indoffset, p1indoffset;
  413. std::tuple<T,T> p0Moffset, p1Moffset;
  414. // Receive the index and message offsets from the computational
  415. // players and combine them
  416. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  417. shape.tio.iostream_p0() >> p0Moffset;
  418. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  419. shape.tio.iostream_p1() >> p1Moffset;
  420. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  421. auto Mshift = combine(p0Moffset, p1Moffset);
  422. // Evaluate the DPFs and subtract them from the blinds
  423. ParallelEval pe(dp, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  424. shape.shape_size, shape.tio.cpu_nthreads(),
  425. shape.tio.aes_ops());
  426. int init = 0;
  427. pe.reduce(init, [&dp, &shape, &Mshift] (int thread_num,
  428. address_t i, const RDPFPair::node &leaf) {
  429. // The values from the two DPFs
  430. auto V = dp.scaled<T>(leaf) + dp.unit<T>(leaf) * Mshift;
  431. // shape.get_server(i) returns a pair of references to the
  432. // appropriate cells in the two blinded databases, so we can
  433. // subtract the pair directly.
  434. shape.get_server(i) -= V;
  435. return 0;
  436. });
  437. }
  438. return *this;
  439. }
  440. // Oblivious sort with the provided other element. Without
  441. // reconstructing the values, *this will become a share of the
  442. // smaller of the reconstructed values, and other will become a
  443. // share of the larger.
  444. //
  445. // Note: this only works for additively shared databases
  446. template <> template <typename U,typename V>
  447. void Duoram<RegAS>::Flat::osort(const U &idx1, const V &idx2, bool dir)
  448. {
  449. // Load the values in parallel
  450. RegAS val1, val2;
  451. run_coroutines(yield,
  452. [this, &idx1, &val1](yield_t &yield) {
  453. Flat Acoro = context(yield);
  454. val1 = Acoro[idx1];
  455. },
  456. [this, &idx2, &val2](yield_t &yield) {
  457. Flat Acoro = context(yield);
  458. val2 = Acoro[idx2];
  459. });
  460. // Get a CDPF
  461. CDPF cdpf = tio.cdpf(yield);
  462. // Use it to compare the values
  463. RegAS diff = val1-val2;
  464. auto [lt, eq, gt] = cdpf.compare(tio, yield, diff, tio.aes_ops());
  465. RegBS cmp = dir ? lt : gt;
  466. // Get additive shares of cmp*diff
  467. RegAS cmp_diff;
  468. mpc_flagmult(tio, yield, cmp_diff, cmp, diff);
  469. // Update the two locations in parallel
  470. run_coroutines(yield,
  471. [this, &idx1, &cmp_diff](yield_t &yield) {
  472. Flat Acoro = context(yield);
  473. Acoro[idx1] -= cmp_diff;
  474. },
  475. [this, &idx2, &cmp_diff](yield_t &yield) {
  476. Flat Acoro = context(yield);
  477. Acoro[idx2] += cmp_diff;
  478. });
  479. }
  480. // Explicit read from a given index of Duoram memory
  481. template <typename T>
  482. Duoram<T>::Shape::MemRefExpl::operator T()
  483. {
  484. Shape &shape = this->shape;
  485. T res;
  486. int player = shape.tio.player();
  487. if (player < 2) {
  488. res = std::get<0>(shape.get_comp(idx));
  489. }
  490. return res; // The server will always get 0
  491. }
  492. // Explicit update to a given index of Duoram memory
  493. template <typename T>
  494. typename Duoram<T>::Shape::MemRefExpl
  495. &Duoram<T>::Shape::MemRefExpl::operator+=(const T& M)
  496. {
  497. Shape &shape = this->shape;
  498. int player = shape.tio.player();
  499. // In explicit-only mode, just update the local DB; we'll sync the
  500. // blinds and the blinded DB when we leave explicit-only mode.
  501. if (shape.explicitmode) {
  502. if (player < 2) {
  503. auto [ DB, BL, PBD ] = shape.get_comp(idx);
  504. DB += M;
  505. }
  506. return *this;
  507. }
  508. if (player < 2) {
  509. // Computational players do this
  510. // Pick a blinding factor
  511. T blind;
  512. blind.randomize();
  513. // Send the blind to the server, and the blinded value to the
  514. // peer
  515. shape.tio.iostream_server() << blind;
  516. shape.tio.iostream_peer() << (M + blind);
  517. shape.yield();
  518. // Receive the peer's blinded value
  519. T peerblinded;
  520. shape.tio.iostream_peer() >> peerblinded;
  521. // Our database, our blind, the peer's blinded database
  522. auto [ DB, BL, PBD ] = shape.get_comp(idx);
  523. DB += M;
  524. BL += blind;
  525. PBD += peerblinded;
  526. } else if (player == 2) {
  527. // The server does this
  528. // Receive the updates to the blinds
  529. T p0blind, p1blind;
  530. shape.tio.iostream_p0() >> p0blind;
  531. shape.tio.iostream_p1() >> p1blind;
  532. // The two computational parties' blinds
  533. auto [ BL0, BL1 ] = shape.get_server(idx);
  534. BL0 += p0blind;
  535. BL1 += p1blind;
  536. }
  537. return *this;
  538. }