OTPreprocessReceiver.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) by Xiao Shaun Wang <wangxiao@cs.umd.edu>
  2. package com.oblivm.backend.ot;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import com.oblivm.backend.flexsc.CompEnv;
  6. import com.oblivm.backend.flexsc.Flag;
  7. import com.oblivm.backend.gc.GCSignal;
  8. import com.oblivm.backend.network.Network;
  9. public class OTPreprocessReceiver extends OTReceiver {
  10. GCSignal[] buffer = new GCSignal[OTPreprocessSender.bufferSize];
  11. boolean[] choose = new boolean[OTPreprocessSender.bufferSize];
  12. int bufferusage = 0;
  13. public void fillup() {
  14. channel.flush();
  15. while (bufferusage < OTPreprocessSender.bufferSize) {
  16. int l = Math.min(OTPreprocessSender.fillLength, OTPreprocessSender.bufferSize - bufferusage);
  17. for (int i = bufferusage; i < bufferusage + l; ++i)
  18. choose[i] = CompEnv.rnd.nextBoolean();
  19. GCSignal[] kc = null;
  20. try {
  21. kc = reciever.receive(Arrays.copyOfRange(choose, bufferusage, bufferusage + l));
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. System.arraycopy(kc, 0, buffer, bufferusage, kc.length);
  26. bufferusage += l;
  27. }
  28. channel.flush();
  29. }
  30. OTExtReceiver reciever;
  31. public OTPreprocessReceiver(Network channel) {
  32. super(channel);
  33. reciever = new OTExtReceiver(channel);
  34. fillup();
  35. }
  36. public GCSignal receive(boolean b) throws IOException {
  37. bufferusage--;
  38. byte z = (b ^ choose[bufferusage]) ? (byte) 1 : (byte) 0;
  39. Flag.sw.startOTIO();
  40. channel.writeByte(z);
  41. channel.flush();
  42. GCSignal[] y = new GCSignal[] { GCSignal.receive(channel), GCSignal.receive(channel) };
  43. Flag.sw.stopOTIO();
  44. if (bufferusage == 0)
  45. fillup();
  46. return y[b ? 1 : 0].xor(buffer[bufferusage]);
  47. }
  48. public GCSignal[] receive(boolean[] b) throws IOException {
  49. if (bufferusage < b.length)
  50. fillup();
  51. byte[] z = new byte[b.length];
  52. int tmp = bufferusage;
  53. for (int i = 0; i < b.length; ++i) {
  54. --tmp;
  55. z[i] = (b[i] ^ choose[tmp]) ? (byte) 1 : (byte) 0;
  56. }
  57. Flag.sw.startOTIO();
  58. channel.writeByte(z, z.length);
  59. channel.flush();
  60. Flag.sw.stopOTIO();
  61. GCSignal[] ret = new GCSignal[b.length];
  62. for (int i = 0; i < b.length; ++i) {
  63. bufferusage--;
  64. Flag.sw.startOTIO();
  65. GCSignal[] y = new GCSignal[] { GCSignal.receive(channel), GCSignal.receive(channel) };
  66. Flag.sw.stopOTIO();
  67. ret[i] = y[b[i] ? 1 : 0].xor(buffer[bufferusage]);
  68. }
  69. return ret;
  70. }
  71. }