PIRIOT.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package pir;
  2. import communication.Communication;
  3. import crypto.Crypto;
  4. import exceptions.NoSuchPartyException;
  5. import exceptions.SSIOTException;
  6. import oram.Forest;
  7. import oram.Metadata;
  8. import pir.precomputation.PrePIRIOT;
  9. import protocols.Protocol;
  10. import protocols.struct.OutSSIOT;
  11. import protocols.struct.Party;
  12. import protocols.struct.PreData;
  13. import util.M;
  14. import util.P;
  15. import util.Timer;
  16. import util.Util;
  17. public class PIRIOT extends Protocol {
  18. private int pid = P.IOT;
  19. public PIRIOT(Communication con1, Communication con2) {
  20. super(con1, con2);
  21. }
  22. public void runE(PreData predata, int n, byte[] Nip1_pr, Timer timer) {
  23. timer.start(pid, M.online_comp);
  24. // step 1
  25. byte[][] x = new byte[n][];
  26. byte[][] v = new byte[n][];
  27. for (int i = 0; i < n; i++) {
  28. byte[] i_bytes = Util.intToBytes(i);
  29. x[i] = predata.ssiot_r.clone();
  30. for (int j = 0; j < Nip1_pr.length; j++)
  31. x[i][x[i].length - 1 - j] ^= Nip1_pr[Nip1_pr.length - 1 - j] ^ i_bytes[i_bytes.length - 1 - j];
  32. v[i] = predata.ssiot_F_kprime.compute(x[i]);
  33. }
  34. timer.start(pid, M.online_write);
  35. con2.write(pid, v);
  36. timer.stop(pid, M.online_write);
  37. timer.stop(pid, M.online_comp);
  38. }
  39. public void runD(PreData predata, byte[] Nip1_pr, Timer timer) {
  40. timer.start(pid, M.online_comp);
  41. // step 2
  42. byte[] y = predata.ssiot_r;
  43. for (int i = 0; i < Nip1_pr.length; i++)
  44. y[y.length - 1 - i] ^= Nip1_pr[Nip1_pr.length - 1 - i];
  45. byte[] w = predata.ssiot_F_kprime.compute(y);
  46. timer.start(pid, M.online_write);
  47. con2.write(pid, w);
  48. timer.stop(pid, M.online_write);
  49. timer.stop(pid, M.online_comp);
  50. }
  51. public OutSSIOT runC(Timer timer) {
  52. timer.start(pid, M.online_comp);
  53. // step 1
  54. timer.start(pid, M.online_read);
  55. byte[][] v = con1.readDoubleByteArray(pid);
  56. // step 2
  57. byte[] w = con2.read(pid);
  58. timer.stop(pid, M.online_read);
  59. // step 3
  60. int n = v.length;
  61. OutSSIOT output = null;
  62. int invariant = 0;
  63. for (int i = 0; i < n; i++) {
  64. if (Util.equal(v[i], w)) {
  65. output = new OutSSIOT(i, null);
  66. invariant++;
  67. }
  68. }
  69. if (invariant != 1)
  70. throw new SSIOTException("Invariant error: " + invariant);
  71. timer.stop(pid, M.online_comp);
  72. return output;
  73. }
  74. // for testing correctness
  75. @Override
  76. public void run(Party party, Metadata md, Forest forest) {
  77. Timer timer = new Timer();
  78. for (int j = 0; j < 100; j++) {
  79. int twoTauPow = 64;
  80. byte[] sE_Nip1_pr = new byte[1];
  81. byte[] sD_Nip1_pr = new byte[1];
  82. int index = Crypto.sr.nextInt(twoTauPow);
  83. Crypto.sr.nextBytes(sE_Nip1_pr);
  84. sD_Nip1_pr[0] = (byte) (Util.intToBytes(index)[3] ^ sE_Nip1_pr[0]);
  85. PreData predata = new PreData();
  86. PrePIRIOT pressiot = new PrePIRIOT(con1, con2);
  87. if (party == Party.Eddie) {
  88. con1.write(sD_Nip1_pr);
  89. con2.write(index);
  90. pressiot.runE(predata, twoTauPow, timer);
  91. runE(predata, twoTauPow, sE_Nip1_pr, timer);
  92. } else if (party == Party.Debbie) {
  93. sD_Nip1_pr = con1.read();
  94. pressiot.runD(predata, timer);
  95. runD(predata, sD_Nip1_pr, timer);
  96. } else if (party == Party.Charlie) {
  97. index = con1.readInt();
  98. pressiot.runC();
  99. OutSSIOT output = runC(timer);
  100. if (output.t == index)
  101. System.out.println("PIRIOT test passed");
  102. else
  103. System.err.println("PIRIOT test failed");
  104. } else {
  105. throw new NoSuchPartyException(party + "");
  106. }
  107. }
  108. // timer.print();
  109. }
  110. @Override
  111. public void run(Party party, Metadata md, Forest[] forest) {
  112. // TODO Auto-generated method stub
  113. }
  114. }