GCGen.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.oblivm.backend.gc.offline;
  2. import java.io.BufferedOutputStream;
  3. import java.io.IOException;
  4. import com.oblivm.backend.flexsc.CompEnv;
  5. import com.oblivm.backend.flexsc.Flag;
  6. import com.oblivm.backend.flexsc.Mode;
  7. import com.oblivm.backend.gc.GCGenComp;
  8. import com.oblivm.backend.gc.GCSignal;
  9. import com.oblivm.backend.network.Network;
  10. public class GCGen extends GCGenComp {
  11. Garbler gb;
  12. public static BufferedOutputStream fout = null;
  13. public static FileReader fread = null;
  14. public GCGen(Network channel) {
  15. super(channel, Mode.OFFLINE);
  16. gtt[0][1] = GCSignal.freshLabel(CompEnv.rnd);
  17. gtt[1][0] = GCSignal.freshLabel(CompEnv.rnd);
  18. gtt[1][1] = GCSignal.freshLabel(CompEnv.rnd);
  19. gb = new Garbler();
  20. }
  21. private GCSignal[][] gtt = new GCSignal[2][2];
  22. private GCSignal labelL[] = new GCSignal[2];
  23. private GCSignal labelR[] = new GCSignal[2];
  24. GCSignal[] lb = new GCSignal[2];;
  25. private GCSignal garble(GCSignal a, GCSignal b) {
  26. labelL[0] = a;
  27. labelL[1] = R.xor(labelL[0]);
  28. labelR[0] = b;
  29. labelR[1] = R.xor(labelR[0]);
  30. int cL = a.getLSB();
  31. int cR = b.getLSB();
  32. lb[cL & cR] = gb.enc(labelL[cL], labelR[cR], gid, GCSignal.ZERO);
  33. lb[1 - (cL & cR)] = R.xor(lb[cL & cR]);
  34. gtt[0 ^ cL][0 ^ cR] = lb[0];
  35. gtt[0 ^ cL][1 ^ cR] = lb[0];
  36. gtt[1 ^ cL][0 ^ cR] = lb[0];
  37. gtt[1 ^ cL][1 ^ cR] = lb[1];
  38. if (cL != 0 || cR != 0)
  39. gtt[0 ^ cL][0 ^ cR] = gb.enc(labelL[0], labelR[0], gid, gtt[0 ^ cL][0 ^ cR]);
  40. if (cL != 0 || cR != 1)
  41. gtt[0 ^ cL][1 ^ cR] = gb.enc(labelL[0], labelR[1], gid, gtt[0 ^ cL][1 ^ cR]);
  42. if (cL != 1 || cR != 0)
  43. gtt[1 ^ cL][0 ^ cR] = gb.enc(labelL[1], labelR[0], gid, gtt[1 ^ cL][0 ^ cR]);
  44. if (cL != 1 || cR != 1)
  45. gtt[1 ^ cL][1 ^ cR] = gb.enc(labelL[1], labelR[1], gid, gtt[1 ^ cL][1 ^ cR]);
  46. return lb[0];
  47. }
  48. public double t;
  49. private GCSignal readGateFromFile() {
  50. fread.read(gtt[0][1].bytes);
  51. fread.read(gtt[1][0].bytes);
  52. fread.read(gtt[1][1].bytes);
  53. GCSignal a = new GCSignal(fread.read(10));
  54. return a;
  55. }
  56. private void writeGateToFile(GCSignal a) {
  57. gtt[0][1].send(fout);
  58. gtt[1][0].send(fout);
  59. gtt[1][1].send(fout);
  60. a.send(fout);
  61. try {
  62. fout.flush();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. private void sendGTT() {
  68. try {
  69. Flag.sw.startGCIO();
  70. gtt[0][1].send(channel);
  71. gtt[1][0].send(channel);
  72. gtt[1][1].send(channel);
  73. Flag.sw.stopGCIO();
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. System.exit(1);
  77. }
  78. }
  79. public GCSignal and(GCSignal a, GCSignal b) {
  80. Flag.sw.startGC();
  81. GCSignal res = null;
  82. if (a.isPublic() && b.isPublic())
  83. res = ((a.v && b.v) ? new GCSignal(true) : new GCSignal(false));
  84. else if (a.isPublic())
  85. res = a.v ? b : new GCSignal(false);
  86. else if (b.isPublic())
  87. res = b.v ? a : new GCSignal(false);
  88. else {
  89. ++numOfAnds;
  90. if (Flag.offline) {
  91. res = readGateFromFile();
  92. } else {
  93. res = garble(a, b);
  94. writeGateToFile(res);
  95. if (gid % 100000 == 0) {
  96. System.out.println(gid);
  97. }
  98. }
  99. sendGTT();
  100. gid++;
  101. gatesRemain = true;
  102. }
  103. Flag.sw.stopGC();
  104. return res;
  105. }
  106. @Override
  107. public void setEvaluate() {
  108. // TODO Auto-generated method stub
  109. }
  110. }