Access.java 8.2 KB

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