BucketLib.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (C) 2014 by Xiao Shaun Wang <wangxiao@cs.umd.edu>
  2. package com.oblivm.backend.oram;
  3. import com.oblivm.backend.circuits.BitonicSortLib;
  4. import com.oblivm.backend.flexsc.CompEnv;
  5. public class BucketLib<T> extends BitonicSortLib<T> {
  6. public Block<T> dummyBlock;
  7. protected int lengthOfIden;
  8. protected int lengthOfPos;
  9. protected int lengthOfData;
  10. public BucketLib(int lengthOfIden, int lengthOfPos, int lengthOfData, CompEnv<T> e) {
  11. super(e);
  12. this.lengthOfData = lengthOfData;
  13. this.lengthOfIden = lengthOfIden;
  14. this.lengthOfPos = lengthOfPos;
  15. dummyBlock = new Block<T>(zeros(lengthOfIden), zeros(lengthOfPos), zeros(lengthOfData), SIGNAL_ONE);
  16. }
  17. public T isFull(Block<T>[] bucket) {
  18. T res = SIGNAL_ONE;
  19. for (int i = 0; i < bucket.length; ++i) {
  20. res = and(res, not(bucket[i].isDummy));
  21. }
  22. return res;
  23. }
  24. public T isEmpty(Block<T>[] bucket) {
  25. T res = SIGNAL_ONE;
  26. for (int i = 0; i < bucket.length; ++i) {
  27. res = and(res, bucket[i].isDummy);
  28. }
  29. return res;
  30. }
  31. public Block<T> conditionalReadAndRemove(Block<T>[] bucket, T[] iden, T condition) {
  32. Block<T> result = dummyBlock;
  33. for (int i = 0; i < bucket.length; ++i) {
  34. T match = eq(iden, bucket[i].iden);
  35. match = and(match, not(bucket[i].isDummy));
  36. match = and(match, condition);
  37. result = mux(result, bucket[i], match);
  38. bucket[i].isDummy = mux(bucket[i].isDummy, SIGNAL_ONE, match);
  39. }
  40. return result;
  41. }
  42. public Block<T> readAndRemove(Block<T>[] bucket, T[] iden) {
  43. Block<T> result = dummyBlock;
  44. for (int i = 0; i < bucket.length; ++i) {
  45. T match = eq(iden, bucket[i].iden);
  46. match = and(match, not(bucket[i].isDummy));
  47. result = mux(result, bucket[i], match);
  48. bucket[i].isDummy = mux(bucket[i].isDummy, SIGNAL_ONE, match);
  49. }
  50. return result;
  51. }
  52. public Block<T> conditionalReadAndRemove(Block<T>[][] blocks, T[] iden, T condition) {
  53. Block<T>[] res = newBlockArray(blocks.length);
  54. for (int i = 0; i < blocks.length; ++i)
  55. res[i] = conditionalReadAndRemove(blocks[i], iden, condition);
  56. return conditionalReadAndRemove(res, iden, condition);
  57. }
  58. public Block<T> readAndRemove(Block<T>[][] blocks, T[] iden) {
  59. Block<T>[] res = newBlockArray(blocks.length);
  60. for (int i = 0; i < blocks.length; ++i)
  61. res[i] = readAndRemove(blocks[i], iden);
  62. return readAndRemove(res, iden);
  63. }
  64. public void conditionalAdd(Block<T>[] bucket, Block<T> newBlock, T condition) {
  65. T added = not(condition);
  66. for (int i = 0; i < bucket.length; ++i) {
  67. T match = and(not(bucket[i].isDummy), eq(newBlock.iden, bucket[i].iden));
  68. added = or(match, added);
  69. }
  70. for (int i = 0; i < bucket.length; ++i) {
  71. T match = bucket[i].isDummy;
  72. T shouldAdd = and(not(added), match);
  73. added = or(added, shouldAdd);
  74. bucket[i] = mux(bucket[i], newBlock, shouldAdd);
  75. }
  76. }
  77. public void add(Block<T>[] bucket, Block<T> newBlock) {
  78. T added = SIGNAL_ZERO;
  79. for (int i = 0; i < bucket.length; ++i) {
  80. T match = and(not(bucket[i].isDummy), eq(newBlock.iden, bucket[i].iden));
  81. added = or(match, added);
  82. }
  83. for (int i = 0; i < bucket.length; ++i) {
  84. T match = bucket[i].isDummy;
  85. T shouldAdd = and(not(added), match);
  86. added = or(added, shouldAdd);
  87. bucket[i] = mux(bucket[i], newBlock, shouldAdd);
  88. }
  89. }
  90. public Block<T> pop(Block<T>[] bucket) {
  91. Block<T> result = dummyBlock;
  92. T poped = SIGNAL_ZERO;// condition=T => shouldpop => set to poped;
  93. for (int i = 0; i < bucket.length; ++i) {
  94. T notDummy = not(bucket[i].isDummy);
  95. T shouldPop = and(not(poped), notDummy);
  96. poped = or(poped, shouldPop);
  97. result = mux(result, bucket[i], shouldPop);
  98. bucket[i].isDummy = mux(bucket[i].isDummy, SIGNAL_ONE, shouldPop);
  99. }
  100. return result;
  101. }
  102. public Block<T> conditionalPop(Block<T>[] bucket, T condition) {
  103. Block<T> result = dummyBlock;
  104. T poped = not(condition);// condition=T => shouldpop => set to poped;
  105. for (int i = 0; i < bucket.length; ++i) {
  106. T notDummy = not(bucket[i].isDummy);
  107. T shouldPop = and(not(poped), notDummy);
  108. poped = or(poped, shouldPop);
  109. result = mux(result, bucket[i], shouldPop);
  110. bucket[i].isDummy = mux(bucket[i].isDummy, SIGNAL_ONE, shouldPop);
  111. }
  112. return result;
  113. }
  114. public Block<T> mux(Block<T> a, Block<T> b, T choose) {
  115. T[] iden = mux(a.iden, b.iden, choose);
  116. T[] pos = mux(a.pos, b.pos, choose);
  117. T[] data = mux(a.data, b.data, choose);
  118. T isDummy = mux(a.isDummy, b.isDummy, choose);
  119. return new Block<T>(iden, pos, data, isDummy);
  120. }
  121. public Block<T> xor(Block<T> a, Block<T> b) {
  122. T[] iden = xor(a.iden, b.iden);
  123. T[] pos = xor(a.pos, b.pos);
  124. T[] data = xor(a.data, b.data);
  125. T isDummy = xor(a.isDummy, b.isDummy);
  126. return new Block<T>(iden, pos, data, isDummy);
  127. }
  128. public Block<T>[] xor(Block<T>[] a, Block<T>[] b) {
  129. assert (a.length == b.length) : "xor Block<T>s error";
  130. Block<T>[] result = newBlockArray(b.length);
  131. for (int i = 0; i < a.length; ++i)
  132. result[i] = xor(a[i], b[i]);
  133. return result;
  134. }
  135. public Block<T>[][] xor(Block<T>[][] a, Block<T>[][] b) {
  136. assert (a.length == b.length) : "xor Block<T>s error";
  137. Block<T>[][] result = newBlockMatrix(a.length);
  138. for (int i = 0; i < a.length; ++i) {
  139. result[i] = newBlockArray(a[i].length);
  140. for (int j = 0; j < a[i].length; ++j)
  141. result[i][j] = xor(a[i][j], b[i][j]);
  142. }
  143. return result;
  144. }
  145. public Block<T> copy(Block<T> b) {
  146. return new Block<T>(b.iden, b.pos, b.data, b.isDummy);
  147. }
  148. @SuppressWarnings("unchecked")
  149. public Block<T>[] newBlockArray(int len) {
  150. return new Block[len];
  151. }
  152. @SuppressWarnings("unchecked")
  153. public Block<T>[][] newBlockMatrix(int x, int y) {
  154. return new Block[x][y];
  155. }
  156. @SuppressWarnings("unchecked")
  157. public Block<T>[][] newBlockMatrix(int x) {
  158. return new Block[x][];
  159. }
  160. }