PIRRetrieve.java 10 KB

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