SimpleCircuit_2_2.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // by Boyang Wei
  2. package YaoGC;
  3. import java.math.*;
  4. import sprout.oram.PID;
  5. import sprout.oram.TID;
  6. import Cipher.Cipher;
  7. public abstract class SimpleCircuit_2_2 extends Circuit {
  8. protected BigInteger[][] gtt;
  9. public SimpleCircuit_2_2(String name) {
  10. super(2, 2, name);
  11. }
  12. public void build() throws Exception {
  13. createInputWires();
  14. createOutputWires();
  15. }
  16. protected void createInputWires() {
  17. super.createInputWires();
  18. for (int i = 0; i < inDegree; i++)
  19. inputWires[i].addObserver(this, new TransitiveObservable.Socket(
  20. inputWires, i));
  21. }
  22. protected void createOutputWires() {
  23. outputWires[0] = new Wire();
  24. outputWires[1] = new Wire();
  25. }
  26. protected void execute(boolean evaluate) {
  27. if (evaluate) {
  28. execYao();
  29. } else {
  30. passTruthTable();
  31. }
  32. outputWires[0].setReady(evaluate);
  33. outputWires[1].setReady(evaluate);
  34. /*
  35. Wire inWireL = inputWires[0];
  36. Wire inWireR = inputWires[1];
  37. Wire outWire = outputWires[0];
  38. if (evaluate) {
  39. if (inWireL.value != Wire.UNKNOWN_SIG
  40. && inWireR.value != Wire.UNKNOWN_SIG) {
  41. compute();
  42. } else if (inWireL.value != Wire.UNKNOWN_SIG) {
  43. if (shortCut())
  44. outWire.invd = false;
  45. else {
  46. outWire.value = Wire.UNKNOWN_SIG;
  47. outWire.invd = inWireR.invd;
  48. outWire.setLabel(inWireR.lbl);
  49. }
  50. } else if (inWireR.value != Wire.UNKNOWN_SIG) {
  51. if (shortCut())
  52. outWire.invd = false;
  53. else {
  54. outWire.value = Wire.UNKNOWN_SIG;
  55. outWire.invd = inWireL.invd;
  56. outWire.setLabel(inWireL.lbl);
  57. }
  58. } else {
  59. outWire.value = Wire.UNKNOWN_SIG;
  60. outWire.invd = false;
  61. if (collapse()) {
  62. System.err
  63. .println("Same labels detected! Please check label generation.");
  64. } else {
  65. execYao();
  66. }
  67. }
  68. } else
  69. passTruthTable();
  70. outWire.setReady(evaluate);
  71. */
  72. }
  73. protected abstract void execYao();
  74. protected abstract void passTruthTable();
  75. protected abstract boolean shortCut();
  76. protected abstract boolean collapse();
  77. protected void sendGTT() {
  78. timing.stopwatch[PID.gcf][TID.offline].stop();
  79. timing.stopwatch[PID.gcf][TID.offline_write].start();
  80. receiver.write(gtt[0][1]);
  81. receiver.write(gtt[1][0]);
  82. receiver.write(gtt[1][1]);
  83. if (outputWires[0].outBitEncPair != null)
  84. receiver.write(outputWires[0].outBitEncPair);
  85. if (outputWires[1].outBitEncPair != null)
  86. receiver.write(outputWires[1].outBitEncPair);
  87. timing.stopwatch[PID.gcf][TID.offline_write].stop();
  88. timing.stopwatch[PID.gcf][TID.offline].start();
  89. }
  90. protected void receiveGTT() {
  91. try {
  92. gtt = new BigInteger[2][2];
  93. gtt[0][0] = BigInteger.ZERO;
  94. timing.stopwatch[PID.gcf][TID.offline].stop();
  95. timing.stopwatch[PID.gcf][TID.offline_read].start();
  96. gtt[0][1] = sender.readBigInteger();
  97. gtt[1][0] = sender.readBigInteger();
  98. gtt[1][1] = sender.readBigInteger();
  99. if (outputWires[0].outBitEncPair != null)
  100. outputWires[0].outBitEncPair = sender.readBigIntegerArray();
  101. if (outputWires[1].outBitEncPair != null)
  102. outputWires[1].outBitEncPair = sender.readBigIntegerArray();
  103. timing.stopwatch[PID.gcf][TID.offline_read].stop();
  104. timing.stopwatch[PID.gcf][TID.offline].start();
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. System.exit(1);
  108. }
  109. }
  110. protected void encryptTruthTable() {
  111. Wire inWireL = inputWires[0];
  112. Wire inWireR = inputWires[1];
  113. Wire outWire0 = outputWires[0];
  114. Wire outWire1 = outputWires[1];
  115. BigInteger[] labelL = { inWireL.lbl, Wire.conjugate(inWireL.lbl) };
  116. if (inWireL.invd == true) {
  117. BigInteger tmp = labelL[0];
  118. labelL[0] = labelL[1];
  119. labelL[1] = tmp;
  120. }
  121. BigInteger[] labelR = { inWireR.lbl, Wire.conjugate(inWireR.lbl) };
  122. if (inWireR.invd == true) {
  123. BigInteger tmp = labelR[0];
  124. labelR[0] = labelR[1];
  125. labelR[1] = tmp;
  126. }
  127. int k0 = outWire0.serialNum;
  128. int k1 = outWire1.serialNum;
  129. int cL = inWireL.lbl.testBit(0) ? 1 : 0;
  130. int cR = inWireR.lbl.testBit(0) ? 1 : 0;
  131. timing.stopwatch[PID.sha1][TID.offline].start();
  132. if (cL != 0 || cR != 0)
  133. gtt[0 ^ cL][0 ^ cR] = Cipher.encrypt(labelL[0], labelR[0], k0, k1,
  134. gtt[0 ^ cL][0 ^ cR]);
  135. if (cL != 0 || cR != 1)
  136. gtt[0 ^ cL][1 ^ cR] = Cipher.encrypt(labelL[0], labelR[1], k0, k1,
  137. gtt[0 ^ cL][1 ^ cR]);
  138. if (cL != 1 || cR != 0)
  139. gtt[1 ^ cL][0 ^ cR] = Cipher.encrypt(labelL[1], labelR[0], k0, k1,
  140. gtt[1 ^ cL][0 ^ cR]);
  141. if (cL != 1 || cR != 1)
  142. gtt[1 ^ cL][1 ^ cR] = Cipher.encrypt(labelL[1], labelR[1], k0, k1,
  143. gtt[1 ^ cL][1 ^ cR]);
  144. timing.stopwatch[PID.sha1][TID.offline].stop();
  145. }
  146. }