T1_FF0_O_2_1.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (C) 2010 by Yan Huang <yhuang@virginia.edu>
  2. package YaoGC;
  3. import java.math.*;
  4. import Cipher.*;
  5. import sprout.oram.PID;
  6. import sprout.oram.TID;
  7. public abstract class T1_FF0_O_2_1 extends SimpleCircuit_2_1 {
  8. public T1_FF0_O_2_1() {
  9. super("T1_FF0_O_2_1");
  10. }
  11. public static T1_FF0_O_2_1 newInstance() {
  12. if (Circuit.isForGarbling)
  13. return new G_T1_FF0_O_2_1();
  14. else
  15. return new E_T1_FF0_O_2_1();
  16. }
  17. private int output(int l, int r) {
  18. int o;
  19. if (l == 0) {
  20. if (r == 0)
  21. o = 1;
  22. else
  23. o = 0;
  24. } else {
  25. if (r == 0)
  26. o = 0;
  27. else
  28. o = 0;
  29. }
  30. return o;
  31. }
  32. protected void compute() {
  33. int left = inputWires[0].value;
  34. int right = inputWires[1].value;
  35. outputWires[0].value = output(left, right);
  36. }
  37. protected void fillTruthTable() {
  38. Wire inWireL = inputWires[0];
  39. Wire inWireR = inputWires[1];
  40. Wire outWire = outputWires[0];
  41. BigInteger[] labelL = { inWireL.lbl, Wire.conjugate(inWireL.lbl) };
  42. if (inWireL.invd == true) {
  43. BigInteger tmp = labelL[0];
  44. labelL[0] = labelL[1];
  45. labelL[1] = tmp;
  46. }
  47. BigInteger[] labelR = { inWireR.lbl, Wire.conjugate(inWireR.lbl) };
  48. if (inWireR.invd == true) {
  49. BigInteger tmp = labelR[0];
  50. labelR[0] = labelR[1];
  51. labelR[1] = tmp;
  52. }
  53. int k = outWire.serialNum;
  54. gtt = new BigInteger[2][2];
  55. int cL = inWireL.lbl.testBit(0) ? 1 : 0;
  56. int cR = inWireR.lbl.testBit(0) ? 1 : 0;
  57. BigInteger[] lb = new BigInteger[2];
  58. timing.stopwatch[PID.sha1][TID.offline].start();
  59. lb[output(cL, cR)] = Cipher.encrypt(labelL[cL], labelR[cR], k, BigInteger.ZERO);
  60. timing.stopwatch[PID.sha1][TID.offline].stop();
  61. lb[1 - output(cL, cR)] = Wire.conjugate(lb[output(cL, cR)]);
  62. outWire.lbl = lb[0];
  63. gtt[0 ^ cL][0 ^ cR] = lb[1];
  64. gtt[0 ^ cL][1 ^ cR] = lb[0];
  65. gtt[1 ^ cL][0 ^ cR] = lb[0];
  66. gtt[1 ^ cL][1 ^ cR] = lb[0];
  67. int lsb = lb[0].testBit(0) ? 1 : 0;
  68. if (outputWires[0].outBitEncPair != null) {
  69. timing.stopwatch[PID.sha1][TID.offline].start();
  70. outputWires[0].outBitEncPair[lsb] = Cipher.encrypt(k, lb[0], 0);
  71. outputWires[0].outBitEncPair[1 - lsb] = Cipher.encrypt(k, lb[1], 1);
  72. timing.stopwatch[PID.sha1][TID.offline].stop();
  73. }
  74. }
  75. protected boolean shortCut() {
  76. if (inputWires[0].value == 1) {
  77. outputWires[0].value = 0;
  78. return true;
  79. }
  80. if (inputWires[1].value == 1) {
  81. outputWires[0].value = 0;
  82. return true;
  83. }
  84. return false;
  85. }
  86. protected boolean collapse() {
  87. Wire inWireL = inputWires[0];
  88. Wire inWireR = inputWires[1];
  89. Wire outWire = outputWires[0];
  90. if (inWireL.lbl.equals(inWireR.lbl)) {
  91. if (inWireL.invd == inWireR.invd) {
  92. outWire.invd = inWireL.invd;
  93. outWire.setLabel(inWireL.lbl);
  94. } else {
  95. outWire.invd = false;
  96. outWire.value = 0;
  97. }
  98. return true;
  99. }
  100. return false;
  101. }
  102. }