baltree.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <functional>
  2. #include "types.hpp"
  3. #include "duoram.hpp"
  4. #include "baltree.hpp"
  5. struct Cell {
  6. RegAS key;
  7. RegXS pointers;
  8. RegXS value;
  9. // The width (the number of RegAS and RegXS entries) of this type
  10. static const size_t WIDTH = 3;
  11. void dump() const {
  12. printf("[%016lx %016lx %016lx]", key.share(), pointers.share(),
  13. value.share());
  14. }
  15. // You'll need to be able to create a random element, and do the
  16. // operations +=, +, -=, - (binary and unary). Note that for
  17. // XOR-shared fields, + and - are both really XOR.
  18. inline void randomize() {
  19. key.randomize();
  20. pointers.randomize();
  21. value.randomize();
  22. }
  23. inline Cell &operator+=(const Cell &rhs) {
  24. this->key += rhs.key;
  25. this->pointers += rhs.pointers;
  26. this->value += rhs.value;
  27. return *this;
  28. }
  29. inline Cell operator+(const Cell &rhs) const {
  30. Cell res = *this;
  31. res += rhs;
  32. return res;
  33. }
  34. inline Cell &operator-=(const Cell &rhs) {
  35. this->key -= rhs.key;
  36. this->pointers -= rhs.pointers;
  37. this->value -= rhs.value;
  38. return *this;
  39. }
  40. inline Cell operator-(const Cell &rhs) const {
  41. Cell res = *this;
  42. res -= rhs;
  43. return res;
  44. }
  45. inline Cell operator-() const {
  46. Cell res;
  47. res.key = -this->key;
  48. res.pointers = -this->pointers;
  49. res.value = -this->value;
  50. return res;
  51. }
  52. // Multiply each field by the local share of the corresponding field
  53. // in the argument
  54. inline Cell mulshare(const Cell &rhs) const {
  55. Cell res = *this;
  56. res.key.mulshareeq(rhs.key);
  57. res.pointers.mulshareeq(rhs.pointers);
  58. res.value.mulshareeq(rhs.value);
  59. return res;
  60. }
  61. // You need a method to turn a leaf node of a DPF into a share of a
  62. // unit element of your type. Typically set each RegAS to
  63. // dpf.unit_as(leaf) and each RegXS or RegBS to dpf.unit_bs(leaf).
  64. // Note that RegXS will extend a RegBS of 1 to the all-1s word, not
  65. // the word with value 1. This is used for ORAM reads, where the
  66. // same DPF is used for all the fields.
  67. inline void unit(const RDPF &dpf, DPFnode leaf) {
  68. key = dpf.unit_as(leaf);
  69. pointers = dpf.unit_bs(leaf);
  70. value = dpf.unit_bs(leaf);
  71. }
  72. // You need a method to turn WIDTH DPFs into an element of your
  73. // type, using each DPF's scaled_sum or scaled_xor value as
  74. // appropriate for each field. We need WIDTH of them because
  75. // reusing a scaled value from a DPF leaks information. The dpfs
  76. // argument is a function that given 0 <= f < WIDTH, returns a
  77. // reference to DPF number f.
  78. inline void scaled_value(std::function<const RDPF &(size_t)> dpfs) {
  79. key = dpfs(0).scaled_sum;
  80. pointers = dpfs(1).scaled_xor;
  81. value = dpfs(2).scaled_xor;
  82. }
  83. };
  84. template <typename T>
  85. T& operator>>(T& is, Cell &x)
  86. {
  87. is >> x.key >> x.pointers >> x.value;
  88. return is;
  89. }
  90. template <typename T>
  91. T& operator<<(T& os, const Cell &x)
  92. {
  93. os << x.key << x.pointers << x.value;
  94. return os;
  95. }
  96. DEFAULT_TUPLE_IO(Cell)
  97. void baltree (MPCIO &mpcio,
  98. const PRACOptions &opts, char **args)
  99. {
  100. nbits_t depth=4;
  101. if (*args) {
  102. depth = atoi(*args);
  103. ++args;
  104. }
  105. MPCTIO tio(mpcio, 0, opts.num_threads);
  106. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  107. size_t size = size_t(1)<<depth;
  108. Duoram<Cell> oram(tio.player(), size);
  109. auto A = oram.flat(tio, yield);
  110. Cell c;
  111. c.key.set(0x0102030405060708);
  112. c.pointers.set(0x1112131415161718);
  113. c.value.set(0x2122232425262728);
  114. A[0] = c;
  115. RegAS idx;
  116. Cell expl_read_c = A[0];
  117. printf("expl_read_c = ");
  118. expl_read_c.dump();
  119. printf("\n");
  120. Cell oram_read_c = A[idx];
  121. printf ("oram_read_c = ");
  122. oram_read_c.dump();
  123. printf("\n");
  124. printf("\n");
  125. oram.dump();
  126. });
  127. }