PermuteIndex.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.precomputation.PrePermuteIndex;
  9. import protocols.struct.Party;
  10. import protocols.struct.PreData;
  11. import util.M;
  12. import util.P;
  13. import util.Timer;
  14. import util.Util;
  15. public class PermuteIndex extends Protocol {
  16. private int pid = P.PI;
  17. public PermuteIndex(Communication con1, Communication con2) {
  18. super(con1, con2);
  19. }
  20. public void runE() {
  21. }
  22. public int[] runD(PreData predata, boolean firstTree, byte[][] ti, int w, Timer timer) {
  23. if (firstTree)
  24. return null;
  25. timer.start(pid, M.online_comp);
  26. byte[][] z = Util.xor(ti, predata.pi_p);
  27. timer.start(pid, M.online_write);
  28. con2.write(pid, z);
  29. timer.stop(pid, M.online_write);
  30. timer.start(pid, M.online_read);
  31. byte[][] g = con2.readDoubleByteArray();
  32. timer.stop(pid, M.online_read);
  33. ti = Util.xor(predata.pi_a, g);
  34. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  35. int[] ti_pp = new int[ti.length];
  36. for (int i = 0; i < ti.length; i++)
  37. ti_pp[i] = Util.getSubBits(new BigInteger(ti[i]), logW, 0).intValue();
  38. timer.stop(pid, M.online_comp);
  39. return ti_pp;
  40. }
  41. public void runC(PreData predata, boolean firstTree, Timer timer) {
  42. if (firstTree)
  43. return;
  44. timer.start(pid, M.online_comp);
  45. timer.start(pid, M.online_read);
  46. byte[][] z = con2.readDoubleByteArray();
  47. timer.stop(pid, M.online_read);
  48. z = Util.xor(z, predata.pi_r);
  49. z = Util.permute(z, predata.evict_pi);
  50. byte[][] g = Util.xor(predata.evict_rho, z);
  51. timer.start(pid, M.online_write);
  52. con2.write(pid, g);
  53. timer.stop(pid, M.online_write);
  54. timer.stop(pid, M.online_comp);
  55. }
  56. // for testing correctness
  57. @Override
  58. public void run(Party party, Metadata md, Forest forest) {
  59. Timer timer = new Timer();
  60. for (int i = 0; i < 100; i++) {
  61. System.out.println("i=" + i);
  62. PreData predata = new PreData();
  63. PrePermuteIndex prepermuteindex = new PrePermuteIndex(con1, con2);
  64. if (party == Party.Eddie) {
  65. int d = Crypto.sr.nextInt(15) + 5;
  66. int w = Crypto.sr.nextInt(15) + 5;
  67. int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
  68. byte[][] ti = new byte[d][];
  69. predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
  70. predata.evict_rho = new byte[d][];
  71. for (int j = 0; j < d; j++) {
  72. ti[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
  73. predata.evict_rho[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
  74. }
  75. con1.write(predata.evict_pi);
  76. con1.write(predata.evict_rho);
  77. con1.write(ti);
  78. con1.write(w);
  79. con2.write(predata.evict_pi);
  80. con2.write(predata.evict_rho);
  81. prepermuteindex.runE(predata, d, w, timer);
  82. runE();
  83. int[] ti_pp = con1.readIntArray();
  84. ti = Util.permute(ti, predata.evict_pi);
  85. int j = 0;
  86. for (; j < d; j++) {
  87. int tmp = Util.getSubBits(new BigInteger(Util.xor(predata.evict_rho[j], ti[j])), logW, 0)
  88. .intValue();
  89. if (tmp != ti_pp[j]) {
  90. System.err.println("PermuteIndex test failed");
  91. break;
  92. }
  93. }
  94. if (j == d)
  95. System.out.println("PermuteIndex test passed");
  96. } else if (party == Party.Debbie) {
  97. predata.evict_pi = con1.readIntArray();
  98. predata.evict_rho = con1.readDoubleByteArray();
  99. byte[][] ti = con1.readDoubleByteArray();
  100. int w = con1.readInt();
  101. prepermuteindex.runD(predata, timer);
  102. int[] ti_pp = runD(predata, false, ti, w, timer);
  103. con1.write(ti_pp);
  104. } else if (party == Party.Charlie) {
  105. predata.evict_pi = con1.readIntArray();
  106. predata.evict_rho = con1.readDoubleByteArray();
  107. prepermuteindex.runC(predata, timer);
  108. runC(predata, false, timer);
  109. } else {
  110. throw new NoSuchPartyException(party + "");
  111. }
  112. }
  113. // timer.print();
  114. }
  115. }