Eviction.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package protocols;
  2. import java.util.Arrays;
  3. import com.oblivm.backend.flexsc.CompEnv;
  4. import com.oblivm.backend.gc.GCSignal;
  5. import com.oblivm.backend.gc.regular.GCEva;
  6. import com.oblivm.backend.gc.regular.GCGen;
  7. import com.oblivm.backend.network.Network;
  8. import communication.Communication;
  9. import crypto.Crypto;
  10. import gc.GCRoute;
  11. import gc.GCUtil;
  12. import oram.Bucket;
  13. import oram.Forest;
  14. import oram.Metadata;
  15. import oram.Tree;
  16. import oram.Tuple;
  17. import protocols.struct.Party;
  18. import protocols.struct.PreData;
  19. import util.M;
  20. import util.Util;
  21. // TODO: set bucket on path
  22. public class Eviction extends Protocol {
  23. public Eviction(Communication con1, Communication con2) {
  24. super(con1, con2);
  25. }
  26. private int[] prepareEviction(int[] target, int[] ti, int W) {
  27. int d = ti.length;
  28. int[] evict = new int[W * d];
  29. for (int r = 0; r < d; r++) {
  30. int tupleIndex = r * W + ti[r];
  31. for (int c = 0; c < W; c++) {
  32. int currIndex = r * W + c;
  33. if (currIndex == tupleIndex) {
  34. int targetIndex = target[r] * W + ti[target[r]];
  35. evict[targetIndex] = currIndex;
  36. } else
  37. evict[currIndex] = currIndex;
  38. }
  39. }
  40. return evict;
  41. }
  42. public void runE(PreData predata, boolean firstTree, int[] tupleParam, byte[] Li, Tuple[] originalPath, Tree OTi) {
  43. if (firstTree) {
  44. // OTi.setBucketsOnPath(new BigInteger(1, Li).longValue(), new Bucket[] { new
  45. // Bucket(originalPath) });
  46. return;
  47. }
  48. timer.start(M.offline_comp);
  49. // GC
  50. int d = OTi.getD();
  51. int sw = OTi.getStashSize();
  52. int w = OTi.getW();
  53. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  54. predata.evict_LiKeyPairs = GCUtil.genKeyPairs(d - 1);
  55. GCSignal[] LiZeroKeys = GCUtil.getZeroKeys(predata.evict_LiKeyPairs);
  56. predata.evict_E_feKeyPairs = new GCSignal[d][][];
  57. predata.evict_C_feKeyPairs = new GCSignal[d][][];
  58. GCSignal[][] E_feZeroKeys = new GCSignal[d][];
  59. GCSignal[][] C_feZeroKeys = new GCSignal[d][];
  60. predata.evict_E_labelKeyPairs = new GCSignal[d][w][][];
  61. predata.evict_C_labelKeyPairs = new GCSignal[d][w][][];
  62. GCSignal[][][] E_labelZeroKeys = new GCSignal[d][w][];
  63. GCSignal[][][] C_labelZeroKeys = new GCSignal[d][w][];
  64. predata.evict_deltaKeyPairs = new GCSignal[d][][];
  65. GCSignal[][] deltaZeroKeys = new GCSignal[d][];
  66. for (int i = 0; i < d; i++) {
  67. predata.evict_E_feKeyPairs[i] = GCUtil.genKeyPairs(w);
  68. predata.evict_C_feKeyPairs[i] = GCUtil.genKeyPairs(w);
  69. E_feZeroKeys[i] = GCUtil.getZeroKeys(predata.evict_E_feKeyPairs[i]);
  70. C_feZeroKeys[i] = GCUtil.getZeroKeys(predata.evict_C_feKeyPairs[i]);
  71. predata.evict_deltaKeyPairs[i] = GCUtil.genKeyPairs(logW);
  72. deltaZeroKeys[i] = GCUtil.getZeroKeys(predata.evict_deltaKeyPairs[i]);
  73. for (int j = 0; j < w; j++) {
  74. predata.evict_E_labelKeyPairs[i][j] = GCUtil.genKeyPairs(d - 1);
  75. predata.evict_C_labelKeyPairs[i][j] = GCUtil.genKeyPairs(d - 1);
  76. E_labelZeroKeys[i][j] = GCUtil.getZeroKeys(predata.evict_E_labelKeyPairs[i][j]);
  77. C_labelZeroKeys[i][j] = GCUtil.getZeroKeys(predata.evict_C_labelKeyPairs[i][j]);
  78. }
  79. }
  80. Network channel = new Network(null, con1);
  81. CompEnv<GCSignal> gen = new GCGen(channel, timer, offline_band, M.offline_write);
  82. GCSignal[][][] outZeroKeys = new GCRoute<GCSignal>(gen, d, w).routing(LiZeroKeys, E_feZeroKeys, C_feZeroKeys,
  83. E_labelZeroKeys, C_labelZeroKeys, deltaZeroKeys);
  84. ((GCGen) gen).sendLastSetGTT();
  85. predata.evict_tiOutKeyHashes = new byte[d][][];
  86. predata.evict_targetOutKeyPairs = new GCSignal[d][][];
  87. for (int i = 0; i < d; i++) {
  88. predata.evict_tiOutKeyHashes[i] = GCUtil.genOutKeyHashes(outZeroKeys[1][i]);
  89. predata.evict_targetOutKeyPairs[i] = GCUtil.recoverOutKeyPairs(outZeroKeys[0][i]);
  90. }
  91. timer.start(M.offline_write);
  92. con2.write(predata.evict_C_feKeyPairs);
  93. con2.write(predata.evict_C_labelKeyPairs);
  94. con1.write(predata.evict_tiOutKeyHashes);
  95. con1.write(predata.evict_targetOutKeyPairs);
  96. timer.stop(M.offline_write);
  97. // Permutation
  98. predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
  99. predata.evict_delta = new byte[d][];
  100. predata.evict_rho = new byte[d][];
  101. predata.evict_delta_p = new int[d][];
  102. predata.evict_rho_p = new int[d][];
  103. for (int i = 0; i < d; i++) {
  104. predata.evict_delta[i] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
  105. predata.evict_rho[i] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
  106. predata.evict_delta_p[i] = Util.getXorPermutation(predata.evict_delta[i], logW);
  107. predata.evict_rho_p[i] = Util.getXorPermutation(predata.evict_rho[i], logW);
  108. }
  109. timer.start(M.offline_write);
  110. con2.write(predata.evict_pi);
  111. con2.write(predata.evict_delta);
  112. con2.write(predata.evict_rho);
  113. con2.write(predata.evict_delta_p);
  114. con2.write(predata.evict_rho_p);
  115. timer.stop(M.offline_write);
  116. timer.stop(M.offline_comp);
  117. ///////////////////////////////////////////////////////////////////
  118. timer.start(M.online_comp);
  119. Tuple[] pathTuples = new Tuple[d * w];
  120. System.arraycopy(originalPath, 0, pathTuples, 0, w);
  121. System.arraycopy(originalPath, sw, pathTuples, w, (d - 1) * w);
  122. Bucket[] pathBuckets = Bucket.tuplesToBuckets(pathTuples, d, w, w);
  123. GCSignal[] LiInputKeys = GCUtil.revSelectKeys(predata.evict_LiKeyPairs, Li);
  124. GCSignal[][] E_feInputKeys = new GCSignal[d][];
  125. GCSignal[][][] E_labelInputKeys = new GCSignal[d][][];
  126. GCSignal[][] deltaInputKeys = new GCSignal[d][];
  127. for (int i = 0; i < d; i++) {
  128. E_feInputKeys[i] = GCUtil.selectFeKeys(predata.evict_E_feKeyPairs[i], pathBuckets[i].getTuples());
  129. E_labelInputKeys[i] = GCUtil.selectLabelKeys(predata.evict_E_labelKeyPairs[i], pathBuckets[i].getTuples());
  130. deltaInputKeys[i] = GCUtil.revSelectKeys(predata.evict_deltaKeyPairs[i], predata.evict_delta[i]);
  131. }
  132. timer.start(M.online_write);
  133. con1.write(online_band, LiInputKeys);
  134. con1.write(online_band, E_feInputKeys);
  135. con1.write(online_band, E_labelInputKeys);
  136. con1.write(online_band, deltaInputKeys);
  137. timer.stop(M.online_write);
  138. PermuteTarget permutetarget = new PermuteTarget(con1, con2);
  139. permutetarget.runE(d, predata.evict_pi, predata.evict_targetOutKeyPairs);
  140. PermuteIndex permuteindex = new PermuteIndex(con1, con2);
  141. permuteindex.runE(w, predata.evict_pi);
  142. int W = (int) Math.pow(2, logW);
  143. for (int i = 0; i < d; i++) {
  144. pathBuckets[i].expand(W);
  145. pathBuckets[i].permute(predata.evict_delta_p[i]);
  146. }
  147. pathBuckets = Util.permute(pathBuckets, predata.evict_pi);
  148. for (int i = 0; i < d; i++) {
  149. pathBuckets[i].permute(predata.evict_rho_p[i]);
  150. }
  151. pathTuples = Bucket.bucketsToTuples(pathBuckets);
  152. SSXOT ssxot = new SSXOT(con1, con2);
  153. pathTuples = ssxot.runE(pathTuples, tupleParam);
  154. pathBuckets = Bucket.tuplesToBuckets(pathTuples, d, W, W);
  155. for (int i = 0; i < d; i++) {
  156. int[] rho_ivs = Util.inversePermutation(predata.evict_rho_p[i]);
  157. pathBuckets[i].permute(rho_ivs);
  158. }
  159. int[] pi_ivs = Util.inversePermutation(predata.evict_pi);
  160. pathBuckets = Util.permute(pathBuckets, pi_ivs);
  161. for (int i = 0; i < d; i++) {
  162. int[] delta_ivs = Util.inversePermutation(predata.evict_delta_p[i]);
  163. pathBuckets[i].permute(delta_ivs);
  164. pathBuckets[i].shrink(w);
  165. }
  166. pathBuckets[0].expand(Arrays.copyOfRange(originalPath, w, sw));
  167. // OTi.setBucketsOnPath(new BigInteger(1, Li).longValue(), pathBuckets);
  168. timer.stop(M.online_comp);
  169. }
  170. public void runD(PreData predata, boolean firstTree, int[] tupleParam, byte[] Li, Tree OTi) {
  171. if (firstTree) {
  172. timer.start(M.online_read);
  173. // Tuple[] originalPath = con2.readTupleArrayAndDec();
  174. timer.stop(M.online_read);
  175. // OTi.setBucketsOnPath(new BigInteger(1, Li).longValue(), new Bucket[] { new
  176. // Bucket(originalPath) });
  177. return;
  178. }
  179. timer.start(M.offline_comp);
  180. // GC
  181. int d = OTi.getD();
  182. int w = OTi.getW();
  183. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  184. GCSignal[] LiZeroKeys = GCUtil.genEmptyKeys(d - 1);
  185. GCSignal[][] E_feZeroKeys = new GCSignal[d][];
  186. GCSignal[][] C_feZeroKeys = new GCSignal[d][];
  187. GCSignal[][][] E_labelZeroKeys = new GCSignal[d][w][];
  188. GCSignal[][][] C_labelZeroKeys = new GCSignal[d][w][];
  189. GCSignal[][] deltaZeroKeys = new GCSignal[d][];
  190. for (int i = 0; i < d; i++) {
  191. E_feZeroKeys[i] = GCUtil.genEmptyKeys(w);
  192. C_feZeroKeys[i] = GCUtil.genEmptyKeys(w);
  193. deltaZeroKeys[i] = GCUtil.genEmptyKeys(logW);
  194. for (int j = 0; j < w; j++) {
  195. E_labelZeroKeys[i][j] = GCUtil.genEmptyKeys(d - 1);
  196. C_labelZeroKeys[i][j] = GCUtil.genEmptyKeys(d - 1);
  197. }
  198. }
  199. Network channel = new Network(con1, null);
  200. CompEnv<GCSignal> eva = new GCEva(channel, timer, M.offline_read);
  201. predata.evict_gcroute = new GCRoute<GCSignal>(eva, d, w);
  202. predata.evict_gcroute.routing(LiZeroKeys, E_feZeroKeys, C_feZeroKeys, E_labelZeroKeys, C_labelZeroKeys,
  203. deltaZeroKeys);
  204. ((GCEva) eva).receiveLastSetGTT();
  205. eva.setEvaluate();
  206. timer.start(M.offline_read);
  207. predata.evict_tiOutKeyHashes = con1.readTripleByteArray();
  208. predata.evict_targetOutKeyPairs = con1.readTripleGCSignalArray();
  209. timer.stop(M.offline_read);
  210. timer.stop(M.offline_comp);
  211. //////////////////////////////////////////////////////////////////////
  212. timer.start(M.online_comp);
  213. timer.start(M.online_read);
  214. GCSignal[] LiInputKeys = con1.readGCSignalArrayAndDec();
  215. GCSignal[][] E_feInputKeys = con1.readDoubleGCSignalArrayAndDec();
  216. GCSignal[][][] E_labelInputKeys = con1.readTripleGCSignalArrayAndDec();
  217. GCSignal[][] deltaInputKeys = con1.readDoubleGCSignalArrayAndDec();
  218. GCSignal[][] C_feInputKeys = con2.readDoubleGCSignalArrayAndDec();
  219. GCSignal[][][] C_labelInputKeys = con2.readTripleGCSignalArrayAndDec();
  220. timer.stop(M.online_read);
  221. GCSignal[][][] outKeys = predata.evict_gcroute.routing(LiInputKeys, E_feInputKeys, C_feInputKeys,
  222. E_labelInputKeys, C_labelInputKeys, deltaInputKeys);
  223. byte[][] ti_p = new byte[deltaInputKeys.length][];
  224. for (int i = 0; i < ti_p.length; i++) {
  225. ti_p[i] = Util.padArray(GCUtil.evaOutKeys(outKeys[1][i], predata.evict_tiOutKeyHashes[i]).toByteArray(),
  226. (logW + 7) / 8);
  227. }
  228. PermuteTarget permutetarget = new PermuteTarget(con1, con2);
  229. int[] target_pp = permutetarget.runD(firstTree, outKeys[0]);
  230. PermuteIndex permuteindex = new PermuteIndex(con1, con2);
  231. int[] ti_pp = permuteindex.runD(firstTree, ti_p, w);
  232. int W = (int) Math.pow(2, logW);
  233. int[] evict = prepareEviction(target_pp, ti_pp, W);
  234. SSXOT ssxot = new SSXOT(con1, con2);
  235. ssxot.runD(evict.length, evict.length, tupleParam, evict);
  236. timer.start(M.online_read);
  237. // Bucket[] pathBuckets = con2.readBucketArrayAndDec();
  238. timer.stop(M.online_read);
  239. // OTi.setBucketsOnPath(new BigInteger(1, Li).longValue(), pathBuckets);
  240. timer.stop(M.online_comp);
  241. }
  242. public void runC(PreData predata, boolean firstTree, int[] tupleParam, Tuple[] originalPath, int d, int sw, int w) {
  243. if (firstTree) {
  244. timer.start(M.online_write);
  245. // con2.write(online_band, originalPath);
  246. timer.stop(M.online_write);
  247. return;
  248. }
  249. timer.start(M.offline_comp);
  250. // GC
  251. timer.start(M.offline_read);
  252. predata.evict_C_feKeyPairs = con1.readTripleGCSignalArray();
  253. predata.evict_C_labelKeyPairs = con1.readQuadGCSignalArray();
  254. // Permutation
  255. predata.evict_pi = con1.readIntArray();
  256. predata.evict_delta = con1.readDoubleByteArray();
  257. predata.evict_rho = con1.readDoubleByteArray();
  258. predata.evict_delta_p = con1.readDoubleIntArray();
  259. predata.evict_rho_p = con1.readDoubleIntArray();
  260. timer.stop(M.offline_read);
  261. timer.stop(M.offline_comp);
  262. ///////////////////////////////////////////////////////////////
  263. timer.start(M.online_comp);
  264. Tuple[] pathTuples = new Tuple[d * w];
  265. System.arraycopy(originalPath, 0, pathTuples, 0, w);
  266. System.arraycopy(originalPath, sw, pathTuples, w, (d - 1) * w);
  267. Bucket[] pathBuckets = Bucket.tuplesToBuckets(pathTuples, d, w, w);
  268. GCSignal[][] C_feInputKeys = new GCSignal[d][];
  269. GCSignal[][][] C_labelInputKeys = new GCSignal[d][][];
  270. for (int i = 0; i < d; i++) {
  271. C_feInputKeys[i] = GCUtil.selectFeKeys(predata.evict_C_feKeyPairs[i], pathBuckets[i].getTuples());
  272. C_labelInputKeys[i] = GCUtil.selectLabelKeys(predata.evict_C_labelKeyPairs[i], pathBuckets[i].getTuples());
  273. }
  274. timer.start(M.online_write);
  275. con2.write(online_band, C_feInputKeys);
  276. con2.write(online_band, C_labelInputKeys);
  277. timer.stop(M.online_write);
  278. PermuteTarget permutetarget = new PermuteTarget(con1, con2);
  279. permutetarget.runC(firstTree, d, predata.evict_pi);
  280. PermuteIndex permuteindex = new PermuteIndex(con1, con2);
  281. permuteindex.runC(firstTree, w, predata.evict_pi, predata.evict_rho);
  282. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  283. int W = (int) Math.pow(2, logW);
  284. for (int i = 0; i < d; i++) {
  285. pathBuckets[i].expand(W);
  286. pathBuckets[i].permute(predata.evict_delta_p[i]);
  287. }
  288. pathBuckets = Util.permute(pathBuckets, predata.evict_pi);
  289. for (int i = 0; i < d; i++) {
  290. pathBuckets[i].permute(predata.evict_rho_p[i]);
  291. }
  292. pathTuples = Bucket.bucketsToTuples(pathBuckets);
  293. SSXOT ssxot = new SSXOT(con1, con2);
  294. pathTuples = ssxot.runC(pathTuples, tupleParam);
  295. pathBuckets = Bucket.tuplesToBuckets(pathTuples, d, W, W);
  296. for (int i = 0; i < d; i++) {
  297. int[] rho_ivs = Util.inversePermutation(predata.evict_rho_p[i]);
  298. pathBuckets[i].permute(rho_ivs);
  299. }
  300. int[] pi_ivs = Util.inversePermutation(predata.evict_pi);
  301. pathBuckets = Util.permute(pathBuckets, pi_ivs);
  302. for (int i = 0; i < d; i++) {
  303. int[] delta_ivs = Util.inversePermutation(predata.evict_delta_p[i]);
  304. pathBuckets[i].permute(delta_ivs);
  305. pathBuckets[i].shrink(w);
  306. }
  307. pathBuckets[0].expand(Arrays.copyOfRange(originalPath, w, sw));
  308. timer.start(M.online_write);
  309. // con2.write(online_band, pathBuckets);
  310. timer.stop(M.online_write);
  311. timer.stop(M.online_comp);
  312. }
  313. @Override
  314. public void run(Party party, Metadata md, Forest[] forest) {
  315. System.out.println("Use PIRRetrieve to test Eviction");
  316. }
  317. @Override
  318. public void run(Party party, Metadata md, Forest forest) {
  319. }
  320. }