SSPIR.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 SSPIR extends Protocol {
  14. SecureRandom sr1;
  15. SecureRandom sr2;
  16. int pid = P.PIR;
  17. public SSPIR(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 SSPIR(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) {
  38. timer.start(M.offline_comp);
  39. int l = x.length;
  40. int m = x[0].length;
  41. byte[] a1 = new byte[l];
  42. byte[] r = new byte[m];
  43. sr2.nextBytes(a1);
  44. sr1.nextBytes(r);
  45. timer.stop(M.offline_comp);
  46. // ----------------------------------------- //
  47. timer.start(M.online_comp);
  48. byte[] z = Util.xorSelect(x, a1);
  49. Util.setXor(z, r);
  50. timer.stop(M.online_comp);
  51. return z;
  52. }
  53. public byte[] runP2(byte[][] x) {
  54. timer.start(M.offline_comp);
  55. int m = x[0].length;
  56. byte[] r = new byte[m];
  57. sr1.nextBytes(r);
  58. timer.stop(M.offline_comp);
  59. // ----------------------------------------- //
  60. timer.start(M.online_comp);
  61. timer.start(M.online_read);
  62. byte[] a2 = con2.readAndDec();
  63. timer.stop(M.online_read);
  64. byte[] z = Util.xorSelect(x, a2);
  65. Util.setXor(z, r);
  66. timer.stop(M.online_comp);
  67. return z;
  68. }
  69. public void runP3(int l, int t) {
  70. timer.start(M.offline_comp);
  71. byte[] a = new byte[l];
  72. sr1.nextBytes(a);
  73. timer.stop(M.offline_comp);
  74. // ----------------------------------------- //
  75. timer.start(M.online_comp);
  76. a[t] = (byte) (a[t] ^ 1);
  77. timer.start(M.online_write);
  78. con2.write(online_band, a);
  79. timer.stop(M.online_write);
  80. timer.stop(M.online_comp);
  81. }
  82. @Override
  83. public void run(Party party, Metadata md, Forest[] forest) {
  84. for (int j = 0; j < 100; j++) {
  85. int l = 100;
  86. int m = 50;
  87. byte[][] x = new byte[l][m];
  88. for (int i = 0; i < l; i++) {
  89. Crypto.sr.nextBytes(x[i]);
  90. }
  91. if (party == Party.Eddie) {
  92. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
  93. con1.write(x);
  94. byte[] out = this.runP1(x);
  95. con2.write(out);
  96. con2.write(x);
  97. } else if (party == Party.Debbie) {
  98. this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
  99. x = con1.readDoubleByteArray();
  100. byte[] out = this.runP2(x);
  101. con2.write(out);
  102. } else if (party == Party.Charlie) {
  103. this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
  104. int index = Crypto.sr.nextInt(l);
  105. this.runP3(l, index);
  106. byte[] out1 = con1.read();
  107. x = con1.readDoubleByteArray();
  108. byte[] out2 = con2.read();
  109. Util.setXor(out1, out2);
  110. if (!Util.equal(out1, x[index]))
  111. System.err.println(j + ": SSPIR test failed");
  112. else
  113. System.out.println(j + ": SSPIR test passed");
  114. } else {
  115. throw new NoSuchPartyException(party + "");
  116. }
  117. }
  118. }
  119. @Override
  120. public void run(Party party, Metadata md, Forest forest) {
  121. }
  122. }