SSCOT.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package protocols;
  2. import java.math.BigInteger;
  3. import org.apache.commons.lang3.tuple.Pair;
  4. import communication.Communication;
  5. import oram.Forest;
  6. import oram.Metadata;
  7. public class SSCOT extends Protocol {
  8. public SSCOT(Communication con1, Communication con2) {
  9. super(con1, con2);
  10. }
  11. public Pair<Integer, BigInteger> executeCharlie(Communication D,
  12. Communication E, int i, int N, int l, int l_p) {
  13. // protocol
  14. // step 1
  15. timing.stopwatch[PID.sscot][TID.online_read].start();
  16. byte[] msg_ev = E.read();
  17. // step 2
  18. byte[] msg_pw = D.read();
  19. timing.stopwatch[PID.sscot][TID.online_read].stop();
  20. // step 3
  21. timing.stopwatch[PID.sscot][TID.online].start();
  22. byte[][] e = new byte[N][];
  23. byte[][] v = new byte[N][];
  24. byte[][] p = new byte[N][];
  25. byte[][] w = new byte[N][];
  26. PRG G = new PRG(l);
  27. int gBytes = (l + 7) / 8;
  28. for (int t = 0; t < N; t++) {
  29. e[t] = Arrays.copyOfRange(msg_ev, t * gBytes, (t + 1) * gBytes);
  30. v[t] = Arrays.copyOfRange(msg_ev, N * gBytes + t * SR.kBytes, N
  31. * gBytes + (t + 1) * SR.kBytes);
  32. p[t] = Arrays.copyOfRange(msg_pw, t * SR.kBytes, (t + 1)
  33. * SR.kBytes);
  34. w[t] = Arrays.copyOfRange(msg_pw, (N + t) * SR.kBytes, (N + t + 1)
  35. * SR.kBytes);
  36. if (new BigInteger(1, v[t]).compareTo(new BigInteger(1, w[t])) == 0) {
  37. //BigInteger m_t = new BigInteger(1, e[t]).xor(new BigInteger(1, G.compute(p[t])));
  38. timing.stopwatch[PID.aes_prg][TID.online].start();
  39. byte[] tmp = G.compute(p[t]);
  40. timing.stopwatch[PID.aes_prg][TID.online].stop();
  41. BigInteger m_t = new BigInteger(1, e[t]).xor(new BigInteger(1, tmp));
  42. timing.stopwatch[PID.sscot][TID.online].stop();
  43. return Pair.of(t, m_t);
  44. }
  45. }
  46. timing.stopwatch[PID.sscot][TID.online].stop();
  47. // error
  48. return null;
  49. }
  50. public void executeDebbie(Communication C, Communication E, int i, int N,
  51. int l, int l_p, BigInteger[] b) {
  52. // protocol
  53. // step 2
  54. timing.stopwatch[PID.sscot][TID.online].start();
  55. int diffBits = SR.kBits - l_p;
  56. BigInteger[] y = new BigInteger[N];
  57. byte[][][] pw = new byte[2][N][];
  58. byte[] msg_pw = new byte[SR.kBytes * N * 2];
  59. PRF F_k = new PRF(SR.kBits);
  60. PRF F_k_p = new PRF(SR.kBits);
  61. F_k.init(PreData.sscot_k[i]);
  62. F_k_p.init(PreData.sscot_k_p[i]);
  63. for (int t = 0; t < N; t++) {
  64. y[t] = PreData.sscot_r[i][t].xor(b[t].shiftLeft(diffBits));
  65. timing.stopwatch[PID.aes_prf][TID.online].start();
  66. pw[0][t] = F_k.compute(y[t].toByteArray());
  67. pw[1][t] = F_k_p.compute(y[t].toByteArray());
  68. timing.stopwatch[PID.aes_prf][TID.online].stop();
  69. System.arraycopy(pw[0][t], 0, msg_pw, t * SR.kBytes, SR.kBytes);
  70. System.arraycopy(pw[1][t], 0, msg_pw, (N + t) * SR.kBytes,
  71. SR.kBytes);
  72. }
  73. timing.stopwatch[PID.sscot][TID.online].stop();
  74. timing.stopwatch[PID.sscot][TID.online_write].start();
  75. C.write(msg_pw, PID.sscot);
  76. timing.stopwatch[PID.sscot][TID.online_write].stop();
  77. }
  78. public void executeEddie(Communication C, Communication D, int i, int N,
  79. int l, int l_p, BigInteger[] m, BigInteger[] a) {
  80. // protocol
  81. // step 1
  82. timing.stopwatch[PID.sscot][TID.online].start();
  83. int gBytes = (l + 7) / 8;
  84. int diffBits = SR.kBits - l_p;
  85. BigInteger[] x = new BigInteger[N];
  86. byte[][][] ev = new byte[2][N][];
  87. byte[] msg_ev = new byte[(SR.kBytes + gBytes) * N];
  88. PRF F_k = new PRF(SR.kBits);
  89. PRF F_k_p = new PRF(SR.kBits);
  90. PRG G = new PRG(l);
  91. F_k.init(PreData.sscot_k[i]);
  92. F_k_p.init(PreData.sscot_k_p[i]);
  93. for (int t = 0; t < N; t++) {
  94. x[t] = PreData.sscot_r[i][t].xor(a[t].shiftLeft(diffBits));
  95. //ev[0][t] = new BigInteger(1, G.compute(F_k.compute(x[t].toByteArray()))).xor(m[t]).toByteArray();
  96. timing.stopwatch[PID.aes_prf][TID.online].start();
  97. ev[1][t] = F_k_p.compute(x[t].toByteArray());
  98. byte[] tmp = F_k.compute(x[t].toByteArray());
  99. timing.stopwatch[PID.aes_prf][TID.online].stop();
  100. timing.stopwatch[PID.aes_prg][TID.online].start();
  101. tmp = G.compute(tmp);
  102. timing.stopwatch[PID.aes_prg][TID.online].stop();
  103. ev[0][t] = new BigInteger(1, tmp).xor(m[t]).toByteArray();
  104. if (ev[0][t].length < gBytes)
  105. System.arraycopy(ev[0][t], 0, msg_ev, (t + 1) * gBytes
  106. - ev[0][t].length, ev[0][t].length);
  107. else
  108. System.arraycopy(ev[0][t], ev[0][t].length - gBytes, msg_ev, t
  109. * gBytes, gBytes);
  110. System.arraycopy(ev[1][t], 0, msg_ev, N * gBytes + t * SR.kBytes,
  111. SR.kBytes);
  112. }
  113. timing.stopwatch[PID.sscot][TID.online].stop();
  114. timing.stopwatch[PID.sscot][TID.online_write].start();
  115. C.write(msg_ev, PID.sscot);
  116. timing.stopwatch[PID.sscot][TID.online_write].stop();
  117. }
  118. // for testing correctness
  119. @Override
  120. public void run(Party party, Forest forest) throws ForestException {
  121. System.out.println("##### Testing SSCOT #####");
  122. timing = new Timing();
  123. for (int ii = 0; ii < 20; ii++) {
  124. if (party == Party.Eddie) {
  125. int levels = ForestMetadata.getLevels();
  126. int i = SR.rand.nextInt(levels - 1) + 1;
  127. int N = SR.rand.nextInt(50) + 150; // 150-199
  128. int l = ForestMetadata.getTupleBits(i);
  129. int l_p = 1 + ForestMetadata.getNBits(i);
  130. int t = SR.rand.nextInt(N);
  131. PreData.sscot_k = new byte[levels][16];
  132. PreData.sscot_k_p = new byte[levels][16];
  133. PreData.sscot_r = new BigInteger[levels][N];
  134. BigInteger[] a = new BigInteger[N];
  135. BigInteger[] b = new BigInteger[N];
  136. BigInteger[] m = new BigInteger[N];
  137. SR.rand.nextBytes(PreData.sscot_k[i]);
  138. SR.rand.nextBytes(PreData.sscot_k_p[i]);
  139. for (int o = 0; o < N; o++) {
  140. PreData.sscot_r[i][o] = new BigInteger(SR.kBits, SR.rand);
  141. a[o] = new BigInteger(l_p, SR.rand);
  142. b[o] = new BigInteger(l_p, SR.rand);
  143. while (a[o].compareTo(b[o]) == 0)
  144. b[o] = new BigInteger(l_p, SR.rand);
  145. m[o] = new BigInteger(l, SR.rand);
  146. }
  147. a[t] = b[t];
  148. con1.write(i);
  149. con1.write(N);
  150. con1.write(l);
  151. con1.write(l_p);
  152. con2.write(i);
  153. con2.write(N);
  154. con2.write(l);
  155. con2.write(l_p);
  156. con2.write(PreData.sscot_k[i]);
  157. con2.write(PreData.sscot_k_p[i]);
  158. con2.write(PreData.sscot_r[i]);
  159. con2.write(b);
  160. executeEddie(con1, con2, i, N, l, l_p, m, a);
  161. int output_t = con1.readInt();
  162. BigInteger m_t = con1.readBigInteger();
  163. System.out.println("i = " + i);
  164. if (t == output_t && m[t].compareTo(m_t) == 0) {
  165. System.out.println("SSCOT test passed:");
  166. } else {
  167. System.out.println("SSCOT test failed:");
  168. }
  169. System.out.println("t=" + t + ", output_t=" + output_t);
  170. System.out.println("m[t]=" + m[t] + ", m_t=" + m_t);
  171. } else if (party == Party.Debbie) {
  172. int i = con2.readInt();
  173. int N = con2.readInt();
  174. int l = con2.readInt();
  175. int l_p = con2.readInt();
  176. int levels = ForestMetadata.getLevels();
  177. PreData.sscot_k = new byte[levels][];
  178. PreData.sscot_k_p = new byte[levels][];
  179. PreData.sscot_r = new BigInteger[levels][];
  180. PreData.sscot_k[i] = con2.read();
  181. PreData.sscot_k_p[i] = con2.read();
  182. PreData.sscot_r[i] = con2.readBigIntegerArray();
  183. BigInteger[] b = con2.readBigIntegerArray();
  184. executeDebbie(con1, con2, i, N, l, l_p, b);
  185. } else if (party == Party.Charlie) {
  186. int i = con2.readInt();
  187. int N = con2.readInt();
  188. int l = con2.readInt();
  189. int l_p = con2.readInt();
  190. Pair<Integer, BigInteger> output = executeCharlie(con1, con2,
  191. i, N, l, l_p);
  192. int t = output.getLeft();
  193. BigInteger m_t = output.getRight();
  194. con2.write(t);
  195. con2.write(m_t);
  196. }
  197. }
  198. System.out.println("##### Testing SSCOT Finished #####");
  199. }
  200. @Override
  201. public void run(protocols.Party party, Metadata md, oram.Forest forest) {
  202. // TODO Auto-generated method stub
  203. }
  204. @Override
  205. public void run(Party party, Metadata md, Forest forest) {
  206. // TODO Auto-generated method stub
  207. }
  208. }