Reshuffle.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package protocols;
  2. import java.math.BigInteger;
  3. import communication.Communication;
  4. import crypto.Crypto;
  5. import exceptions.AccessException;
  6. import exceptions.NoSuchPartyException;
  7. import measure.M;
  8. import measure.P;
  9. import measure.Timer;
  10. import oram.Forest;
  11. import oram.Metadata;
  12. import oram.Tree;
  13. import oram.Tuple;
  14. import util.Util;
  15. public class Reshuffle extends Protocol {
  16. public Reshuffle(Communication con1, Communication con2) {
  17. super(con1, con2);
  18. }
  19. public Tuple[] runE(PreData predata, Tuple[] path, boolean firstTree, Timer timer) {
  20. if (firstTree)
  21. return path;
  22. timer.start(P.RSF, M.online_comp);
  23. // step 1
  24. timer.start(P.RSF, M.online_read);
  25. Tuple[] z = con2.readObject();
  26. timer.stop(P.RSF, M.online_read);
  27. // step 2
  28. Tuple[] b = new Tuple[z.length];
  29. for (int i = 0; i < b.length; i++)
  30. b[i] = path[i].xor(z[i]).xor(predata.reshuffle_r[i]);
  31. Tuple[] b_prime = Util.permute(b, predata.reshuffle_pi);
  32. timer.stop(P.RSF, M.online_comp);
  33. return b_prime;
  34. }
  35. public void runD() {
  36. }
  37. public Tuple[] runC(PreData predata, Tuple[] path, boolean firstTree, Timer timer) {
  38. if (firstTree)
  39. return path;
  40. timer.start(P.RSF, M.online_comp);
  41. // step 1
  42. Tuple[] z = new Tuple[path.length];
  43. for (int i = 0; i < z.length; i++)
  44. z[i] = path[i].xor(predata.reshuffle_p[i]);
  45. timer.start(P.RSF, M.online_write);
  46. con1.write(z);
  47. timer.stop(P.RSF, M.online_write);
  48. timer.stop(P.RSF, M.online_comp);
  49. return predata.reshuffle_a_prime;
  50. }
  51. // for testing correctness
  52. @Override
  53. public void run(Party party, Metadata md, Forest forest) {
  54. int records = 5;
  55. int repeat = 5;
  56. int tau = md.getTau();
  57. int numTrees = md.getNumTrees();
  58. long numInsert = md.getNumInsertRecords();
  59. int addrBits = md.getAddrBits();
  60. Timer timer = new Timer();
  61. sanityCheck();
  62. System.out.println();
  63. for (int i = 0; i < records; i++) {
  64. long N = Util.nextLong(numInsert, Crypto.sr);
  65. for (int j = 0; j < repeat; j++) {
  66. System.out.println("Test: " + i + " " + j);
  67. System.out.println("N=" + BigInteger.valueOf(N).toString(2));
  68. byte[] Li = new byte[0];
  69. for (int ti = 0; ti < numTrees; ti++) {
  70. long Ni_value = Util.getSubBits(N, addrBits, addrBits - md.getNBitsOfTree(ti));
  71. long Nip1_pr_value = Util.getSubBits(N, addrBits - md.getNBitsOfTree(ti),
  72. Math.max(addrBits - md.getNBitsOfTree(ti) - tau, 0));
  73. byte[] Ni = Util.longToBytes(Ni_value, md.getNBytesOfTree(ti));
  74. byte[] Nip1_pr = Util.longToBytes(Nip1_pr_value, (tau + 7) / 8);
  75. PreData predata = new PreData();
  76. PreAccess preaccess = new PreAccess(con1, con2);
  77. Access access = new Access(con1, con2);
  78. PreReshuffle prereshuffle = new PreReshuffle(con1, con2);
  79. if (party == Party.Eddie) {
  80. Tree OTi = forest.getTree(ti);
  81. int numTuples = (OTi.getD() - 1) * OTi.getW() + OTi.getStashSize();
  82. preaccess.runE(predata, OTi, numTuples, timer);
  83. byte[] sE_Ni = Util.nextBytes(Ni.length, Crypto.sr);
  84. byte[] sD_Ni = Util.xor(Ni, sE_Ni);
  85. con1.write(sD_Ni);
  86. byte[] sE_Nip1_pr = Util.nextBytes(Nip1_pr.length, Crypto.sr);
  87. byte[] sD_Nip1_pr = Util.xor(Nip1_pr, sE_Nip1_pr);
  88. con1.write(sD_Nip1_pr);
  89. OutAccess outaccess = access.runE(predata, OTi, sE_Ni, sE_Nip1_pr, timer);
  90. if (ti == numTrees - 1)
  91. con2.write(N);
  92. prereshuffle.runE(predata, timer);
  93. Tuple[] E_P_prime = runE(predata, outaccess.E_P, ti == 0, timer);
  94. Tuple[] C_P = con2.readObject();
  95. Tuple[] C_P_prime = con2.readObject();
  96. Tuple[] oldPath = new Tuple[C_P.length];
  97. Tuple[] newPath = new Tuple[C_P.length];
  98. for (int k = 0; k < C_P.length; k++) {
  99. oldPath[k] = outaccess.E_P[k].xor(C_P[k]);
  100. newPath[k] = E_P_prime[k].xor(C_P_prime[k]);
  101. }
  102. oldPath = Util.permute(oldPath, predata.reshuffle_pi);
  103. boolean pass = true;
  104. for (int k = 0; k < newPath.length; k++) {
  105. if (!oldPath[k].equals(newPath[k])) {
  106. System.err.println("Reshuffle test failed");
  107. pass = false;
  108. break;
  109. }
  110. }
  111. if (pass)
  112. System.out.println("Reshuffle test passed");
  113. } else if (party == Party.Debbie) {
  114. Tree OTi = forest.getTree(ti);
  115. preaccess.runD(predata, timer);
  116. byte[] sD_Ni = con1.read();
  117. byte[] sD_Nip1_pr = con1.read();
  118. access.runD(predata, OTi, sD_Ni, sD_Nip1_pr, timer);
  119. int[] tupleParam = new int[] { ti == 0 ? 0 : 1, md.getNBytesOfTree(ti), md.getLBytesOfTree(ti),
  120. md.getABytesOfTree(ti) };
  121. prereshuffle.runD(predata, tupleParam, timer);
  122. runD();
  123. } else if (party == Party.Charlie) {
  124. preaccess.runC(timer);
  125. System.out.println("L" + ti + "=" + new BigInteger(1, Li).toString(2));
  126. OutAccess outaccess = access.runC(md, ti, Li, timer);
  127. prereshuffle.runC(predata, timer);
  128. Tuple[] C_P_prime = runC(predata, outaccess.C_P, ti == 0, timer);
  129. Li = outaccess.C_Lip1;
  130. if (ti == numTrees - 1) {
  131. N = con1.readObject();
  132. long data = new BigInteger(1, outaccess.C_Ti.getA()).longValue();
  133. if (N == data) {
  134. System.out.println("Access passed");
  135. System.out.println();
  136. } else {
  137. throw new AccessException("Access failed");
  138. }
  139. }
  140. con1.write(outaccess.C_P);
  141. con1.write(C_P_prime);
  142. } else {
  143. throw new NoSuchPartyException(party + "");
  144. }
  145. }
  146. }
  147. }
  148. // timer.print();
  149. }
  150. }