ShiftXorPIR.java 3.6 KB

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