PIRCOT.java 5.4 KB

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