ThreeShiftXorPIR.java 6.7 KB

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