Shift.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package subprotocols;
  2. import communication.Communication;
  3. import crypto.Crypto;
  4. import exceptions.NoSuchPartyException;
  5. import oram.Forest;
  6. import oram.Metadata;
  7. import protocols.Protocol;
  8. import struct.Party;
  9. import util.M;
  10. import util.Util;
  11. public class Shift extends Protocol {
  12. public Shift(Communication con1, Communication con2) {
  13. super(con1, con2);
  14. }
  15. public byte[][] runE(byte[][] x, int s) {
  16. timer.start(M.offline_comp);
  17. int n = x.length;
  18. int l = x[0].length;
  19. byte[][] q = new byte[n][];
  20. for (int i = 0; i < n; i++)
  21. q[i] = Util.nextBytes(l, Crypto.sr_CE);
  22. byte[][] r = new byte[n][];
  23. for (int i = 0; i < n; i++)
  24. r[i] = Util.nextBytes(l, Crypto.sr_DE);
  25. timer.stop(M.offline_comp);
  26. // ----------------------------------------- //
  27. timer.start(M.online_comp);
  28. timer.start(M.online_read);
  29. byte[][] z = con2.readDoubleByteArrayAndDec();
  30. timer.stop(M.online_read);
  31. byte[][] b = new byte[n][];
  32. for (int i = 0; i < n; i++) {
  33. Util.setXor(z[i], r[i]);
  34. Util.setXor(z[i], x[i]);
  35. }
  36. for (int i = 0; i < n; i++) {
  37. b[i] = z[(i + s) % n];
  38. Util.setXor(b[i], q[i]);
  39. }
  40. timer.stop(M.online_comp);
  41. return b;
  42. }
  43. public void runD(int s, int n, int l) {
  44. timer.start(M.offline_comp);
  45. byte[][] r = new byte[n][];
  46. for (int i = 0; i < n; i++)
  47. r[i] = Util.nextBytes(l, Crypto.sr_DE);
  48. byte[][] p = new byte[n][];
  49. for (int i = 0; i < n; i++)
  50. p[i] = Util.nextBytes(l, Crypto.sr_CD);
  51. for (int i = 0; i < n; i++)
  52. Util.setXor(p[i], r[i]);
  53. timer.stop(M.offline_comp);
  54. // ----------------------------------------- //
  55. timer.start(M.online_comp);
  56. byte[][] a = new byte[n][];
  57. for (int i = 0; i < n; i++)
  58. a[i] = p[(i + s) % n];
  59. timer.start(M.online_write);
  60. con2.write(online_band, a);
  61. timer.stop(M.online_write);
  62. timer.stop(M.online_comp);
  63. return;
  64. }
  65. public byte[][] runC(byte[][] x) {
  66. timer.start(M.offline_comp);
  67. int n = x.length;
  68. int l = x[0].length;
  69. byte[][] p = new byte[n][];
  70. for (int i = 0; i < n; i++)
  71. p[i] = Util.nextBytes(l, Crypto.sr_CD);
  72. byte[][] q = new byte[n][];
  73. for (int i = 0; i < n; i++)
  74. q[i] = Util.nextBytes(l, Crypto.sr_CE);
  75. timer.stop(M.offline_comp);
  76. // ----------------------------------------- //
  77. timer.start(M.online_comp);
  78. for (int i = 0; i < n; i++)
  79. Util.setXor(p[i], x[i]);
  80. timer.start(M.online_write);
  81. con1.write(online_band, p);
  82. timer.stop(M.online_write);
  83. timer.start(M.online_read);
  84. byte[][] a = con2.readDoubleByteArrayAndDec();
  85. timer.stop(M.online_read);
  86. for (int i = 0; i < n; i++)
  87. Util.setXor(a[i], q[i]);
  88. timer.stop(M.online_comp);
  89. return a;
  90. }
  91. @Override
  92. public void run(Party party, Metadata md, Forest[] forest) {
  93. for (int j = 0; j < 100; j++) {
  94. int n = 500;
  95. int l = 50;
  96. byte[][] x = new byte[n][l];
  97. for (int i = 0; i < n; i++) {
  98. Crypto.sr.nextBytes(x[i]);
  99. }
  100. int s = Crypto.sr.nextInt(n);
  101. if (party == Party.Eddie) {
  102. con1.write(s);
  103. byte[][] y1 = this.runE(x, s);
  104. byte[][] x2 = con2.readDoubleByteArray();
  105. byte[][] y2 = con2.readDoubleByteArray();
  106. for (int i = 0; i < n; i++) {
  107. Util.setXor(x2[i], x[i]);
  108. Util.setXor(y2[i], y1[i]);
  109. }
  110. boolean fail = false;
  111. for (int i = 0; i < n; i++) {
  112. if (!Util.equal(y2[i], x2[(i + s) % n])) {
  113. System.err.println(j + ": Shift test failed");
  114. fail = true;
  115. break;
  116. }
  117. }
  118. if (!fail)
  119. System.out.println(j + ": Shift test passed");
  120. } else if (party == Party.Debbie) {
  121. s = con1.readInt();
  122. this.runD(s, n, l);
  123. } else if (party == Party.Charlie) {
  124. byte[][] y2 = this.runC(x);
  125. con1.write(x);
  126. con1.write(y2);
  127. } else {
  128. throw new NoSuchPartyException(party + "");
  129. }
  130. }
  131. }
  132. @Override
  133. public void run(Party party, Metadata md, Forest forest) {
  134. }
  135. }