FF10_3_SC_2_2.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // by Boyang Wei
  2. package YaoGC;
  3. import java.math.*;
  4. import Cipher.*;
  5. import sprout.oram.PID;
  6. import sprout.oram.TID;
  7. public abstract class FF10_3_SC_2_2 extends SimpleCircuit_2_2 {
  8. public FF10_3_SC_2_2() {
  9. super("FF10_3_SC_2_2");
  10. }
  11. public static FF10_3_SC_2_2 newInstance() {
  12. if (Circuit.isForGarbling)
  13. return new G_FF10_3_SC_2_2();
  14. else
  15. return new E_FF10_3_SC_2_2();
  16. }
  17. protected void compute() {}
  18. private int outS(int l, int r) {
  19. int o;
  20. if (l == 0) {
  21. if (r == 0)
  22. o = 0;
  23. else
  24. o = 0;
  25. } else {
  26. if (r == 0)
  27. o = 1;
  28. else
  29. o = 1;
  30. }
  31. return o;
  32. }
  33. private int outO(int l, int r) {
  34. int o;
  35. if (l == 0) {
  36. if (r == 0)
  37. o = 0;
  38. else
  39. o = 0;
  40. } else {
  41. if (r == 0)
  42. o = 0;
  43. else
  44. o = 0;
  45. }
  46. return o;
  47. }
  48. protected void fillTruthTable() {
  49. Wire inWireL = inputWires[0];
  50. Wire inWireR = inputWires[1];
  51. Wire outWire0 = outputWires[0];
  52. Wire outWire1 = outputWires[1];
  53. BigInteger[] labelL = { inWireL.lbl, Wire.conjugate(inWireL.lbl) };
  54. if (inWireL.invd == true) {
  55. BigInteger tmp = labelL[0];
  56. labelL[0] = labelL[1];
  57. labelL[1] = tmp;
  58. }
  59. BigInteger[] labelR = { inWireR.lbl, Wire.conjugate(inWireR.lbl) };
  60. if (inWireR.invd == true) {
  61. BigInteger tmp = labelR[0];
  62. labelR[0] = labelR[1];
  63. labelR[1] = tmp;
  64. }
  65. int k0 = outWire0.serialNum;
  66. int k1 = outWire1.serialNum;
  67. gtt = new BigInteger[2][2];
  68. int cL = inWireL.lbl.testBit(0) ? 1 : 0;
  69. int cR = inWireR.lbl.testBit(0) ? 1 : 0;
  70. timing.stopwatch[PID.sha1][TID.offline].start();
  71. BigInteger lb = Cipher.encrypt(labelL[cL], labelR[cR], k0, k1, BigInteger.ZERO);
  72. timing.stopwatch[PID.sha1][TID.offline].stop();
  73. BigInteger[] lbS = new BigInteger[2];
  74. BigInteger[] lbO = new BigInteger[2];
  75. lbS[outS(cL, cR)] = lb.shiftRight(Wire.labelBitLength);
  76. lbO[outO(cL, cR)] = lb.and(Cipher.mask);
  77. lbS[1 - outS(cL, cR)] = Wire.conjugate(lbS[outS(cL, cR)]);
  78. lbO[1 - outO(cL, cR)] = Wire.conjugate(lbO[outO(cL, cR)]);
  79. outWire0.lbl = lbS[0];
  80. outWire1.lbl = lbO[0];
  81. gtt[0 ^ cL][0 ^ cR] = lbS[outS(0, 0)].shiftLeft(Wire.labelBitLength).xor(lbO[outO(0, 0)]);
  82. gtt[0 ^ cL][1 ^ cR] = lbS[outS(0, 1)].shiftLeft(Wire.labelBitLength).xor(lbO[outO(0, 1)]);
  83. gtt[1 ^ cL][0 ^ cR] = lbS[outS(1, 0)].shiftLeft(Wire.labelBitLength).xor(lbO[outO(1, 0)]);
  84. gtt[1 ^ cL][1 ^ cR] = lbS[outS(1, 1)].shiftLeft(Wire.labelBitLength).xor(lbO[outO(1, 1)]);
  85. int lsb = lbS[0].testBit(0) ? 1 : 0;
  86. if (outputWires[0].outBitEncPair != null) {
  87. timing.stopwatch[PID.sha1][TID.offline].start();
  88. outputWires[0].outBitEncPair[lsb] = Cipher.encrypt(k0, lbS[0], 0);
  89. outputWires[0].outBitEncPair[1 - lsb] = Cipher.encrypt(k0, lbS[1], 1);
  90. timing.stopwatch[PID.sha1][TID.offline].stop();
  91. }
  92. lsb = lbO[0].testBit(0) ? 1 : 0;
  93. if (outputWires[1].outBitEncPair != null) {
  94. timing.stopwatch[PID.sha1][TID.offline].start();
  95. outputWires[1].outBitEncPair[lsb] = Cipher.encrypt(k1, lbO[0], 0);
  96. outputWires[1].outBitEncPair[1 - lsb] = Cipher.encrypt(k1, lbO[1], 1);
  97. timing.stopwatch[PID.sha1][TID.offline].stop();
  98. }
  99. }
  100. protected boolean shortCut() {
  101. return false;
  102. }
  103. protected boolean collapse() {
  104. return false;
  105. }
  106. }