PermuteIndex.java 4.5 KB

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