PermuteTarget.java 6.4 KB

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