PermuteTarget.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package protocols;
  2. import java.math.BigInteger;
  3. import com.oblivm.backend.gc.GCSignal;
  4. import communication.Communication;
  5. import crypto.Crypto;
  6. import exceptions.NoSuchPartyException;
  7. import gc.GCUtil;
  8. import oram.Forest;
  9. import oram.Metadata;
  10. import protocols.struct.Party;
  11. import util.M;
  12. import util.Util;
  13. public class PermuteTarget extends Protocol {
  14. public PermuteTarget(Communication con1, Communication con2) {
  15. super(con1, con2);
  16. }
  17. public void runE(int d, int[] evict_pi, GCSignal[][][] evict_targetOutKeyPairs) {
  18. timer.start(M.offline_comp);
  19. // PermuteTargetI
  20. int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
  21. byte[][][] keyT = new byte[d][d][];
  22. byte[][][] targetT = new byte[d][d][];
  23. byte[][][] maskT = new byte[d][d][];
  24. for (int i = 0; i < d; i++) {
  25. for (int j = 0; j < d; j++) {
  26. GCSignal[] keys = GCUtil.revSelectKeys(evict_targetOutKeyPairs[i], BigInteger.valueOf(j).toByteArray());
  27. keyT[i][j] = GCUtil.hashAll(keys);
  28. maskT[i][j] = Util.nextBytes((logD + 7) / 8, Crypto.sr);
  29. targetT[i][j] = Util.xor(Util.padArray(BigInteger.valueOf(evict_pi[j]).toByteArray(), (logD + 7) / 8),
  30. maskT[i][j]);
  31. }
  32. int[] randPerm = Util.randomPermutation(d, Crypto.sr);
  33. keyT[i] = Util.permute(keyT[i], randPerm);
  34. maskT[i] = Util.permute(maskT[i], randPerm);
  35. targetT[i] = Util.permute(targetT[i], randPerm);
  36. }
  37. timer.start(M.offline_write);
  38. con1.write(offline_band, keyT);
  39. con1.write(offline_band, targetT);
  40. con2.write(offline_band, maskT);
  41. timer.stop(M.offline_write);
  42. // PermuteTargetII
  43. byte[][] p = new byte[d][(logD + 7) / 8];
  44. byte[][] r = new byte[d][(logD + 7) / 8];
  45. byte[][] a = new byte[d][];
  46. for (int i = 0; i < d; i++) {
  47. Crypto.sr_DE.nextBytes(p[i]);
  48. Crypto.sr_CE.nextBytes(r[i]);
  49. a[i] = Util.xor(p[i], r[i]);
  50. }
  51. a = Util.permute(a, evict_pi);
  52. timer.start(M.offline_write);
  53. con1.write(offline_band, a);
  54. timer.stop(M.offline_write);
  55. timer.stop(M.offline_comp);
  56. }
  57. public int[] runD(boolean firstTree, GCSignal[][] targetOutKeys) {
  58. if (firstTree)
  59. return null;
  60. timer.start(M.offline_comp);
  61. int d = targetOutKeys.length;
  62. int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
  63. timer.start(M.offline_read);
  64. // PermuteTargetI
  65. byte[][][] keyT = con1.readTripleByteArrayAndDec();
  66. byte[][][] targetT = con1.readTripleByteArrayAndDec();
  67. // PermuteTargetII
  68. byte[][] a = con1.readDoubleByteArrayAndDec();
  69. timer.stop(M.offline_read);
  70. byte[][] p = new byte[d][(logD + 7) / 8];
  71. for (int i = 0; i < d; i++) {
  72. Crypto.sr_DE.nextBytes(p[i]);
  73. }
  74. timer.stop(M.offline_comp);
  75. //////////////////////////////////////////////////////////////
  76. timer.start(M.online_comp);
  77. // PermuteTargetI
  78. int I[] = new int[d];
  79. byte[][] target = new byte[d][];
  80. for (int i = 0; i < d; i++) {
  81. byte[] hashKeys = GCUtil.hashAll(targetOutKeys[i]);
  82. for (int j = 0; j < d; j++) {
  83. if (Util.equal(hashKeys, keyT[i][j])) {
  84. I[i] = j;
  85. target[i] = targetT[i][j];
  86. break;
  87. }
  88. }
  89. }
  90. // PermuteTargetII
  91. byte[][] z = Util.xor(target, p);
  92. timer.start(M.online_write);
  93. con2.write(online_band, z);
  94. con2.write(online_band, I);
  95. timer.stop(M.online_write);
  96. timer.start(M.online_read);
  97. byte[][] g = con2.readDoubleByteArrayAndDec();
  98. timer.stop(M.online_read);
  99. target = Util.xor(a, g);
  100. int[] target_pp = new int[d];
  101. for (int i = 0; i < d; i++)
  102. target_pp[i] = Util.getSubBits(new BigInteger(target[i]), logD, 0).intValue();
  103. timer.stop(M.online_comp);
  104. return target_pp;
  105. }
  106. public void runC(boolean firstTree, int d, int[] evict_pi) {
  107. if (firstTree)
  108. return;
  109. timer.start(M.offline_comp);
  110. int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
  111. timer.start(M.offline_read);
  112. // PermuteTargetI
  113. byte[][][] maskT = con1.readTripleByteArrayAndDec();
  114. timer.stop(M.offline_read);
  115. // PermuteTargetII
  116. byte[][] r = new byte[d][(logD + 7) / 8];
  117. for (int i = 0; i < d; i++) {
  118. Crypto.sr_CE.nextBytes(r[i]);
  119. }
  120. timer.stop(M.offline_comp);
  121. //////////////////////////////////////////////////////////////
  122. timer.start(M.online_comp);
  123. // PermuteTargetII
  124. timer.start(M.online_read);
  125. byte[][] z = con2.readDoubleByteArrayAndDec();
  126. int[] I = con2.readIntArrayAndDec();
  127. timer.stop(M.online_read);
  128. byte[][] mk = new byte[z.length][];
  129. for (int i = 0; i < mk.length; i++) {
  130. mk[i] = Util.xor(maskT[i][I[i]], z[i]);
  131. mk[i] = Util.xor(r[i], mk[i]);
  132. }
  133. byte[][] g = Util.permute(mk, evict_pi);
  134. timer.start(M.online_write);
  135. con2.write(online_band, g);
  136. timer.stop(M.online_write);
  137. timer.stop(M.online_comp);
  138. }
  139. @Override
  140. public void run(Party party, Metadata md, Forest[] forest) {
  141. for (int i = 0; i < 100; i++) {
  142. System.out.println("i=" + i);
  143. if (party == Party.Eddie) {
  144. int d = Crypto.sr.nextInt(20) + 5;
  145. int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
  146. int[] target = Util.randomPermutation(d, Crypto.sr);
  147. int[] evict_pi = Util.randomPermutation(d, Crypto.sr);
  148. GCSignal[][][] evict_targetOutKeyPairs = new GCSignal[d][][];
  149. GCSignal[][] targetOutKeys = new GCSignal[d][];
  150. for (int j = 0; j < d; j++) {
  151. evict_targetOutKeyPairs[j] = GCUtil.genKeyPairs(logD);
  152. targetOutKeys[j] = GCUtil.revSelectKeys(evict_targetOutKeyPairs[j],
  153. BigInteger.valueOf(target[j]).toByteArray());
  154. }
  155. con1.write(targetOutKeys);
  156. con2.write(d);
  157. con2.write(evict_pi);
  158. runE(d, evict_pi, evict_targetOutKeyPairs);
  159. int[] target_pp = con1.readIntArray();
  160. int[] pi_ivs = Util.inversePermutation(evict_pi);
  161. int[] piTargetPiIvs = new int[d];
  162. int j = 0;
  163. for (; j < d; j++) {
  164. piTargetPiIvs[j] = evict_pi[target[pi_ivs[j]]];
  165. if (piTargetPiIvs[j] != target_pp[j]) {
  166. System.err.println("PermuteTarget test failed");
  167. break;
  168. }
  169. }
  170. if (j == d)
  171. System.out.println("PermuteTarget test passed");
  172. } else if (party == Party.Debbie) {
  173. GCSignal[][] targetOutKeys = con1.readDoubleGCSignalArray();
  174. int[] target_pp = runD(false, targetOutKeys);
  175. con1.write(target_pp);
  176. } else if (party == Party.Charlie) {
  177. int d = con1.readInt();
  178. int[] evict_pi = con1.readIntArray();
  179. runC(false, d, evict_pi);
  180. } else {
  181. throw new NoSuchPartyException(party + "");
  182. }
  183. }
  184. }
  185. @Override
  186. public void run(Party party, Metadata md, Forest forest) {
  187. }
  188. }