Wire.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2010 by Yan Huang <yhuang@virginia.edu>
  2. package YaoGC;
  3. import java.math.*;
  4. import java.security.SecureRandom;
  5. import sprout.crypto.SR;
  6. public class Wire extends TransitiveObservable {
  7. public static final int UNKNOWN_SIG = -1;
  8. // These four fields are for garbling
  9. public static int K = 0;
  10. private static SecureRandom rnd = SR.rand;
  11. public static final int labelBitLength = 80;
  12. public static final int labelBytes = (labelBitLength + 7) / 8;
  13. public static final BigInteger R = new BigInteger(labelBitLength - 1, rnd);
  14. public final int serialNum;
  15. public int value = UNKNOWN_SIG;
  16. public BigInteger lbl;
  17. public boolean invd = false;
  18. public BigInteger[] outBitEncPair = null;
  19. public Wire() {
  20. serialNum = K++;
  21. lbl = new BigInteger(labelBitLength, rnd);
  22. }
  23. public static BigInteger[] newLabelPair() {
  24. BigInteger[] res = new BigInteger[2];
  25. res[0] = new BigInteger(labelBitLength, rnd);
  26. res[1] = conjugate(res[0]);
  27. return res;
  28. }
  29. public static BigInteger conjugate(BigInteger label) {
  30. if (label == null)
  31. return null;
  32. return label.xor(R.shiftLeft(1).setBit(0));
  33. }
  34. public void setLabel(BigInteger label) {
  35. lbl = label;
  36. }
  37. public void setReady(boolean evaluate) {
  38. setChanged();
  39. notifyObservers(evaluate);
  40. }
  41. public void connectTo(Wire[] ws, int idx) {
  42. Wire w = ws[idx];
  43. for (int i = 0; i < w.observers.size(); i++) {
  44. TransitiveObserver ob = w.observers.get(i);
  45. TransitiveObservable.Socket s = w.exports.get(i);
  46. this.addObserver(ob, s);
  47. s.updateSocket(this);
  48. }
  49. w.deleteObservers();
  50. ws[idx] = this;
  51. }
  52. public void fixWire(int v) {
  53. this.value = v;
  54. for (int i = 0; i < this.observers.size(); i++) {
  55. Circuit c = (Circuit) this.observers.get(i);
  56. c.inDegree--;
  57. if (c.inDegree == 0) {
  58. c.compute();
  59. for (int j = 0; j < c.outDegree; j++)
  60. c.outputWires[j].fixWire(c.outputWires[j].value);
  61. }
  62. }
  63. }
  64. protected static int getLSB(BigInteger lp) {
  65. return lp.testBit(0) ? 1 : 0;
  66. }
  67. }