cell.cpp 3.3 KB

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