cell.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <functional>
  2. #include "types.hpp"
  3. #include "duoram.hpp"
  4. #include "cell.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. // I/O operations (for sending over the network)
  14. template <typename T>
  15. T& operator>>(T& is, Cell &x)
  16. {
  17. is >> x.key >> x.pointers >> x.value;
  18. return is;
  19. }
  20. template <typename T>
  21. T& operator<<(T& os, const Cell &x)
  22. {
  23. os << x.key << x.pointers << x.value;
  24. return os;
  25. }
  26. // This macro will define I/O on tuples of two or three of the cell type
  27. DEFAULT_TUPLE_IO(Cell)
  28. // Now we use the cell in various ways. This function is called by
  29. // online.cpp.
  30. void cell(MPCIO &mpcio,
  31. const PRACOptions &opts, char **args)
  32. {
  33. nbits_t depth=4;
  34. if (*args) {
  35. depth = atoi(*args);
  36. ++args;
  37. }
  38. MPCTIO tio(mpcio, 0, opts.num_threads);
  39. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  40. size_t size = size_t(1)<<depth;
  41. Duoram<Cell> oram(tio.player(), size);
  42. auto A = oram.flat(tio, yield);
  43. Cell c;
  44. c.key.set(0x0102030405060708);
  45. c.pointers.set(0x1112131415161718);
  46. c.value.set(0x2122232425262728);
  47. // Explicit write
  48. A[0] = c;
  49. RegAS idx;
  50. // Explicit read
  51. Cell expl_read_c = A[0];
  52. printf("expl_read_c = ");
  53. expl_read_c.dump();
  54. printf("\n");
  55. // ORAM read
  56. Cell oram_read_c = A[idx];
  57. printf("oram_read_c = ");
  58. oram_read_c.dump();
  59. printf("\n");
  60. RegXS valueupdate;
  61. valueupdate.set(0x4040404040404040 * tio.player());
  62. RegXS pointersset;
  63. pointersset.set(0x123456789abcdef0 * tio.player());
  64. // Explicit update and write of individual fields
  65. A[1].CELL_VALUE += valueupdate;
  66. A[3].CELL_POINTERS = pointersset;
  67. // Explicit read of individual field
  68. RegXS pointval = A[0].CELL_POINTERS;
  69. printf("pointval = ");
  70. pointval.dump();
  71. printf("\n");
  72. idx.set(1 * tio.player());
  73. // ORAM read of individual field
  74. RegXS oram_value_read = A[idx].CELL_VALUE;
  75. printf("oram_value_read = ");
  76. oram_value_read.dump();
  77. printf("\n");
  78. valueupdate.set(0x8080808080808080 * tio.player());
  79. // ORAM update of individual field
  80. A[idx].CELL_VALUE += valueupdate;
  81. idx.set(2 * tio.player());
  82. // ORAM write of individual field
  83. A[idx].CELL_VALUE = valueupdate;
  84. c.key.set(0x0102030405060708 * tio.player());
  85. c.pointers.set(0x1112131415161718 * tio.player());
  86. c.value.set(0x2122232425262728 * tio.player());
  87. // ORAM update of full Cell
  88. A[idx] += c;
  89. idx.set(3 * tio.player());
  90. // ORAM write of full Cell
  91. A[idx] = c;
  92. printf("\n");
  93. if (depth < 10) {
  94. oram.dump();
  95. auto R = A.reconstruct();
  96. if (tio.player() == 0) {
  97. for(size_t i=0;i<R.size();++i) {
  98. printf("\n%04lx ", i);
  99. R[i].dump();
  100. }
  101. printf("\n");
  102. }
  103. }
  104. });
  105. }