node.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_key_zero(Node n1, MPCTIO tio, yield_t &yield) {
  126. CDPF cdpf = tio.cdpf(yield);
  127. RegAS zero;
  128. auto [lt, eq, gt] = cdpf.compare(tio, yield, n1.key - zero, tio.aes_ops());
  129. return eq;
  130. }
  131. RegBS check_ptr_zero(RegXS ptr, MPCTIO tio, yield_t &yield) {
  132. CDPF cdpf = tio.cdpf(yield);
  133. RegAS ptr_as;
  134. mpc_xs_to_as(tio, yield, ptr_as, ptr);
  135. RegAS zero;
  136. auto [lt, eq, gt] = cdpf.compare(tio, yield, ptr_as - zero, tio.aes_ops());
  137. return eq;
  138. }
  139. // Assuming pointer of 64 bits is split as:
  140. // - 32 bits Left ptr
  141. // - 32 bits Right ptr
  142. // < Left, Right>
  143. inline RegXS extractLeftPtr(RegXS pointer){
  144. return ((pointer&(0xFFFFFFFF00000000))>>32);
  145. }
  146. inline RegXS extractRightPtr(RegXS pointer){
  147. return (pointer&(0x00000000FFFFFFFF));
  148. }
  149. inline void setLeftPtr(RegXS &pointer, RegXS new_ptr){
  150. pointer&=(0x00000000FFFFFFFF);
  151. pointer+=(new_ptr<<32);
  152. }
  153. inline void setRightPtr(RegXS &pointer, RegXS new_ptr){
  154. pointer&=(0xFFFFFFFF00000000);
  155. pointer+=(new_ptr);
  156. }
  157. std::tuple<RegXS, RegBS> insert(MPCTIO &tio, yield_t &yield, RegXS &ptr, Node &new_node, Duoram<Node>::Flat A, int TTL, RegBS isDummy) {
  158. if(TTL==0) {
  159. RegBS zero;
  160. return {ptr, zero};
  161. }
  162. Node cnode = A[ptr];
  163. //Compare key
  164. auto [lteq, gt] = compare_keys(cnode, new_node, tio, yield);
  165. // Depending on [lteq, gt] select the next ptr/index as
  166. // upper 32 bits of cnode.pointers if lteq
  167. // lower 32 bits of cnode.pointers if gt
  168. RegXS left = extractLeftPtr(cnode.pointers);
  169. RegXS right = extractRightPtr(cnode.pointers);
  170. RegXS next_ptr;
  171. mpc_select(tio, yield, next_ptr, gt, left, right, 32);
  172. RegBS F_z = check_ptr_zero(next_ptr, tio, yield);
  173. RegBS F_i;
  174. if(tio.player()==0) {
  175. isDummy^=1;
  176. }
  177. mpc_and(tio, yield, F_i, (isDummy), F_z);
  178. if(tio.player()==0) {
  179. isDummy^=1;
  180. }
  181. isDummy^=F_i;
  182. auto [wptr, direction] = insert(tio, yield, next_ptr, new_node, A, TTL-1, isDummy);
  183. RegXS ret_ptr;
  184. RegBS ret_direction;
  185. mpc_select(tio, yield, ret_ptr, F_i, wptr, ptr);
  186. //ret_direction = direction + F_p(direction - gt)
  187. mpc_and(tio, yield, ret_direction, F_i, direction^gt);
  188. ret_direction^=direction;
  189. return {ret_ptr, ret_direction};
  190. }
  191. // Insert(root, ptr, key, TTL, isDummy) -> (new_ptr, wptr, wnode, f_p)
  192. void insert(MPCTIO &tio, yield_t &yield, RegXS &root, Node &node, Duoram<Node>::Flat A, size_t num_items) {
  193. if(num_items==0) {
  194. Node zero;
  195. A[0] = zero;
  196. A[1] = node;
  197. (root).set(1*tio.player());
  198. num_items++;
  199. return;
  200. }
  201. else {
  202. // Insert node into next free slot in the ORAM
  203. int new_id = 1 + num_items;
  204. int TTL = num_items++;
  205. A[new_id] = node;
  206. RegXS new_addr;
  207. new_addr.set(new_id * tio.player());
  208. RegBS isDummy;
  209. //Do a recursive insert
  210. auto [wptr, direction] = insert(tio, yield, root, node, A, TTL, isDummy);
  211. //Complete the insertion by reading wptr and updating its pointers
  212. RegXS pointers = A[wptr].NODE_POINTERS;
  213. RegXS left_ptr = extractLeftPtr(pointers);
  214. RegXS right_ptr = extractRightPtr(pointers);
  215. RegXS new_right_ptr, new_left_ptr;
  216. mpc_select(tio, yield, new_right_ptr, direction, right_ptr, new_addr);
  217. if(tio.player()==0) {
  218. direction^=1;
  219. }
  220. mpc_select(tio, yield, new_left_ptr, direction, left_ptr, new_addr);
  221. setLeftPtr(pointers, new_left_ptr);
  222. setRightPtr(pointers, new_right_ptr);
  223. A[wptr].NODE_POINTERS = pointers;
  224. }
  225. }
  226. void newnode(Node &a) {
  227. a.key.randomize(8);
  228. a.pointers.set(0);
  229. a.value.randomize();
  230. }
  231. // Now we use the node in various ways. This function is called by
  232. // online.cpp.
  233. void bst(MPCIO &mpcio,
  234. const PRACOptions &opts, char **args)
  235. {
  236. nbits_t depth=5;
  237. if (*args) {
  238. depth = atoi(*args);
  239. ++args;
  240. }
  241. MPCTIO tio(mpcio, 0, opts.num_threads);
  242. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  243. size_t size = size_t(1)<<depth;
  244. Duoram<Node> oram(tio.player(), size);
  245. auto A = oram.flat(tio, yield);
  246. size_t num_items = 0;
  247. RegXS root;
  248. Node c;
  249. for(int i = 0; i<30; i++) {
  250. newnode(c);
  251. insert(tio, yield, root, c, A, num_items);
  252. }
  253. if (depth < 10) {
  254. oram.dump();
  255. auto R = A.reconstruct();
  256. if (tio.player() == 0) {
  257. for(size_t i=0;i<R.size();++i) {
  258. printf("\n%04lx ", i);
  259. R[i].dump();
  260. }
  261. printf("\n");
  262. }
  263. }
  264. });
  265. }