bst.cpp 9.0 KB

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