PermuteIndex.java 4.5 KB

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