ThreeShiftXorPIR.java 5.8 KB

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