Access.java 8.1 KB

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