ThreeShiftXorPIR.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package subprotocols;
  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 protocols.Protocol;
  10. import struct.OutPIRCOT;
  11. import struct.Party;
  12. import struct.TwoOneXor;
  13. import struct.TwoThreeXorByte;
  14. import util.M;
  15. import util.P;
  16. import util.Util;
  17. public class ThreeShiftXorPIR extends Protocol {
  18. SecureRandom sr1;
  19. SecureRandom sr2;
  20. int pid = P.TSXPIR;
  21. public ThreeShiftXorPIR(Communication con1, Communication con2) {
  22. super(con1, con2);
  23. online_band = all.online_band[pid];
  24. offline_band = all.offline_band[pid];
  25. timer = all.timer[pid];
  26. }
  27. public ThreeShiftXorPIR(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  28. super(con1, con2);
  29. this.sr1 = sr1;
  30. this.sr2 = sr2;
  31. online_band = all.online_band[pid];
  32. offline_band = all.offline_band[pid];
  33. timer = all.timer[pid];
  34. }
  35. public void reinit(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  36. this.con1 = con1;
  37. this.con2 = con2;
  38. this.sr1 = sr1;
  39. this.sr2 = sr2;
  40. }
  41. public TwoThreeXorByte runE(byte[][] x_DE, byte[][] x_CE, OutPIRCOT i, TwoOneXor dN, int ttp) {
  42. timer.start(M.online_comp);
  43. int n = x_DE.length;
  44. ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2, sr1, sr2);
  45. byte[] e1 = sftpir.runP1(x_DE, i.s_DE, dN.s_DE, ttp);
  46. sftpir.reinit(con2, con1, sr2, sr1);
  47. byte[] e2 = sftpir.runP2(x_CE, i.s_CE, dN.s_CE, ttp);
  48. sftpir.reinit(con1, con2, sr1, sr2);
  49. sftpir.runP3(i.t_E, dN.t_E, n, ttp);
  50. Util.setXor(e1, e2);
  51. timer.start(M.online_write);
  52. con1.write(online_band, e1);
  53. con2.write(online_band, e1);
  54. timer.stop(M.online_write);
  55. timer.start(M.online_read);
  56. byte[] d = con1.readAndDec();
  57. byte[] c = con2.readAndDec();
  58. timer.stop(M.online_read);
  59. TwoThreeXorByte nextL = new TwoThreeXorByte();
  60. nextL.DE = e1;
  61. nextL.CD = d;
  62. nextL.CE = c;
  63. timer.stop(M.online_comp);
  64. return nextL;
  65. }
  66. public TwoThreeXorByte runD(byte[][] x_DE, byte[][] x_CD, OutPIRCOT i, TwoOneXor dN, int ttp) {
  67. timer.start(M.online_comp);
  68. int n = x_DE.length;
  69. ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2, sr1, sr2);
  70. byte[] d1 = sftpir.runP2(x_DE, i.s_DE, dN.s_DE, ttp);
  71. sftpir.reinit(con2, con1, sr2, sr1);
  72. sftpir.runP3(i.t_D, dN.t_D, n, ttp);
  73. sftpir.reinit(con2, con1, sr2, sr1);
  74. byte[] d2 = sftpir.runP1(x_CD, i.s_CD, dN.s_CD, ttp);
  75. Util.setXor(d1, d2);
  76. timer.start(M.online_write);
  77. con1.write(online_band, d1);
  78. con2.write(online_band, d1);
  79. timer.stop(M.online_write);
  80. timer.start(M.online_read);
  81. byte[] e = con1.readAndDec();
  82. byte[] c = con2.readAndDec();
  83. timer.stop(M.online_read);
  84. TwoThreeXorByte nextL = new TwoThreeXorByte();
  85. nextL.DE = e;
  86. nextL.CD = d1;
  87. nextL.CE = c;
  88. timer.stop(M.online_comp);
  89. return nextL;
  90. }
  91. public TwoThreeXorByte runC(byte[][] x_CD, byte[][] x_CE, OutPIRCOT i, TwoOneXor dN, int ttp) {
  92. timer.start(M.online_comp);
  93. int n = x_CD.length;
  94. ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2, sr1, sr2);
  95. sftpir.runP3(i.t_C, dN.t_C, n, ttp);
  96. sftpir.reinit(con1, con2, sr1, sr2);
  97. byte[] c1 = sftpir.runP1(x_CE, i.s_CE, dN.s_CE, ttp);
  98. sftpir.reinit(con2, con1, sr2, sr1);
  99. byte[] c2 = sftpir.runP2(x_CD, i.s_CD, dN.s_CD, ttp);
  100. Util.setXor(c1, c2);
  101. timer.start(M.online_write);
  102. con1.write(online_band, c1);
  103. con2.write(online_band, c1);
  104. timer.stop(M.online_write);
  105. timer.start(M.online_read);
  106. byte[] e = con1.readAndDec();
  107. byte[] d = con2.readAndDec();
  108. timer.stop(M.online_read);
  109. TwoThreeXorByte nextL = new TwoThreeXorByte();
  110. nextL.DE = e;
  111. nextL.CD = d;
  112. nextL.CE = c1;
  113. timer.stop(M.online_comp);
  114. return nextL;
  115. }
  116. @Override
  117. public void run(Party party, Metadata md, Forest[] forest) {
  118. for (int j = 0; j < 100; j++) {
  119. int n = 500;
  120. int m = 16;
  121. int l = 4;
  122. byte[][] x_CD = new byte[n][m * l];
  123. byte[][] x_CE = new byte[n][m * l];
  124. byte[][] x_DE = new byte[n][m * l];
  125. for (int i = 0; i < n; i++) {
  126. Crypto.sr.nextBytes(x_CD[i]);
  127. Crypto.sr.nextBytes(x_DE[i]);
  128. Crypto.sr.nextBytes(x_CE[i]);
  129. }
  130. int i1 = Crypto.sr.nextInt(n);
  131. OutPIRCOT ks = new OutPIRCOT();
  132. ks.t_C = Crypto.sr.nextInt(n);
  133. ks.t_D = Crypto.sr.nextInt(n);
  134. ks.t_E = Crypto.sr.nextInt(n);
  135. ks.s_DE = (i1 - ks.t_C + n) % n;
  136. ks.s_CE = (i1 - ks.t_D + n) % n;
  137. ks.s_CD = (i1 - ks.t_E + n) % n;
  138. int i2 = Crypto.sr.nextInt(m);
  139. TwoOneXor tox = new TwoOneXor();
  140. tox.t_C = Crypto.sr.nextInt(m);
  141. tox.t_D = Crypto.sr.nextInt(m);
  142. tox.t_E = Crypto.sr.nextInt(m);
  143. tox.s_DE = i2 ^ tox.t_C;
  144. tox.s_CE = i2 ^ tox.t_D;
  145. tox.s_CD = i2 ^ tox.t_E;
  146. if (party == Party.Eddie) {
  147. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
  148. con1.write(x_CD);
  149. con1.write(x_DE);
  150. con2.write(x_CD);
  151. con2.write(x_CE);
  152. con1.write(ks.t_D);
  153. con1.write(ks.s_DE);
  154. con1.write(ks.s_CD);
  155. con2.write(ks.t_C);
  156. con2.write(ks.s_CE);
  157. con2.write(ks.s_CD);
  158. con1.write(tox.t_D);
  159. con1.write(tox.s_DE);
  160. con1.write(tox.s_CD);
  161. con2.write(tox.t_C);
  162. con2.write(tox.s_CE);
  163. con2.write(tox.s_CD);
  164. TwoThreeXorByte nextL = this.runE(x_DE, x_CE, ks, tox, m);
  165. byte[] e = Util.xor(Util.xor(nextL.DE, nextL.CE), nextL.CD);
  166. byte[] d = con1.read();
  167. byte[] c = con2.read();
  168. byte[] x = x_DE[i1];
  169. Util.setXor(x, x_CE[i1]);
  170. Util.setXor(x, x_CD[i1]);
  171. byte[] expect = Arrays.copyOfRange(x, i2 * l, (i2 + 1) * l);
  172. if (!Util.equal(expect, e) || !Util.equal(expect, d) || !Util.equal(expect, c))
  173. System.err.println(j + ": 3ShiftXorPIR test failed");
  174. else
  175. System.out.println(j + ": 3ShiftXorPIR test passed");
  176. } else if (party == Party.Debbie) {
  177. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
  178. x_CD = con1.readDoubleByteArray();
  179. x_DE = con1.readDoubleByteArray();
  180. ks.t_D = con1.readInt();
  181. ks.s_DE = con1.readInt();
  182. ks.s_CD = con1.readInt();
  183. tox.t_D = con1.readInt();
  184. tox.s_DE = con1.readInt();
  185. tox.s_CD = con1.readInt();
  186. TwoThreeXorByte nextL = this.runD(x_DE, x_CD, ks, tox, m);
  187. byte[] d = Util.xor(Util.xor(nextL.DE, nextL.CE), nextL.CD);
  188. con1.write(d);
  189. } else if (party == Party.Charlie) {
  190. this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
  191. x_CD = con1.readDoubleByteArray();
  192. x_CE = con1.readDoubleByteArray();
  193. ks.t_C = con1.readInt();
  194. ks.s_CE = con1.readInt();
  195. ks.s_CD = con1.readInt();
  196. tox.t_C = con1.readInt();
  197. tox.s_CE = con1.readInt();
  198. tox.s_CD = con1.readInt();
  199. TwoThreeXorByte nextL = this.runC(x_CD, x_CE, ks, tox, m);
  200. byte[] c = Util.xor(Util.xor(nextL.DE, nextL.CE), nextL.CD);
  201. con1.write(c);
  202. } else {
  203. throw new NoSuchPartyException(party + "");
  204. }
  205. }
  206. }
  207. @Override
  208. public void run(Party party, Metadata md, Forest forest) {
  209. }
  210. }