ShiftPIR.java 2.9 KB

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