SSPIR.java 3.1 KB

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