XOR_2_1.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2010 by Yan Huang <yhuang@virginia.edu>
  2. package YaoGC;
  3. import java.math.*;
  4. public abstract class XOR_2_1 extends SimpleCircuit_2_1 {
  5. public XOR_2_1() {
  6. super("XOR_2_1");
  7. }
  8. public static XOR_2_1 newInstance() {
  9. if (Circuit.isForGarbling)
  10. return new G_XOR_2_1();
  11. else
  12. return new E_XOR_2_1();
  13. }
  14. protected void compute() {
  15. int left = inputWires[0].value;
  16. int right = inputWires[1].value;
  17. outputWires[0].value = left ^ right;
  18. }
  19. public void execute(boolean evaluate) {
  20. Wire inWireL = inputWires[0];
  21. Wire inWireR = inputWires[1];
  22. Wire outWire = outputWires[0];
  23. if (inWireL.value != Wire.UNKNOWN_SIG
  24. && inWireR.value != Wire.UNKNOWN_SIG) {
  25. compute();
  26. } else if (inWireL.value != Wire.UNKNOWN_SIG) {
  27. if (inWireL.value == 0)
  28. outWire.invd = inWireR.invd;
  29. else
  30. outWire.invd = !inWireR.invd;
  31. outWire.value = Wire.UNKNOWN_SIG;
  32. outWire.setLabel(inWireR.lbl);
  33. } else if (inWireR.value != Wire.UNKNOWN_SIG) {
  34. if (inWireR.value == 0)
  35. outWire.invd = inWireL.invd;
  36. else
  37. outWire.invd = !inWireL.invd;
  38. outWire.value = Wire.UNKNOWN_SIG;
  39. outWire.setLabel(inWireL.lbl);
  40. } else {
  41. if (collapse()) {
  42. System.err
  43. .println("Same labels detected! Please check label generation.");
  44. } else {
  45. BigInteger l = inWireL.lbl;
  46. BigInteger r = inWireR.lbl;
  47. BigInteger out = l.xor(r);
  48. outWire.invd = inWireL.invd ^ inWireR.invd;
  49. outWire.value = Wire.UNKNOWN_SIG;
  50. outWire.setLabel(out);
  51. }
  52. }
  53. if (!evaluate)
  54. sendOutBitEncPair();
  55. outWire.setReady(evaluate);
  56. }
  57. protected boolean collapse() {
  58. Wire inWireL = inputWires[0];
  59. Wire inWireR = inputWires[1];
  60. Wire outWire = outputWires[0];
  61. if (inWireL.lbl.equals(inWireR.lbl)) {
  62. if (inWireL.invd == inWireR.invd) {
  63. outWire.invd = false;
  64. outWire.value = 0;
  65. } else {
  66. outWire.invd = false;
  67. outWire.value = 1;
  68. }
  69. return true;
  70. }
  71. return false;
  72. }
  73. protected void sendOutBitEncPair() {
  74. }
  75. // Never used for XOR gate.
  76. protected boolean shortCut() {
  77. return false;
  78. }
  79. // Never used for XOR gate.
  80. protected void fillTruthTable() {
  81. }
  82. protected void execYao() {
  83. }
  84. protected void passTruthTable() {
  85. }
  86. }