OramParty.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2014 by Xiao Shaun Wang <wangxiao@cs.umd.edu>
  2. package com.oblivm.backend.oram;
  3. import java.util.Arrays;
  4. import com.oblivm.backend.flexsc.CompEnv;
  5. import com.oblivm.backend.flexsc.Mode;
  6. import com.oblivm.backend.flexsc.Party;
  7. import com.oblivm.backend.util.Utils;
  8. public abstract class OramParty<T> {
  9. public int N;
  10. int dataSize;
  11. public int logN;
  12. public int lengthOfIden;
  13. public int lengthOfPos;
  14. public int lengthOfData;
  15. public CompEnv<T> env;
  16. public Party p;
  17. public Mode mode;
  18. public BucketLib<T> lib;
  19. boolean[] dummyArray;
  20. void setParameters(CompEnv<T> env, int N, int dataSize) {
  21. this.env = env;
  22. this.dataSize = dataSize;
  23. long a = 1;
  24. logN = 1;
  25. while (a < N) {
  26. a *= 2;
  27. ++logN;
  28. }
  29. --logN;
  30. this.N = 1 << logN;
  31. lengthOfData = dataSize;
  32. lengthOfIden = logN;
  33. }
  34. public OramParty(CompEnv<T> env, int N, int dataSize) {
  35. setParameters(env, N, dataSize);
  36. lengthOfPos = logN - 1;
  37. p = env.party;
  38. mode = env.mode;
  39. init();
  40. }
  41. public OramParty(CompEnv<T> env, int N, int dataSize, int lengthOfPos) {
  42. setParameters(env, N, dataSize);
  43. this.lengthOfPos = lengthOfPos;
  44. p = env.party;
  45. mode = env.mode;
  46. init();
  47. }
  48. public void init() {
  49. dummyArray = new boolean[lengthOfIden + lengthOfPos + lengthOfData + 1];
  50. for (int i = 0; i < dummyArray.length; ++i)
  51. dummyArray[i] = false;
  52. lib = new BucketLib<T>(lengthOfIden, lengthOfPos, lengthOfData, env);
  53. boolean[] data = new boolean[lengthOfData];
  54. for (int i = 0; i < lengthOfData; ++i)
  55. data[i] = true;
  56. pb_for_count_mode = new PlainBlock(0, 0, data, false);
  57. }
  58. public Block<T>[] prepareBlocks(PlainBlock[] clientBlock, PlainBlock[] serverBlock) {
  59. Block<T>[] s = inputBucketOfServer(serverBlock);
  60. Block<T>[] c = inputBucketOfClient(clientBlock);
  61. return lib.xor(s, c);
  62. }
  63. public Block<T> prepareBlock(PlainBlock clientBlock, PlainBlock serverBlock) {
  64. Block<T> s = inputBlockOfServer(serverBlock);
  65. Block<T> c = inputBlockOfClient(clientBlock);
  66. return lib.xor(s, c);
  67. }
  68. public PlainBlock preparePlainBlock(Block<T> blocks, Block<T> randomBlock) {
  69. PlainBlock result = outputBlock(lib.xor(blocks, randomBlock));
  70. return result;
  71. }
  72. public PlainBlock[] preparePlainBlocks(Block<T>[] blocks, Block<T>[] randomBlock) {
  73. PlainBlock[] result = outputBucket(lib.xor(blocks, randomBlock));
  74. return result;
  75. }
  76. public Block<T> inputBlockOfServer(PlainBlock b) {
  77. T[] TArray = env.inputOfBob(b.toBooleanArray(lengthOfIden, lengthOfPos));
  78. return new Block<T>(TArray, lengthOfIden, lengthOfPos, lengthOfData);
  79. }
  80. public Block<T> inputBlockOfClient(PlainBlock b) {
  81. T[] TArray = env.inputOfAlice(b.toBooleanArray(lengthOfIden, lengthOfPos));
  82. return new Block<T>(TArray, lengthOfIden, lengthOfPos, lengthOfData);
  83. }
  84. public Block<T>[] toBlocks(T[] Tarray, int lengthOfIden, int lengthOfPos, int lengthOfData, int capacity) {
  85. int blockSize = lengthOfIden + lengthOfPos + lengthOfData + 1;
  86. Block<T>[] result = lib.newBlockArray(capacity);
  87. for (int i = 0; i < capacity; ++i) {
  88. result[i] = new Block<T>(Arrays.copyOfRange(Tarray, i * blockSize, (i + 1) * blockSize), lengthOfIden,
  89. lengthOfPos, lengthOfData);
  90. }
  91. return result;
  92. }
  93. public Block<T>[] inputBucketOfServer(PlainBlock[] b) {
  94. T[] TArray = env.inputOfBob(PlainBlock.toBooleanArray(b, lengthOfIden, lengthOfPos));
  95. return toBlocks(TArray, lengthOfIden, lengthOfPos, lengthOfData, b.length);// new
  96. // Block<T>(TArray,
  97. // lengthOfIden,lengthOfPos,lengthOfData);
  98. }
  99. public Block<T>[] inputBucketOfClient(PlainBlock[] b) {
  100. T[] TArray = env.inputOfAlice(PlainBlock.toBooleanArray(b, lengthOfIden, lengthOfPos));
  101. env.flush();
  102. return toBlocks(TArray, lengthOfIden, lengthOfPos, lengthOfData, b.length);
  103. }
  104. public PlainBlock outputBlock(Block<T> b) {
  105. boolean[] iden = env.outputToAlice(b.iden);
  106. boolean[] pos = env.outputToAlice(b.pos);
  107. boolean[] data = env.outputToAlice(b.data);
  108. boolean isDummy = env.outputToAlice(b.isDummy);
  109. return new PlainBlock(Utils.toLong(iden), Utils.toLong(pos), data, isDummy);
  110. }
  111. public PlainBlock[] outputBucket(Block<T>[] b) {
  112. PlainBlock[] result = new PlainBlock[b.length];
  113. for (int i = 0; i < b.length; ++i)
  114. result[i] = outputBlock(b[i]);
  115. return result;
  116. }
  117. public PlainBlock[][] outputBuckets(Block<T>[][] b) {
  118. PlainBlock[][] result = new PlainBlock[b.length][];
  119. for (int i = 0; i < b.length; ++i)
  120. result[i] = outputBucket(b[i]);
  121. env.flush();
  122. return result;
  123. }
  124. PlainBlock pb_for_count_mode;
  125. public PlainBlock getDummyBlock(boolean b) {
  126. if (mode == Mode.COUNT)
  127. return pb_for_count_mode;
  128. boolean[] data = new boolean[lengthOfData];
  129. for (int i = 0; i < lengthOfData; ++i)
  130. data[i] = true;
  131. return new PlainBlock(0, 0, data, b);
  132. }
  133. PlainBlock r = getDummyBlock(true);
  134. public PlainBlock randomBlock() {
  135. if (mode == Mode.COUNT)
  136. return pb_for_count_mode;
  137. // PlainBlock result = getDummyBlock(true);
  138. // for (int i = 0; i < lengthOfIden; ++i)
  139. // result.iden[i] = CompEnv.rnd.nextBoolean();
  140. // for (int i = 0; i < lengthOfPos; ++i)
  141. // result.pos[i] = CompEnv.rnd.nextBoolean();
  142. boolean[] data = new boolean[lengthOfData];
  143. for (int i = 0; i < lengthOfData; ++i)
  144. data[i] = CompEnv.rnd.nextBoolean();
  145. boolean isDummy = CompEnv.rnd.nextBoolean();
  146. return new PlainBlock(CompEnv.rnd.nextLong(), CompEnv.rnd.nextLong(), data, isDummy);
  147. }
  148. public PlainBlock[] randomBucket(int length) {
  149. PlainBlock[] result = new PlainBlock[length];
  150. for (int i = 0; i < length; ++i)
  151. result[i] = randomBlock();
  152. return result;
  153. }
  154. }