PermuteIndex.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package protocols;
  2. import java.math.BigInteger;
  3. import communication.Communication;
  4. import crypto.Crypto;
  5. import exceptions.NoSuchPartyException;
  6. import measure.Timer;
  7. import oram.Forest;
  8. import oram.Metadata;
  9. import util.Util;
  10. public class PermuteIndex extends Protocol {
  11. public PermuteIndex(Communication con1, Communication con2) {
  12. super(con1, con2);
  13. }
  14. public void runE() {
  15. }
  16. public int[] runD(PreData predata, boolean firstTree, int[] ti, Timer timer) {
  17. if (firstTree)
  18. return null;
  19. BigInteger[] ti_p = new BigInteger[ti.length];
  20. for (int i = 0; i < ti.length; i++)
  21. ti_p[i] = BigInteger.valueOf(ti[i]);
  22. BigInteger[] z = Util.xor(ti_p, predata.pi_p);
  23. con2.write(z);
  24. BigInteger[] g = con2.readObject();
  25. ti_p = Util.xor(predata.pi_a, g);
  26. int[] ti_pp = new int[ti.length];
  27. for (int i = 0; i < ti.length; i++)
  28. ti_pp[i] = ti_p[i].intValue();
  29. return ti_pp;
  30. }
  31. public void runC(PreData predata, boolean firstTree, Timer timer) {
  32. if (firstTree)
  33. return;
  34. BigInteger[] z = con2.readObject();
  35. z = Util.xor(z, predata.pi_r);
  36. z = Util.permute(z, predata.evict_pi);
  37. BigInteger[] g = Util.xor(predata.evict_rho, z);
  38. con2.write(g);
  39. }
  40. // for testing correctness
  41. @Override
  42. public void run(Party party, Metadata md, Forest forest) {
  43. Timer timer = new Timer();
  44. for (int i = 0; i < 10; i++) {
  45. System.out.println("i=" + i);
  46. PreData predata = new PreData();
  47. PrePermuteIndex prepermuteindex = new PrePermuteIndex(con1, con2);
  48. if (party == Party.Eddie) {
  49. int d = Crypto.sr.nextInt(5) + 5;
  50. int w = Crypto.sr.nextInt(5) + 5;
  51. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  52. int[] ti = new int[d];
  53. predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
  54. predata.evict_rho = new BigInteger[d];
  55. for (int j = 0; j < d; j++) {
  56. ti[j] = Crypto.sr.nextInt(w + 1);
  57. predata.evict_rho[j] = new BigInteger(logW, Crypto.sr);
  58. }
  59. con1.write(predata.evict_pi);
  60. con1.write(predata.evict_rho);
  61. con1.write(ti);
  62. con2.write(predata.evict_pi);
  63. con2.write(predata.evict_rho);
  64. prepermuteindex.runE(predata, d, w, timer);
  65. runE();
  66. ti = Util.permute(ti, predata.evict_pi);
  67. for (int j = 0; j < d; j++) {
  68. ti[j] = predata.evict_rho[j].intValue() ^ ti[j];
  69. System.out.print(ti[j] + " ");
  70. }
  71. System.out.println();
  72. } else if (party == Party.Debbie) {
  73. predata.evict_pi = con1.readObject();
  74. predata.evict_rho = con1.readObject();
  75. int[] ti = con1.readObject();
  76. prepermuteindex.runD(predata, timer);
  77. int[] ti_pp = runD(predata, false, ti, timer);
  78. for (int j = 0; j < ti.length; j++) {
  79. System.out.print(ti_pp[j] + " ");
  80. }
  81. System.out.println();
  82. } else if (party == Party.Charlie) {
  83. predata.evict_pi = con1.readObject();
  84. predata.evict_rho = con1.readObject();
  85. prepermuteindex.runC(predata, timer);
  86. runC(predata, false, timer);
  87. } else {
  88. throw new NoSuchPartyException(party + "");
  89. }
  90. }
  91. // timer.print();
  92. }
  93. }