rdpf.tcc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Templated method implementations for rdpf.hpp
  2. // Create a StreamEval object that will start its output at index start.
  3. // It will wrap around to 0 when it hits 2^depth. If use_expansion
  4. // is true, then if the DPF has been expanded, just output values
  5. // from that. If use_expansion=false or if the DPF has not been
  6. // expanded, compute the values on the fly.
  7. template <typename T>
  8. StreamEval<T>::StreamEval(const T &rdpf, address_t start,
  9. size_t &op_counter, bool use_expansion) : rdpf(rdpf),
  10. op_counter(op_counter), use_expansion(use_expansion)
  11. {
  12. depth = rdpf.depth();
  13. // Prevent overflow of 1<<depth
  14. if (depth < ADDRESS_MAX_BITS) {
  15. indexmask = (address_t(1)<<depth)-1;
  16. } else {
  17. indexmask = ~0;
  18. }
  19. start &= indexmask;
  20. // Record that we haven't actually output the leaf for index start
  21. // itself yet
  22. nextindex = start;
  23. if (use_expansion && rdpf.has_expansion()) {
  24. // We just need to keep the counter, not compute anything
  25. return;
  26. }
  27. path.resize(depth);
  28. pathindex = start;
  29. path[0] = rdpf.get_seed();
  30. for (nbits_t i=1;i<depth;++i) {
  31. bool dir = !!(pathindex & (address_t(1)<<(depth-i)));
  32. path[i] = rdpf.descend(path[i-1], i-1, dir, op_counter);
  33. }
  34. }
  35. template <typename T>
  36. typename T::node StreamEval<T>::next()
  37. {
  38. if (use_expansion && rdpf.has_expansion()) {
  39. // Just use the precomputed values
  40. typename T::node leaf = rdpf.get_expansion(nextindex);
  41. nextindex = (nextindex + 1) & indexmask;
  42. return leaf;
  43. }
  44. // Invariant: in the first call to next(), nextindex = pathindex.
  45. // Otherwise, nextindex = pathindex+1.
  46. // Get the XOR of nextindex and pathindex, and strip the low bit.
  47. // If nextindex and pathindex are equal, or pathindex is even
  48. // and nextindex is the consecutive odd number, index_xor will be 0,
  49. // indicating that we don't have to update the path, but just
  50. // compute the appropriate leaf given by the low bit of nextindex.
  51. //
  52. // Otherwise, say for example pathindex is 010010111 and nextindex
  53. // is 010011000. Then their XOR is 000001111, and stripping the low
  54. // bit yields 000001110, so how_many_1_bits will be 3.
  55. // That indicates (typically) that path[depth-3] was a left child,
  56. // and now we need to change it to a right child by descending right
  57. // from path[depth-4], and then filling the path after that with
  58. // left children.
  59. //
  60. // When we wrap around, however, index_xor will be 111111110 (after
  61. // we strip the low bit), and how_many_1_bits will be depth-1, but
  62. // the new top child (of the root seed) we have to compute will be a
  63. // left, not a right, child.
  64. uint64_t index_xor = (nextindex ^ pathindex) & ~1;
  65. nbits_t how_many_1_bits = __builtin_popcount(index_xor);
  66. if (how_many_1_bits > 0) {
  67. // This will almost always be 1, unless we've just wrapped
  68. // around from the right subtree back to the left, in which case
  69. // it will be 0.
  70. bool top_changed_bit =
  71. nextindex & (address_t(1) << how_many_1_bits);
  72. path[depth-how_many_1_bits] =
  73. rdpf.descend(path[depth-how_many_1_bits-1],
  74. depth-how_many_1_bits-1, top_changed_bit, op_counter);
  75. for (nbits_t i = depth-how_many_1_bits; i < depth-1; ++i) {
  76. path[i+1] = rdpf.descend(path[i], i, 0, op_counter);
  77. }
  78. }
  79. typename T::node leaf = rdpf.descend(path[depth-1], depth-1,
  80. nextindex & 1, op_counter);
  81. pathindex = nextindex;
  82. nextindex = (nextindex + 1) & indexmask;
  83. return leaf;
  84. }
  85. // I/O for RDPFs
  86. template <typename T>
  87. T& operator>>(T &is, RDPF &rdpf)
  88. {
  89. is.read((char *)&rdpf.seed, sizeof(rdpf.seed));
  90. uint8_t depth;
  91. // The whichhalf bit is the high bit of depth
  92. is.read((char *)&depth, sizeof(depth));
  93. rdpf.whichhalf = !!(depth & 0x80);
  94. depth &= 0x7f;
  95. bool read_expanded = false;
  96. if (depth > 64) {
  97. read_expanded = true;
  98. depth -= 64;
  99. }
  100. assert(depth <= ADDRESS_MAX_BITS);
  101. rdpf.cw.clear();
  102. for (uint8_t i=0; i<depth; ++i) {
  103. DPFnode cw;
  104. is.read((char *)&cw, sizeof(cw));
  105. rdpf.cw.push_back(cw);
  106. }
  107. if (read_expanded) {
  108. rdpf.expansion.resize(1<<depth);
  109. is.read((char *)rdpf.expansion.data(),
  110. sizeof(rdpf.expansion[0])<<depth);
  111. }
  112. value_t cfbits = 0;
  113. is.read((char *)&cfbits, BITBYTES(depth));
  114. rdpf.cfbits = cfbits;
  115. is.read((char *)&rdpf.unit_sum_inverse, sizeof(rdpf.unit_sum_inverse));
  116. is.read((char *)&rdpf.scaled_sum, sizeof(rdpf.scaled_sum));
  117. is.read((char *)&rdpf.scaled_xor, sizeof(rdpf.scaled_xor));
  118. return is;
  119. }
  120. // Write the DPF to the output stream. If expanded=true, then include
  121. // the expansion _if_ the DPF is itself already expanded. You can use
  122. // this to write DPFs to files.
  123. template <typename T>
  124. T& write_maybe_expanded(T &os, const RDPF &rdpf,
  125. bool expanded = true)
  126. {
  127. os.write((const char *)&rdpf.seed, sizeof(rdpf.seed));
  128. uint8_t depth = rdpf.cw.size();
  129. assert(depth <= ADDRESS_MAX_BITS);
  130. // The whichhalf bit is the high bit of depth
  131. // If we're writing an expansion, add 64 to depth as well
  132. uint8_t whichhalf_and_depth = depth |
  133. (uint8_t(rdpf.whichhalf)<<7);
  134. bool write_expansion = false;
  135. if (expanded && rdpf.expansion.size() == (size_t(1)<<depth)) {
  136. write_expansion = true;
  137. whichhalf_and_depth += 64;
  138. }
  139. os.write((const char *)&whichhalf_and_depth,
  140. sizeof(whichhalf_and_depth));
  141. for (uint8_t i=0; i<depth; ++i) {
  142. os.write((const char *)&rdpf.cw[i], sizeof(rdpf.cw[i]));
  143. }
  144. if (write_expansion) {
  145. os.write((const char *)rdpf.expansion.data(),
  146. sizeof(rdpf.expansion[0])<<depth);
  147. }
  148. os.write((const char *)&rdpf.cfbits, BITBYTES(depth));
  149. os.write((const char *)&rdpf.unit_sum_inverse, sizeof(rdpf.unit_sum_inverse));
  150. os.write((const char *)&rdpf.scaled_sum, sizeof(rdpf.scaled_sum));
  151. os.write((const char *)&rdpf.scaled_xor, sizeof(rdpf.scaled_xor));
  152. return os;
  153. }
  154. // The ordinary << version never writes the expansion, since this is
  155. // what we use to send DPFs over the network.
  156. template <typename T>
  157. T& operator<<(T &os, const RDPF &rdpf)
  158. {
  159. return write_maybe_expanded(os, rdpf, false);
  160. }
  161. // I/O for RDPF Triples
  162. // We never write RDPFTriples over the network, so always write
  163. // the DPF expansions if they're available.
  164. template <typename T>
  165. T& operator<<(T &os, const RDPFTriple &rdpftrip)
  166. {
  167. write_maybe_expanded(os, rdpftrip.dpf[0], true);
  168. write_maybe_expanded(os, rdpftrip.dpf[1], true);
  169. write_maybe_expanded(os, rdpftrip.dpf[2], true);
  170. nbits_t depth = rdpftrip.dpf[0].depth();
  171. os.write((const char *)&rdpftrip.as_target.ashare, BITBYTES(depth));
  172. os.write((const char *)&rdpftrip.xs_target.xshare, BITBYTES(depth));
  173. return os;
  174. }
  175. template <typename T>
  176. T& operator>>(T &is, RDPFTriple &rdpftrip)
  177. {
  178. is >> rdpftrip.dpf[0] >> rdpftrip.dpf[1] >> rdpftrip.dpf[2];
  179. nbits_t depth = rdpftrip.dpf[0].depth();
  180. rdpftrip.as_target.ashare = 0;
  181. is.read((char *)&rdpftrip.as_target.ashare, BITBYTES(depth));
  182. rdpftrip.xs_target.xshare = 0;
  183. is.read((char *)&rdpftrip.xs_target.xshare, BITBYTES(depth));
  184. return is;
  185. }
  186. // I/O for RDPF Pairs
  187. // We never write RDPFPairs over the network, so always write
  188. // the DPF expansions if they're available.
  189. template <typename T>
  190. T& operator<<(T &os, const RDPFPair &rdpfpair)
  191. {
  192. write_maybe_expanded(os, rdpfpair.dpf[0], true);
  193. write_maybe_expanded(os, rdpfpair.dpf[1], true);
  194. return os;
  195. }
  196. template <typename T>
  197. T& operator>>(T &is, RDPFPair &rdpfpair)
  198. {
  199. is >> rdpfpair.dpf[0] >> rdpfpair.dpf[1];
  200. return is;
  201. }