PIRRetrieve.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package pir;
  2. import java.math.BigInteger;
  3. import java.util.Arrays;
  4. import communication.Communication;
  5. import exceptions.NoSuchPartyException;
  6. import oram.Forest;
  7. import oram.Metadata;
  8. import oram.Tree;
  9. import oram.Tuple;
  10. import protocols.Eviction;
  11. import protocols.Protocol;
  12. import protocols.UpdateRoot;
  13. import protocols.precomputation.PreEviction;
  14. import protocols.precomputation.PreUpdateRoot;
  15. import protocols.struct.OutFF;
  16. import protocols.struct.OutPIRAccess;
  17. import protocols.struct.OutULiT;
  18. import protocols.struct.Party;
  19. import protocols.struct.PreData;
  20. import protocols.struct.TwoThreeXorByte;
  21. import protocols.struct.TwoThreeXorInt;
  22. import util.Bandwidth;
  23. import util.M;
  24. import util.P;
  25. import util.Timer;
  26. import util.Util;
  27. // TODO: really FlipFlag on path, and update path in Eviction
  28. public class PIRRetrieve extends Protocol {
  29. private int pid = P.PIRRtv;
  30. Communication[] cons1;
  31. Communication[] cons2;
  32. public PIRRetrieve(Communication con1, Communication con2) {
  33. super(con1, con2);
  34. }
  35. public void setCons(Communication[] a, Communication[] b) {
  36. cons1 = a;
  37. cons2 = b;
  38. }
  39. public OutPIRAccess runE(Metadata md, PreData predata, Tree tree_DE, Tree tree_CE, byte[] Li, TwoThreeXorByte L,
  40. TwoThreeXorByte N, TwoThreeXorInt dN, Timer timer) {
  41. timer.start(pid, M.online_comp);
  42. int treeIndex = tree_DE.getTreeIndex();
  43. boolean isLastTree = treeIndex == md.getNumTrees() - 1;
  44. boolean isFirstTree = treeIndex == 0;
  45. PIRAccess piracc = new PIRAccess(con1, con2);
  46. OutPIRAccess outpiracc = piracc.runE(md, predata, tree_DE, tree_CE, Li, L, N, dN, timer);
  47. OutULiT T = new OutULiT();
  48. if (!isLastTree) {
  49. TwoThreeXorByte Lp = new TwoThreeXorByte(md.getLBytesOfTree(treeIndex));
  50. TwoThreeXorByte Lpi = new TwoThreeXorByte(md.getLBytesOfTree(treeIndex + 1));
  51. ULiT ulit = new ULiT(con1, con2);
  52. T = ulit.runE(predata, outpiracc.X, N, dN, Lp, Lpi, outpiracc.nextL, md.getTwoTauPow(), timer);
  53. } else {
  54. T.DE = outpiracc.pathTuples_DE[0];
  55. T.CE = outpiracc.pathTuples_CE[0];
  56. }
  57. int pathTuples = outpiracc.pathTuples_CE.length;
  58. if (!isFirstTree) {
  59. byte[][] fb_DE = new byte[pathTuples][];
  60. byte[][] fb_CE = new byte[pathTuples][];
  61. for (int i = 0; i < pathTuples; i++) {
  62. fb_DE[i] = outpiracc.pathTuples_DE[i].getF();
  63. fb_CE[i] = outpiracc.pathTuples_CE[i].getF();
  64. }
  65. FlipFlag ff = new FlipFlag(con1, con2);
  66. OutFF outff = ff.runE(predata, fb_DE, fb_CE, outpiracc.j.s_DE, timer);
  67. for (int i = 0; i < pathTuples; i++) {
  68. // outpiracc.pathTuples_DE[i].setF(outff.fb_DE[i]);
  69. // outpiracc.pathTuples_CE[i].setF(outff.fb_CE[i]);
  70. }
  71. }
  72. int stashSize = tree_DE.getStashSize();
  73. PreUpdateRoot preupdateroot = new PreUpdateRoot(con1, con2);
  74. preupdateroot.runE(predata, isFirstTree, stashSize, md.getLBitsOfTree(treeIndex), timer);
  75. Tuple[] path = new Tuple[pathTuples];
  76. for (int i = 0; i < pathTuples; i++) {
  77. path[i] = outpiracc.pathTuples_DE[i].xor(outpiracc.pathTuples_CE[i]);
  78. }
  79. Tuple[] R = Arrays.copyOfRange(path, 0, stashSize);
  80. UpdateRoot updateroot = new UpdateRoot(con1, con2);
  81. R = updateroot.runE(predata, isFirstTree, Li, R, T.DE.xor(T.CE), timer);
  82. System.arraycopy(R, 0, path, 0, R.length);
  83. PreEviction preeviction = new PreEviction(con1, con2);
  84. preeviction.runE(predata, isFirstTree, tree_DE.getD(), tree_DE.getW(), timer);
  85. Eviction eviction = new Eviction(con1, con2);
  86. eviction.runE(predata, isFirstTree, Li, path, tree_DE, timer);
  87. // simulation of Reshare
  88. timer.start(pid, M.online_write);
  89. con2.write(pid, path);
  90. timer.stop(pid, M.online_write);
  91. timer.start(pid, M.online_read);
  92. con2.readTupleArray(pid);
  93. timer.stop(pid, M.online_read);
  94. // second eviction sim
  95. preupdateroot.runE(predata, isFirstTree, stashSize, md.getLBitsOfTree(treeIndex), timer);
  96. for (int i = 0; i < pathTuples; i++) {
  97. path[i] = outpiracc.pathTuples_DE[i].xor(outpiracc.pathTuples_CE[i]);
  98. }
  99. R = Arrays.copyOfRange(path, 0, stashSize);
  100. R = updateroot.runE(predata, isFirstTree, Li, R, T.DE.xor(T.CE), timer);
  101. System.arraycopy(R, 0, path, 0, R.length);
  102. preeviction.runE(predata, isFirstTree, tree_DE.getD(), tree_DE.getW(), timer);
  103. eviction.runE(predata, isFirstTree, Li, path, tree_DE, timer);
  104. // simulation of Reshare
  105. timer.start(pid, M.online_write);
  106. con2.write(pid, path);
  107. timer.stop(pid, M.online_write);
  108. timer.start(pid, M.online_read);
  109. con2.readTupleArray(pid);
  110. timer.stop(pid, M.online_read);
  111. timer.stop(pid, M.online_comp);
  112. return outpiracc;
  113. }
  114. public OutPIRAccess runD(Metadata md, PreData predata, Tree tree_DE, Tree tree_CD, byte[] Li, TwoThreeXorByte L,
  115. TwoThreeXorByte N, TwoThreeXorInt dN, Timer timer) {
  116. timer.start(pid, M.online_comp);
  117. int treeIndex = tree_DE.getTreeIndex();
  118. boolean isLastTree = treeIndex == md.getNumTrees() - 1;
  119. boolean isFirstTree = treeIndex == 0;
  120. PIRAccess piracc = new PIRAccess(con1, con2);
  121. OutPIRAccess outpiracc = piracc.runD(md, predata, tree_DE, tree_CD, Li, L, N, dN, timer);
  122. OutULiT T = new OutULiT();
  123. if (!isLastTree) {
  124. TwoThreeXorByte Lp = new TwoThreeXorByte(md.getLBytesOfTree(treeIndex));
  125. TwoThreeXorByte Lpi = new TwoThreeXorByte(md.getLBytesOfTree(treeIndex + 1));
  126. ULiT ulit = new ULiT(con1, con2);
  127. T = ulit.runD(predata, outpiracc.X, N, dN, Lp, Lpi, outpiracc.nextL, md.getTwoTauPow(), timer);
  128. } else {
  129. T.CD = outpiracc.pathTuples_CD[0];
  130. T.DE = outpiracc.pathTuples_DE[0];
  131. }
  132. int pathTuples = outpiracc.pathTuples_CD.length;
  133. if (!isFirstTree) {
  134. byte[][] fb_DE = new byte[pathTuples][];
  135. byte[][] fb_CD = new byte[pathTuples][];
  136. for (int i = 0; i < pathTuples; i++) {
  137. fb_DE[i] = outpiracc.pathTuples_DE[i].getF();
  138. fb_CD[i] = outpiracc.pathTuples_CD[i].getF();
  139. }
  140. FlipFlag ff = new FlipFlag(con1, con2);
  141. OutFF outff = ff.runD(predata, fb_DE, fb_CD, outpiracc.j.s_DE, timer);
  142. for (int i = 0; i < pathTuples; i++) {
  143. // outpiracc.pathTuples_DE[i].setF(outff.fb_DE[i]);
  144. // outpiracc.pathTuples_CD[i].setF(outff.fb_CD[i]);
  145. }
  146. }
  147. int stashSize = tree_DE.getStashSize();
  148. int[] tupleParam = new int[] { treeIndex == 0 ? 0 : 1, md.getNBytesOfTree(treeIndex),
  149. md.getLBytesOfTree(treeIndex), md.getABytesOfTree(treeIndex) };
  150. PreUpdateRoot preupdateroot = new PreUpdateRoot(con1, con2);
  151. preupdateroot.runD(predata, isFirstTree, stashSize, md.getLBitsOfTree(treeIndex), tupleParam, timer);
  152. UpdateRoot updateroot = new UpdateRoot(con1, con2);
  153. updateroot.runD(predata, isFirstTree, Li, tree_DE.getW(), timer);
  154. PreEviction preeviction = new PreEviction(con1, con2);
  155. preeviction.runD(predata, isFirstTree, tree_DE.getD(), tree_DE.getW(), tupleParam, timer);
  156. Eviction eviction = new Eviction(con1, con2);
  157. eviction.runD(predata, isFirstTree, Li, tree_DE, timer);
  158. // second eviction sim
  159. preupdateroot.runD(predata, isFirstTree, stashSize, md.getLBitsOfTree(treeIndex), tupleParam, timer);
  160. updateroot.runD(predata, isFirstTree, Li, tree_DE.getW(), timer);
  161. preeviction.runD(predata, isFirstTree, tree_DE.getD(), tree_DE.getW(), tupleParam, timer);
  162. eviction.runD(predata, isFirstTree, Li, tree_DE, timer);
  163. timer.stop(pid, M.online_comp);
  164. return outpiracc;
  165. }
  166. public OutPIRAccess runC(Metadata md, PreData predata, Tree tree_CD, Tree tree_CE, byte[] Li, TwoThreeXorByte L,
  167. TwoThreeXorByte N, TwoThreeXorInt dN, Timer timer) {
  168. timer.start(pid, M.online_comp);
  169. int treeIndex = tree_CE.getTreeIndex();
  170. boolean isLastTree = treeIndex == md.getNumTrees() - 1;
  171. boolean isFirstTree = treeIndex == 0;
  172. PIRAccess piracc = new PIRAccess(con1, con2);
  173. OutPIRAccess outpiracc = piracc.runC(md, predata, tree_CD, tree_CE, Li, L, N, dN, timer);
  174. OutULiT T = new OutULiT();
  175. if (!isLastTree) {
  176. TwoThreeXorByte Lp = new TwoThreeXorByte(md.getLBytesOfTree(treeIndex));
  177. TwoThreeXorByte Lpi = new TwoThreeXorByte(md.getLBytesOfTree(treeIndex + 1));
  178. ULiT ulit = new ULiT(con1, con2);
  179. T = ulit.runC(predata, outpiracc.X, N, dN, Lp, Lpi, outpiracc.nextL, md.getTwoTauPow(), timer);
  180. } else {
  181. T.CD = outpiracc.pathTuples_CD[0];
  182. T.CE = outpiracc.pathTuples_CE[0];
  183. }
  184. int pathTuples = outpiracc.pathTuples_CD.length;
  185. if (!isFirstTree) {
  186. byte[][] fb_CE = new byte[pathTuples][];
  187. byte[][] fb_CD = new byte[pathTuples][];
  188. for (int i = 0; i < pathTuples; i++) {
  189. fb_CE[i] = outpiracc.pathTuples_CE[i].getF();
  190. fb_CD[i] = outpiracc.pathTuples_CD[i].getF();
  191. }
  192. FlipFlag ff = new FlipFlag(con1, con2);
  193. OutFF outff = ff.runC(predata, fb_CD, fb_CE, outpiracc.j.t_C, timer);
  194. for (int i = 0; i < pathTuples; i++) {
  195. // outpiracc.pathTuples_CD[i].setF(outff.fb_CD[i]);
  196. // outpiracc.pathTuples_CE[i].setF(outff.fb_CE[i]);
  197. }
  198. }
  199. int stashSize = tree_CE.getStashSize();
  200. PreUpdateRoot preupdateroot = new PreUpdateRoot(con1, con2);
  201. preupdateroot.runC(predata, isFirstTree, timer);
  202. Tuple[] path = outpiracc.pathTuples_CD;
  203. Tuple[] R = Arrays.copyOfRange(path, 0, stashSize);
  204. UpdateRoot updateroot = new UpdateRoot(con1, con2);
  205. R = updateroot.runC(predata, isFirstTree, R, T.CD, timer);
  206. System.arraycopy(R, 0, path, 0, R.length);
  207. PreEviction preeviction = new PreEviction(con1, con2);
  208. preeviction.runC(predata, isFirstTree, timer);
  209. Eviction eviction = new Eviction(con1, con2);
  210. eviction.runC(predata, isFirstTree, path, tree_CD.getD(), stashSize, tree_CD.getW(), timer);
  211. // simulation of Reshare
  212. timer.start(pid, M.online_write);
  213. con1.write(pid, path);
  214. timer.stop(pid, M.online_write);
  215. timer.start(pid, M.online_read);
  216. con1.readTupleArray(pid);
  217. timer.stop(pid, M.online_read);
  218. // second eviction sim
  219. preupdateroot.runC(predata, isFirstTree, timer);
  220. R = Arrays.copyOfRange(path, 0, stashSize);
  221. R = updateroot.runC(predata, isFirstTree, R, T.CD, timer);
  222. System.arraycopy(R, 0, path, 0, R.length);
  223. preeviction.runC(predata, isFirstTree, timer);
  224. eviction.runC(predata, isFirstTree, path, tree_CD.getD(), stashSize, tree_CD.getW(), timer);
  225. // simulation of Reshare
  226. timer.start(pid, M.online_write);
  227. con1.write(pid, path);
  228. timer.stop(pid, M.online_write);
  229. timer.start(pid, M.online_read);
  230. con1.readTupleArray(pid);
  231. timer.stop(pid, M.online_read);
  232. timer.stop(pid, M.online_comp);
  233. return outpiracc;
  234. }
  235. @Override
  236. public void run(Party party, Metadata md, Forest[] forest) {
  237. Timer timer = new Timer();
  238. PreData predata = new PreData();
  239. Tree tree_CD = null;
  240. Tree tree_DE = null;
  241. Tree tree_CE = null;
  242. int iterations = 100;
  243. int reset = 20;
  244. for (int test = 0; test < iterations; test++) {
  245. if (test == reset) {
  246. timer.reset();
  247. }
  248. if (test == 1) {
  249. for (int k = 0; k < cons1.length; k++) {
  250. cons1[k].bandSwitch = false;
  251. cons2[k].bandSwitch = false;
  252. }
  253. }
  254. for (int treeIndex = 0; treeIndex < md.getNumTrees(); treeIndex++) {
  255. if (party == Party.Eddie) {
  256. tree_DE = forest[0].getTree(treeIndex);
  257. tree_CE = forest[1].getTree(treeIndex);
  258. } else if (party == Party.Debbie) {
  259. tree_DE = forest[0].getTree(treeIndex);
  260. tree_CD = forest[1].getTree(treeIndex);
  261. } else if (party == Party.Charlie) {
  262. tree_CE = forest[0].getTree(treeIndex);
  263. tree_CD = forest[1].getTree(treeIndex);
  264. } else {
  265. throw new NoSuchPartyException(party + "");
  266. }
  267. int Llen = md.getLBytesOfTree(treeIndex);
  268. int Nlen = md.getNBytesOfTree(treeIndex);
  269. TwoThreeXorInt dN = new TwoThreeXorInt();
  270. TwoThreeXorByte N = new TwoThreeXorByte();
  271. N.CD = new byte[Nlen];
  272. N.DE = new byte[Nlen];
  273. N.CE = new byte[Nlen];
  274. TwoThreeXorByte L = new TwoThreeXorByte();
  275. L.CD = new byte[Llen];
  276. L.DE = new byte[Llen];
  277. L.CE = new byte[Llen];
  278. byte[] Li = new byte[Llen];
  279. if (party == Party.Eddie) {
  280. OutPIRAccess out = this.runE(md, predata, tree_DE, tree_CE, Li, L, N, dN, timer);
  281. out.j.t_D = con1.readInt();
  282. out.j.t_C = con2.readInt();
  283. out.X.CD = con1.read();
  284. int pathTuples = out.pathTuples_CE.length;
  285. int index = (out.j.t_D + out.j.s_CE) % pathTuples;
  286. byte[] X = Util.xor(Util.xor(out.X.DE, out.X.CE), out.X.CD);
  287. boolean fail = false;
  288. if (index != 0) {
  289. System.err.println(test + " " + treeIndex + ": PIRAcc test failed on KSearch index");
  290. fail = true;
  291. }
  292. if (new BigInteger(1, X).intValue() != 0) {
  293. System.err.println(test + " " + treeIndex + ": PIRAcc test failed on 3ShiftPIR X");
  294. fail = true;
  295. }
  296. if (treeIndex < md.getNumTrees() - 1 && new BigInteger(1, out.Lip1).intValue() != 0) {
  297. System.err.println(test + " " + treeIndex + ": PIRAcc test failed on 3ShiftXorPIR Lip1");
  298. fail = true;
  299. }
  300. if (!fail)
  301. System.out.println(test + " " + treeIndex + ": PIRAcc test passed");
  302. } else if (party == Party.Debbie) {
  303. OutPIRAccess out = this.runD(md, predata, tree_DE, tree_CD, Li, L, N, dN, timer);
  304. con1.write(out.j.t_D);
  305. con1.write(out.X.CD);
  306. } else if (party == Party.Charlie) {
  307. OutPIRAccess out = this.runC(md, predata, tree_CD, tree_CE, Li, L, N, dN, timer);
  308. con1.write(out.j.t_C);
  309. } else {
  310. throw new NoSuchPartyException(party + "");
  311. }
  312. }
  313. }
  314. Bandwidth total = new Bandwidth("Total Online");
  315. for (int i = 0; i < P.size; i++) {
  316. for (int j = 0; j < cons1.length; j++)
  317. total.add(cons1[j].bandwidth[i].add(cons2[j].bandwidth[i]).bandwidth);
  318. }
  319. System.out.println(total.toString());
  320. // timer.divideBy(iterations - reset);
  321. // timer.print();
  322. sanityCheck();
  323. }
  324. // for testing correctness
  325. @Override
  326. public void run(Party party, Metadata md, Forest forest) {
  327. }
  328. }