PIRCOT.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package subprotocols;
  2. import communication.Communication;
  3. import crypto.Crypto;
  4. import crypto.PRF;
  5. import exceptions.NoSuchPartyException;
  6. import exceptions.PIRCOTException;
  7. import oram.Forest;
  8. import oram.Metadata;
  9. import protocols.Protocol;
  10. import struct.OutPIRCOT;
  11. import struct.Party;
  12. import util.M;
  13. import util.P;
  14. import util.Util;
  15. // KSearch
  16. public class PIRCOT extends Protocol {
  17. int pid = P.KSER;
  18. public PIRCOT(Communication con1, Communication con2) {
  19. super(con1, con2);
  20. online_band = all.online_band[pid];
  21. offline_band = all.offline_band[pid];
  22. timer = all.timer[pid];
  23. }
  24. public OutPIRCOT runE(byte[][] u, byte[] v) {
  25. timer.start(M.offline_comp);
  26. int l = u.length;
  27. byte[] k = PRF.generateKey(Crypto.sr_DE);
  28. byte[][] r = new byte[l][];
  29. for (int i = 0; i < l; i++) {
  30. r[i] = new byte[Crypto.secParamBytes];
  31. Crypto.sr_DE.nextBytes(r[i]);
  32. }
  33. int s_DE = Crypto.sr_DE.nextInt(l);
  34. int s_CE = Crypto.sr_CE.nextInt(l);
  35. PRF F_k = new PRF(Crypto.secParam);
  36. F_k.init(k);
  37. timer.stop(M.offline_comp);
  38. //////////////////////////////////////////////////////////////
  39. timer.start(M.online_comp);
  40. byte[][] a = new byte[l][];
  41. for (int j = 0; j < l; j++) {
  42. a[j] = Util.xor(u[(j + s_DE) % l], v);
  43. a[j] = Util.padArray(a[j], r[j].length);
  44. Util.setXor(a[j], r[j]);
  45. a[j] = F_k.compute(a[j]);
  46. }
  47. timer.start(M.online_write);
  48. con2.write(online_band, a);
  49. timer.stop(M.online_write);
  50. timer.start(M.online_read);
  51. int delta = con2.readIntAndDec();
  52. timer.stop(M.online_read);
  53. int t_E = (s_DE + delta) % l;
  54. OutPIRCOT out = new OutPIRCOT();
  55. out.t_E = t_E;
  56. out.s_DE = s_DE;
  57. out.s_CE = s_CE;
  58. timer.stop(M.online_comp);
  59. return out;
  60. }
  61. public OutPIRCOT runD(byte[][] u, byte[] v) {
  62. timer.start(M.offline_comp);
  63. int l = u.length;
  64. byte[] k = PRF.generateKey(Crypto.sr_DE);
  65. byte[][] r = new byte[l][];
  66. for (int i = 0; i < l; i++) {
  67. r[i] = new byte[Crypto.secParamBytes];
  68. Crypto.sr_DE.nextBytes(r[i]);
  69. }
  70. int s_DE = Crypto.sr_DE.nextInt(l);
  71. int s_CD = Crypto.sr_CD.nextInt(l);
  72. PRF F_k = new PRF(Crypto.secParam);
  73. F_k.init(k);
  74. timer.stop(M.offline_comp);
  75. ///////////////////////////////////////////////////////////
  76. timer.start(M.online_comp);
  77. byte[][] a = new byte[l][];
  78. for (int j = 0; j < l; j++) {
  79. a[j] = Util.xor(u[(j + s_DE) % l], v);
  80. a[j] = Util.padArray(a[j], r[j].length);
  81. Util.setXor(a[j], r[j]);
  82. a[j] = F_k.compute(a[j]);
  83. }
  84. timer.start(M.online_write);
  85. con2.write(online_band, a);
  86. timer.stop(M.online_write);
  87. timer.start(M.online_read);
  88. int delta = con2.readIntAndDec();
  89. timer.stop(M.online_read);
  90. int t_D = (s_DE + delta) % l;
  91. OutPIRCOT out = new OutPIRCOT();
  92. out.t_D = t_D;
  93. out.s_DE = s_DE;
  94. out.s_CD = s_CD;
  95. timer.stop(M.online_comp);
  96. return out;
  97. }
  98. public OutPIRCOT runC(int l) {
  99. timer.start(M.offline_comp);
  100. int s_CE = Crypto.sr_CE.nextInt(l);
  101. int s_CD = Crypto.sr_CD.nextInt(l);
  102. timer.stop(M.offline_comp);
  103. /////////////////////////////////////////////////
  104. timer.start(M.online_comp);
  105. timer.start(M.online_read);
  106. byte[][] x = con1.readDoubleByteArrayAndDec();
  107. byte[][] y = con2.readDoubleByteArrayAndDec();
  108. timer.stop(M.online_read);
  109. int count = 0;
  110. int t_C = 0;
  111. for (int i = 0; i < l; i++) {
  112. if (Util.equal(x[i], y[i])) {
  113. t_C = i;
  114. count++;
  115. }
  116. }
  117. if (count != 1) {
  118. throw new PIRCOTException("Invariant error: " + count);
  119. }
  120. int delta_D = (t_C - s_CE + l) % l;
  121. int delta_E = (t_C - s_CD + l) % l;
  122. timer.start(M.online_write);
  123. con2.write(online_band, delta_D);
  124. con1.write(online_band, delta_E);
  125. timer.stop(M.online_write);
  126. OutPIRCOT out = new OutPIRCOT();
  127. out.t_C = t_C;
  128. out.s_CE = s_CE;
  129. out.s_CD = s_CD;
  130. timer.stop(M.online_comp);
  131. return out;
  132. }
  133. @Override
  134. public void run(Party party, Metadata md, Forest[] forest) {
  135. for (int j = 0; j < 100; j++) {
  136. int n = 500;
  137. int FN = 5;
  138. byte[][] a = new byte[n][FN];
  139. byte[][] b = new byte[n][FN];
  140. for (int i = 0; i < n; i++) {
  141. Crypto.sr.nextBytes(a[i]);
  142. }
  143. int index = Crypto.sr.nextInt(n);
  144. byte[] v = a[index].clone();
  145. OutPIRCOT output;
  146. if (party == Party.Eddie) {
  147. con2.write(index);
  148. output = runE(a, v);
  149. con2.write(output.t_E);
  150. con2.write(output.s_CE);
  151. con2.write(output.s_DE);
  152. } else if (party == Party.Debbie) {
  153. output = runD(b, new byte[FN]);
  154. con2.write(output.t_D);
  155. con2.write(output.s_DE);
  156. con2.write(output.s_CD);
  157. } else if (party == Party.Charlie) {
  158. index = con1.readInt();
  159. output = runC(n);
  160. int t_E = con1.readInt();
  161. int s_CE = con1.readInt();
  162. int s_DE = con1.readInt();
  163. if ((t_E + output.s_CD) % n != index)
  164. System.err.println(j + ": PIRCOT test failed 1");
  165. else if (s_CE != output.s_CE)
  166. System.err.println(j + ": PIRCOT test failed 2");
  167. else if ((s_DE + output.t_C) % n != index)
  168. System.err.println(j + ": PIRCOT test failed 3");
  169. else
  170. System.out.println(j + ": PIRCOT first half test passed");
  171. int t_D = con2.readInt();
  172. s_DE = con2.readInt();
  173. int s_CD = con2.readInt();
  174. if ((t_D + output.s_CE) % n != index)
  175. System.err.println(j + ": PIRCOT test failed 4");
  176. else if (s_CD != output.s_CD)
  177. System.err.println(j + ": PIRCOT test failed 5");
  178. else if ((s_DE + output.t_C) % n != index)
  179. System.err.println(j + ": PIRCOT test failed 6");
  180. else
  181. System.out.println(j + ": PIRCOT all test passed");
  182. } else {
  183. throw new NoSuchPartyException(party + "");
  184. }
  185. }
  186. }
  187. // for testing correctness
  188. @Override
  189. public void run(Party party, Metadata md, Forest forest) {
  190. }
  191. }