Circuit.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2010 by Yan Huang <yhuang@virginia.edu>
  2. package YaoGC;
  3. import java.math.*;
  4. import java.io.*;
  5. import sprout.communication.Communication;
  6. import sprout.util.Timing;
  7. abstract public class Circuit implements TransitiveObserver {
  8. public static boolean isForGarbling;
  9. public Wire[] inputWires;
  10. public Wire[] outputWires;
  11. protected int inDegree, outDegree;
  12. protected String name;
  13. public static ObjectOutputStream oos = null;
  14. public static ObjectInputStream ois = null;
  15. public static Communication sender = null;
  16. public static Communication receiver = null;
  17. public static Timing timing = null;
  18. // public boolean sendOutBitsLookup = false;
  19. public static void setSender(Communication s) {
  20. sender = s;
  21. }
  22. public static void setReceiver(Communication r) {
  23. receiver = r;
  24. }
  25. private int inputWireCount = 0;
  26. public Circuit(int inDegree, int outDegree, String name) {
  27. this.inDegree = inDegree;
  28. this.outDegree = outDegree;
  29. this.name = name;
  30. inputWires = new Wire[inDegree];
  31. outputWires = new Wire[outDegree];
  32. }
  33. public static void setIOStream(ObjectInputStream ois, ObjectOutputStream oos) {
  34. Circuit.ois = ois;
  35. Circuit.oos = oos;
  36. }
  37. abstract public void build() throws Exception;
  38. protected void createInputWires() {
  39. for (int i = 0; i < inDegree; i++) {
  40. inputWires[i] = new Wire();
  41. }
  42. }
  43. public void startExecuting(int[] vals, boolean[] invd, BigInteger[] glbs)
  44. throws Exception {
  45. if (vals.length != invd.length || invd.length != glbs.length
  46. || glbs.length != this.inDegree)
  47. throw new Exception("Unmatched number of input labels.");
  48. for (int i = 0; i < this.inDegree; i++) {
  49. inputWires[i].value = vals[i];
  50. inputWires[i].invd = invd[i];
  51. inputWires[i].setLabel(glbs[i]);
  52. inputWires[i].setReady(true);
  53. }
  54. }
  55. public void receiveTruthTables() {
  56. for (int i = 0; i < this.inDegree; i++) {
  57. inputWires[i].setReady(false);
  58. }
  59. }
  60. public void sendTruthTables(State s) {
  61. if (s.getWidth() != this.inDegree) {
  62. Exception e = new Exception("Unmatched number of input labels."
  63. + s.getWidth() + " != " + inDegree);
  64. e.printStackTrace();
  65. System.exit(1);
  66. }
  67. for (int i = 0; i < this.inDegree; i++) {
  68. inputWires[i].value = s.wires[i].value;
  69. inputWires[i].invd = s.wires[i].invd;
  70. inputWires[i].setLabel(s.wires[i].lbl);
  71. inputWires[i].setReady(false);
  72. }
  73. }
  74. public State startExecuting(State s) {
  75. if (s.getWidth() != this.inDegree) {
  76. Exception e = new Exception("Unmatched number of input labels."
  77. + s.getWidth() + " != " + inDegree);
  78. e.printStackTrace();
  79. System.exit(1);
  80. }
  81. for (int i = 0; i < this.inDegree; i++) {
  82. inputWires[i].value = s.wires[i].value;
  83. inputWires[i].invd = s.wires[i].invd;
  84. inputWires[i].setLabel(s.wires[i].lbl);
  85. inputWires[i].setReady(true);
  86. }
  87. return State.fromWires(this.outputWires);
  88. }
  89. public BigInteger interpretOutputELabels(BigInteger[] eLabels)
  90. throws Exception {
  91. if (eLabels.length != outDegree)
  92. throw new Exception("Length Error.");
  93. BigInteger output = BigInteger.ZERO;
  94. for (int i = 0; i < this.outDegree; i++) {
  95. if (outputWires[i].value != Wire.UNKNOWN_SIG) {
  96. if (outputWires[i].value == 1)
  97. output = output.setBit(i);
  98. } else if (eLabels[i]
  99. .equals(outputWires[i].invd ? outputWires[i].lbl
  100. : outputWires[i].lbl.xor(Wire.R.shiftLeft(1)
  101. .setBit(0)))) {
  102. output = output.setBit(i);
  103. } else if (!eLabels[i]
  104. .equals(outputWires[i].invd ? outputWires[i].lbl.xor(Wire.R
  105. .shiftLeft(1).setBit(0)) : outputWires[i].lbl))
  106. throw new Exception("Bad Label encountered at ouputWire[" + i
  107. + "]:\n" + eLabels[i] + " is neither "
  108. + outputWires[i].lbl + " nor "
  109. + outputWires[i].lbl.xor(Wire.R.shiftLeft(1).setBit(0)));
  110. }
  111. return output;
  112. }
  113. public void update(boolean evaluate, TransitiveObservable o, Object arg) {
  114. inputWireCount++;
  115. if (inputWireCount % inDegree == 0)
  116. execute(evaluate);
  117. }
  118. abstract protected void compute();
  119. abstract protected void execute(boolean evaluate);
  120. // abstract public void sendOutBitsLookup(boolean send);
  121. }