PermuteIndex.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package protocols;
  2. import java.math.BigInteger;
  3. import communication.Communication;
  4. import crypto.Crypto;
  5. import exceptions.NoSuchPartyException;
  6. import oram.Forest;
  7. import oram.Metadata;
  8. import protocols.struct.Party;
  9. import util.M;
  10. import util.Util;
  11. public class PermuteIndex extends Protocol {
  12. public PermuteIndex(Communication con1, Communication con2) {
  13. super(con1, con2);
  14. }
  15. public void runE(int w, int[] evict_pi) {
  16. timer.start(M.offline_comp);
  17. int d = evict_pi.length;
  18. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  19. byte[][] p = new byte[d][(logW + 7) / 8];
  20. byte[][] r = new byte[d][(logW + 7) / 8];
  21. byte[][] a = new byte[d][];
  22. for (int i = 0; i < d; i++) {
  23. Crypto.sr_DE.nextBytes(p[i]);
  24. Crypto.sr_CE.nextBytes(r[i]);
  25. a[i] = Util.xor(p[i], r[i]);
  26. }
  27. a = Util.permute(a, evict_pi);
  28. timer.start(M.offline_write);
  29. con1.write(offline_band, a);
  30. timer.stop(M.offline_write);
  31. timer.stop(M.offline_comp);
  32. }
  33. public int[] runD(boolean firstTree, byte[][] ti, int w) {
  34. if (firstTree)
  35. return null;
  36. timer.start(M.offline_comp);
  37. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  38. timer.start(M.offline_read);
  39. byte[][] a = con1.readDoubleByteArrayAndDec();
  40. timer.stop(M.offline_read);
  41. int d = a.length;
  42. byte[][] p = new byte[d][(logW + 7) / 8];
  43. for (int i = 0; i < d; i++) {
  44. Crypto.sr_DE.nextBytes(p[i]);
  45. }
  46. timer.stop(M.offline_comp);
  47. ////////////////////////////////////////////////////////////
  48. timer.start(M.online_comp);
  49. byte[][] z = Util.xor(ti, p);
  50. timer.start(M.online_write);
  51. con2.write(online_band, z);
  52. timer.stop(M.online_write);
  53. timer.start(M.online_read);
  54. byte[][] g = con2.readDoubleByteArrayAndDec();
  55. timer.stop(M.online_read);
  56. ti = Util.xor(a, g);
  57. int[] ti_pp = new int[ti.length];
  58. for (int i = 0; i < ti.length; i++)
  59. ti_pp[i] = Util.getSubBits(new BigInteger(ti[i]), logW, 0).intValue();
  60. timer.stop(M.online_comp);
  61. return ti_pp;
  62. }
  63. public void runC(boolean firstTree, int w, int[] evict_pi, byte[][] evict_rho) {
  64. if (firstTree)
  65. return;
  66. timer.start(M.offline_comp);
  67. int d = evict_pi.length;
  68. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  69. byte[][] r = new byte[d][(logW + 7) / 8];
  70. for (int i = 0; i < d; i++) {
  71. Crypto.sr_CE.nextBytes(r[i]);
  72. }
  73. timer.stop(M.offline_comp);
  74. ////////////////////////////////////////////////////////////////
  75. timer.start(M.online_comp);
  76. timer.start(M.online_read);
  77. byte[][] z = con2.readDoubleByteArrayAndDec();
  78. timer.stop(M.online_read);
  79. z = Util.xor(z, r);
  80. z = Util.permute(z, evict_pi);
  81. byte[][] g = Util.xor(evict_rho, z);
  82. timer.start(M.online_write);
  83. con2.write(online_band, g);
  84. timer.stop(M.online_write);
  85. timer.stop(M.online_comp);
  86. }
  87. @Override
  88. public void run(Party party, Metadata md, Forest[] forest) {
  89. for (int i = 0; i < 100; i++) {
  90. System.out.println("i=" + i);
  91. if (party == Party.Eddie) {
  92. int d = Crypto.sr.nextInt(15) + 5;
  93. int w = Crypto.sr.nextInt(15) + 5;
  94. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  95. byte[][] ti = new byte[d][];
  96. int[] evict_pi = Util.randomPermutation(d, Crypto.sr);
  97. byte[][] evict_rho = new byte[d][];
  98. for (int j = 0; j < d; j++) {
  99. ti[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
  100. evict_rho[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
  101. }
  102. con1.write(ti);
  103. con1.write(w);
  104. con2.write(w);
  105. con2.write(evict_pi);
  106. con2.write(evict_rho);
  107. runE(w, evict_pi);
  108. int[] ti_pp = con1.readIntArray();
  109. ti = Util.permute(ti, evict_pi);
  110. int j = 0;
  111. for (; j < d; j++) {
  112. int tmp = Util.getSubBits(new BigInteger(Util.xor(evict_rho[j], ti[j])), logW, 0).intValue();
  113. if (tmp != ti_pp[j]) {
  114. System.err.println("PermuteIndex test failed");
  115. break;
  116. }
  117. }
  118. if (j == d)
  119. System.out.println("PermuteIndex test passed");
  120. } else if (party == Party.Debbie) {
  121. byte[][] ti = con1.readDoubleByteArray();
  122. int w = con1.readInt();
  123. int[] ti_pp = runD(false, ti, w);
  124. con1.write(ti_pp);
  125. } else if (party == Party.Charlie) {
  126. int w = con1.readInt();
  127. int[] evict_pi = con1.readIntArray();
  128. byte[][] evict_rho = con1.readDoubleByteArray();
  129. runC(false, w, evict_pi, evict_rho);
  130. } else {
  131. throw new NoSuchPartyException(party + "");
  132. }
  133. }
  134. }
  135. @Override
  136. public void run(Party party, Metadata md, Forest forest) {
  137. }
  138. }