PermuteTarget.java 6.4 KB

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