CircuitOramLib.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Party;
  5. import com.oblivm.backend.util.Utils;
  6. public class CircuitOramLib<T> extends BucketLib<T> {
  7. int logN;
  8. int loglogN;
  9. int capacity;
  10. public CircuitOramLib(int lengthOfIden, int lengthOfPos, int lengthOfData, int logN, int capacity, CompEnv<T> e) {
  11. super(lengthOfIden, lengthOfPos, lengthOfData, e);
  12. this.logN = logN;
  13. this.capacity = capacity;
  14. }
  15. public T[] deepestLevellocal(T[] pos, T[] path) {
  16. T[] xored = xor(pos, path);
  17. return leadingZeros(xored);
  18. }
  19. public T[][] deepestBlockShort(Block<T>[] bucket, T[] pathSignal) {
  20. T[][] deepest = env.newTArray(bucket.length, 0);// ;new
  21. // T[nodeCapacity][];
  22. for (int j = 0; j < bucket.length; ++j) {
  23. deepest[j] = deepestLevellocal(bucket[j].pos, pathSignal);
  24. }
  25. T[] maxIden = bucket[0].iden;
  26. T[] maxdepth = deepest[0];
  27. T isDummy = bucket[0].isDummy;
  28. for (int j = 1; j < bucket.length; ++j) {
  29. T greater = geq(deepest[j], maxdepth);
  30. greater = and(greater, not(bucket[j].isDummy));
  31. maxIden = mux(maxIden, bucket[j].iden, greater);
  32. maxdepth = mux(maxdepth, deepest[j], greater);
  33. isDummy = mux(isDummy, bucket[j].isDummy, greater);
  34. }
  35. T[][] result = env.newTArray(3, 0);
  36. result[0] = maxIden;
  37. result[1] = maxdepth;
  38. result[2] = env.newTArray(1);
  39. result[2][0] = isDummy;
  40. return result;
  41. }
  42. public void flush(Block<T>[][] scPath, boolean[] path, Block<T>[] scQueue) {
  43. // make path to signal
  44. T[] pathSignal = env.newTArray(path.length);
  45. for (int i = 0; i < path.length; ++i)
  46. pathSignal[i] = path[i] ? SIGNAL_ONE : SIGNAL_ZERO;
  47. // PrepareDeepest(path)
  48. T[][] stashDeepest = deepestBlockShort(scQueue, pathSignal);
  49. loglogN = stashDeepest[1].length;
  50. T[][] deepest = env.newTArray(scPath.length + 1, 0);
  51. T[][] deepestIden = env.newTArray(scPath.length + 1, 0);
  52. for (int i = 0; i < deepest.length; ++i) {
  53. deepest[i] = zeros(loglogN);
  54. deepestIden[i] = zeros(lengthOfIden);
  55. }
  56. T[] deepestBot = ones(scPath.length + 1);
  57. T[] cur = zeros(loglogN);
  58. T curBot = SIGNAL_ONE;
  59. // T emptyStash = isEmpty(scQueue);
  60. T[] curv = stashDeepest[1];// mux(, zeros(loglogN), emptyStash);
  61. deepestIden[0] = stashDeepest[0];
  62. curBot = stashDeepest[2][0];
  63. for (int i = 0; i < logN; ++i) {
  64. T[] iSignal = toSignals(i + 1, loglogN);
  65. T curvGEQI = geq(curv, iSignal);
  66. deepest[i + 1] = mux(deepest[i + 1], cur, curvGEQI);
  67. deepestBot[i + 1] = mux(deepestBot[i + 1], curBot, curvGEQI);
  68. T[][] pathiDeepest = deepestBlockShort(scPath[i], pathSignal);
  69. deepestIden[i + 1] = pathiDeepest[0];
  70. T lGcurv = not(leq(pathiDeepest[1], curv));
  71. lGcurv = and(lGcurv, not(pathiDeepest[2][0]));
  72. lGcurv = or(lGcurv, curBot);
  73. curv = mux(curv, pathiDeepest[1], lGcurv);
  74. cur = mux(cur, iSignal, lGcurv);
  75. curBot = mux(curBot, SIGNAL_ZERO, lGcurv);
  76. }
  77. // prepareTarget(path)
  78. T[] c = toSignals(0, loglogN);
  79. T cBot = SIGNAL_ONE;
  80. T[] l = toSignals(0, loglogN);
  81. T lBot = SIGNAL_ONE;
  82. T[][] target = env.newTArray(scPath.length + 1, 0);
  83. for (int i = 0; i < target.length; ++i)
  84. target[i] = zeros(loglogN);
  85. T[] targetBot = ones(scPath.length + 1);
  86. for (int i = logN; i >= 0; --i) {
  87. // prepare conditions
  88. T[] iSignal = toSignals(i, curv.length);
  89. T iEQl;
  90. iEQl = and(not(lBot), eq(iSignal, l));
  91. T isFull;
  92. if (i > 0) {
  93. isFull = isFull(scPath[i - 1]);
  94. } else {
  95. isFull = isFull(scQueue);
  96. }
  97. T hasSlot = or(and(cBot, not(isFull)), not(targetBot[i]));
  98. T canPush = not(deepestBot[i]);
  99. // begin assignment
  100. target[i] = mux(target[i], c, iEQl);
  101. targetBot[i] = mux(targetBot[i], cBot, iEQl);
  102. if (i > 0) {// the last one can be skipped
  103. cBot = mux(cBot, SIGNAL_ONE, iEQl);
  104. lBot = mux(lBot, SIGNAL_ONE, iEQl);
  105. T secondIf = and(hasSlot, canPush);
  106. l = mux(l, deepest[i], secondIf);
  107. lBot = mux(lBot, deepestBot[i], secondIf);
  108. c = mux(c, iSignal, secondIf);
  109. cBot = mux(cBot, SIGNAL_ZERO, secondIf);
  110. }
  111. }
  112. // evictionFast(path)
  113. Block<T> hold = dummyBlock;
  114. lBot = SIGNAL_ONE;
  115. // do it for stash first
  116. {
  117. T toRemove = not(targetBot[0]);
  118. hold = conditionalReadAndRemove(scQueue, deepestIden[0], toRemove);
  119. l = mux(l, target[0], toRemove);
  120. lBot = mux(lBot, targetBot[0], toRemove);
  121. }
  122. for (int i = 0; i < logN; ++i) {
  123. T[] iSignal = toSignals(i + 1, curv.length);
  124. T iEQl = eq(iSignal, l);
  125. iEQl = and(iEQl, not(lBot));
  126. T firstIf = and(not(hold.isDummy), iEQl);
  127. Block<T> holdTmp = copy(hold);
  128. hold.isDummy = mux(hold.isDummy, SIGNAL_ONE, firstIf);
  129. lBot = mux(lBot, SIGNAL_ONE, firstIf);
  130. T notBot = not(targetBot[i + 1]);
  131. if (i != logN - 1) {
  132. Block<T> tmp = conditionalReadAndRemove(scPath[i], deepestIden[i + 1], notBot);
  133. hold = mux(hold, tmp, notBot);
  134. }
  135. l = mux(l, target[i + 1], notBot);
  136. lBot = mux(lBot, targetBot[i + 1], notBot);
  137. conditionalAdd(scPath[i], holdTmp, firstIf);
  138. }
  139. }
  140. public void print(T[][] data, T[] bot) {
  141. if (env.getParty() == Party.Bob || env instanceof com.oblivm.backend.gc.GCCompEnv)
  142. return;
  143. for (int i = 0; i < data.length; ++i) {
  144. if ((Boolean) bot[i]) {
  145. System.out.print("d ");
  146. } else
  147. System.out.print(Utils.toInt(Utils.tobooleanArray((Boolean[]) data[i])) + " ");
  148. }
  149. System.out.print("\n");
  150. }
  151. public void print(T[] data, T bot) {
  152. if (env.getParty() == Party.Bob || env instanceof com.oblivm.backend.gc.GCCompEnv)
  153. return;
  154. if ((Boolean) bot) {
  155. System.out.print("d ");
  156. } else
  157. System.out.print(Utils.toInt(Utils.tobooleanArray((Boolean[]) data)) + " ");
  158. System.out.print("\n");
  159. }
  160. public void print(String s, T[] data, T bot) {
  161. if (env.getParty() == Party.Bob)
  162. return;
  163. System.out.print(s);
  164. if ((Boolean) bot) {
  165. System.out.print("d ");
  166. } else
  167. System.out.print(Utils.toInt(Utils.tobooleanArray((Boolean[]) data)) + " ");
  168. System.out.print("\n");
  169. }
  170. }