PermuteTarget.java 6.3 KB

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