Access.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package protocols;
  2. import java.math.BigInteger;
  3. import java.util.Arrays;
  4. import org.apache.commons.lang3.ArrayUtils;
  5. import communication.Communication;
  6. import crypto.Crypto;
  7. import exceptions.AccessException;
  8. import exceptions.NoSuchPartyException;
  9. import oram.Bucket;
  10. import oram.Forest;
  11. import oram.Metadata;
  12. import oram.Tree;
  13. import oram.Tuple;
  14. import protocols.precomputation.PreAccess;
  15. import protocols.struct.OutAccess;
  16. import protocols.struct.OutSSCOT;
  17. import protocols.struct.OutSSIOT;
  18. import protocols.struct.Party;
  19. import protocols.struct.PreData;
  20. import util.M;
  21. import util.P;
  22. import util.StopWatch;
  23. import util.Timer;
  24. import util.Util;
  25. public class Access extends Protocol {
  26. public Access(Communication con1, Communication con2) {
  27. super(con1, con2);
  28. }
  29. public OutAccess runE(PreData predata, Tree OTi, byte[] Ni, byte[] Nip1_pr, Timer timer) {
  30. timer.start(P.ACC, M.online_comp);
  31. // step 0: get Li from C
  32. timer.start(P.ACC, M.online_read);
  33. byte[] Li = new byte[0];
  34. if (OTi.getTreeIndex() > 0)
  35. Li = con2.read();
  36. timer.stop(P.ACC, M.online_read);
  37. // step 1
  38. Bucket[] pathBuckets = OTi.getBucketsOnPath(new BigInteger(1, Li).longValue());
  39. Tuple[] pathTuples = Bucket.bucketsToTuples(pathBuckets);
  40. for (int i = 0; i < pathTuples.length; i++)
  41. pathTuples[i].setXor(predata.access_p[i]);
  42. pathTuples = Util.permute(pathTuples, predata.access_sigma);
  43. // step 3
  44. byte[] y = null;
  45. if (OTi.getTreeIndex() == 0)
  46. y = pathTuples[0].getA();
  47. else if (OTi.getTreeIndex() < OTi.getH() - 1)
  48. y = Util.nextBytes(OTi.getABytes(), Crypto.sr);
  49. else
  50. y = new byte[OTi.getABytes()];
  51. if (OTi.getTreeIndex() > 0) {
  52. byte[][] a = new byte[pathTuples.length][];
  53. byte[][] m = new byte[pathTuples.length][];
  54. for (int i = 0; i < pathTuples.length; i++) {
  55. m[i] = Util.xor(pathTuples[i].getA(), y);
  56. a[i] = ArrayUtils.addAll(pathTuples[i].getF(), pathTuples[i].getN());
  57. for (int j = 0; j < Ni.length; j++)
  58. a[i][a[i].length - 1 - j] ^= Ni[Ni.length - 1 - j];
  59. }
  60. SSCOT sscot = new SSCOT(con1, con2);
  61. sscot.runE(predata, m, a, timer);
  62. }
  63. // step 4
  64. if (OTi.getTreeIndex() < OTi.getH() - 1) {
  65. int ySegBytes = y.length / OTi.getTwoTauPow();
  66. byte[][] y_array = new byte[OTi.getTwoTauPow()][];
  67. for (int i = 0; i < OTi.getTwoTauPow(); i++)
  68. y_array[i] = Arrays.copyOfRange(y, i * ySegBytes, (i + 1) * ySegBytes);
  69. SSIOT ssiot = new SSIOT(con1, con2);
  70. ssiot.runE(predata, y_array, Nip1_pr, timer);
  71. }
  72. // step 5
  73. Tuple Ti = null;
  74. if (OTi.getTreeIndex() == 0)
  75. Ti = pathTuples[0];
  76. else
  77. Ti = new Tuple(new byte[1], Ni, Li, y);
  78. OutAccess outaccess = new OutAccess(Li, null, null, null, null, Ti, pathTuples);
  79. timer.stop(P.ACC, M.online_comp);
  80. return outaccess;
  81. }
  82. public byte[] runD(PreData predata, Tree OTi, byte[] Ni, byte[] Nip1_pr, Timer timer) {
  83. timer.start(P.ACC, M.online_comp);
  84. // step 0: get Li from C
  85. timer.start(P.ACC, M.online_read);
  86. byte[] Li = new byte[0];
  87. if (OTi.getTreeIndex() > 0)
  88. Li = con2.read();
  89. timer.stop(P.ACC, M.online_read);
  90. // step 1
  91. Bucket[] pathBuckets = OTi.getBucketsOnPath(new BigInteger(1, Li).longValue());
  92. Tuple[] pathTuples = Bucket.bucketsToTuples(pathBuckets);
  93. for (int i = 0; i < pathTuples.length; i++)
  94. pathTuples[i].setXor(predata.access_p[i]);
  95. pathTuples = Util.permute(pathTuples, predata.access_sigma);
  96. // step 2
  97. timer.start(P.ACC, M.online_write);
  98. con2.write(pathTuples);
  99. con2.write(Ni);
  100. timer.stop(P.ACC, M.online_write);
  101. // step 3
  102. if (OTi.getTreeIndex() > 0) {
  103. byte[][] b = new byte[pathTuples.length][];
  104. for (int i = 0; i < pathTuples.length; i++) {
  105. b[i] = ArrayUtils.addAll(pathTuples[i].getF(), pathTuples[i].getN());
  106. b[i][0] ^= 1;
  107. for (int j = 0; j < Ni.length; j++)
  108. b[i][b[i].length - 1 - j] ^= Ni[Ni.length - 1 - j];
  109. }
  110. SSCOT sscot = new SSCOT(con1, con2);
  111. sscot.runD(predata, b, timer);
  112. }
  113. // step 4
  114. if (OTi.getTreeIndex() < OTi.getH() - 1) {
  115. SSIOT ssiot = new SSIOT(con1, con2);
  116. ssiot.runD(predata, Nip1_pr, timer);
  117. }
  118. timer.stop(P.ACC, M.online_comp);
  119. return Li;
  120. }
  121. public OutAccess runC(Metadata md, int treeIndex, byte[] Li, Timer timer) {
  122. timer.start(P.ACC, M.online_comp);
  123. // step 0: send Li to E and D
  124. timer.start(P.ACC, M.online_write);
  125. if (treeIndex > 0) {
  126. con1.write(Li);
  127. con2.write(Li);
  128. }
  129. timer.stop(P.ACC, M.online_write);
  130. // step 2
  131. timer.start(P.ACC, M.online_read);
  132. Tuple[] pathTuples = con2.readObject();
  133. byte[] Ni = con2.read();
  134. timer.stop(P.ACC, M.online_read);
  135. // step 3
  136. int j1 = 0;
  137. byte[] z = null;
  138. if (treeIndex == 0) {
  139. z = pathTuples[0].getA();
  140. } else {
  141. SSCOT sscot = new SSCOT(con1, con2);
  142. OutSSCOT je = sscot.runC(timer);
  143. j1 = je.t;
  144. byte[] d = pathTuples[j1].getA();
  145. z = Util.xor(je.m_t, d);
  146. }
  147. // step 4
  148. int j2 = 0;
  149. byte[] Lip1 = null;
  150. if (treeIndex < md.getNumTrees() - 1) {
  151. SSIOT ssiot = new SSIOT(con1, con2);
  152. OutSSIOT jy = ssiot.runC(timer);
  153. // step 5
  154. j2 = jy.t;
  155. int lSegBytes = md.getABytesOfTree(treeIndex) / md.getTwoTauPow();
  156. byte[] z_j2 = Arrays.copyOfRange(z, j2 * lSegBytes, (j2 + 1) * lSegBytes);
  157. Lip1 = Util.xor(jy.m_t, z_j2);
  158. }
  159. Tuple Ti = null;
  160. if (treeIndex == 0) {
  161. Ti = pathTuples[0];
  162. } else {
  163. Ti = new Tuple(new byte[] { 1 }, Ni, new byte[md.getLBytesOfTree(treeIndex)], z);
  164. pathTuples[j1].getF()[0] = (byte) (1 - pathTuples[j1].getF()[0]);
  165. Crypto.sr.nextBytes(pathTuples[j1].getN());
  166. Crypto.sr.nextBytes(pathTuples[j1].getL());
  167. Crypto.sr.nextBytes(pathTuples[j1].getA());
  168. }
  169. OutAccess outaccess = new OutAccess(Li, Lip1, Ti, pathTuples, j2, null, null);
  170. timer.stop(P.ACC, M.online_comp);
  171. return outaccess;
  172. }
  173. // for testing correctness
  174. @Override
  175. public void run(Party party, Metadata md, Forest forest) {
  176. int records = 5;
  177. int repeat = 5;
  178. int tau = md.getTau();
  179. int numTrees = md.getNumTrees();
  180. long numInsert = md.getNumInsertRecords();
  181. int addrBits = md.getAddrBits();
  182. Timer timer = new Timer();
  183. StopWatch sw = new StopWatch();
  184. sanityCheck();
  185. System.out.println();
  186. for (int i = 0; i < records; i++) {
  187. long N = Util.nextLong(numInsert, Crypto.sr);
  188. for (int j = 0; j < repeat; j++) {
  189. System.out.println("Test: " + i + " " + j);
  190. System.out.println("N=" + BigInteger.valueOf(N).toString(2));
  191. byte[] Li = new byte[0];
  192. for (int ti = 0; ti < numTrees; ti++) {
  193. long Ni_value = Util.getSubBits(N, addrBits, addrBits - md.getNBitsOfTree(ti));
  194. long Nip1_pr_value = Util.getSubBits(N, addrBits - md.getNBitsOfTree(ti),
  195. Math.max(addrBits - md.getNBitsOfTree(ti) - tau, 0));
  196. byte[] Ni = Util.longToBytes(Ni_value, md.getNBytesOfTree(ti));
  197. byte[] Nip1_pr = Util.longToBytes(Nip1_pr_value, (tau + 7) / 8);
  198. PreData predata = new PreData();
  199. PreAccess preaccess = new PreAccess(con1, con2);
  200. if (party == Party.Eddie) {
  201. Tree OTi = forest.getTree(ti);
  202. int numTuples = (OTi.getD() - 1) * OTi.getW() + OTi.getStashSize();
  203. int[] tupleParam = new int[] { ti == 0 ? 0 : 1, md.getNBytesOfTree(ti), md.getLBytesOfTree(ti),
  204. md.getABytesOfTree(ti) };
  205. preaccess.runE(predata, md.getTwoTauPow(), numTuples, tupleParam, timer);
  206. byte[] sE_Ni = Util.nextBytes(Ni.length, Crypto.sr);
  207. byte[] sD_Ni = Util.xor(Ni, sE_Ni);
  208. con1.write(sD_Ni);
  209. byte[] sE_Nip1_pr = Util.nextBytes(Nip1_pr.length, Crypto.sr);
  210. byte[] sD_Nip1_pr = Util.xor(Nip1_pr, sE_Nip1_pr);
  211. con1.write(sD_Nip1_pr);
  212. sw.start();
  213. runE(predata, OTi, sE_Ni, sE_Nip1_pr, timer);
  214. sw.stop();
  215. if (ti == numTrees - 1)
  216. con2.write(N);
  217. } else if (party == Party.Debbie) {
  218. Tree OTi = forest.getTree(ti);
  219. preaccess.runD(predata, timer);
  220. byte[] sD_Ni = con1.read();
  221. byte[] sD_Nip1_pr = con1.read();
  222. sw.start();
  223. runD(predata, OTi, sD_Ni, sD_Nip1_pr, timer);
  224. sw.stop();
  225. } else if (party == Party.Charlie) {
  226. preaccess.runC(timer);
  227. System.out.println("L" + ti + "=" + new BigInteger(1, Li).toString(2));
  228. sw.start();
  229. OutAccess outaccess = runC(md, ti, Li, timer);
  230. sw.stop();
  231. Li = outaccess.C_Lip1;
  232. if (ti == numTrees - 1) {
  233. N = con1.readObject();
  234. long data = new BigInteger(1, outaccess.C_Ti.getA()).longValue();
  235. if (N == data) {
  236. System.out.println("Access passed");
  237. System.out.println();
  238. } else {
  239. throw new AccessException("Access failed");
  240. }
  241. }
  242. } else {
  243. throw new NoSuchPartyException(party + "");
  244. }
  245. }
  246. }
  247. }
  248. timer.print();
  249. System.out.println();
  250. System.out.println(sw.toMS());
  251. }
  252. }