bst.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include <functional>
  2. #include "types.hpp"
  3. #include "duoram.hpp"
  4. #include "node.hpp"
  5. // This file demonstrates how to implement custom ORAM wide cell types.
  6. // Such types can be structures of arbitrary numbers of RegAS and RegXS
  7. // fields. The example here imagines a node of a binary search tree,
  8. // where you would want the key to be additively shared (so that you can
  9. // easily do comparisons), the pointers field to be XOR shared (so that
  10. // you can easily do bit operations to pack two pointers and maybe some
  11. // tree balancing information into one field) and the value doesn't
  12. // really matter, but XOR shared is usually slightly more efficient.
  13. struct Node {
  14. RegAS key;
  15. RegXS pointers;
  16. RegXS value;
  17. // Field-access macros so we can write A[i].NODE_KEY instead of
  18. // A[i].field(&Node::key)
  19. #define NODE_KEY field(&Node::key)
  20. #define NODE_POINTERS field(&Node::pointers)
  21. #define NODE_VALUE field(&Node::value)
  22. // For debugging and checking answers
  23. void dump() const {
  24. printf("[%016lx %016lx %016lx]", key.share(), pointers.share(),
  25. value.share());
  26. }
  27. // You'll need to be able to create a random element, and do the
  28. // operations +=, +, -=, - (binary and unary). Note that for
  29. // XOR-shared fields, + and - are both really XOR.
  30. inline void randomize() {
  31. key.randomize();
  32. pointers.randomize();
  33. value.randomize();
  34. }
  35. inline Node &operator+=(const Node &rhs) {
  36. this->key += rhs.key;
  37. this->pointers += rhs.pointers;
  38. this->value += rhs.value;
  39. return *this;
  40. }
  41. inline Node operator+(const Node &rhs) const {
  42. Node res = *this;
  43. res += rhs;
  44. return res;
  45. }
  46. inline Node &operator-=(const Node &rhs) {
  47. this->key -= rhs.key;
  48. this->pointers -= rhs.pointers;
  49. this->value -= rhs.value;
  50. return *this;
  51. }
  52. inline Node operator-(const Node &rhs) const {
  53. Node res = *this;
  54. res -= rhs;
  55. return res;
  56. }
  57. inline Node operator-() const {
  58. Node res;
  59. res.key = -this->key;
  60. res.pointers = -this->pointers;
  61. res.value = -this->value;
  62. return res;
  63. }
  64. // Multiply each field by the local share of the corresponding field
  65. // in the argument
  66. inline Node mulshare(const Node &rhs) const {
  67. Node res = *this;
  68. res.key.mulshareeq(rhs.key);
  69. res.pointers.mulshareeq(rhs.pointers);
  70. res.value.mulshareeq(rhs.value);
  71. return res;
  72. }
  73. // You need a method to turn a leaf node of a DPF into a share of a
  74. // unit element of your type. Typically set each RegAS to
  75. // dpf.unit_as(leaf) and each RegXS or RegBS to dpf.unit_bs(leaf).
  76. // Note that RegXS will extend a RegBS of 1 to the all-1s word, not
  77. // the word with value 1. This is used for ORAM reads, where the
  78. // same DPF is used for all the fields.
  79. inline void unit(const RDPF &dpf, DPFnode leaf) {
  80. key = dpf.unit_as(leaf);
  81. pointers = dpf.unit_bs(leaf);
  82. value = dpf.unit_bs(leaf);
  83. }
  84. // Perform an update on each of the fields, using field-specific
  85. // MemRefs constructed from the Shape shape and the index idx
  86. template <typename Sh, typename U>
  87. inline static void update(Sh &shape, yield_t &shyield, U idx,
  88. const Node &M) {
  89. run_coroutines(shyield,
  90. [&shape, &idx, &M] (yield_t &yield) {
  91. Sh Sh_coro = shape.context(yield);
  92. Sh_coro[idx].NODE_KEY += M.key;
  93. },
  94. [&shape, &idx, &M] (yield_t &yield) {
  95. Sh Sh_coro = shape.context(yield);
  96. Sh_coro[idx].NODE_POINTERS += M.pointers;
  97. },
  98. [&shape, &idx, &M] (yield_t &yield) {
  99. Sh Sh_coro = shape.context(yield);
  100. Sh_coro[idx].NODE_VALUE += M.value;
  101. });
  102. }
  103. };
  104. // I/O operations (for sending over the network)
  105. template <typename T>
  106. T& operator>>(T& is, Node &x)
  107. {
  108. is >> x.key >> x.pointers >> x.value;
  109. return is;
  110. }
  111. template <typename T>
  112. T& operator<<(T& os, const Node &x)
  113. {
  114. os << x.key << x.pointers << x.value;
  115. return os;
  116. }
  117. // This macro will define I/O on tuples of two or three of the cell type
  118. DEFAULT_TUPLE_IO(Node)
  119. std::tuple<RegBS, RegBS> compare_keys(Node n1, Node n2, MPCTIO tio, yield_t &yield) {
  120. CDPF cdpf = tio.cdpf(yield);
  121. auto [lt, eq, gt] = cdpf.compare(tio, yield, n2.key - n1.key, tio.aes_ops());
  122. RegBS lteq = lt^eq;
  123. return {lteq, gt};
  124. }
  125. RegBS check_ptr_zero(RegXS ptr, MPCTIO tio, yield_t &yield) {
  126. CDPF cdpf = tio.cdpf(yield);
  127. RegAS ptr_as;
  128. mpc_xs_to_as(tio, yield, ptr_as, ptr);
  129. RegAS zero;
  130. auto [lt, eq, gt] = cdpf.compare(tio, yield, ptr_as - zero, tio.aes_ops());
  131. return eq;
  132. }
  133. // Assuming pointer of 64 bits is split as:
  134. // - 32 bits Left ptr
  135. // - 32 bits Right ptr
  136. // < Left, Right>
  137. inline RegXS extractLeftPtr(RegXS pointer){
  138. return ((pointer&(0xFFFFFFFF00000000))>>32);
  139. }
  140. inline RegXS extractRightPtr(RegXS pointer){
  141. return (pointer&(0x00000000FFFFFFFF));
  142. }
  143. inline void setLeftPtr(RegXS &pointer, RegXS new_ptr){
  144. pointer&=(0x00000000FFFFFFFF);
  145. pointer+=(new_ptr<<32);
  146. }
  147. inline void setRightPtr(RegXS &pointer, RegXS new_ptr){
  148. pointer&=(0xFFFFFFFF00000000);
  149. pointer+=(new_ptr);
  150. }
  151. std::tuple<RegXS, RegBS> insert(MPCTIO &tio, yield_t &yield, RegXS ptr, Node new_node, Duoram<Node>::Flat A, int TTL, RegBS isDummy) {
  152. if(TTL==0) {
  153. RegBS zero;
  154. return {ptr, zero};
  155. }
  156. Node cnode = A[ptr];
  157. //Compare key
  158. auto [lteq, gt] = compare_keys(cnode, new_node, tio, yield);
  159. // Depending on [lteq, gt] select the next ptr/index as
  160. // upper 32 bits of cnode.pointers if lteq
  161. // lower 32 bits of cnode.pointers if gt
  162. RegXS left = extractLeftPtr(cnode.pointers);
  163. RegXS right = extractRightPtr(cnode.pointers);
  164. RegXS next_ptr;
  165. mpc_select(tio, yield, next_ptr, gt, left, right, 32);
  166. //RegBS F_z = check_ptr_zero(next_ptr, tio, yield);
  167. CDPF dpf;
  168. size_t &aes_ops = tio.aes_ops();
  169. //RegAS next_ptr_as;
  170. //mpc_xs_to_as(tio, yield, next_ptr_as, next_ptr);
  171. //RegBS F_z = dpf.is_zero(tio, yield, next_ptr, aes_ops);
  172. RegBS F_i;
  173. if(tio.player()==0) {
  174. isDummy^=1;
  175. }
  176. mpc_and(tio, yield, F_i, (isDummy), F_z);
  177. if(tio.player()==0) {
  178. isDummy^=1;
  179. }
  180. isDummy^=F_i;
  181. auto [wptr, direction] = insert(tio, yield, next_ptr, new_node, A, TTL-1, isDummy);
  182. RegXS ret_ptr;
  183. RegBS ret_direction;
  184. mpc_select(tio, yield, ret_ptr, F_i, wptr, ptr);
  185. //ret_direction = direction + F_p(direction - gt)
  186. mpc_and(tio, yield, ret_direction, F_i, direction^gt);
  187. ret_direction^=direction;
  188. return {ret_ptr, ret_direction};
  189. }
  190. // Insert(root, ptr, key, TTL, isDummy) -> (new_ptr, wptr, wnode, f_p)
  191. void insert(MPCTIO &tio, yield_t &yield, RegXS &root, Node node, Duoram<Node>::Flat A, size_t &num_items) {
  192. if(num_items==0) {
  193. Node zero;
  194. A[0] = zero;
  195. A[1] = node;
  196. (root).set(1*tio.player());
  197. num_items++;
  198. return;
  199. }
  200. else {
  201. // Insert node into next free slot in the ORAM
  202. int new_id = 1 + num_items;
  203. int TTL = num_items++;
  204. A[new_id] = node;
  205. RegXS new_addr;
  206. new_addr.set(new_id * tio.player());
  207. RegBS isDummy;
  208. //Do a recursive insert
  209. auto [wptr, direction] = insert(tio, yield, root, node, A, TTL, isDummy);
  210. //Complete the insertion by reading wptr and updating its pointers
  211. RegXS pointers = A[wptr].NODE_POINTERS;
  212. RegXS left_ptr = extractLeftPtr(pointers);
  213. RegXS right_ptr = extractRightPtr(pointers);
  214. RegXS new_right_ptr, new_left_ptr;
  215. mpc_select(tio, yield, new_right_ptr, direction, right_ptr, new_addr);
  216. if(tio.player()==0) {
  217. direction^=1;
  218. }
  219. mpc_select(tio, yield, new_left_ptr, direction, left_ptr, new_addr);
  220. setLeftPtr(pointers, new_left_ptr);
  221. setRightPtr(pointers, new_right_ptr);
  222. A[wptr].NODE_POINTERS = pointers;
  223. }
  224. }
  225. void newnode(Node &a) {
  226. a.key.randomize(8);
  227. a.pointers.set(0);
  228. a.value.randomize();
  229. }
  230. // Now we use the node in various ways. This function is called by
  231. // online.cpp.
  232. void bst(MPCIO &mpcio,
  233. const PRACOptions &opts, char **args)
  234. {
  235. nbits_t depth=5;
  236. if (*args) {
  237. depth = atoi(*args);
  238. ++args;
  239. }
  240. MPCTIO tio(mpcio, 0, opts.num_threads);
  241. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  242. size_t size = size_t(1)<<depth;
  243. Duoram<Node> oram(tio.player(), size);
  244. auto A = oram.flat(tio, yield);
  245. size_t num_items = 0;
  246. RegXS root;
  247. Node c;
  248. for(int i = 0; i<30; i++) {
  249. newnode(c);
  250. insert(tio, yield, root, c, A, num_items);
  251. }
  252. if (depth < 10) {
  253. oram.dump();
  254. auto R = A.reconstruct();
  255. if (tio.player() == 0) {
  256. for(size_t i=0;i<R.size();++i) {
  257. printf("\n%04lx ", i);
  258. R[i].dump();
  259. }
  260. printf("\n");
  261. }
  262. }
  263. });
  264. }