TreeBasedOramParty.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2014 by Xiao Shaun Wang <wangxiao@cs.umd.edu>
  2. package com.oblivm.backend.oram;
  3. import com.oblivm.backend.flexsc.CompEnv;
  4. import com.oblivm.backend.flexsc.Mode;
  5. import com.oblivm.backend.flexsc.Party;
  6. public abstract class TreeBasedOramParty<T> extends OramParty<T> {
  7. public PlainBlock[][] tree;
  8. protected int capacity;
  9. public TreeBasedOramParty(CompEnv<T> env, int N, int dataSize, int capacity) {
  10. super(env, N, dataSize);
  11. this.capacity = capacity;
  12. if (env.mode != Mode.COUNT) {
  13. tree = new PlainBlock[this.N][capacity];
  14. PlainBlock b = getDummyBlock(p == Party.Alice);
  15. for (int i = 0; i < this.N; ++i)
  16. for (int j = 0; j < capacity; ++j)
  17. tree[i][j] = b;
  18. }
  19. }
  20. protected PlainBlock[][] getPath(boolean[] path) {
  21. PlainBlock[][] result = new PlainBlock[logN][];
  22. if (env.mode == Mode.COUNT) {
  23. for (int i = 0; i < logN; ++i) {
  24. result[i] = new PlainBlock[capacity];
  25. for (int j = 0; j < capacity; ++j)
  26. result[i][j] = getDummyBlock(true);
  27. }
  28. return result;
  29. }
  30. int index = 1;
  31. result[0] = tree[index];
  32. for (int i = 1; i < logN; ++i) {
  33. index *= 2;
  34. if (path[lengthOfPos - i])
  35. ++index;
  36. result[i] = tree[index];
  37. }
  38. return result;
  39. }
  40. protected void putPath(PlainBlock[][] blocks, boolean[] path) {
  41. if (env.mode == Mode.COUNT)
  42. return;
  43. int index = 1;
  44. tree[index] = blocks[0];
  45. for (int i = 1; i < logN; ++i) {
  46. index *= 2;
  47. if (path[lengthOfPos - i])
  48. ++index;
  49. tree[index] = blocks[i];
  50. }
  51. }
  52. public Block<T>[][] preparePath(PlainBlock[][] clientBlock, PlainBlock[][] serverBlock) {
  53. Block<T>[][] s = inputPathOfServer(serverBlock);
  54. Block<T>[][] c = inputPathOfClient(clientBlock);
  55. return lib.xor(s, c);
  56. }
  57. public Block<T>[][] inputPathOfClient(PlainBlock[][] b) {
  58. int length = 0;
  59. for (int i = 0; i < b.length; ++i)
  60. length += b[i].length;
  61. PlainBlock[] tmp = new PlainBlock[length];
  62. int cnt = 0;
  63. for (int i = 0; i < b.length; ++i)
  64. for (int j = 0; j < b[i].length; ++j)
  65. tmp[cnt++] = b[i][j];
  66. Block<T>[] tmpResult = inputBucketOfClient(tmp);
  67. cnt = 0;
  68. Block<T>[][] result = lib.newBlockMatrix(b.length);
  69. for (int i = 0; i < b.length; ++i) {
  70. result[i] = lib.newBlockArray(b[i].length);
  71. for (int j = 0; j < b[i].length; ++j)
  72. result[i][j] = tmpResult[cnt++];
  73. }
  74. return result;
  75. }
  76. public Block<T>[][] inputPathOfServer(PlainBlock[][] b) {
  77. int length = 0;
  78. for (int i = 0; i < b.length; ++i)
  79. length += b[i].length;
  80. PlainBlock[] tmp = new PlainBlock[length];
  81. int cnt = 0;
  82. for (int i = 0; i < b.length; ++i)
  83. for (int j = 0; j < b[i].length; ++j)
  84. tmp[cnt++] = b[i][j];
  85. Block<T>[] tmpResult = inputBucketOfServer(tmp);
  86. cnt = 0;
  87. Block<T>[][] result = lib.newBlockMatrix(b.length);
  88. for (int i = 0; i < b.length; ++i) {
  89. result[i] = lib.newBlockArray(b[i].length);
  90. for (int j = 0; j < b[i].length; ++j)
  91. result[i][j] = tmpResult[cnt++];
  92. }
  93. return result;
  94. }
  95. public PlainBlock[][] preparePlainPath(Block<T>[][] blocks) {
  96. Block<T>[][] randomSCPath = lib.newBlockMatrix(blocks.length);
  97. PlainBlock[][] randomPath = new PlainBlock[blocks.length][];
  98. for (int i = 0; i < randomPath.length; ++i) {
  99. randomPath[i] = randomBucket(blocks[i].length);
  100. }
  101. env.flush();
  102. randomSCPath = inputPathOfServer(randomPath);
  103. PlainBlock[][] result = outputBuckets(lib.xor(blocks, randomSCPath));
  104. if (p == Party.Alice)
  105. return result;
  106. else
  107. return randomPath;
  108. }
  109. public PlainBlock[][] randomPath(PlainBlock[][] path) {
  110. PlainBlock[][] result = new PlainBlock[path.length][];
  111. for (int i = 0; i < path.length; ++i)
  112. result[i] = randomBucket(path[i].length);
  113. return result;
  114. }
  115. }