PermuteTarget.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.precomputation.PrePermuteTarget;
  11. import protocols.struct.Party;
  12. import protocols.struct.PreData;
  13. import util.M;
  14. import util.P;
  15. import util.Timer;
  16. import util.Util;
  17. public class PermuteTarget extends Protocol {
  18. private int pid = P.PT;
  19. public PermuteTarget(Communication con1, Communication con2) {
  20. super(con1, con2);
  21. }
  22. public void runE() {
  23. }
  24. public int[] runD(PreData predata, boolean firstTree, GCSignal[][] targetOutKeys, Timer timer) {
  25. if (firstTree)
  26. return null;
  27. timer.start(pid, M.online_comp);
  28. // PermuteTargetI
  29. int d = targetOutKeys.length;
  30. int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
  31. int I[] = new int[d];
  32. byte[][] target = new byte[d][];
  33. for (int i = 0; i < d; i++) {
  34. byte[] hashKeys = GCUtil.hashAll(targetOutKeys[i]);
  35. for (int j = 0; j < d; j++) {
  36. if (Util.equal(hashKeys, predata.pt_keyT[i][j])) {
  37. I[i] = j;
  38. target[i] = predata.pt_targetT[i][j];
  39. break;
  40. }
  41. }
  42. }
  43. // PermuteTargetII
  44. byte[][] z = Util.xor(target, predata.pt_p);
  45. timer.start(pid, M.online_write);
  46. con2.write(pid, z);
  47. con2.write(pid, I);
  48. timer.stop(pid, M.online_write);
  49. timer.start(pid, M.online_read);
  50. byte[][] g = con2.readDoubleByteArray(pid);
  51. timer.stop(pid, M.online_read);
  52. target = Util.xor(predata.pt_a, g);
  53. int[] target_pp = new int[d];
  54. for (int i = 0; i < d; i++)
  55. target_pp[i] = Util.getSubBits(new BigInteger(target[i]), logD, 0).intValue();
  56. timer.stop(pid, M.online_comp);
  57. return target_pp;
  58. }
  59. public void runC(PreData predata, boolean firstTree, Timer timer) {
  60. if (firstTree)
  61. return;
  62. timer.start(pid, M.online_comp);
  63. // PermuteTargetII
  64. timer.start(pid, M.online_read);
  65. byte[][] z = con2.readDoubleByteArray(pid);
  66. int[] I = con2.readIntArray(pid);
  67. timer.stop(pid, M.online_read);
  68. byte[][] mk = new byte[z.length][];
  69. for (int i = 0; i < mk.length; i++) {
  70. mk[i] = Util.xor(predata.pt_maskT[i][I[i]], z[i]);
  71. mk[i] = Util.xor(predata.pt_r[i], mk[i]);
  72. }
  73. byte[][] g = Util.permute(mk, predata.evict_pi);
  74. timer.start(pid, M.online_write);
  75. con2.write(pid, g);
  76. timer.stop(pid, M.online_write);
  77. timer.stop(pid, M.online_comp);
  78. }
  79. // for testing correctness
  80. @Override
  81. public void run(Party party, Metadata md, Forest forest) {
  82. Timer timer = new Timer();
  83. for (int i = 0; i < 100; i++) {
  84. System.out.println("i=" + i);
  85. PreData predata = new PreData();
  86. PrePermuteTarget prepermutetarget = new PrePermuteTarget(con1, con2);
  87. if (party == Party.Eddie) {
  88. int d = Crypto.sr.nextInt(20) + 5;
  89. int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
  90. int[] target = Util.randomPermutation(d, Crypto.sr);
  91. predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
  92. predata.evict_targetOutKeyPairs = new GCSignal[d][][];
  93. GCSignal[][] targetOutKeys = new GCSignal[d][];
  94. for (int j = 0; j < d; j++) {
  95. predata.evict_targetOutKeyPairs[j] = GCUtil.genKeyPairs(logD);
  96. targetOutKeys[j] = GCUtil.revSelectKeys(predata.evict_targetOutKeyPairs[j],
  97. BigInteger.valueOf(target[j]).toByteArray());
  98. }
  99. con1.write(d);
  100. con1.write(predata.evict_pi);
  101. con1.write(predata.evict_targetOutKeyPairs);
  102. con1.write(targetOutKeys);
  103. con2.write(predata.evict_pi);
  104. prepermutetarget.runE(predata, d, timer);
  105. runE();
  106. int[] target_pp = con1.readIntArray();
  107. int[] pi_ivs = Util.inversePermutation(predata.evict_pi);
  108. int[] piTargetPiIvs = new int[d];
  109. int j = 0;
  110. for (; j < d; j++) {
  111. piTargetPiIvs[j] = predata.evict_pi[target[pi_ivs[j]]];
  112. if (piTargetPiIvs[j] != target_pp[j]) {
  113. System.err.println("PermuteTarget test failed");
  114. break;
  115. }
  116. }
  117. if (j == d)
  118. System.out.println("PermuteTarget test passed");
  119. } else if (party == Party.Debbie) {
  120. int d = con1.readInt();
  121. predata.evict_pi = con1.readIntArray();
  122. predata.evict_targetOutKeyPairs = con1.readTripleGCSignalArray();
  123. GCSignal[][] targetOutKeys = con1.readDoubleGCSignalArray();
  124. prepermutetarget.runD(predata, d, timer);
  125. int[] target_pp = runD(predata, false, targetOutKeys, timer);
  126. con1.write(target_pp);
  127. } else if (party == Party.Charlie) {
  128. predata.evict_pi = con1.readIntArray();
  129. prepermutetarget.runC(predata, timer);
  130. runC(predata, false, timer);
  131. } else {
  132. throw new NoSuchPartyException(party + "");
  133. }
  134. }
  135. // timer.print();
  136. }
  137. }