PIRCOT.java 6.7 KB

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