PermuteTarget.java 3.9 KB

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