ULiT.java 8.6 KB

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