PermuteIndex.java 4.4 KB

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