AND_2_1.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 AND_2_1 extends SimpleCircuit_2_1 {
  8. public AND_2_1() {
  9. super("AND_2_1");
  10. }
  11. public static AND_2_1 newInstance() {
  12. if (Circuit.isForGarbling)
  13. return new G_AND_2_1();
  14. else
  15. return new E_AND_2_1();
  16. }
  17. protected void compute() {
  18. int left = inputWires[0].value;
  19. int right = inputWires[1].value;
  20. outputWires[0].value = left & right;
  21. }
  22. protected void fillTruthTable() {
  23. Wire inWireL = inputWires[0];
  24. Wire inWireR = inputWires[1];
  25. Wire outWire = outputWires[0];
  26. BigInteger[] labelL = { inWireL.lbl, Wire.conjugate(inWireL.lbl) };
  27. if (inWireL.invd == true) {
  28. BigInteger tmp = labelL[0];
  29. labelL[0] = labelL[1];
  30. labelL[1] = tmp;
  31. }
  32. BigInteger[] labelR = { inWireR.lbl, Wire.conjugate(inWireR.lbl) };
  33. if (inWireR.invd == true) {
  34. BigInteger tmp = labelR[0];
  35. labelR[0] = labelR[1];
  36. labelR[1] = tmp;
  37. }
  38. int k = outWire.serialNum;
  39. gtt = new BigInteger[2][2];
  40. int cL = inWireL.lbl.testBit(0) ? 1 : 0;
  41. int cR = inWireR.lbl.testBit(0) ? 1 : 0;
  42. BigInteger[] lb = new BigInteger[2];
  43. timing.stopwatch[PID.sha1][TID.offline].start();
  44. lb[cL & cR] = Cipher.encrypt(labelL[cL], labelR[cR], k, BigInteger.ZERO);
  45. timing.stopwatch[PID.sha1][TID.offline].stop();
  46. lb[1 - (cL & cR)] = Wire.conjugate(lb[cL & cR]);
  47. outWire.lbl = lb[0];
  48. gtt[0 ^ cL][0 ^ cR] = lb[0];
  49. gtt[0 ^ cL][1 ^ cR] = lb[0];
  50. gtt[1 ^ cL][0 ^ cR] = lb[0];
  51. gtt[1 ^ cL][1 ^ cR] = lb[1];
  52. int lsb = lb[0].testBit(0) ? 1 : 0;
  53. if (outputWires[0].outBitEncPair != null) {
  54. timing.stopwatch[PID.sha1][TID.offline].start();
  55. outputWires[0].outBitEncPair[lsb] = Cipher.encrypt(k, lb[0], 0);
  56. outputWires[0].outBitEncPair[1 - lsb] = Cipher.encrypt(k, lb[1], 1);
  57. timing.stopwatch[PID.sha1][TID.offline].stop();
  58. }
  59. }
  60. protected boolean shortCut() {
  61. if (inputWires[0].value == 0) {
  62. outputWires[0].value = 0;
  63. return true;
  64. }
  65. if (inputWires[1].value == 0) {
  66. outputWires[0].value = 0;
  67. return true;
  68. }
  69. return false;
  70. }
  71. protected boolean collapse() {
  72. Wire inWireL = inputWires[0];
  73. Wire inWireR = inputWires[1];
  74. Wire outWire = outputWires[0];
  75. if (inWireL.lbl.equals(inWireR.lbl)) {
  76. if (inWireL.invd == inWireR.invd) {
  77. outWire.invd = inWireL.invd;
  78. outWire.setLabel(inWireL.lbl);
  79. } else {
  80. outWire.invd = false;
  81. outWire.value = 0;
  82. }
  83. return true;
  84. }
  85. return false;
  86. }
  87. }