Shift.java 3.9 KB

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