ShiftXorPIR.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package subprotocols;
  2. import java.security.SecureRandom;
  3. import java.util.Arrays;
  4. import communication.Communication;
  5. import crypto.Crypto;
  6. import exceptions.NoSuchPartyException;
  7. import oram.Forest;
  8. import oram.Metadata;
  9. import protocols.Protocol;
  10. import struct.Party;
  11. import util.M;
  12. import util.P;
  13. import util.Util;
  14. public class ShiftXorPIR extends Protocol {
  15. SecureRandom sr1;
  16. SecureRandom sr2;
  17. int pid = P.SXPIR;
  18. public ShiftXorPIR(Communication con1, Communication con2) {
  19. super(con1, con2);
  20. online_band = all.online_band[pid];
  21. offline_band = all.offline_band[pid];
  22. timer = all.timer[pid];
  23. }
  24. public ShiftXorPIR(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  25. super(con1, con2);
  26. this.sr1 = sr1;
  27. this.sr2 = sr2;
  28. online_band = all.online_band[pid];
  29. offline_band = all.offline_band[pid];
  30. timer = all.timer[pid];
  31. }
  32. public void reinit(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  33. this.con1 = con1;
  34. this.con2 = con2;
  35. this.sr1 = sr1;
  36. this.sr2 = sr2;
  37. }
  38. public byte[] runP1(byte[][] x, int s1, int s2, int m) {
  39. timer.start(M.online_comp);
  40. int n = x.length;
  41. int l = x[0].length / m;
  42. byte[][] xp = new byte[n * m][];
  43. for (int i = 0; i < n; i++) {
  44. for (int j = 0; j < m; j++) {
  45. xp[i * m + j] = Arrays.copyOfRange(x[(i + s1) % n], (j ^ s2) * l, ((j ^ s2) + 1) * l);
  46. }
  47. }
  48. SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
  49. byte[] z = sspir.runP1(xp);
  50. timer.stop(M.online_comp);
  51. return z;
  52. }
  53. public byte[] runP2(byte[][] x, int s1, int s2, int m) {
  54. timer.start(M.online_comp);
  55. int n = x.length;
  56. int l = x[0].length / m;
  57. byte[][] xp = new byte[n * m][];
  58. for (int i = 0; i < n; i++) {
  59. for (int j = 0; j < m; j++) {
  60. xp[i * m + j] = Arrays.copyOfRange(x[(i + s1) % n], (j ^ s2) * l, ((j ^ s2) + 1) * l);
  61. }
  62. }
  63. SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
  64. byte[] z = sspir.runP2(xp);
  65. timer.stop(M.online_comp);
  66. return z;
  67. }
  68. public void runP3(int t1, int t2, int n, int m) {
  69. timer.start(M.online_comp);
  70. int t = t1 * m + t2;
  71. SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
  72. sspir.runP3(n * m, t);
  73. timer.stop(M.online_comp);
  74. }
  75. @Override
  76. public void run(Party party, Metadata md, Forest[] forest) {
  77. for (int j = 0; j < 100; j++) {
  78. int n = 500;
  79. int m = 16;
  80. int l = 4;
  81. byte[][] x = new byte[n][m * l];
  82. for (int i = 0; i < n; i++) {
  83. Crypto.sr.nextBytes(x[i]);
  84. }
  85. int s1 = Crypto.sr.nextInt(n);
  86. int t1 = Crypto.sr.nextInt(n);
  87. int s2 = Crypto.sr.nextInt(m);
  88. int t2 = Crypto.sr.nextInt(m);
  89. if (party == Party.Eddie) {
  90. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
  91. con1.write(x);
  92. con1.write(s1);
  93. con1.write(s2);
  94. con2.write(t1);
  95. con2.write(t2);
  96. byte[] e = this.runP1(x, s1, s2, m);
  97. byte[] d = con1.read();
  98. Util.setXor(e, d);
  99. int i1 = (s1 + t1) % n;
  100. int i2 = s2 ^ t2;
  101. byte[] expect = Arrays.copyOfRange(x[i1], i2 * l, (i2 + 1) * l);
  102. if (!Util.equal(e, expect))
  103. System.err.println(j + ": ShiftXorPIR test failed");
  104. else
  105. System.out.println(j + ": ShiftXorPIR test passed");
  106. } else if (party == Party.Debbie) {
  107. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
  108. x = con1.readDoubleByteArray();
  109. s1 = con1.readInt();
  110. s2 = con1.readInt();
  111. byte[] d = this.runP2(x, s1, s2, m);
  112. con1.write(d);
  113. } else if (party == Party.Charlie) {
  114. this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
  115. t1 = con1.readInt();
  116. t2 = con1.readInt();
  117. this.runP3(t1, t2, n, m);
  118. } else {
  119. throw new NoSuchPartyException(party + "");
  120. }
  121. }
  122. }
  123. @Override
  124. public void run(Party party, Metadata md, Forest forest) {
  125. }
  126. }