ThreeShiftPIR.java 5.2 KB

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