ThreeShiftXorPIR.java 6.5 KB

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