ShiftXorPIR.java 3.4 KB

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