Access.java 8.3 KB

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