Access.java 9.6 KB

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