SimpleCircuit_2_1.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (C) 2010 by Yan Huang <yhuang@virginia.edu>
  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_1 extends Circuit {
  8. protected BigInteger[][] gtt;
  9. public SimpleCircuit_2_1(String name) {
  10. super(2, 1, 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. }
  25. protected void execute(boolean evaluate) {
  26. Wire inWireL = inputWires[0];
  27. Wire inWireR = inputWires[1];
  28. Wire outWire = outputWires[0];
  29. if (evaluate) {
  30. if (inWireL.value != Wire.UNKNOWN_SIG
  31. && inWireR.value != Wire.UNKNOWN_SIG) {
  32. compute();
  33. } else if (inWireL.value != Wire.UNKNOWN_SIG) {
  34. if (shortCut())
  35. outWire.invd = false;
  36. else {
  37. outWire.value = Wire.UNKNOWN_SIG;
  38. outWire.invd = inWireR.invd;
  39. outWire.setLabel(inWireR.lbl);
  40. }
  41. } else if (inWireR.value != Wire.UNKNOWN_SIG) {
  42. if (shortCut())
  43. outWire.invd = false;
  44. else {
  45. outWire.value = Wire.UNKNOWN_SIG;
  46. outWire.invd = inWireL.invd;
  47. outWire.setLabel(inWireL.lbl);
  48. }
  49. } else {
  50. outWire.value = Wire.UNKNOWN_SIG;
  51. outWire.invd = false;
  52. if (collapse()) {
  53. System.err
  54. .println("Same labels detected! Please check label generation.");
  55. } else {
  56. execYao();
  57. }
  58. }
  59. } else
  60. passTruthTable();
  61. outWire.setReady(evaluate);
  62. }
  63. protected abstract void execYao();
  64. protected abstract void passTruthTable();
  65. protected abstract boolean shortCut();
  66. protected abstract boolean collapse();
  67. protected void sendGTT() {
  68. timing.stopwatch[PID.gcf][TID.offline].stop();
  69. timing.stopwatch[PID.gcf][TID.offline_write].start();
  70. receiver.write(gtt[0][1]);
  71. receiver.write(gtt[1][0]);
  72. receiver.write(gtt[1][1]);
  73. if (outputWires[0].outBitEncPair != null)
  74. receiver.write(outputWires[0].outBitEncPair);
  75. timing.stopwatch[PID.gcf][TID.offline_write].stop();
  76. timing.stopwatch[PID.gcf][TID.offline].start();
  77. }
  78. protected void receiveGTT() {
  79. try {
  80. gtt = new BigInteger[2][2];
  81. gtt[0][0] = BigInteger.ZERO;
  82. timing.stopwatch[PID.gcf][TID.offline].stop();
  83. timing.stopwatch[PID.gcf][TID.offline_read].start();
  84. gtt[0][1] = sender.readBigInteger();
  85. gtt[1][0] = sender.readBigInteger();
  86. gtt[1][1] = sender.readBigInteger();
  87. if (outputWires[0].outBitEncPair != null)
  88. outputWires[0].outBitEncPair = sender.readBigIntegerArray();
  89. timing.stopwatch[PID.gcf][TID.offline_read].stop();
  90. timing.stopwatch[PID.gcf][TID.offline].start();
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. System.exit(1);
  94. }
  95. }
  96. protected void encryptTruthTable() {
  97. Wire inWireL = inputWires[0];
  98. Wire inWireR = inputWires[1];
  99. Wire outWire = outputWires[0];
  100. BigInteger[] labelL = { inWireL.lbl, Wire.conjugate(inWireL.lbl) };
  101. if (inWireL.invd == true) {
  102. BigInteger tmp = labelL[0];
  103. labelL[0] = labelL[1];
  104. labelL[1] = tmp;
  105. }
  106. BigInteger[] labelR = { inWireR.lbl, Wire.conjugate(inWireR.lbl) };
  107. if (inWireR.invd == true) {
  108. BigInteger tmp = labelR[0];
  109. labelR[0] = labelR[1];
  110. labelR[1] = tmp;
  111. }
  112. int k = outWire.serialNum;
  113. int cL = inWireL.lbl.testBit(0) ? 1 : 0;
  114. int cR = inWireR.lbl.testBit(0) ? 1 : 0;
  115. timing.stopwatch[PID.sha1][TID.offline].start();
  116. if (cL != 0 || cR != 0)
  117. gtt[0 ^ cL][0 ^ cR] = Cipher.encrypt(labelL[0], labelR[0], k,
  118. gtt[0 ^ cL][0 ^ cR]);
  119. if (cL != 0 || cR != 1)
  120. gtt[0 ^ cL][1 ^ cR] = Cipher.encrypt(labelL[0], labelR[1], k,
  121. gtt[0 ^ cL][1 ^ cR]);
  122. if (cL != 1 || cR != 0)
  123. gtt[1 ^ cL][0 ^ cR] = Cipher.encrypt(labelL[1], labelR[0], k,
  124. gtt[1 ^ cL][0 ^ cR]);
  125. if (cL != 1 || cR != 1)
  126. gtt[1 ^ cL][1 ^ cR] = Cipher.encrypt(labelL[1], labelR[1], k,
  127. gtt[1 ^ cL][1 ^ cR]);
  128. timing.stopwatch[PID.sha1][TID.offline].stop();
  129. }
  130. }