SSXOT.java 4.4 KB

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