ThreeShiftPIR.java 5.0 KB

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