Access.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.Timer;
  23. import util.Util;
  24. public class Access extends Protocol {
  25. private int pid = P.ACC;
  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(pid, M.online_comp);
  31. // step 0: get Li from C
  32. byte[] Li = new byte[0];
  33. timer.start(pid, M.online_read);
  34. if (OTi.getTreeIndex() > 0)
  35. Li = con2.read();
  36. timer.stop(pid, M.online_read);
  37. // step 1
  38. Bucket[] pathBuckets = OTi.getBucketsOnPath(Li);
  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(pid, 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(pid, M.online_comp);
  84. // step 0: get Li from C
  85. byte[] Li = new byte[0];
  86. timer.start(pid, M.online_read);
  87. if (OTi.getTreeIndex() > 0)
  88. Li = con2.read();
  89. timer.stop(pid, M.online_read);
  90. // step 1
  91. Bucket[] pathBuckets = OTi.getBucketsOnPath(Li);
  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(pid, M.online_write);
  98. con2.write(pid, pathTuples);
  99. con2.write(pid, Ni);
  100. timer.stop(pid, 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(pid, M.online_comp);
  119. return Li;
  120. }
  121. public OutAccess runC(Metadata md, int treeIndex, byte[] Li, Timer timer) {
  122. timer.start(pid, M.online_comp);
  123. // step 0: send Li to E and D
  124. timer.start(pid, M.online_write);
  125. if (treeIndex > 0) {
  126. con1.write(Li);
  127. con2.write(Li);
  128. }
  129. timer.stop(pid, M.online_write);
  130. // step 2
  131. timer.start(pid, M.online_read);
  132. Tuple[] pathTuples = con2.readTupleArray();
  133. byte[] Ni = con2.read();
  134. timer.stop(pid, 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(pid, M.online_comp);
  171. return outaccess;
  172. }
  173. public OutAccess runE2(Tree OTi, Timer timer) {
  174. timer.start(pid, M.online_comp);
  175. // step 0: get Li from C
  176. byte[] Li = new byte[0];
  177. timer.start(pid, M.online_read);
  178. if (OTi.getTreeIndex() > 0)
  179. Li = con2.read();
  180. timer.stop(pid, M.online_read);
  181. // step 1
  182. Bucket[] pathBuckets = OTi.getBucketsOnPath(Li);
  183. Tuple[] pathTuples = Bucket.bucketsToTuples(pathBuckets);
  184. // step 5
  185. Tuple Ti = null;
  186. if (OTi.getTreeIndex() == 0)
  187. Ti = pathTuples[0];
  188. else {
  189. Ti = new Tuple(1, OTi.getNBytes(), OTi.getLBytes(), OTi.getABytes(), Crypto.sr);
  190. Ti.setF(new byte[1]);
  191. }
  192. OutAccess outaccess = new OutAccess(Li, null, null, null, null, Ti, pathTuples);
  193. timer.stop(pid, M.online_comp);
  194. return outaccess;
  195. }
  196. public byte[] runD2(Tree OTi, Timer timer) {
  197. timer.start(pid, M.online_comp);
  198. // step 0: get Li from C
  199. byte[] Li = new byte[0];
  200. timer.start(pid, M.online_read);
  201. if (OTi.getTreeIndex() > 0)
  202. Li = con2.read();
  203. timer.stop(pid, M.online_read);
  204. // step 1
  205. Bucket[] pathBuckets = OTi.getBucketsOnPath(Li);
  206. Tuple[] pathTuples = Bucket.bucketsToTuples(pathBuckets);
  207. // step 2
  208. timer.start(pid, M.online_write);
  209. con2.write(pid, pathTuples);
  210. timer.stop(pid, M.online_write);
  211. timer.stop(pid, M.online_comp);
  212. return Li;
  213. }
  214. public OutAccess runC2(Metadata md, int treeIndex, byte[] Li, Timer timer) {
  215. timer.start(pid, M.online_comp);
  216. // step 0: send Li to E and D
  217. timer.start(pid, M.online_write);
  218. if (treeIndex > 0) {
  219. con1.write(Li);
  220. con2.write(Li);
  221. }
  222. timer.stop(pid, M.online_write);
  223. // step 2
  224. timer.start(pid, M.online_read);
  225. Tuple[] pathTuples = con2.readTupleArray();
  226. timer.stop(pid, M.online_read);
  227. // step 5
  228. Tuple Ti = null;
  229. if (treeIndex == 0) {
  230. Ti = pathTuples[0];
  231. } else {
  232. Ti = new Tuple(1, md.getNBytesOfTree(treeIndex), md.getLBytesOfTree(treeIndex),
  233. md.getABytesOfTree(treeIndex), Crypto.sr);
  234. Ti.setF(new byte[1]);
  235. }
  236. OutAccess outaccess = new OutAccess(Li, null, Ti, pathTuples, null, null, null);
  237. timer.stop(pid, M.online_comp);
  238. return outaccess;
  239. }
  240. // for testing correctness
  241. @Override
  242. public void run(Party party, Metadata md, Forest forest) {
  243. int records = 5;
  244. int repeat = 5;
  245. int tau = md.getTau();
  246. int numTrees = md.getNumTrees();
  247. long numInsert = md.getNumInsertRecords();
  248. int addrBits = md.getAddrBits();
  249. Timer timer = new Timer();
  250. sanityCheck();
  251. System.out.println();
  252. for (int i = 0; i < records; i++) {
  253. long N = Metadata.cheat ? 0 : Util.nextLong(numInsert, Crypto.sr);
  254. for (int j = 0; j < repeat; j++) {
  255. System.out.println("Test: " + i + " " + j);
  256. System.out.println("N=" + BigInteger.valueOf(N).toString(2));
  257. byte[] Li = new byte[0];
  258. for (int ti = 0; ti < numTrees; ti++) {
  259. long Ni_value = Util.getSubBits(N, addrBits, addrBits - md.getNBitsOfTree(ti));
  260. long Nip1_pr_value = Util.getSubBits(N, addrBits - md.getNBitsOfTree(ti),
  261. Math.max(addrBits - md.getNBitsOfTree(ti) - tau, 0));
  262. byte[] Ni = Util.longToBytes(Ni_value, md.getNBytesOfTree(ti));
  263. byte[] Nip1_pr = Util.longToBytes(Nip1_pr_value, (tau + 7) / 8);
  264. PreData predata = new PreData();
  265. PreAccess preaccess = new PreAccess(con1, con2);
  266. if (party == Party.Eddie) {
  267. Tree OTi = forest.getTree(ti);
  268. int numTuples = (OTi.getD() - 1) * OTi.getW() + OTi.getStashSize();
  269. int[] tupleParam = new int[] { ti == 0 ? 0 : 1, md.getNBytesOfTree(ti), md.getLBytesOfTree(ti),
  270. md.getABytesOfTree(ti) };
  271. preaccess.runE(predata, md.getTwoTauPow(), numTuples, tupleParam, timer);
  272. byte[] sE_Ni = Util.nextBytes(Ni.length, Crypto.sr);
  273. byte[] sD_Ni = Util.xor(Ni, sE_Ni);
  274. con1.write(sD_Ni);
  275. byte[] sE_Nip1_pr = Util.nextBytes(Nip1_pr.length, Crypto.sr);
  276. byte[] sD_Nip1_pr = Util.xor(Nip1_pr, sE_Nip1_pr);
  277. con1.write(sD_Nip1_pr);
  278. runE(predata, OTi, sE_Ni, sE_Nip1_pr, timer);
  279. if (ti == numTrees - 1)
  280. con2.write(N);
  281. } else if (party == Party.Debbie) {
  282. Tree OTi = forest.getTree(ti);
  283. preaccess.runD(predata, timer);
  284. byte[] sD_Ni = con1.read();
  285. byte[] sD_Nip1_pr = con1.read();
  286. runD(predata, OTi, sD_Ni, sD_Nip1_pr, timer);
  287. } else if (party == Party.Charlie) {
  288. preaccess.runC(timer);
  289. System.out.println("L" + ti + "=" + new BigInteger(1, Li).toString(2));
  290. OutAccess outaccess = runC(md, ti, Li, timer);
  291. Li = outaccess.C_Lip1;
  292. if (ti == numTrees - 1) {
  293. N = con1.readLong();
  294. long data = new BigInteger(1, outaccess.C_Ti.getA()).longValue();
  295. if (N == data) {
  296. System.out.println("Access passed");
  297. System.out.println();
  298. } else {
  299. throw new AccessException("Access failed");
  300. }
  301. }
  302. } else {
  303. throw new NoSuchPartyException(party + "");
  304. }
  305. }
  306. }
  307. }
  308. // timer.print();
  309. }
  310. }