ULiT.java 8.4 KB

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