ThreeShiftPIR.java 4.8 KB

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