SSXOT.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package protocols;
  2. import java.math.BigInteger;
  3. import communication.Communication;
  4. import crypto.Crypto;
  5. import exceptions.NoSuchPartyException;
  6. import oram.Metadata;
  7. import oram.Tuple;
  8. import protocols.precomputation.PreSSXOT;
  9. import protocols.struct.Party;
  10. import protocols.struct.PreData;
  11. import util.M;
  12. import util.P;
  13. import util.Timer;
  14. import util.Util;
  15. public class SSXOT extends Protocol {
  16. private int id;
  17. private int pid;
  18. public SSXOT(Communication con1, Communication con2) {
  19. super(con1, con2);
  20. this.id = 0;
  21. pid = id == 0 ? P.URXOT : P.XOT;
  22. }
  23. public SSXOT(Communication con1, Communication con2, int id) {
  24. super(con1, con2);
  25. this.id = id;
  26. pid = id == 0 ? P.URXOT : P.XOT;
  27. }
  28. public Tuple[] runE(PreData predata, Tuple[] m, Timer timer) {
  29. timer.start(pid, M.online_comp);
  30. // step 1
  31. Tuple[] a = new Tuple[m.length];
  32. for (int i = 0; i < m.length; i++)
  33. a[i] = m[predata.ssxot_E_pi[id][i]].xor(predata.ssxot_E_r[id][i]);
  34. timer.start(pid, M.online_write);
  35. con2.write(pid, a);
  36. timer.stop(pid, M.online_write);
  37. timer.start(pid, M.online_read);
  38. a = con2.readTupleArray();
  39. // step 2
  40. int[] j = con1.readIntArray();
  41. Tuple[] p = con1.readTupleArray();
  42. timer.stop(pid, M.online_read);
  43. // step 3
  44. Tuple[] z = new Tuple[j.length];
  45. for (int i = 0; i < j.length; i++)
  46. z[i] = a[j[i]].xor(p[i]);
  47. timer.stop(pid, M.online_comp);
  48. return z;
  49. }
  50. public void runD(PreData predata, int[] index, Timer timer) {
  51. timer.start(pid, M.online_comp);
  52. // step 2
  53. int k = index.length;
  54. int[] E_j = new int[k];
  55. int[] C_j = new int[k];
  56. Tuple[] E_p = new Tuple[k];
  57. Tuple[] C_p = new Tuple[k];
  58. for (int i = 0; i < k; i++) {
  59. E_j[i] = predata.ssxot_E_pi_ivs[id][index[i]];
  60. C_j[i] = predata.ssxot_C_pi_ivs[id][index[i]];
  61. E_p[i] = predata.ssxot_E_r[id][E_j[i]].xor(predata.ssxot_delta[id][i]);
  62. C_p[i] = predata.ssxot_C_r[id][C_j[i]].xor(predata.ssxot_delta[id][i]);
  63. }
  64. timer.start(pid, M.online_write);
  65. con2.write(pid, E_j);
  66. con2.write(pid, E_p);
  67. con1.write(pid, C_j);
  68. con1.write(pid, C_p);
  69. timer.stop(pid, M.online_write);
  70. timer.stop(pid, M.online_comp);
  71. }
  72. public Tuple[] runC(PreData predata, Tuple[] m, Timer timer) {
  73. timer.start(pid, M.online_comp);
  74. // step 1
  75. Tuple[] a = new Tuple[m.length];
  76. for (int i = 0; i < m.length; i++)
  77. a[i] = m[predata.ssxot_C_pi[id][i]].xor(predata.ssxot_C_r[id][i]);
  78. timer.start(pid, M.online_write);
  79. con1.write(pid, a);
  80. timer.stop(pid, M.online_write);
  81. timer.start(pid, M.online_read);
  82. a = con1.readTupleArray();
  83. // step 2
  84. int[] j = con2.readIntArray();
  85. Tuple[] p = con2.readTupleArray();
  86. timer.stop(pid, M.online_read);
  87. // step 3
  88. Tuple[] z = new Tuple[j.length];
  89. for (int i = 0; i < j.length; i++)
  90. z[i] = a[j[i]].xor(p[i]);
  91. timer.stop(pid, M.online_comp);
  92. return z;
  93. }
  94. // for testing correctness
  95. @Override
  96. public void run(protocols.struct.Party party, Metadata md, oram.Forest forest) {
  97. Timer timer = new Timer();
  98. for (int j = 0; j < 100; j++) {
  99. int n = 100;
  100. int k = Crypto.sr.nextInt(50) + 50;
  101. int[] index = Util.randomPermutation(k, Crypto.sr);
  102. int[] tupleParam = new int[] { 1, 2, 3, 4 };
  103. Tuple[] E_m = new Tuple[n];
  104. Tuple[] C_m = new Tuple[n];
  105. for (int i = 0; i < n; i++) {
  106. E_m[i] = new Tuple(tupleParam[0], tupleParam[1], tupleParam[2], tupleParam[3], Crypto.sr);
  107. C_m[i] = new Tuple(tupleParam[0], tupleParam[1], tupleParam[2], tupleParam[3], null);
  108. }
  109. PreData predata = new PreData();
  110. PreSSXOT pressxot = new PreSSXOT(con1, con2, 0);
  111. if (party == Party.Eddie) {
  112. pressxot.runE(predata, timer);
  113. Tuple[] E_out_m = runE(predata, E_m, timer);
  114. con2.write(E_m);
  115. con2.write(E_out_m);
  116. } else if (party == Party.Debbie) {
  117. pressxot.runD(predata, n, k, tupleParam, timer);
  118. runD(predata, index, timer);
  119. con2.write(index);
  120. } else if (party == Party.Charlie) {
  121. pressxot.runC(predata, timer);
  122. Tuple[] C_out_m = runC(predata, C_m, timer);
  123. index = con2.readIntArray();
  124. E_m = con1.readTupleArray();
  125. Tuple[] E_out_m = con1.readTupleArray();
  126. boolean pass = true;
  127. for (int i = 0; i < index.length; i++) {
  128. int input = new BigInteger(1, E_m[index[i]].getA()).intValue();
  129. int output = new BigInteger(1, Util.xor(E_out_m[i].getA(), C_out_m[i].getA())).intValue();
  130. if (input != output) {
  131. System.err.println("SSXOT test failed");
  132. pass = false;
  133. break;
  134. }
  135. }
  136. if (pass)
  137. System.out.println("SSXOT test passed");
  138. } else {
  139. throw new NoSuchPartyException(party + "");
  140. }
  141. }
  142. // timer.print();
  143. }
  144. }