ULiT.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package pir;
  2. import java.util.Arrays;
  3. import communication.Communication;
  4. import crypto.Crypto;
  5. import exceptions.NoSuchPartyException;
  6. import oram.Forest;
  7. import oram.Metadata;
  8. import oram.Tuple;
  9. import protocols.Protocol;
  10. import protocols.struct.OutULiT;
  11. import protocols.struct.Party;
  12. import protocols.struct.PreData;
  13. import protocols.struct.TwoThreeXorByte;
  14. import protocols.struct.TwoThreeXorInt;
  15. import util.M;
  16. import util.P;
  17. import util.Timer;
  18. import util.Util;
  19. public class ULiT extends Protocol {
  20. private int pid = P.ULiT;
  21. public ULiT(Communication con1, Communication con2) {
  22. super(con1, con2);
  23. }
  24. public OutULiT runE(PreData predata, TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
  25. TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp, Timer timer) {
  26. timer.start(pid, M.offline_comp);
  27. int l = Li.CE.length;
  28. timer.start(pid, M.offline_read);
  29. byte[] x2 = con1.read();
  30. timer.stop(pid, M.offline_read);
  31. timer.stop(pid, M.offline_comp);
  32. // ----------------------------------------- //
  33. timer.start(pid, M.online_comp);
  34. int dN_E = dN.CE;
  35. byte[] xorLi_E = Util.xor(Lpi.CE, Li.CE);
  36. InsLbl inslbl = new InsLbl(con1, con2);
  37. inslbl.runP1(predata, dN_E, xorLi_E, ttp, timer);
  38. inslbl = new InsLbl(con2, con1);
  39. byte[] b1 = inslbl.runP3(predata, ttp, l, timer);
  40. timer.start(pid, M.online_read);
  41. byte[] me = con1.read(pid);
  42. timer.stop(pid, M.online_read);
  43. byte[] x3 = Util.xor(me, b1);
  44. Util.setXor(X.CE, x3);
  45. Util.setXor(X.DE, x2);
  46. OutULiT out = new OutULiT();
  47. out.CE = new Tuple(new byte[] { 1 }, N.CE, Lp.CE, X.CE);
  48. out.DE = new Tuple(new byte[] { 1 }, N.DE, Lp.DE, X.DE);
  49. timer.stop(pid, M.online_comp);
  50. return out;
  51. }
  52. public OutULiT runD(PreData predata, TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
  53. TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp, Timer timer) {
  54. timer.start(pid, M.offline_comp);
  55. byte[] x1 = Util.nextBytes(X.CD.length, Crypto.sr);
  56. byte[] x2 = Util.nextBytes(X.CD.length, Crypto.sr);
  57. timer.start(pid, M.offline_write);
  58. con2.write(x1);
  59. con1.write(x2);
  60. timer.stop(pid, M.offline_write);
  61. timer.stop(pid, M.offline_comp);
  62. // ----------------------------------------- //
  63. timer.start(pid, M.online_comp);
  64. int dN_D = dN.CD ^ dN.DE;
  65. byte[] xorLi_D = Util.xor(Util.xor(Lpi.CD, Li.CD), Util.xor(Lpi.DE, Li.DE));
  66. InsLbl inslbl = new InsLbl(con1, con2);
  67. byte[] a2 = inslbl.runP2(predata, dN_D, xorLi_D, ttp, timer);
  68. inslbl = new InsLbl(con2, con1);
  69. byte[] a1 = inslbl.runP2(predata, dN_D, xorLi_D, ttp, timer);
  70. Util.setXor(a1, x1);
  71. Util.setXor(a1, x2);
  72. Util.setXor(a2, x1);
  73. Util.setXor(a2, x2);
  74. timer.start(pid, M.online_write);
  75. con1.write(pid, a1);
  76. con2.write(pid, a2);
  77. timer.stop(pid, M.online_write);
  78. Util.setXor(X.CD, x1);
  79. Util.setXor(X.DE, x2);
  80. OutULiT out = new OutULiT();
  81. out.CD = new Tuple(new byte[] { 1 }, N.CD, Lp.CD, X.CD);
  82. out.DE = new Tuple(new byte[] { 1 }, N.DE, Lp.DE, X.DE);
  83. timer.stop(pid, M.online_comp);
  84. return out;
  85. }
  86. public OutULiT runC(PreData predata, TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
  87. TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp, Timer timer) {
  88. timer.start(pid, M.offline_comp);
  89. int l = Li.CE.length;
  90. timer.start(pid, M.offline_read);
  91. byte[] x1 = con2.read();
  92. timer.stop(pid, M.offline_read);
  93. timer.stop(pid, M.offline_comp);
  94. // ----------------------------------------- //
  95. timer.start(pid, M.online_comp);
  96. InsLbl inslbl = new InsLbl(con1, con2);
  97. byte[] b2 = inslbl.runP3(predata, ttp, l, timer);
  98. int dN_C = dN.CE;
  99. byte[] xorLi_C = Util.xor(Lpi.CE, Li.CE);
  100. inslbl = new InsLbl(con2, con1);
  101. inslbl.runP1(predata, dN_C, xorLi_C, ttp, timer);
  102. timer.start(pid, M.online_read);
  103. byte[] mc = con2.read(pid);
  104. timer.stop(pid, M.online_read);
  105. byte[] x3 = Util.xor(mc, b2);
  106. Util.setXor(X.CD, x1);
  107. Util.setXor(X.CE, x3);
  108. OutULiT out = new OutULiT();
  109. out.CD = new Tuple(new byte[] { 1 }, N.CD, Lp.CD, X.CD);
  110. out.CE = new Tuple(new byte[] { 1 }, N.CE, Lp.CE, X.CE);
  111. timer.stop(pid, M.online_comp);
  112. return out;
  113. }
  114. @Override
  115. public void run(Party party, Metadata md, Forest[] forest) {
  116. Timer timer = new Timer();
  117. PreData predata = new PreData();
  118. for (int j = 0; j < 100; j++) {
  119. int ttp = (int) Math.pow(2, 8);
  120. int l = 10;
  121. int Llen = 9;
  122. int Nlen = 20;
  123. int Xlen = ttp * l;
  124. TwoThreeXorInt dN = new TwoThreeXorInt();
  125. dN.CD = Crypto.sr.nextInt(ttp);
  126. dN.DE = Crypto.sr.nextInt(ttp);
  127. dN.CE = Crypto.sr.nextInt(ttp);
  128. int trueDN = dN.CD ^ dN.CE ^ dN.DE;
  129. TwoThreeXorByte X = new TwoThreeXorByte();
  130. X.CD = Util.nextBytes(Xlen, Crypto.sr);
  131. X.DE = Util.nextBytes(Xlen, Crypto.sr);
  132. X.CE = Util.nextBytes(Xlen, Crypto.sr);
  133. TwoThreeXorByte N = new TwoThreeXorByte();
  134. N.CD = Util.nextBytes(Nlen, Crypto.sr);
  135. N.DE = Util.nextBytes(Nlen, Crypto.sr);
  136. N.CE = Util.nextBytes(Nlen, Crypto.sr);
  137. TwoThreeXorByte Lp = new TwoThreeXorByte();
  138. Lp.CD = Util.nextBytes(Llen, Crypto.sr);
  139. Lp.DE = Util.nextBytes(Llen, Crypto.sr);
  140. Lp.CE = Util.nextBytes(Llen, Crypto.sr);
  141. TwoThreeXorByte Lpi = new TwoThreeXorByte();
  142. Lpi.CD = Util.nextBytes(l, Crypto.sr);
  143. Lpi.DE = Util.nextBytes(l, Crypto.sr);
  144. Lpi.CE = Util.nextBytes(l, Crypto.sr);
  145. byte[] trueX = Util.xor(X.CD, X.CE);
  146. Util.setXor(trueX, X.DE);
  147. TwoThreeXorByte Li = new TwoThreeXorByte();
  148. Li.CD = Util.nextBytes(l, Crypto.sr);
  149. Li.DE = Util.nextBytes(l, Crypto.sr);
  150. Li.CE = Arrays.copyOfRange(trueX, trueDN * l, trueDN * l + l);
  151. Util.setXor(Li.CE, Li.CD);
  152. Util.setXor(Li.CE, Li.DE);
  153. if (party == Party.Eddie) {
  154. con1.write(X.CD);
  155. con1.write(X.DE);
  156. con1.write(N.CD);
  157. con1.write(N.DE);
  158. con1.write(Lp.CD);
  159. con1.write(Lp.DE);
  160. con1.write(Lpi.CD);
  161. con1.write(Lpi.DE);
  162. con1.write(Li.CD);
  163. con1.write(Li.DE);
  164. con1.write(dN.CD);
  165. con1.write(dN.DE);
  166. con2.write(X.CD);
  167. con2.write(X.CE);
  168. con2.write(N.CD);
  169. con2.write(N.CE);
  170. con2.write(Lp.CD);
  171. con2.write(Lp.CE);
  172. con2.write(Lpi.CD);
  173. con2.write(Lpi.CE);
  174. con2.write(Li.CD);
  175. con2.write(Li.CE);
  176. con2.write(dN.CD);
  177. con2.write(dN.CE);
  178. OutULiT out = this.runE(predata, X, N, dN, Lp, Lpi, Li, ttp, timer);
  179. out.CD = con1.readTuple();
  180. Tuple T = out.CD.xor(out.CE);
  181. T.setXor(out.DE);
  182. byte[] trueN = Util.xor(N.CD, N.CE);
  183. Util.setXor(trueN, N.DE);
  184. byte[] trueLp = Util.xor(Lp.CD, Lp.CE);
  185. Util.setXor(trueLp, Lp.DE);
  186. byte[] trueLpi = Util.xor(Lpi.CD, Lpi.CE);
  187. Util.setXor(trueLpi, Lpi.DE);
  188. byte[] expectLpi = Arrays.copyOfRange(T.getA(), trueDN * l, trueDN * l + l);
  189. byte[] expectX = T.getA();
  190. boolean fail = false;
  191. if ((T.getF()[0] & 1) != 1) {
  192. System.err.println(j + ": ULiT test failed on F");
  193. fail = true;
  194. }
  195. if (!Util.equal(T.getN(), trueN)) {
  196. System.err.println(j + ": ULiT test failed on N");
  197. fail = true;
  198. }
  199. if (!Util.equal(T.getL(), trueLp)) {
  200. System.err.println(j + ": ULiT test failed on Lp");
  201. fail = true;
  202. }
  203. if (!Util.equal(expectLpi, trueLpi)) {
  204. System.err.println(j + ": ULiT test failed on Lpi");
  205. fail = true;
  206. }
  207. for (int i = 0; i < trueDN * l; i++) {
  208. if (expectX[i] != trueX[i]) {
  209. System.err.println(j + ": ULiT test failed 1");
  210. fail = true;
  211. break;
  212. }
  213. }
  214. for (int i = trueDN * l + l; i < trueX.length; i++) {
  215. if (expectX[i] != trueX[i]) {
  216. System.err.println(j + ": ULiT test failed 2");
  217. fail = true;
  218. break;
  219. }
  220. }
  221. if (!fail)
  222. System.out.println(j + ": ULiT test passed");
  223. } else if (party == Party.Debbie) {
  224. X.CD = con1.read();
  225. X.DE = con1.read();
  226. N.CD = con1.read();
  227. N.DE = con1.read();
  228. Lp.CD = con1.read();
  229. Lp.DE = con1.read();
  230. Lpi.CD = con1.read();
  231. Lpi.DE = con1.read();
  232. Li.CD = con1.read();
  233. Li.DE = con1.read();
  234. dN.CD = con1.readInt();
  235. dN.DE = con1.readInt();
  236. OutULiT out = this.runD(predata, X, N, dN, Lp, Lpi, Li, ttp, timer);
  237. con1.write(out.CD);
  238. } else if (party == Party.Charlie) {
  239. X.CD = con1.read();
  240. X.CE = con1.read();
  241. N.CD = con1.read();
  242. N.CE = con1.read();
  243. Lp.CD = con1.read();
  244. Lp.CE = con1.read();
  245. Lpi.CD = con1.read();
  246. Lpi.CE = con1.read();
  247. Li.CD = con1.read();
  248. Li.CE = con1.read();
  249. dN.CD = con1.readInt();
  250. dN.CE = con1.readInt();
  251. this.runC(predata, X, N, dN, Lp, Lpi, Li, ttp, timer);
  252. } else {
  253. throw new NoSuchPartyException(party + "");
  254. }
  255. }
  256. }
  257. @Override
  258. public void run(Party party, Metadata md, Forest forest) {
  259. }
  260. }