GCGenComp.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.oblivm.backend.gc;
  2. import java.io.IOException;
  3. import com.oblivm.backend.flexsc.CompEnv;
  4. import com.oblivm.backend.flexsc.Flag;
  5. import com.oblivm.backend.flexsc.Mode;
  6. import com.oblivm.backend.flexsc.Party;
  7. import com.oblivm.backend.network.Network;
  8. import com.oblivm.backend.ot.FakeOTSender;
  9. import com.oblivm.backend.ot.OTExtSender;
  10. import com.oblivm.backend.ot.OTPreprocessSender;
  11. import com.oblivm.backend.ot.OTSender;
  12. public abstract class GCGenComp extends GCCompEnv {
  13. static public GCSignal R = null;
  14. static {
  15. R = GCSignal.freshLabel(CompEnv.rnd);
  16. R.setLSB();
  17. }
  18. OTSender snd;
  19. protected long gid = 0;
  20. public GCGenComp(Network channel, Mode mode) {
  21. super(channel, Party.Alice, mode);
  22. if (channel.sender == null && channel.receiver == null) {
  23. if (Flag.FakeOT)
  24. snd = new FakeOTSender(80, channel);
  25. else if (Flag.ProprocessOT)
  26. snd = new OTPreprocessSender(80, channel);
  27. else
  28. snd = new OTExtSender(80, channel);
  29. }
  30. }
  31. public static GCSignal[] genPairForLabel(Mode mode) {
  32. GCSignal[] label = new GCSignal[2];
  33. if (mode != Mode.OFFLINE || !Flag.offline)
  34. label[0] = GCSignal.freshLabel(rnd);
  35. if (mode == Mode.OFFLINE) {
  36. if (Flag.offline) {
  37. label[0] = new GCSignal(com.oblivm.backend.gc.offline.GCGen.fread.read(10));
  38. } else
  39. label[0].send(com.oblivm.backend.gc.offline.GCGen.fout);
  40. }
  41. label[1] = R.xor(label[0]);
  42. return label;
  43. }
  44. public static GCSignal[] genPair() {
  45. GCSignal[] label = new GCSignal[2];
  46. label[0] = GCSignal.freshLabel(rnd);
  47. label[1] = R.xor(label[0]);
  48. return label;
  49. }
  50. public GCSignal inputOfAlice(boolean in) {
  51. Flag.sw.startOT();
  52. GCSignal[] label = genPairForLabel(mode);
  53. Flag.sw.startOTIO();
  54. label[in ? 1 : 0].send(channel);
  55. flush();
  56. Flag.sw.stopOTIO();
  57. Flag.sw.stopOT();
  58. return label[0];
  59. }
  60. public GCSignal inputOfBob(boolean in) {
  61. Flag.sw.startOT();
  62. GCSignal[] label = genPairForLabel(mode);
  63. try {
  64. snd.send(label);
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. Flag.sw.stopOT();
  69. return label[0];
  70. }
  71. public GCSignal[] inputOfAlice(boolean[] x) {
  72. Flag.sw.startOT();
  73. GCSignal[][] pairs = new GCSignal[x.length][2];
  74. GCSignal[] result = new GCSignal[x.length];
  75. for (int i = 0; i < x.length; ++i) {
  76. pairs[i] = genPairForLabel(mode);
  77. result[i] = pairs[i][0];
  78. }
  79. Flag.sw.startOTIO();
  80. for (int i = 0; i < x.length; ++i)
  81. pairs[i][x[i] ? 1 : 0].send(channel);
  82. flush();
  83. Flag.sw.stopOTIO();
  84. Flag.sw.stopOT();
  85. return result;
  86. }
  87. public GCSignal[] inputOfBob(boolean[] x) {
  88. Flag.sw.startOT();
  89. GCSignal[][] pair = new GCSignal[x.length][2];
  90. for (int i = 0; i < x.length; ++i)
  91. pair[i] = genPairForLabel(mode);
  92. try {
  93. snd.send(pair);
  94. } catch (IOException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. }
  98. GCSignal[] result = new GCSignal[x.length];
  99. for (int i = 0; i < x.length; ++i)
  100. result[i] = pair[i][0];
  101. Flag.sw.stopOT();
  102. return result;
  103. }
  104. protected boolean gatesRemain = false;
  105. public boolean outputToAlice(GCSignal out) {
  106. if (gatesRemain) {
  107. gatesRemain = false;
  108. flush();
  109. }
  110. if (out.isPublic())
  111. return out.v;
  112. GCSignal lb = GCSignal.receive(channel);
  113. if (lb.equals(out))
  114. return false;
  115. else if (lb.equals(R.xor(out)))
  116. return true;
  117. try {
  118. throw new Exception("bad label at final output.");
  119. } catch (Exception e) {
  120. // TODO Auto-generated catch block
  121. e.printStackTrace();
  122. }
  123. return false;
  124. }
  125. public boolean outputToBob(GCSignal out) {
  126. if (!out.isPublic())
  127. out.send(channel);
  128. return false;
  129. }
  130. public boolean[] outputToBob(GCSignal[] out) {
  131. boolean[] result = new boolean[out.length];
  132. for (int i = 0; i < result.length; ++i) {
  133. if (!out[i].isPublic())
  134. out[i].send(channel);
  135. }
  136. flush();
  137. for (int i = 0; i < result.length; ++i)
  138. result[i] = false;
  139. return result;
  140. }
  141. public boolean[] outputToAlice(GCSignal[] out) {
  142. boolean[] result = new boolean[out.length];
  143. for (int i = 0; i < result.length; ++i) {
  144. result[i] = outputToAlice(out[i]);
  145. }
  146. return result;
  147. }
  148. public GCSignal xor(GCSignal a, GCSignal b) {
  149. if (a.isPublic() && b.isPublic())
  150. return new GCSignal(a.v ^ b.v);
  151. else if (a.isPublic())
  152. return a.v ? not(b) : new GCSignal(b);
  153. else if (b.isPublic())
  154. return b.v ? not(a) : new GCSignal(a);
  155. else {
  156. return a.xor(b);
  157. }
  158. }
  159. public GCSignal not(GCSignal a) {
  160. if (a.isPublic())
  161. return new GCSignal(!a.v);
  162. else
  163. return R.xor(a);
  164. }
  165. }