ShiftPIR.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package subprotocols;
  2. import java.security.SecureRandom;
  3. import communication.Communication;
  4. import crypto.Crypto;
  5. import exceptions.NoSuchPartyException;
  6. import oram.Forest;
  7. import oram.Metadata;
  8. import protocols.Protocol;
  9. import struct.Party;
  10. import util.M;
  11. import util.P;
  12. import util.Util;
  13. public class ShiftPIR extends Protocol {
  14. SecureRandom sr1;
  15. SecureRandom sr2;
  16. int pid = P.SPIR;
  17. public ShiftPIR(Communication con1, Communication con2) {
  18. super(con1, con2);
  19. online_band = all.online_band[pid];
  20. offline_band = all.offline_band[pid];
  21. timer = all.timer[pid];
  22. }
  23. public ShiftPIR(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  24. super(con1, con2);
  25. this.sr1 = sr1;
  26. this.sr2 = sr2;
  27. online_band = all.online_band[pid];
  28. offline_band = all.offline_band[pid];
  29. timer = all.timer[pid];
  30. }
  31. public void reinit(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
  32. this.con1 = con1;
  33. this.con2 = con2;
  34. this.sr1 = sr1;
  35. this.sr2 = sr2;
  36. }
  37. public byte[] runP1(byte[][] x, int s) {
  38. timer.start(M.online_comp);
  39. // TODO: do in place shift
  40. byte[][] xp = new byte[x.length][];
  41. for (int i = 0; i < x.length; i++) {
  42. xp[i] = x[(i + s) % x.length];
  43. }
  44. SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
  45. byte[] z = sspir.runP1(xp);
  46. timer.stop(M.online_comp);
  47. return z;
  48. }
  49. public byte[] runP2(byte[][] x, int s) {
  50. timer.start(M.online_comp);
  51. byte[][] xp = new byte[x.length][];
  52. for (int i = 0; i < x.length; i++) {
  53. xp[i] = x[(i + s) % x.length];
  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 l, int t) {
  61. timer.start(M.online_comp);
  62. SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
  63. sspir.runP3(l, t);
  64. timer.stop(M.online_comp);
  65. }
  66. @Override
  67. public void run(Party party, Metadata md, Forest[] forest) {
  68. for (int j = 0; j < 100; j++) {
  69. int l = 100;
  70. int m = 50;
  71. byte[][] x = new byte[l][m];
  72. for (int i = 0; i < l; i++) {
  73. Crypto.sr.nextBytes(x[i]);
  74. }
  75. int s = Crypto.sr.nextInt(l);
  76. int t = Crypto.sr.nextInt(l);
  77. if (party == Party.Eddie) {
  78. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
  79. con1.write(x);
  80. con1.write(s);
  81. byte[] out = this.runP1(x, s);
  82. con2.write(out);
  83. con2.write(x);
  84. con2.write(s);
  85. } else if (party == Party.Debbie) {
  86. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
  87. x = con1.readDoubleByteArray();
  88. s = con1.readInt();
  89. byte[] out = this.runP2(x, s);
  90. con2.write(out);
  91. } else if (party == Party.Charlie) {
  92. this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
  93. this.runP3(l, t);
  94. byte[] out1 = con1.read();
  95. x = con1.readDoubleByteArray();
  96. s = con1.readInt();
  97. byte[] out2 = con2.read();
  98. Util.setXor(out1, out2);
  99. int index = (s + t) % l;
  100. if (!Util.equal(out1, x[index]))
  101. System.err.println(j + ": ShiftPIR test failed");
  102. else
  103. System.out.println(j + ": ShiftPIR test passed");
  104. } else {
  105. throw new NoSuchPartyException(party + "");
  106. }
  107. }
  108. }
  109. @Override
  110. public void run(Party party, Metadata md, Forest forest) {
  111. }
  112. }