Reshuffle.java 5.7 KB

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