ShiftPIR.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package pir;
  2. import communication.Communication;
  3. import crypto.Crypto;
  4. import exceptions.NoSuchPartyException;
  5. import oram.Forest;
  6. import oram.Metadata;
  7. import protocols.Protocol;
  8. import protocols.struct.Party;
  9. import protocols.struct.PreData;
  10. import util.M;
  11. import util.P;
  12. import util.Timer;
  13. import util.Util;
  14. public class ShiftPIR extends Protocol {
  15. private int pid = P.SftPIR;
  16. public ShiftPIR(Communication con1, Communication con2) {
  17. super(con1, con2);
  18. }
  19. public byte[] runP1(PreData predata, byte[][] x, int s, Timer timer) {
  20. timer.start(pid, M.online_comp);
  21. // TODO: do in place shift
  22. byte[][] xp = new byte[x.length][];
  23. for (int i = 0; i < x.length; i++) {
  24. xp[i] = x[(i + s) % x.length];
  25. }
  26. SSPIR sspir = new SSPIR(con1, con2);
  27. byte[] z = sspir.runP1(predata, xp, timer);
  28. timer.stop(pid, M.online_comp);
  29. return z;
  30. }
  31. public byte[] runP2(PreData predata, byte[][] x, int s, Timer timer) {
  32. timer.start(pid, M.online_comp);
  33. byte[][] xp = new byte[x.length][];
  34. for (int i = 0; i < x.length; i++) {
  35. xp[i] = x[(i + s) % x.length];
  36. }
  37. SSPIR sspir = new SSPIR(con1, con2);
  38. byte[] z = sspir.runP2(predata, xp, timer);
  39. timer.stop(pid, M.online_comp);
  40. return z;
  41. }
  42. public void runP3(PreData predata, int t, Timer timer) {
  43. timer.start(pid, M.online_comp);
  44. SSPIR sspir = new SSPIR(con1, con2);
  45. sspir.runP3(predata, t, timer);
  46. timer.stop(pid, M.online_comp);
  47. }
  48. @Override
  49. public void run(Party party, Metadata md, Forest[] forest) {
  50. Timer timer = new Timer();
  51. PreData predata = new PreData();
  52. for (int j = 0; j < 100; j++) {
  53. int l = 100;
  54. int m = 50;
  55. byte[][] x = new byte[l][m];
  56. for (int i = 0; i < l; i++) {
  57. Crypto.sr.nextBytes(x[i]);
  58. }
  59. int s = Crypto.sr.nextInt(l);
  60. int t = Crypto.sr.nextInt(l);
  61. if (party == Party.Eddie) {
  62. con1.write(x);
  63. con1.write(s);
  64. byte[] out = this.runP1(predata, x, s, timer);
  65. con2.write(out);
  66. con2.write(x);
  67. con2.write(s);
  68. } else if (party == Party.Debbie) {
  69. x = con1.readDoubleByteArray();
  70. s = con1.readInt();
  71. byte[] out = this.runP2(predata, x, s, timer);
  72. con2.write(out);
  73. } else if (party == Party.Charlie) {
  74. this.runP3(predata, t, timer);
  75. byte[] out1 = con1.read();
  76. x = con1.readDoubleByteArray();
  77. s = con1.readInt();
  78. byte[] out2 = con2.read();
  79. Util.setXor(out1, out2);
  80. int index = (s + t) % l;
  81. if (!Util.equal(out1, x[index]))
  82. System.err.println(j + ": ShiftPIR test failed");
  83. else
  84. System.out.println(j + ": ShiftPIR test passed");
  85. } else {
  86. throw new NoSuchPartyException(party + "");
  87. }
  88. }
  89. }
  90. @Override
  91. public void run(Party party, Metadata md, Forest forest) {
  92. }
  93. }