ThreeShiftPIR.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package subprotocols;
  2. import java.security.SecureRandom;
  3. import communication.Communication;
  4. import crypto.Crypto;
  5. import exceptions.NoSuchPartyException;
  6. import oram.Forest;
  7. import oram.Metadata;
  8. import protocols.Protocol;
  9. import struct.OutPIRCOT;
  10. import struct.Party;
  11. import struct.TwoThreeXorByte;
  12. import util.M;
  13. import util.P;
  14. import util.Util;
  15. public class ThreeShiftPIR extends Protocol {
  16. SecureRandom sr1;
  17. SecureRandom sr2;
  18. int pid = P.TSPIR;
  19. public ThreeShiftPIR(Communication con1, Communication con2) {
  20. super(con1, con2);
  21. online_band = all.online_band[pid];
  22. offline_band = all.offline_band[pid];
  23. timer = all.timer[pid];
  24. }
  25. public ThreeShiftPIR(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  26. super(con1, con2);
  27. this.sr1 = sr1;
  28. this.sr2 = sr2;
  29. online_band = all.online_band[pid];
  30. offline_band = all.offline_band[pid];
  31. timer = all.timer[pid];
  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) {
  40. timer.start(M.online_comp);
  41. int l = x_DE.length;
  42. ShiftPIR sftpir = new ShiftPIR(con1, con2, sr1, sr2);
  43. byte[] e1 = sftpir.runP1(x_DE, i.s_DE);
  44. sftpir.reinit(con2, con1, sr2, sr1);
  45. byte[] e2 = sftpir.runP2(x_CE, i.s_CE);
  46. sftpir.reinit(con1, con2, sr1, sr2);
  47. sftpir.runP3(l, i.t_E);
  48. Util.setXor(e1, e2);
  49. TwoThreeXorByte X = new TwoThreeXorByte();
  50. X.DE = e1;
  51. timer.start(M.online_write);
  52. con1.write(online_band, X.DE);
  53. timer.stop(M.online_write);
  54. timer.start(M.online_read);
  55. X.CE = con2.readAndDec();
  56. timer.stop(M.online_read);
  57. timer.stop(M.online_comp);
  58. return X;
  59. }
  60. public TwoThreeXorByte runD(byte[][] x_DE, byte[][] x_CD, OutPIRCOT i) {
  61. timer.start(M.online_comp);
  62. int l = x_DE.length;
  63. ShiftPIR sftpir = new ShiftPIR(con1, con2, sr1, sr2);
  64. byte[] d1 = sftpir.runP2(x_DE, i.s_DE);
  65. sftpir.reinit(con2, con1, sr2, sr1);
  66. sftpir.runP3(l, i.t_D);
  67. sftpir.reinit(con2, con1, sr2, sr1);
  68. byte[] d2 = sftpir.runP1(x_CD, i.s_CD);
  69. Util.setXor(d1, d2);
  70. TwoThreeXorByte X = new TwoThreeXorByte();
  71. X.CD = d1;
  72. timer.start(M.online_write);
  73. con2.write(online_band, X.CD);
  74. timer.stop(M.online_write);
  75. timer.start(M.online_read);
  76. X.DE = con1.readAndDec();
  77. timer.stop(M.online_read);
  78. timer.stop(M.online_comp);
  79. return X;
  80. }
  81. public TwoThreeXorByte runC(byte[][] x_CD, byte[][] x_CE, OutPIRCOT i) {
  82. timer.start(M.online_comp);
  83. int l = x_CD.length;
  84. ShiftPIR sftpir = new ShiftPIR(con1, con2, sr1, sr2);
  85. sftpir.runP3(l, i.t_C);
  86. sftpir.reinit(con1, con2, sr1, sr2);
  87. byte[] c1 = sftpir.runP1(x_CE, i.s_CE);
  88. sftpir.reinit(con2, con1, sr2, sr1);
  89. byte[] c2 = sftpir.runP2(x_CD, i.s_CD);
  90. Util.setXor(c1, c2);
  91. TwoThreeXorByte X = new TwoThreeXorByte();
  92. X.CE = c1;
  93. timer.start(M.online_write);
  94. con1.write(online_band, X.CE);
  95. timer.stop(M.online_write);
  96. timer.start(M.online_read);
  97. X.CD = con2.readAndDec();
  98. timer.stop(M.online_read);
  99. timer.stop(M.online_comp);
  100. return X;
  101. }
  102. @Override
  103. public void run(Party party, Metadata md, Forest[] forest) {
  104. for (int j = 0; j < 100; j++) {
  105. int l = 500;
  106. int m = 50;
  107. byte[][] x_CD = new byte[l][m];
  108. byte[][] x_CE = new byte[l][m];
  109. byte[][] x_DE = new byte[l][m];
  110. for (int i = 0; i < l; i++) {
  111. Crypto.sr.nextBytes(x_CD[i]);
  112. Crypto.sr.nextBytes(x_DE[i]);
  113. Crypto.sr.nextBytes(x_CE[i]);
  114. }
  115. int index = Crypto.sr.nextInt(l);
  116. OutPIRCOT ks = new OutPIRCOT();
  117. ks.t_C = Crypto.sr.nextInt(l);
  118. ks.t_D = Crypto.sr.nextInt(l);
  119. ks.t_E = Crypto.sr.nextInt(l);
  120. ks.s_DE = (index - ks.t_C + l) % l;
  121. ks.s_CE = (index - ks.t_D + l) % l;
  122. ks.s_CD = (index - ks.t_E + l) % l;
  123. TwoThreeXorByte X = new TwoThreeXorByte();
  124. if (party == Party.Eddie) {
  125. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
  126. con1.write(x_CD);
  127. con1.write(x_DE);
  128. con2.write(x_CD);
  129. con2.write(x_CE);
  130. con1.write(ks.t_D);
  131. con1.write(ks.s_DE);
  132. con1.write(ks.s_CD);
  133. con2.write(ks.t_C);
  134. con2.write(ks.s_CE);
  135. con2.write(ks.s_CD);
  136. X = this.runE(x_DE, x_CE, ks);
  137. X.CD = con1.read();
  138. byte[] e = X.CE;
  139. Util.setXor(e, X.CD);
  140. Util.setXor(e, X.DE);
  141. byte[] x = x_DE[index];
  142. Util.setXor(x, x_CE[index]);
  143. Util.setXor(x, x_CD[index]);
  144. if (!Util.equal(x, e))
  145. System.err.println(j + ": 3ShiftPIR test failed");
  146. else
  147. System.out.println(j + ": 3ShiftPIR test passed");
  148. } else if (party == Party.Debbie) {
  149. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
  150. x_CD = con1.readDoubleByteArray();
  151. x_DE = con1.readDoubleByteArray();
  152. ks.t_D = con1.readInt();
  153. ks.s_DE = con1.readInt();
  154. ks.s_CD = con1.readInt();
  155. X = this.runD(x_DE, x_CD, ks);
  156. con1.write(X.CD);
  157. } else if (party == Party.Charlie) {
  158. this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
  159. x_CD = con1.readDoubleByteArray();
  160. x_CE = con1.readDoubleByteArray();
  161. ks.t_C = con1.readInt();
  162. ks.s_CE = con1.readInt();
  163. ks.s_CD = con1.readInt();
  164. this.runC(x_CD, x_CE, ks);
  165. } else {
  166. throw new NoSuchPartyException(party + "");
  167. }
  168. }
  169. }
  170. @Override
  171. public void run(Party party, Metadata md, Forest forest) {
  172. }
  173. }