PIRCOT.java 5.5 KB

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